/** * Parse the message schema specification. * * @param sbeSchemaFilename file containing the SBE specification for the * @return the parsed {@link MessageSchema} for the specification found in the file. * @throws Exception if an error occurs when parsing the specification. */ public static MessageSchema parseSchema(final String sbeSchemaFilename) throws Exception { final ParserOptions.Builder optionsBuilder = ParserOptions.builder() .xsdFilename(System.getProperty(VALIDATION_XSD)) .xIncludeAware(Boolean.parseBoolean(System.getProperty(XINCLUDE_AWARE))) .stopOnError(Boolean.parseBoolean(System.getProperty(VALIDATION_STOP_ON_ERROR))) .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)))) { return XmlSchemaParser.parse(in, optionsBuilder.build()); } }
/** * Creates an instance of {@link ParserOptions} with all the values set. * * @return an instance of {@link ParserOptions} with all the values set. */ public ParserOptions build() { return new ParserOptions(stopOnError, warningsFatal, suppressOutput, xIncludeAware, xsdFilename); } }
/** * Construct a new {@link ErrorHandler} that outputs to a provided {@link PrintStream}. * * @param stream to which output should be sent. * @param options the parsing options. */ public ErrorHandler(final PrintStream stream, final ParserOptions options) { out = stream; stopOnError = options.stopOnError(); warningsFatal = options.warningsFatal(); suppressOutput = options.suppressOutput(); }
/** * Validate the document against a given schema. Error will be written to {@link java.lang.System#err} * * @param xsdFilename schema to validate against. * @param in document to be validated. * @param options to be applied during parsing. * @throws Exception if an error occurs when parsing the document or schema. */ public static void validate(final String xsdFilename, final InputStream in, final ParserOptions options) throws Exception { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); factory.setSchema(schemaFactory.newSchema(new File(xsdFilename))); factory.setNamespaceAware(true); if (options.xIncludeAware()) { factory.setXIncludeAware(true); factory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false); } factory.newDocumentBuilder().parse(in); }
if (options.xIncludeAware())
/** * Validate the SBE Schema against the XSD. * * @param sbeSchemaFilename to be validated * @param xsdFilename XSD against which to validate * @throws Exception if an error occurs while validating */ public static void validateAgainstSchema(final String sbeSchemaFilename, final String xsdFilename) throws Exception { final ParserOptions.Builder optionsBuilder = ParserOptions.builder() .xsdFilename(System.getProperty(VALIDATION_XSD)) .xIncludeAware(Boolean.parseBoolean(System.getProperty(XINCLUDE_AWARE))) .stopOnError(Boolean.parseBoolean(System.getProperty(VALIDATION_STOP_ON_ERROR))) .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)))) { XmlSchemaParser.validate(xsdFilename, in, optionsBuilder.build()); } }
/** * Construct a new {@link ErrorHandler} that outputs to a provided {@link PrintStream}. * * @param stream to which output should be sent. * @param options the parsing options. */ public ErrorHandler(final PrintStream stream, final ParserOptions options) { out = stream; stopOnError = options.stopOnError(); warningsFatal = options.warningsFatal(); suppressOutput = options.suppressOutput(); }
/** * Creates an instance of {@link ParserOptions} with all the values set. * * @return an instance of {@link ParserOptions} with all the values set. */ public ParserOptions build() { return new ParserOptions(stopOnError, warningsFatal, suppressOutput, xIncludeAware, xsdFilename); } }
/** * Validate the document against a given schema. Error will be written to {@link java.lang.System#err} * * @param xsdFilename schema to validate against. * @param in document to be validated. * @param options to be applied during parsing. * @throws Exception if an error occurs when parsing the document or schema. */ public static void validate(final String xsdFilename, final InputStream in, final ParserOptions options) throws Exception { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); factory.setSchema(schemaFactory.newSchema(new File(xsdFilename))); factory.setNamespaceAware(true); if (options.xIncludeAware()) { factory.setXIncludeAware(true); factory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false); } factory.newDocumentBuilder().parse(in); }
private static Map<String, Type> parseTestXmlWithMap(final String xPathExpr, final String xml) throws ParserConfigurationException, XPathExpressionException, IOException, SAXException { final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( new ByteArrayInputStream(xml.getBytes())); final XPath xPath = XPathFactory.newInstance().newXPath(); final NodeList list = (NodeList)xPath.compile(xPathExpr).evaluate(document, XPathConstants.NODESET); final Map<String, Type> map = new HashMap<>(); final ParserOptions options = ParserOptions.builder().stopOnError(true).suppressOutput(true).build(); document.setUserData(XmlSchemaParser.ERROR_HANDLER_KEY, new ErrorHandler(options), null); for (int i = 0, size = list.getLength(); i < size; i++) { final Type t = new EncodedDataType(list.item(i)); map.put(t.name(), t); } return map; } }
/** * Construct a new {@link ErrorHandler} that outputs to a provided {@link PrintStream}. * * @param stream to which output should be sent. * @param options the parsing options. */ public ErrorHandler(final PrintStream stream, final ParserOptions options) { out = stream; stopOnError = options.stopOnError(); warningsFatal = options.warningsFatal(); suppressOutput = options.suppressOutput(); }
/** * Creates an instance of {@link ParserOptions} with all the values set. * * @return an instance of {@link ParserOptions} with all the values set. */ public ParserOptions build() { return new ParserOptions(stopOnError, warningsFatal, suppressOutput, xIncludeAware, xsdFilename); } }
/** * Validate the document against a given schema. Error will be written to {@link java.lang.System#err} * * @param xsdFilename schema to validate against. * @param in document to be validated. * @param options to be applied during parsing. * @throws Exception if an error occurs when parsing the document or schema. */ public static void validate(final String xsdFilename, final InputStream in, final ParserOptions options) throws Exception { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); factory.setSchema(schemaFactory.newSchema(new File(xsdFilename))); factory.setNamespaceAware(true); if (options.xIncludeAware()) { factory.setXIncludeAware(true); factory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false); } factory.newDocumentBuilder().parse(in); }
private static Map<String, Type> parseTestXmlWithMap(final String xPathExpr, final String xml) throws ParserConfigurationException, XPathExpressionException, IOException, SAXException { final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( new ByteArrayInputStream(xml.getBytes())); final XPath xPath = XPathFactory.newInstance().newXPath(); final NodeList list = (NodeList)xPath.compile(xPathExpr).evaluate(document, XPathConstants.NODESET); final Map<String, Type> map = new HashMap<>(); final ParserOptions options = ParserOptions.builder().stopOnError(true).suppressOutput(true).build(); document.setUserData(XmlSchemaParser.ERROR_HANDLER_KEY, new ErrorHandler(options), null); for (int i = 0, size = list.getLength(); i < size; i++) { final Type t = new CompositeType(list.item(i)); map.put(t.name(), t); } return map; } }
/** * Construct a new {@link ErrorHandler} that outputs to a provided {@link PrintStream}. * * @param stream to which output should be sent. * @param options the parsing options. */ public ErrorHandler(final PrintStream stream, final ParserOptions options) { out = stream; stopOnError = options.stopOnError(); warningsFatal = options.warningsFatal(); suppressOutput = options.suppressOutput(); }
/** * Creates an instance of {@link ParserOptions} with all the values set. * * @return an instance of {@link ParserOptions} with all the values set. */ public ParserOptions build() { return new ParserOptions(stopOnError, warningsFatal, suppressOutput, xsdFilename); } }
if (options.xIncludeAware())
private static Map<String, Type> parseTestXmlWithMap(final String xPathExpr, final String xml) throws ParserConfigurationException, XPathExpressionException, IOException, SAXException { final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( new ByteArrayInputStream(xml.getBytes())); final XPath xPath = XPathFactory.newInstance().newXPath(); final NodeList list = (NodeList)xPath.compile(xPathExpr).evaluate(document, XPathConstants.NODESET); final Map<String, Type> map = new HashMap<>(); final ParserOptions options = ParserOptions.builder() .stopOnError(true) .suppressOutput(true) .warningsFatal(true) .build(); document.setUserData(XmlSchemaParser.ERROR_HANDLER_KEY, new ErrorHandler(options), null); for (int i = 0, size = list.getLength(); i < size; i++) { final Type t = new EnumType(list.item(i)); map.put(t.name(), t); } return map; } }
if (options.xIncludeAware())
private static Map<String, Type> parseTestXmlWithMap(final String xPathExpr, final String xml) throws ParserConfigurationException, XPathExpressionException, IOException, SAXException { final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( new ByteArrayInputStream(xml.getBytes())); final XPath xPath = XPathFactory.newInstance().newXPath(); final NodeList list = (NodeList)xPath.compile(xPathExpr).evaluate(document, XPathConstants.NODESET); final Map<String, Type> map = new HashMap<>(); final ParserOptions options = ParserOptions.builder() .stopOnError(true) .suppressOutput(true) .warningsFatal(true) .build(); document.setUserData(XmlSchemaParser.ERROR_HANDLER_KEY, new ErrorHandler(options), null); for (int i = 0, size = list.getLength(); i < size; i++) { final Type t = new SetType(list.item(i)); map.put(t.name(), t); } return map; } }