Class XmlUtils

java.lang.Object
org.apache.juneau.marshall.xml.XmlUtils

public class XmlUtils extends Object
XML utility methods.
See Also:
  • Method Details

    • collapseTextNodes

      public static List<Object> collapseTextNodes(List<Object> value)
      Given a list of Strings and other Objects, combines Strings that are next to each other in the list.
      Parameters:
      value - The list of text nodes to collapse.
      Must not be null.
      Returns:
      The same list.
    • decode

      public static String decode(String value, StringBuilder sb)
      Translates any _x####_ sequences (introduced by the various encode methods) back into their original characters.
      Parameters:
      value - The string being decoded.
      Can be null (returns null).
      sb - The string builder to use as a scratch pad.
      Can be null (a new one is created).
      Returns:
      The decoded string, or null if the input was null or represented a null value.
    • encodeAttrName

      public static Writer encodeAttrName(Writer w, Object value) throws IOException
      Serializes and encodes the specified object as valid XML attribute name.
      Parameters:
      w - The writer to send the output to.
      Must not be null.
      value - The object being serialized.
      Can be null (written as the _x0000_ null marker).
      Returns:
      This object.
      Throws:
      IOException - If a problem occurred.
    • encodeAttrValue

      public static Writer encodeAttrValue(Writer w, Object value, boolean trim)
      Encodes the specified attribute value and sends the results to the specified writer.

      Encodes any invalid XML text characters to _x####_ sequences and sends the response to the specified writer.
      Encodes '&', '<', '>', '"', and '\'' as XML entities.
      Encodes invalid XML text characters to _x####_ sequences.

      Parameters:
      w - The writer to send the output to.
      Must not be null.
      value - The object being encoded.
      Can be null (written as the _x0000_ null marker).
      trim - Trim the text before serializing it. If true, leading and trailing whitespace characters will be encoded.
      Returns:
      The same writer passed in.
    • encodeElementName

      public static String encodeElementName(Object value)
      Encodes any invalid XML element name characters to _x####_ sequences.
      Parameters:
      value - The object being encoded.
      Can be null (returns the _x0000_ null marker).
      Returns:
      The encoded element name string.
    • encodeElementName

      public static Writer encodeElementName(Writer w, Object value)
      Encodes any invalid XML element name characters to _x####_ sequences.
      Parameters:
      w - The writer to send the output to.
      Must not be null.
      value - The object being encoded.
      Can be null (written as the _x0000_ null marker).
      Returns:
      The same writer passed in.
    • encodeText

      public static Writer encodeText(Writer w, Object value, boolean trim, boolean preserveWhitespace)
      Encodes the specified element text and sends the results to the specified writer.

      Encodes any invalid XML text characters to _x####_ sequences and sends the response to the specified writer.
      Encodes '&', '<', and '>' as XML entities.
      Encodes invalid XML text characters to _x####_ sequences.

      Parameters:
      w - The writer to send the output to.
      Must not be null.
      value - The object being encoded.
      Can be null (written as the _x0000_ null marker).
      trim - Trim the text before serializing it.
      preserveWhitespace - Specifies whether we're in preserve-whitespace mode. (e.g. XmlFormat.MIXED_PWS or XmlFormat.TEXT_PWS. If true, leading and trailing whitespace characters will be encoded.
      Returns:
      The same writer passed in.
    • escapeText

      public static String escapeText(Object value)
      Escapes invalid XML text characters to _x####_ sequences.
      Parameters:
      value - The object being encoded.
      Can be null (returns the _x0000_ null marker).
      Returns:
      The encoded string.
    • findNamespace

      public static Namespace findNamespace(List<Xml> xmls, List<XmlSchema> schemas)
      Find the namespace given a list of @Xml and @XmlSchema annotations.

      The annotations should be a parent-to-child ordering of annotations found on a class or method.

      Parameters:
      xmls - The list of @Xml annotations.
      Must not be null.
      schemas - The list of @XmlSchema annotations.
      Must not be null.
      Returns:
      The namespace, or null if it couldn't be found.
    • toReadableEvent

      Utility method that converts the current event on the XML stream to something human-readable for debug purposes.
      Parameters:
      r - The XML stream reader whose current event is to be converted to a readable string.
      Must not be null.
      Returns:
      The event in human-readable form.