Class XMLEncode


  • final class XMLEncode
    extends java.lang.Object
    Collection of XML encoding/decoding helpers.
    This is all about the special characters & and <, and for attributes " and '. These must be encoded/decoded from/to XML.
    • Constructor Summary

      Constructors 
      Constructor Description
      XMLEncode()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean isCompatibleWithCDATABlock​(java.lang.String text)
      Can this text be stored into a CDATA block?
      static boolean isWhiteSpace​(java.lang.String text)
      Checks if this text purely consists of the white space characters ' ', TAB, NEWLINE.
      private static char lookAhead​(int la, int offset, java.lang.String data)  
      static boolean needsEncoding​(java.lang.String text)
      Checks if this text needs encoding in order to be represented in XML.
      static boolean needsEncoding​(java.lang.String data, boolean checkForAttr)
      Checks if this text needs encoding in order to be represented in XML.
      static java.lang.String xmlDecodeTextToCDATA​(java.lang.String pcdata)
      Make CDATA out of possibly encoded PCDATA.
      static java.lang.String xmlEncodeText​(java.lang.String text)
      Encodes text as XML in the most suitable way, either CDATA block or PCDATA.
      static void xmlEncodeText​(java.lang.String text, java.io.Writer writer)  
      static java.lang.String xmlEncodeTextAsCDATABlock​(java.lang.String text)
      Returns string as CDATA block if possible, otherwise null.
      static java.lang.String xmlEncodeTextAsPCDATA​(java.lang.String text)
      Encodes any text as PCDATA.
      static java.lang.String xmlEncodeTextAsPCDATA​(java.lang.String text, boolean forAttribute)
      Encodes any text as PCDATA.
      static java.lang.String xmlEncodeTextAsPCDATA​(java.lang.String text, boolean forAttribute, char quoteChar)
      Encodes any text as PCDATA.
      static void xmlEncodeTextAsPCDATA​(java.lang.String text, boolean forAttribute, char quoteChar, java.io.Writer n)  
      static java.lang.String xmlEncodeTextForAttribute​(java.lang.String text, char quoteChar)
      Makes any text fit into XML attributes.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • CDATA_BLOCK_THRESHOLD_LENGTH

        private static final int CDATA_BLOCK_THRESHOLD_LENGTH
        See Also:
        Constant Field Values
    • Constructor Detail

      • XMLEncode

        XMLEncode()
    • Method Detail

      • isWhiteSpace

        public static boolean isWhiteSpace​(java.lang.String text)
        Checks if this text purely consists of the white space characters ' ', TAB, NEWLINE.
      • xmlEncodeTextForAttribute

        public static java.lang.String xmlEncodeTextForAttribute​(java.lang.String text,
                                                                 char quoteChar)
        Makes any text fit into XML attributes.
      • xmlEncodeText

        public static java.lang.String xmlEncodeText​(java.lang.String text)
        Encodes text as XML in the most suitable way, either CDATA block or PCDATA.
      • xmlEncodeText

        public static void xmlEncodeText​(java.lang.String text,
                                         java.io.Writer writer)
      • xmlEncodeTextAsPCDATA

        public static java.lang.String xmlEncodeTextAsPCDATA​(java.lang.String text)
        Encodes any text as PCDATA.
      • xmlEncodeTextAsPCDATA

        public static java.lang.String xmlEncodeTextAsPCDATA​(java.lang.String text,
                                                             boolean forAttribute)
        Encodes any text as PCDATA.
        Parameters:
        forAttribute - if you want quotes and apostrophes specially treated for attributes
      • xmlEncodeTextAsPCDATA

        public static java.lang.String xmlEncodeTextAsPCDATA​(java.lang.String text,
                                                             boolean forAttribute,
                                                             char quoteChar)
        Encodes any text as PCDATA.
        Parameters:
        forAttribute - if you want quotes and apostrophes specially treated for attributes
        quoteChar - if this is for attributes this char is used to quote the attribute value
      • xmlEncodeTextAsPCDATA

        public static void xmlEncodeTextAsPCDATA​(java.lang.String text,
                                                 boolean forAttribute,
                                                 char quoteChar,
                                                 java.io.Writer n)
      • xmlEncodeTextAsCDATABlock

        public static java.lang.String xmlEncodeTextAsCDATABlock​(java.lang.String text)
        Returns string as CDATA block if possible, otherwise null.
      • needsEncoding

        public static boolean needsEncoding​(java.lang.String text)
        Checks if this text needs encoding in order to be represented in XML.
      • needsEncoding

        public static boolean needsEncoding​(java.lang.String data,
                                            boolean checkForAttr)
        Checks if this text needs encoding in order to be represented in XML.

        Set checkForAttr if you want to check for storability in an attribute.

      • isCompatibleWithCDATABlock

        public static boolean isCompatibleWithCDATABlock​(java.lang.String text)
        Can this text be stored into a CDATA block?
      • xmlDecodeTextToCDATA

        public static java.lang.String xmlDecodeTextToCDATA​(java.lang.String pcdata)
        Make CDATA out of possibly encoded PCDATA.
        E.g. make '&' out of '&amp;'
      • lookAhead

        private static char lookAhead​(int la,
                                      int offset,
                                      java.lang.String data)