Class Json2Xml

All Implemented Interfaces:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler, XMLFilter, XMLReader

public class Json2Xml extends XmlAligner
XML Schema guided JSON to XML converter;
Author:
Gerrit van Brakel
  • Field Details

  • Constructor Details

    • Json2Xml

      public Json2Xml(ValidatorHandler validatorHandler, List<org.apache.xerces.xs.XSModel> schemaInformation, boolean insertElementContainerElements, String rootElement, boolean strictSyntax)
  • Method Details

    • startParse

      public void startParse(jakarta.json.JsonValue node) throws SAXException
      Throws:
      SAXException
    • handleElementContents

      public void handleElementContents(org.apache.xerces.xs.XSElementDeclaration elementDeclaration, jakarta.json.JsonValue node) throws SAXException
      Throws:
      SAXException
    • getNodeText

      public String getNodeText(jakarta.json.JsonValue node)
    • isNil

      public boolean isNil(jakarta.json.JsonValue node)
    • getAttributes

      @Nonnull public Map<String,String> getAttributes(org.apache.xerces.xs.XSElementDeclaration elementDeclaration, jakarta.json.JsonValue node) throws SAXException
      Throws:
      SAXException
    • hasChild

      public boolean hasChild(jakarta.json.JsonValue node, String childName) throws SAXException
      Throws:
      SAXException
    • getAllNodeChildNames

      @Nonnull public Set<String> getAllNodeChildNames(jakarta.json.JsonValue node) throws SAXException
      Throws:
      SAXException
    • getNodeChildrenByName

      @Nullable public Iterable<jakarta.json.JsonValue> getNodeChildrenByName(jakarta.json.JsonValue node, org.apache.xerces.xs.XSElementDeclaration childElementDeclaration) throws SAXException
      Throws:
      SAXException
    • getSubstitutedChild

      @Nullable protected jakarta.json.JsonValue getSubstitutedChild(String childName)
    • getOverride

      protected String getOverride(jakarta.json.JsonValue node)
    • processChildElement

      protected void processChildElement(jakarta.json.JsonValue node, String parentName, org.apache.xerces.xs.XSElementDeclaration childElementDeclaration, boolean mandatory, Set<String> processedChildren) throws SAXException
      Throws:
      SAXException
    • isEmptyNode

      protected boolean isEmptyNode(jakarta.json.JsonValue node)
    • filterNodeChildren

      @Nonnull protected jakarta.json.JsonValue filterNodeChildren(jakarta.json.JsonValue node, List<org.apache.xerces.xs.XSParticle> allowedChildren)
      Create a copy of the JSON node that contains only keys from the allowedNames set in the top level.
      Parameters:
      node - Node to copy
      allowedChildren - Names of child-nodes to keep in the copy
      Returns:
      Copy of the JSON node.
    • translate

      public static String translate(jakarta.json.JsonStructure jsonStructure, URL schemaURL, boolean compactJsonArrays, String rootElement, String targetNamespace) throws SAXException
      Helper method for tests
      Throws:
      SAXException
    • create

      public static Json2Xml create(URL schemaURL, boolean compactJsonArrays, String rootElement, boolean strictSyntax, boolean deepSearch, String targetNamespace, Map<String,Object> overrideValues) throws SAXException
      Helper method for tests
      Throws:
      SAXException
    • getChildrenByName

      public final Iterable<jakarta.json.JsonValue> getChildrenByName(jakarta.json.JsonValue node, org.apache.xerces.xs.XSElementDeclaration childElementDeclaration) throws SAXException
      Throws:
      SAXException
    • getText

      public final String getText(org.apache.xerces.xs.XSElementDeclaration elementDeclaration, jakarta.json.JsonValue node)
    • getUnprocessedChildElementNames

      protected Set<String> getUnprocessedChildElementNames(jakarta.json.JsonValue node, Set<String> processedChildren) throws SAXException
      Throws:
      SAXException
    • setSubstitutionProvider

      public void setSubstitutionProvider(SubstitutionProvider<?> substitutions)
    • setOverrideValues

      public void setOverrideValues(Map<String,Object> overrideValues)
    • asSource

      public Source asSource(jakarta.json.JsonValue container)
      Obtain the XmlAligner as a Source that can be used as input of a Transformer.
    • parse

      public void parse(InputSource input) throws SAXException, IOException
      Start the parse, obtain the container to parse from the InputSource when set by asSource(JsonValue). Normally, the parse is started via {#startParse(C container)}, but this implementation allows asSource(JsonValue) to function.
      Specified by:
      parse in interface XMLReader
      Overrides:
      parse in class XMLFilterImpl
      Throws:
      SAXException
      IOException
    • handleRootNode

      public void handleRootNode(jakarta.json.JsonValue container, String name, String nodeNamespace) throws SAXException
      Pushes node through validator. Must push all nodes through validatorhandler, recursively, respecting the alignment request. Must set current=node before calling validatorHandler.startElement(), in order to get the right argument for the onStartElement / performAlignment callbacks.
      Throws:
      SAXException
    • handleElement

      public void handleElement(org.apache.xerces.xs.XSElementDeclaration elementDeclaration, jakarta.json.JsonValue node) throws SAXException
      Throws:
      SAXException
    • handleComplexTypedElement

      protected void handleComplexTypedElement(org.apache.xerces.xs.XSElementDeclaration elementDeclaration, jakarta.json.JsonValue node) throws SAXException
      Throws:
      SAXException
    • handleSimpleTypedElement

      protected void handleSimpleTypedElement(org.apache.xerces.xs.XSElementDeclaration elementDeclaration, jakarta.json.JsonValue node) throws SAXException
      Throws:
      SAXException
    • getBestChildElementPath

      @Nonnull public List<org.apache.xerces.xs.XSParticle> getBestChildElementPath(org.apache.xerces.xs.XSElementDeclaration elementDeclaration, jakarta.json.JsonValue node, boolean silent) throws SAXException
      Throws:
      SAXException
    • getBestMatchingElementPath

      public boolean getBestMatchingElementPath(org.apache.xerces.xs.XSElementDeclaration baseElementDeclaration, jakarta.json.JsonValue baseNode, org.apache.xerces.xs.XSParticle particle, List<org.apache.xerces.xs.XSParticle> path, List<String> failureReasons) throws SAXException
      Parameters:
      baseElementDeclaration - XSD Type Declaration of the base element
      baseNode - Node from which to search for path
      particle - XSD Particle for which to search for path
      failureReasons - returns the reasons why no match was found
      path - in this list the longest list of child elements, that matches the available, is maintained. Null if no matching.
      Returns:
      true when a matching path is found. if false, failureReasons will contain reasons why.
      Throws:
      SAXException - If there was any exception
    • sendString

      protected void sendString(String string) throws SAXException
      Throws:
      SAXException
    • handleError

      public void handleError(String msg) throws SAXException
      Throws:
      SAXException
    • handleError

      public void handleError(SAXException e) throws SAXException
      Throws:
      SAXException
    • getQName

      public String getQName(String namespace, String name) throws SAXException
      Throws:
      SAXException
    • getNamespacePrefix

      public String getNamespacePrefix(String uri) throws SAXException
      Throws:
      SAXException
    • translate

      public void translate(jakarta.json.JsonValue data, ContentHandler handler) throws SAXException
      Throws:
      SAXException
    • translate

      public String translate(jakarta.json.JsonValue data) throws SAXException
      Throws:
      SAXException
    • isReadAttributes

      public boolean isReadAttributes()
    • setReadAttributes

      public void setReadAttributes(boolean readAttributes)
    • getRootElement

      public String getRootElement()
    • setRootElement

      public void setRootElement(String rootElement)
    • getTargetNamespace

      public String getTargetNamespace()
    • setTargetNamespace

      public void setTargetNamespace(String targetNamespace)
    • isDeepSearch

      public boolean isDeepSearch()
    • setDeepSearch

      public void setDeepSearch(boolean deepSearch)
    • isFailOnWildcards

      public boolean isFailOnWildcards()
    • setFailOnWildcards

      public void setFailOnWildcards(boolean failOnWildcards)