Class XMLCoder

Object
XMLCoder

public class XMLCoder extends Object
Encode and decode XML strings.
Author:
Christoph Hauert
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static boolean
    true to strictly adhering to XML standard.
    private static final Map<String,String>
    Lookup table for decoding XML strings.
    private static final Map<Character,String>
    Lookup table for encoding XML strings.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Suppresses default constructor, ensuring non-instantiability.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    decode(String string)
    Decode XML string.
    static String
    encode(String string)
    Encode string as XML.
    private static void
    Initialize lookup tables for encoding and decoding XML strings.
    static boolean
    Check if encoding/decoding is strictly XML compliant.
    static void
    setStrict(boolean strict)
    Set the encoding/decoding mode to strict.

    Methods inherited from class Object

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

    • XMLDecode

      private static final Map<String,String> XMLDecode
      Lookup table for decoding XML strings.
    • XMLEncode

      private static final Map<Character,String> XMLEncode
      Lookup table for encoding XML strings.
    • strict

      private static boolean strict
      true to strictly adhering to XML standard. If false some additional characters are encoded/decoded that are invalid in XML (or XHTML) but acceptable in ePubs.
  • Constructor Details

    • XMLCoder

      private XMLCoder()
      Suppresses default constructor, ensuring non-instantiability.
  • Method Details

    • setStrict

      public static void setStrict(boolean strict)
      Set the encoding/decoding mode to strict. The encoding/decoding strictly adheres to XML standards if true. If the mode changed the (static) lookup tables are re-initialized.
      Parameters:
      strict - encoding/decoding mode
      See Also:
    • isStrict

      public static boolean isStrict()
      Check if encoding/decoding is strictly XML compliant.
      Returns:
      true if strictly compliant
    • encode

      public static String encode(String string)
      Encode string as XML. If strict==false some additional characters are encoded that still validate in ePub's.

      Note: XHTML is very fussy about characters in argument to innerHTML. It does not seem to accept any kind of special character even when properly encoded. For example, the properly encoded '√' () or both result in an error (even though the latter is perfectly fine in XHTML documents).

      Parameters:
      string - the string to encode in XML
      Returns:
      the encoded string
    • decode

      public static String decode(String string)
      Decode XML string. If strict==false some additional characters are decoded that still validate in ePub's.
      Parameters:
      string - the XML string to decode
      Returns:
      the decoded string
    • init

      private static void init()
      Initialize lookup tables for encoding and decoding XML strings.