Class JsonXmlUtils

java.lang.Object
uk.blankaspect.common.jsonxml.JsonXmlUtils

public class JsonXmlUtils extends Object
This class provides utility methods that are related to JSON-XML.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Element
    child(Element element, int index)
    Returns the element at the specified index in the sequence of JSON-XML elements that are children of the specified XML element.
    Returns an iterator over the JSON-XML elements that are children of the specified XML element.
    static List<Element>
    children(Element element)
    Returns a list of the child nodes of the specified XML element that are JSON-XML elements.
    static List<Node>
    clean(Element element)
    Removes all the descendant nodes of the specified XML element that are not JSON-XML elements.
    static Element
    copy(IElementFacade sourceElementFacade, IElementFacade destElementFacade, Element element)
    Creates and returns a deep copy of the specified JSON-XML element.
    static int
    Returns a count of the child nodes of the specified XML element that are JSON-XML elements.
    static int
    Returns a count of the descendant nodes of the specified XML element that are JSON-XML elements.
    Returns a new instance of an element facade that is associated with the owner document of the specified XML element.
    static boolean
    equals(Element element1, Element element2)
    Performs a deep comparison of the specified pair of JSON-XML elements and returns true if the elements are equal to each other.
    static boolean
    equals(IElementFacade elementFacade1, Element element1, IElementFacade elementFacade2, Element element2)
    Performs a deep comparison of the specified pair of JSON-XML elements and returns true if the elements are equal to each other.
    static boolean
    equals(IElementFacade elementFacade1, Element element1, IElementFacade elementFacade2, Element element2, boolean includeNameAttr)
    Performs a deep comparison of the specified pair of JSON-XML elements and returns true if the elements are equal to each other.
    static boolean
    equalsIgnoreNS(Element element1, Element element2)
    Performs a deep comparison of the specified pair of JSON-XML elements and returns true if the elements are equal to each other.
    static boolean
    equalsIgnoreNS(Element element1, Element element2, boolean includeNameAttr)
    Performs a deep comparison of the specified pair of JSON-XML elements and returns true if the elements are equal to each other.
    static String
    getAttrIgnoreNS(Element element, String attrName)
    Returns the value of the attribute of the specified XML element with the specified attribute name, ignoring any namespace prefix of the attribute.
    static String
    getName(Element element)
    Returns the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object.
    static String
    getName(IElementFacade elementFacade, Element element)
    Returns the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object.
    static String
    Returns the value of the name attribute of the specified XML element, ignoring any namespace prefix of the attribute.
    static String
    getValue(Element element)
    Returns the value of the value attribute of the specified XML element, which corresponds to a JSON value.
    static String
    getValue(IElementFacade elementFacade, Element element)
    Returns the value of the value attribute of the specified XML element, which corresponds to a JSON value.
    static String
    Returns the value of the value attribute of the specified XML element, ignoring any namespace prefix of the attribute.
    static int
    hashCode(Element element)
    Returns a deep hash code for the specified XML element.
    static int
    hashCode(IElementFacade elementFacade, Element element)
    Returns a deep hash code for the specified XML element.
    static int
    hashCode(IElementFacade elementFacade, Element element, boolean includeNameAttr)
    Returns a deep hash code for the specified XML element.
    static Element
    parent(Element element)
    Returns the parent of the specified element, which is assumed to be an element.
    static void
    setName(Element element, String name)
    Sets the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object.
    static void
    setName(IElementFacade elementFacade, Element element, String name)
    Sets the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object.
    static void
    setValue(Element element, String value)
    Sets the value of the value attribute of the specified XML element, which corresponds to a JSON value.
    static void
    setValue(IElementFacade elementFacade, Element element, String value)
    Sets the value of the value attribute of the specified XML element, which corresponds to a JSON value.
    static String
    toJsonText(Element element, boolean printableAsciiOnly)
    Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and returns the text.
    static String
    toJsonText(IElementFacade elementFacade, Element element, boolean printableAsciiOnly)
    Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and returns the text.
    static String
    toXmlText(Element element, int indent, int indentIncrement)
    Generates a textual representation of the tree of JSON-XML elements whose root is the specified XML element and returns the text.
    static void
    writeJson(Element element, boolean printableAsciiOnly, Writer writer)
    Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and writes the text to the specified character stream.
    static void
    writeJson(IElementFacade elementFacade, Element element, boolean printableAsciiOnly, Writer writer)
    Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and writes the text to the specified character stream.
    static void
    writeXml(Element element, int indent, int indentIncrement, Writer writer)
    Generates a textual representation of the tree of JSON-XML elements whose root is the specified XML element and writes the text to the specified character stream.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getName

      public static String getName(Element element)
      Returns the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object.
      Parameters:
      element - the target element.
      Returns:
      the value of the name attribute of element, or null if the element does not have such an attribute.
    • getName

      public static String getName(IElementFacade elementFacade, Element element)
      Returns the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object. If an element facade is supplied, it is used to access the attribute. An instance of SimpleElementFacade may be used to apply a namespace prefix to the name of the attribute.
      Parameters:
      elementFacade - the facade through which the name attribute will be accessed. If it is null, the name attribute is accessed by calling getAttribute(AttrName.NAME) on element.
      element - the target element.
      Returns:
      the value of the name attribute of element, or null if the element does not have such an attribute.
    • setName

      public static void setName(Element element, String name)
      Sets the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object.
      Parameters:
      element - the target element.
      name - the value to which the name attribute will be set.
    • setName

      public static void setName(IElementFacade elementFacade, Element element, String name)
      Sets the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object. If an element facade is supplied, it is used to access the attribute. An instance of SimpleElementFacade may be used to apply a namespace prefix to the name of the attribute.
      Parameters:
      elementFacade - the facade through which the name attribute will be accessed. If it is null, the name attribute is accessed by calling setAttribute(AttrName.NAME, name) on element.
      element - the target element.
      name - the value to which the name attribute will be set.
    • getValue

      public static String getValue(Element element)
      Returns the value of the value attribute of the specified XML element, which corresponds to a JSON value.
      Parameters:
      element - the target element.
      Returns:
      the value of the value attribute of element, or null if the element does not have such an attribute.
    • getValue

      public static String getValue(IElementFacade elementFacade, Element element)
      Returns the value of the value attribute of the specified XML element, which corresponds to a JSON value. If an element facade is supplied, it is used to access the attribute. An instance of SimpleElementFacade may be used to apply a namespace prefix to the name of the attribute.
      Parameters:
      elementFacade - the facade through which the value attribute will be accessed. If it is null, the value attribute is accessed by calling getAttribute(AttrName.VALUE) on element.
      element - the target element.
      Returns:
      the value of the value attribute of element, or null if the element does not have such an attribute.
    • setValue

      public static void setValue(Element element, String value)
      Sets the value of the value attribute of the specified XML element, which corresponds to a JSON value.
      Parameters:
      element - the target element.
      value - the value to which the value attribute will be set.
    • setValue

      public static void setValue(IElementFacade elementFacade, Element element, String value)
      Sets the value of the value attribute of the specified XML element, which corresponds to a JSON value. If an element facade is supplied, it is used to access the attribute. An instance of SimpleElementFacade may be used to apply a namespace prefix to the name of the attribute.
      Parameters:
      elementFacade - the facade through which the value attribute will be accessed. If it is null, the value attribute is accessed by calling setAttribute(AttrName.VALUE, name) on element.
      element - the target element.
      value - the value to which the value attribute will be set.
    • getNameIgnoreNS

      public static String getNameIgnoreNS(Element element)
      Returns the value of the name attribute of the specified XML element, ignoring any namespace prefix of the attribute. The name attribute of the element corresponds to the name of a member of a JSON object.
      Parameters:
      element - the target element.
      Returns:
      the value of the name attribute of element, ignoring any namespace prefix of the attribute, or null if the element does not have such an attribute.
    • getValueIgnoreNS

      public static String getValueIgnoreNS(Element element)
      Returns the value of the value attribute of the specified XML element, ignoring any namespace prefix of the attribute. The value attribute of the element corresponds to a JSON value.
      Parameters:
      element - the target element.
      Returns:
      the value of the value attribute of element, ignoring any namespace prefix of the attribute, or null if the element does not have such an attribute.
    • getAttrIgnoreNS

      public static String getAttrIgnoreNS(Element element, String attrName)
      Returns the value of the attribute of the specified XML element with the specified attribute name, ignoring any namespace prefix of the attribute.
      Parameters:
      element - the target element.
      attrName - the name of the target attribute, without a namespace prefix.
      Returns:
      the value of the attribute of element that has the name attrName, ignoring any namespace prefix of the attribute, or null if the element does not have such an attribute.
    • elementFacade

      public static IElementFacade elementFacade(Element element)
      Returns a new instance of an element facade that is associated with the owner document of the specified XML element.
      Parameters:
      element - the element with whose owner document the new element facade will be associated.
      Returns:
      a element facade that is associated with the owner document of element.
      Throws:
      IllegalStateException - if element has no owner document.
    • parent

      public static Element parent(Element element)
      Returns the parent of the specified element, which is assumed to be an element.
      Parameters:
      element - the target element.
      Returns:
      the parent of element, or null if the element does not have a parent.
    • countChildren

      public static int countChildren(Element element)
      Returns a count of the child nodes of the specified XML element that are JSON-XML elements. Any child node that is not a JSON-XML elements is omitted from the count.
      Parameters:
      element - the target element.
      Returns:
      a count of the JSON-XML elements that are children of element.
    • countDescendants

      public static int countDescendants(Element element)
      Returns a count of the descendant nodes of the specified XML element that are JSON-XML elements. Any descendant node that is not a JSON-XML element is omitted from the count.
      Parameters:
      element - the target element.
      Returns:
      a count of the JSON-XML elements that are descendants of element.
    • children

      public static List<Element> children(Element element)
      Returns a list of the child nodes of the specified XML element that are JSON-XML elements. Any child node that is not a JSON-XML element is omitted from the list.
      Parameters:
      element - the target element.
      Returns:
      a modifiable list of the JSON-XML elements that are children of element.
    • child

      public static Element child(Element element, int index)
      Returns the element at the specified index in the sequence of JSON-XML elements that are children of the specified XML element. Any child node of the specified element that is not a JSON-XML element is ignored.
      Parameters:
      element - the target element.
      index - the index of the desired element in the sequence of JSON-XML children of element.
      Returns:
      the element at index in the sequence of JSON-XML elements that are children of element, or null if index is out of bounds.
    • childIterator

      public static Iterator<Element> childIterator(Element element)
      Returns an iterator over the JSON-XML elements that are children of the specified XML element. The iterator ignores any child node of the specified element that is not a JSON-XML element.

      The returned iterator does not support the remove operation. The behaviour of an iterator is undefined if the list of children of the specified element is modified structurally while an iteration is in progress.

      Parameters:
      element - the target element.
      Returns:
      an iterator over the JSON-XML elements that are children of element.
    • clean

      public static List<Node> clean(Element element)
      Removes all the descendant nodes of the specified XML element that are not JSON-XML elements.
      Parameters:
      element - the target element.
      Returns:
      a list of the non-JSON-XML descendants of element for which an error occurred when trying to remove them from their parent.
    • equals

      public static boolean equals(Element element1, Element element2)
      Performs a deep comparison of the specified pair of JSON-XML elements and returns true if the elements are equal to each other. If either of the elements has a name attribute, the attribute is ignored when comparing the elements.
      Parameters:
      element1 - the first element.
      element2 - the second element.
      Returns:
      true if element1 and element2 are deeply equal to each other or if both elements are null; false otherwise.
      Throws:
      ElementKind.UnexpectedKindException - if either element1 or element2 is not a JSON-XML element.
    • equals

      public static boolean equals(IElementFacade elementFacade1, Element element1, IElementFacade elementFacade2, Element element2)
      Performs a deep comparison of the specified pair of JSON-XML elements and returns true if the elements are equal to each other. If either of the elements has a name attribute, the attribute is ignored when comparing the elements.

      If an element facade is supplied for an element, it will be used to access the attributes of the element and its descendants. An instance of SimpleElementFacade may be used to apply a namespace prefix to the name of an attribute.

      Parameters:
      elementFacade1 - the facade through which the attributes of the first element and its descendants will be accessed. If it is null, attributes are accessed by calling getAttribute(String) on an element.
      element1 - the first element.
      elementFacade2 - the facade through which the attributes of the second element and its descendants will be accessed. If it is null, attributes are accessed by calling getAttribute(String) on an element.
      element2 - the second element.
      Returns:
      true if element1 and element2 are deeply equal to each other or if both elements are null; false otherwise.
      Throws:
      ElementKind.UnexpectedKindException - if either element1 or element2 is not a JSON-XML element.
    • equals

      public static boolean equals(IElementFacade elementFacade1, Element element1, IElementFacade elementFacade2, Element element2, boolean includeNameAttr)
      Performs a deep comparison of the specified pair of JSON-XML elements and returns true if the elements are equal to each other.

      If an element facade is supplied for an element, it will be used to access the attributes of the element and its descendants. An instance of SimpleElementFacade may be used to apply a namespace prefix to the name of an attribute.

      Parameters:
      elementFacade1 - the facade through which the attributes of the first element and its descendants will be accessed. If it is null, attributes are accessed by calling getAttribute(String) on an element.
      element1 - the first element.
      elementFacade2 - the facade through which the attributes of the second element and its descendants will be accessed. If it is null, attributes are accessed by calling getAttribute(String) on an element.
      element2 - the second element.
      includeNameAttr - if true, the name attributes of element1 and element2 will be included in the comparison of the elements.
      Returns:
      true if element1 and element2 are deeply equal to each other or if both elements are null; false otherwise.
      Throws:
      ElementKind.UnexpectedKindException - if either element1 or element2 is not a JSON-XML element.
    • equalsIgnoreNS

      public static boolean equalsIgnoreNS(Element element1, Element element2)
      Performs a deep comparison of the specified pair of JSON-XML elements and returns true if the elements are equal to each other. When comparing the name and value attributes of JSON-XML elements, any namespace prefix of an attribute is ignored. If either of the elements has a name attribute, the attribute is ignored when comparing the elements.
      Parameters:
      element1 - the first element.
      element2 - the second element.
      Returns:
      true if element1 and element2 are deeply equal to each other or if both elements are null; false otherwise.
      Throws:
      ElementKind.UnexpectedKindException - if either element1 or element2 is not a JSON-XML element.
    • equalsIgnoreNS

      public static boolean equalsIgnoreNS(Element element1, Element element2, boolean includeNameAttr)
      Performs a deep comparison of the specified pair of JSON-XML elements and returns true if the elements are equal to each other. When comparing the name and value attributes of JSON-XML elements, any namespace prefix of an attribute is ignored.
      Parameters:
      element1 - the first element.
      element2 - the second element.
      includeNameAttr - if true, the name attributes of element1 and element2 will be included in the comparison of the elements.
      Returns:
      true if element1 and element2 are deeply equal to each other or if both elements are null; false otherwise.
      Throws:
      ElementKind.UnexpectedKindException - if either element1 or element2 is not a JSON-XML element.
    • hashCode

      public static int hashCode(Element element)
      Returns a deep hash code for the specified XML element.
      Parameters:
      element - the target element.
      Returns:
      a hash code for element.
    • hashCode

      public static int hashCode(IElementFacade elementFacade, Element element)
      Returns a deep hash code for the specified XML element. If an element facade is supplied, it will be used to access the attributes of JSON-XML elements. An instance of SimpleElementFacade may be used to apply a namespace prefix to the name of the attribute.
      Parameters:
      elementFacade - the facade through which the attributes of JSON-XML elements will be accessed. If it is null, attributes are accessed by calling getAttribute(String) on an element.
      element - the target element.
      Returns:
      a hash code for element.
    • hashCode

      public static int hashCode(IElementFacade elementFacade, Element element, boolean includeNameAttr)
      Returns a deep hash code for the specified XML element. If an element facade is supplied, it will be used to access the attributes of JSON-XML elements. An instance of SimpleElementFacade may be used to apply a namespace prefix to the name of the attribute.
      Parameters:
      elementFacade - the facade through which the attributes of JSON-XML elements will be accessed. If it is null, attributes are accessed by calling getAttribute(String) on an element.
      element - the target element.
      includeNameAttr - if true, the name attribute of element will be included in the calculation of the hash code.
      Returns:
      a hash code for element.
    • copy

      public static Element copy(IElementFacade sourceElementFacade, IElementFacade destElementFacade, Element element)
      Creates and returns a deep copy of the specified JSON-XML element. The specified facades are used to create destination elements and to access attributes of source and destination elements.
      Parameters:
      sourceElementFacade - the facade through which the attributes of source JSON-XML elements will be accessed. If it is null, attributes are accessed by calling getAttribute(String) on a source element.
      destElementFacade - the facade through which destination JSON-XML elements will be created and the attributes of destination elements will be accessed. It must not be null.
      element - the element for which a copy is desired.
      Returns:
      a deep copy of element, if it is a JSON-XML element; otherwise, null.
    • writeJson

      public static void writeJson(Element element, boolean printableAsciiOnly, Writer writer) throws IOException
      Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and writes the text to the specified character stream.
      Parameters:
      element - the root of the tree of JSON-XML elements that will be written as JSON text.
      printableAsciiOnly - if true, JSON string values and the names of the members of JSON objects will be escaped so that they contain only printable characters from the US-ASCII character encoding (ie, characters in the range U+0020 to U+007E inclusive).
      writer - the character stream to which the JSON text will be written.
      Throws:
      IOException - if an error occurs when writing the JSON text to the character stream.
    • writeJson

      public static void writeJson(IElementFacade elementFacade, Element element, boolean printableAsciiOnly, Writer writer) throws IOException
      Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and writes the text to the specified character stream.

      If an element facade is supplied, it is used to access the attributes of the JSON-XML elements. An instance of SimpleElementFacade may be used to apply a namespace prefix to the names of attributes.

      Parameters:
      elementFacade - the facade through which the attributes of JSON-XML elements will be accessed. If it is null, the attributes will be accessed directly through the elements.
      element - the root of the tree of JSON-XML elements that will be written as JSON text.
      printableAsciiOnly - if true, JSON string values and the names of the members of JSON objects will be escaped so that they contain only printable characters from the US-ASCII character encoding (ie, characters in the range U+0020 to U+007E inclusive).
      writer - the character stream to which the JSON text will be written.
      Throws:
      IOException - if an error occurs when writing the JSON text to the character stream.
    • toJsonText

      public static String toJsonText(Element element, boolean printableAsciiOnly)
      Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and returns the text.
      Parameters:
      element - the root of the tree of JSON-XML elements that will be converted to JSON text.
      printableAsciiOnly - if true, JSON string values and the names of the members of JSON objects will be escaped so that they contain only printable characters from the US-ASCII character encoding (ie, characters in the range U+0020 to U+007E inclusive).
      Returns:
      the result of converting the tree of JSON-XML elements whose root is element to JSON text.
    • toJsonText

      public static String toJsonText(IElementFacade elementFacade, Element element, boolean printableAsciiOnly)
      Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and returns the text.

      If an element facade is supplied, it is used to access the attributes of the JSON-XML elements. An instance of SimpleElementFacade may be used to apply a namespace prefix to the names of attributes.

      Parameters:
      elementFacade - the facade through which the attributes of JSON-XML elements will be accessed. If it is null, the attributes will be accessed through the elements.
      element - the root of the tree of JSON-XML elements that will be converted to JSON text.
      printableAsciiOnly - if true, JSON string values and the names of the members of JSON objects will be escaped so that they contain only printable characters from the US-ASCII character encoding (ie, characters in the range U+0020 to U+007E inclusive).
      Returns:
      the result of converting the tree of JSON-XML elements whose root is element to JSON text.
    • writeXml

      public static void writeXml(Element element, int indent, int indentIncrement, Writer writer) throws IOException
      Generates a textual representation of the tree of JSON-XML elements whose root is the specified XML element and writes the text to the specified character stream.
      Parameters:
      element - the root of the tree of JSON-XML elements for which a textual representation will be generated.
      indent - the number of spaces by which the element at the root of XML tree will be indented.
      indentIncrement - the number of spaces by which each additional level of the XML tree will be indented.
      writer - the character stream to which the text will be written.
      Throws:
      IOException - if an error occurs when writing the text to the character stream.
    • toXmlText

      public static String toXmlText(Element element, int indent, int indentIncrement)
      Generates a textual representation of the tree of JSON-XML elements whose root is the specified XML element and returns the text.
      Parameters:
      element - the root of the tree of JSON-XML elements for which a textual representation will be generated.
      indent - the number of spaces by which the element at the root of XML tree will be indented.
      indentIncrement - the number of spaces by which each additional level of the XML tree below the root will be indented.
      Returns:
      a textual representation of the tree of JSON-XML elements whose root is element.