Class XmlFriendlyNameCoder

java.lang.Object
com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder
All Implemented Interfaces:
NameCoder, Cloneable
Direct Known Subclasses:
XmlFriendlyReplacer, XStream11NameCoder

public class XmlFriendlyNameCoder extends Object implements NameCoder, Cloneable
Encode and decode tag and attribute names in XML drivers.

This NameCoder is designed to ensure the correct encoding and decoding of names used for Java types and fields to XML tags and attribute names.

The default replacements are:

  • $ (dollar) chars are replaced with _- (underscore dash) string.
  • _ (underscore) chars are replaced with __ (double underscore) string.
  • other characters that are invalid in XML names are encoded with _.XXXX (underscore dot followed by hex representation of character).

The valid characters are defined by the intersection of the XML 1.0 specification (4th edition) and later specifications till XML 1.1 specification.

Since:
1.4
See Also:
  • Field Details

    • XML_NAME_START_CHARS

      private static final BitSet XML_NAME_START_CHARS
    • XML_NAME_CHARS

      private static final BitSet XML_NAME_CHARS
    • dollarReplacement

      private final String dollarReplacement
    • escapeCharReplacement

      private final String escapeCharReplacement
    • escapeCache

      private transient Map escapeCache
    • unescapeCache

      private transient Map unescapeCache
    • hexPrefix

      private final String hexPrefix
  • Constructor Details

    • XmlFriendlyNameCoder

      public XmlFriendlyNameCoder()
      Construct a new XmlFriendlyNameCoder.
      Since:
      1.4
    • XmlFriendlyNameCoder

      public XmlFriendlyNameCoder(String dollarReplacement, String escapeCharReplacement)
      Construct a new XmlFriendlyNameCoder with custom replacement strings for dollar and the escape character.
      Parameters:
      dollarReplacement -
      escapeCharReplacement -
      Since:
      1.4
    • XmlFriendlyNameCoder

      public XmlFriendlyNameCoder(String dollarReplacement, String escapeCharReplacement, String hexPrefix)
      Construct a new XmlFriendlyNameCoder with custom replacement strings for dollar, the escape character and the prefix for hexadecimal encoding of invalid characters in XML names.
      Parameters:
      dollarReplacement -
      escapeCharReplacement -
      Since:
      1.4
  • Method Details

    • decodeAttribute

      public String decodeAttribute(String attributeName)
      Decode an attribute name to an object name.
      Specified by:
      decodeAttribute in interface NameCoder
      Parameters:
      attributeName - the name of the attribute
      Returns:
      the name of the meta-data
    • decodeNode

      public String decodeNode(String elementName)
      Decode a node name to an object name.
      Specified by:
      decodeNode in interface NameCoder
      Parameters:
      elementName - the name of the node
      Returns:
      the name of the object
    • encodeAttribute

      public String encodeAttribute(String name)
      Encode a meta-data name for an attribute in the target format.
      Specified by:
      encodeAttribute in interface NameCoder
      Parameters:
      name - the name of the meta-data
      Returns:
      the attribute name in the target format
    • encodeNode

      public String encodeNode(String name)
      Encode an object name for a node in the target format.
      Specified by:
      encodeNode in interface NameCoder
      Parameters:
      name - the name of the object data
      Returns:
      the node name in the target format
    • encodeName

      private String encodeName(String name)
    • decodeName

      private String decodeName(String name)
    • clone

      public Object clone()
      Overrides:
      clone in class Object
    • readResolve

      private Object readResolve()
    • createCacheMap

      protected Map createCacheMap()
    • isXmlNameStartChar

      private static boolean isXmlNameStartChar(int cp)
    • isXmlNameChar

      private static boolean isXmlNameChar(int cp)