Class XmlSchemaParser

java.lang.Object
uk.co.real_logic.sbe.xml.XmlSchemaParser

public class XmlSchemaParser extends Object
Encapsulate the XML Schema parsing for SBE so that other representations may be used to generate independent representations.
  • Field Details

  • Constructor Details

    • XmlSchemaParser

      public XmlSchemaParser()
  • Method Details

    • validate

      public static void validate(String xsdFilename, InputSource is, ParserOptions options) throws Exception
      Validate the document against a given schema. Errors will be written to System.err.
      Parameters:
      xsdFilename - schema to validate against.
      is - source from which schema is read. Ideally it will have the systemId property set to resolve relative references.
      options - to be applied during parsing.
      Throws:
      Exception - if an error occurs when parsing the document or schema.
    • validate

      public static void validate(String xsdFilename, InputStream in, ParserOptions options) throws Exception
      Wraps the InputStream into an InputSource and delegates to validate(String, InputSource, ParserOptions).

      Note: this method does not set the InputSource.setSystemId(java.lang.String) property. However, it is recommended to use the validate(String, InputSource, ParserOptions) method directly.

      Parameters:
      xsdFilename - schema to validate against.
      in - document to be validated.
      options - to be applied during parsing.
      Throws:
      Exception - if an error occurs when parsing the document or schema.
    • parse

      public static MessageSchema parse(InputSource is, ParserOptions options) throws Exception
      Take an InputSource and parse it generating map of template ID to Message objects, types, and schema.
      Parameters:
      is - source from which schema is read. Ideally it will have the systemId property set to resolve relative references.
      options - to be applied during parsing.
      Returns:
      MessageSchema encoding for the schema.
      Throws:
      Exception - on parsing error.
    • parse

      public static MessageSchema parse(InputStream in, ParserOptions options) throws Exception
      Wraps the InputStream into an InputSource and delegates to parse(InputSource, ParserOptions).

      Note: this method does not set the InputSource.setSystemId(java.lang.String) property. However, it is recommended to use the parse(InputSource, ParserOptions) method directly.

      Parameters:
      in - stream from which schema is read.
      options - to be applied during parsing.
      Returns:
      MessageSchema encoding for the schema.
      Throws:
      Exception - on parsing error.
    • findTypes

      public static Map<String,Type> findTypes(Document document, XPath xPath) throws XPathExpressionException
      Scan XML for all types (encodedDataType, compositeType, enumType, and setType) and save in a map.
      Parameters:
      document - for the XML parsing.
      xPath - for XPath expression reuse.
      Returns:
      Map of name String to Type.
      Throws:
      XPathExpressionException - on parsing error.
    • findMessages

      public static Map<Long,Message> findMessages(Document document, XPath xPath, Map<String,Type> typeByNameMap) throws XPathExpressionException
      Scan XML for all message definitions and save in map.
      Parameters:
      document - for the XML parsing.
      xPath - for XPath expression reuse.
      typeByNameMap - to use for Type objects.
      Returns:
      Map of schemaId to Message.
      Throws:
      XPathExpressionException - on parsing error.
    • handleError

      public static void handleError(Node node, String msg)
      Handle an error condition as consequence of parsing.
      Parameters:
      node - that is the context of the warning.
      msg - associated with the error.
    • handleWarning

      public static void handleWarning(Node node, String msg)
      Handle a warning condition as a consequence of parsing.
      Parameters:
      node - as the context for the warning.
      msg - associated with the warning.
    • getAttributeValue

      public static String getAttributeValue(Node elementNode, String attrName)
      Helper function that throws an exception when the attribute is not set.
      Parameters:
      elementNode - that should have the attribute.
      attrName - that is to be looked up.
      Returns:
      value of the attribute.
      Throws:
      IllegalStateException - if the attribute is not present.
    • getAttributeValue

      public static String getAttributeValue(Node elementNode, String attrName, String defValue)
      Helper function that uses a default value when value not set.
      Parameters:
      elementNode - that should have the attribute.
      attrName - that is to be looked up.
      defValue - value to return if not set.
      Returns:
      value of the attribute or defValue.
    • getTypesPackageAttribute

      public static String getTypesPackageAttribute(Node elementNode)
      To be used with descendant elements of <types> elements. Returns the package attribute value as defined on the ancestor <types> element.
      Parameters:
      elementNode - the node inside the <types> element.
      Returns:
      the package name, or null if not defined.
    • getAttributeValueOrNull

      public static String getAttributeValueOrNull(Node elementNode, String attrName)
      Helper function that hides the null return from NamedNodeMap.getNamedItem(String).
      Parameters:
      elementNode - that could be null.
      attrName - that is to be looked up.
      Returns:
      null or value of the attribute.
    • getByteOrder

      public static ByteOrder getByteOrder(String byteOrderName)
      Helper function to convert a schema byteOrderName into a ByteOrder.
      Parameters:
      byteOrderName - specified as a FIX SBE string.
      Returns:
      ByteOrder representation.
    • checkForValidName

      public static void checkForValidName(Node node, String name)
      Check name against validity for C++ and Java naming. Warning if not valid.
      Parameters:
      node - to have the name checked.
      name - of the node to be checked.