Skip to content

Commit d9c2cc3

Browse files
committed
[Java] Fix bugs after merge of PR #649 due to wrong systemId being set and not dealing with schema validation.
1 parent 31a72fc commit d9c2cc3

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

build.gradle

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,18 +372,16 @@ project(':sbe-samples') {
372372
}
373373

374374
task generateCodecs(type: JavaExec) {
375-
workingDir = 'src/main/resources'
376375
main = 'uk.co.real_logic.sbe.SbeTool'
377376
classpath = project(':sbe-all').sourceSets.main.runtimeClasspath
378377
systemProperties(
379-
'sbe.output.dir': '../../../build/generated',
378+
'sbe.output.dir': 'build/generated',
380379
'sbe.target.language': 'Java',
381380
'sbe.java.generate.interfaces': 'true',
382381
'sbe.validation.stop.on.error': 'true',
383382
'sbe.xinclude.aware': 'true',
384383
'sbe.validation.xsd': validationXsdPath)
385-
args = ['example-schema.xml',
386-
'example-extension-schema.xml']
384+
args = ['src/main/resources/example-schema.xml', 'src/main/resources/example-extension-schema.xml']
387385
}
388386

389387
task runExampleUsingGeneratedStub(dependsOn: 'compileJava', type: JavaExec) {
@@ -710,8 +708,6 @@ task generateCSharpCodecs {
710708
'generateCSharpCodecsWithXIncludes'
711709
}
712710

713-
714-
715711
task generateJavaIrCodecs(type: JavaExec) {
716712
main = 'uk.co.real_logic.sbe.SbeTool'
717713
classpath = project(':sbe-all').sourceSets.main.runtimeClasspath

sbe-tool/src/main/java/uk/co/real_logic/sbe/SbeTool.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,17 @@ public static void validateAgainstSchema(final String sbeSchemaFilename, final S
260260
.warningsFatal(Boolean.parseBoolean(System.getProperty(VALIDATION_WARNINGS_FATAL)))
261261
.suppressOutput(Boolean.parseBoolean(System.getProperty(VALIDATION_SUPPRESS_OUTPUT)));
262262

263-
try (InputStream in = new BufferedInputStream(Files.newInputStream(Paths.get(sbeSchemaFilename))))
263+
final Path filePath = Paths.get(sbeSchemaFilename);
264+
try (InputStream in = new BufferedInputStream(Files.newInputStream(filePath)))
264265
{
265-
XmlSchemaParser.validate(xsdFilename, in, optionsBuilder.build());
266+
final InputSource inputSource = new InputSource(in);
267+
final Path parentPath = filePath.toAbsolutePath().getParent();
268+
if (parentPath != null)
269+
{
270+
inputSource.setSystemId(parentPath.toUri().toString());
271+
}
272+
273+
XmlSchemaParser.validate(xsdFilename, inputSource, optionsBuilder.build());
266274
}
267275
}
268276

@@ -290,8 +298,9 @@ public static MessageSchema parseSchema(final String sbeSchemaFilename)
290298
final Path parentPath = filePath.toAbsolutePath().getParent();
291299
if (parentPath != null)
292300
{
293-
inputSource.setSystemId(filePath.toUri().toString());
301+
inputSource.setSystemId(parentPath.toUri().toString());
294302
}
303+
295304
return XmlSchemaParser.parse(inputSource, optionsBuilder.build());
296305
}
297306
}

sbe-tool/src/main/java/uk/co/real_logic/sbe/xml/XmlSchemaParser.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ public class XmlSchemaParser
7272
* Validate the document against a given schema. Error will be written to {@link java.lang.System#err}
7373
*
7474
* @param xsdFilename schema to validate against.
75-
* @param in document to be validated.
75+
* @param is source from which schema is read. Ideally it will have the systemId property set to resolve
76+
* relative references.
7677
* @param options to be applied during parsing.
7778
* @throws Exception if an error occurs when parsing the document or schema.
7879
*/
79-
public static void validate(final String xsdFilename, final InputStream in, final ParserOptions options)
80+
public static void validate(final String xsdFilename, final InputSource is, final ParserOptions options)
8081
throws Exception
8182
{
8283
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@@ -91,15 +92,32 @@ public static void validate(final String xsdFilename, final InputStream in, fina
9192
factory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
9293
}
9394

94-
factory.newDocumentBuilder().parse(in);
95+
factory.newDocumentBuilder().parse(is);
9596
}
9697

9798
/**
98-
* Take an {@link InputSource} and parse it generating map of template ID to Message objects, types, and schema.
99+
* Wraps the {@link InputStream} into an {@link InputSource} and delegates to
100+
* {@link #validate(String, InputSource, ParserOptions)}.
99101
* <p>
100-
* Exceptions are passed back up for any problems.
102+
* <b>Note:</b> this method does not set the {@link InputSource#setSystemId(java.lang.String)} property.
103+
* However, it is recommended to use the {@link #validate(String, InputSource, ParserOptions)} method directly.
101104
*
102-
* @param is inputSource from which schema is read. Ideally it will have the systemId property set to resolve relative references
105+
* @param xsdFilename schema to validate against.
106+
* @param in document to be validated.
107+
* @param options to be applied during parsing.
108+
* @throws Exception if an error occurs when parsing the document or schema.
109+
*/
110+
public static void validate(final String xsdFilename, final InputStream in, final ParserOptions options)
111+
throws Exception
112+
{
113+
validate(xsdFilename, new InputSource(in), options);
114+
}
115+
116+
/**
117+
* Take an {@link InputSource} and parse it generating map of template ID to Message objects, types, and schema.
118+
*
119+
* @param is source from which schema is read. Ideally it will have the systemId property set to resolve
120+
* relative references.
103121
* @param options to be applied during parsing.
104122
* @return {@link MessageSchema} encoding for the schema.
105123
* @throws Exception on parsing error.
@@ -135,13 +153,11 @@ public static MessageSchema parse(final InputSource is, final ParserOptions opti
135153
}
136154

137155
/**
138-
* Wraps an {@link InputStream} into an {@link InputSource} and delegates to
139-
* {@link #parse(org.xml.sax.InputSource, uk.co.real_logic.sbe.xml.ParserOptions) }.
140-
* <p>Note: this method does not the the {@link InputSource#setSystemId(java.lang.String) } property, however. It is recommended to use the
141-
* {@link #parse(org.xml.sax.InputSource, uk.co.real_logic.sbe.xml.ParserOptions) } method directly.</p>
142-
*
156+
* Wraps the {@link InputStream} into an {@link InputSource} and delegates to
157+
* {@link #parse(InputSource, ParserOptions)}.
143158
* <p>
144-
* Exceptions are passed back up for any problems.
159+
* <b>Note:</b> this method does not set the {@link InputSource#setSystemId(java.lang.String)} property.
160+
* However, it is recommended to use the {@link #parse(InputSource, ParserOptions)} method directly.
145161
*
146162
* @param in stream from which schema is read.
147163
* @param options to be applied during parsing.

sbe-tool/src/test/java/uk/co/real_logic/sbe/xml/RelativeXIncludeTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ public void shouldParseFileInSubDir()
3232
{
3333
final ClassLoader classLoader = getClass().getClassLoader();
3434
final URL testResource = classLoader.getResource("sub/basic-schema.xml");
35-
final InputStream inStream = testResource.openStream();
36-
final InputSource is = new InputSource(inStream);
35+
final InputSource is = new InputSource(testResource.openStream());
3736

3837
final File file = new File(testResource.getFile());
3938
is.setSystemId(file.toPath().toAbsolutePath().getParent().toUri().toString());
4039
parse(is, ParserOptions.DEFAULT);
4140
}
42-
4341
}

0 commit comments

Comments
 (0)