Class XmlUtils

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

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

  • Method Details

    • encodeElementName

      public static final 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.
      value - The object being encoded.
      Returns:
      The same writer passed in.
    • encodeElementName

      public static final String encodeElementName(Object value)
      Encodes any invalid XML element name characters to _x####_ sequences.
      Parameters:
      value - The object being encoded.
      Returns:
      The encoded element name string.
    • escapeText

      public static final String escapeText(Object value)
      Escapes invalid XML text characters to _x####_ sequences.
      Parameters:
      value - The object being encoded.
      Returns:
      The encoded string.
    • encodeText

      public static final 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.
      value - The object being encoded.
      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.
    • encodeAttrName

      public static final 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.
      value - The object being serialized.
      Returns:
      This object.
      Throws:
      IOException - If a problem occurred.
    • encodeAttrValue

      public static final 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.
      value - The object being encoded.
      trim - Trim the text before serializing it. If true, leading and trailing whitespace characters will be encoded.
      Returns:
      The same writer passed in.
    • decode

      public static final 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.
      sb - The string builder to use as a scratch pad.
      Returns:
      The decoded string.
    • collapseTextNodes

      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.
      Returns:
      The same list.
    • 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.
      schemas - The list of @XmlSchema annotations.
      Returns:
      The namespace, or null if it couldn't be found.
    • toReadableEvent

      public static final String toReadableEvent(XMLStreamReader r)
      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.
      Returns:
      The event in human-readable form.