Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support file-relative XInclude href's #649

Merged
merged 3 commits into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions sbe-tool/src/main/java/uk/co/real_logic/sbe/SbeTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.xml.sax.InputSource;

/**
* A tool for running the SBE parser, validator, and code generator.
Expand Down Expand Up @@ -281,9 +283,16 @@ public static MessageSchema parseSchema(final String sbeSchemaFilename)
.warningsFatal(Boolean.parseBoolean(System.getProperty(VALIDATION_WARNINGS_FATAL)))
.suppressOutput(Boolean.parseBoolean(System.getProperty(VALIDATION_SUPPRESS_OUTPUT)));

try (InputStream in = new BufferedInputStream(Files.newInputStream(Paths.get(sbeSchemaFilename))))
final Path filePath = Paths.get(sbeSchemaFilename);
try (InputStream in = new BufferedInputStream(Files.newInputStream(filePath)))
{
return XmlSchemaParser.parse(in, optionsBuilder.build());
final InputSource inputSource = new InputSource(in);
final Path parentPath = filePath.toAbsolutePath().getParent();
if (parentPath != null)
{
inputSource.setSystemId(filePath.toUri().toString());
}
return XmlSchemaParser.parse(inputSource, optionsBuilder.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Map;
import org.xml.sax.InputSource;

import static uk.co.real_logic.sbe.PrimitiveType.*;
import static uk.co.real_logic.sbe.xml.Presence.REQUIRED;
Expand Down Expand Up @@ -94,16 +95,16 @@ public static void validate(final String xsdFilename, final InputStream in, fina
}

/**
* Take an {@link InputStream} and parse it generating map of template ID to Message objects, types, and schema.
* Take an {@link InputSource} and parse it generating map of template ID to Message objects, types, and schema.
* <p>
* Exceptions are passed back up for any problems.
*
* @param in stream from which schema is read.
* @param is inputSource from which schema is read. Ideally it will have the systemId property set to resolve relative references
* @param options to be applied during parsing.
* @return {@link MessageSchema} encoding for the schema.
* @throws Exception on parsing error.
*/
public static MessageSchema parse(final InputStream in, final ParserOptions options) throws Exception
public static MessageSchema parse(final InputSource is, final ParserOptions options) throws Exception
{
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

Expand All @@ -114,7 +115,7 @@ public static MessageSchema parse(final InputStream in, final ParserOptions opti
factory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
}

final Document document = factory.newDocumentBuilder().parse(in);
final Document document = factory.newDocumentBuilder().parse(is);
final XPath xPath = XPathFactory.newInstance().newXPath();

final ErrorHandler errorHandler = new ErrorHandler(options);
Expand All @@ -133,6 +134,25 @@ public static MessageSchema parse(final InputStream in, final ParserOptions opti
return messageSchema;
}

/**
* Wraps an {@link InputStream} into an {@link InputSource} and delegates to
* {@link #parse(org.xml.sax.InputSource, uk.co.real_logic.sbe.xml.ParserOptions) }.
* <p>Note: this method does not the the {@link InputSource#setSystemId(java.lang.String) } property, however. It is recommended to use the
* {@link #parse(org.xml.sax.InputSource, uk.co.real_logic.sbe.xml.ParserOptions) } method directly.</p>
*
* <p>
* Exceptions are passed back up for any problems.
*
* @param in stream from which schema is read.
* @param options to be applied during parsing.
* @return {@link MessageSchema} encoding for the schema.
* @throws Exception on parsing error.
*/
public static MessageSchema parse(final InputStream in, final ParserOptions options) throws Exception
{
return parse(new InputSource(in), options);
}

/**
* Scan XML for all types (encodedDataType, compositeType, enumType, and setType) and save in map
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2013-2019 Real Logic Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.co.real_logic.sbe.xml;

import java.io.File;
import java.io.InputStream;
import java.net.URL;

import org.junit.Test;

import org.xml.sax.InputSource;
import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse;

public class RelativeXIncludeTest
{
@Test
public void shouldParseFileInSubDir()
throws Exception
{
final ClassLoader classLoader = getClass().getClassLoader();
final URL testResource = classLoader.getResource("sub/basic-schema.xml");
final InputStream inStream = testResource.openStream();
final InputSource is = new InputSource(inStream);

final File file = new File(testResource.getFile());
is.setSystemId(file.toPath().toAbsolutePath().getParent().toUri().toString());
parse(is, ParserOptions.DEFAULT);
}

}
12 changes: 12 additions & 0 deletions sbe-tool/src/test/resources/sub/basic-schema.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<messageSchema package="SBE tests"
id="2"
xmlns:xi="http://www.w3.org/2001/XInclude"
semanticVersion="5.2"
description="Unit Test"
byteOrder="littleEndian">
<xi:include href='sub2/common.xml'/>
<message name="TestMessage50001" id="50001" description="TestMessage" blockLength="16">
<field name="Tag40001" id="40001" type="uint32" semanticType="int"/>
</message>
</messageSchema>
10 changes: 10 additions & 0 deletions sbe-tool/src/test/resources/sub/sub2/common.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<types>
<type name="Symbol" description="Symbol" length="20" primitiveType="char" semanticType="char"/>
<composite name="messageHeader" description="Message identifiers and length of message root">
<type name="blockLength" primitiveType="uint16"/>
<type name="templateId" primitiveType="uint16"/>
<type name="schemaId" primitiveType="uint16"/>
<type name="version" primitiveType="uint16"/>
</composite>
</types>