Static Public Member Functions | Private Member Functions

de.mfuchs.xmlstorage.XMLHelper Class Reference

XMLHelper converts XML string literals. More...

List of all members.

Static Public Member Functions

static String encodeXMLString (final String s)
 Encode XML string literals.
static String encodeXMLString (final String s, boolean cdata)
 Encode XML string literals, optionally in CDATA sections.
static String decodeXMLString (final String s)
 Decode XML string literals.
static String unescape (String s)
 remove escape characters from the specified string
static String unescape (String s, char b, char e)
 remove escape characters from the specified string

Private Member Functions

 XMLHelper ()
 private constructor to disable instantiation.

Detailed Description

XMLHelper converts XML string literals.

Definition at line 45 of file XMLHelper.java.


Constructor & Destructor Documentation

de.mfuchs.xmlstorage.XMLHelper.XMLHelper (  ) [private]

private constructor to disable instantiation.

Definition at line 50 of file XMLHelper.java.

    {
    }

Member Function Documentation

static String de.mfuchs.xmlstorage.XMLHelper.decodeXMLString ( final String  s ) [static]

Decode XML string literals.

Definition at line 110 of file XMLHelper.java.

Referenced by de.mfuchs.xmlstorage.stream.XMLStreamNode.getContent(), and de.mfuchs.xmlstorage.XMLNode.getContent().

    {
        StringBuffer buffer = new StringBuffer();

        for(int i=0; i<s.length(); ++i) {
            char c = s.charAt(i);

             // handle string literals
            if (c == '&') {
                if (s.substring(i+1,i+4).equalsIgnoreCase("lt;")) {
                    buffer.append('<');
                    i += 3;
                } else if (s.substring(i+1,i+4).equalsIgnoreCase("gt;")) {
                    buffer.append('>');
                    i += 3;
                } else if (s.substring(i+1,i+5).equalsIgnoreCase("amp;")) {
                    buffer.append('&');
                    i += 4;
                } else if (s.substring(i+1,i+6).equalsIgnoreCase("quot;")) {
                    buffer.append('"');
                    i += 5;
                } else if (s.substring(i+1,i+6).equalsIgnoreCase("apos;")) {
                    buffer.append('\'');
                    i += 5;
                } else
                    buffer.append(c);
            }
             // handle CDATA sections
            else if (c=='<' && s.regionMatches(i+1, "![CDATA[", 0, 8)) {
                int e = s.indexOf("]]>", i+9);
                if (e != -1) {
                    i += 9;
                    int l = e - i;
                    buffer.append(s.substring(i, i+l));
                    i += 3;
                } else
                    buffer.append(c);
            } else
                buffer.append(c);
        }

        return buffer.toString();
    }
static String de.mfuchs.xmlstorage.XMLHelper.encodeXMLString ( final String  s,
boolean  cdata 
) [static]

Encode XML string literals, optionally in CDATA sections.

Definition at line 65 of file XMLHelper.java.

    {
        StringBuffer buffer = new StringBuffer();

        if (cdata) {
            buffer.append("<![CDATA[");
            buffer.append(s);
            buffer.append("]]>");
        } else {
            for(int i=0; i<s.length(); ++i) {
                char c = s.charAt(i);

                switch(c) {
                  case '<':
                    buffer.append("&lt;");
                    break;

                  case '>':
                    buffer.append("&gt;");
                    break;

                  case '&':
                    buffer.append("&amp;");
                    break;

                  case '"':
                    buffer.append("&quot;");
                    break;

                  case '\'':
                    buffer.append("&apos;");
                    break;

                  default:
                    buffer.append(c);
                }
            }
        }

        return buffer.toString();
    }
static String de.mfuchs.xmlstorage.XMLHelper.encodeXMLString ( final String  s ) [static]
static String de.mfuchs.xmlstorage.XMLHelper.unescape ( String  s ) [static]

remove escape characters from the specified string

Definition at line 155 of file XMLHelper.java.

References de.mfuchs.xmlstorage.XMLHelper.unescape().

Referenced by de.mfuchs.xmlstorage.XPathElement.parse(), and de.mfuchs.xmlstorage.XMLHelper.unescape().

    {
        return XMLHelper.unescape(s, '"', '"');
    }

Here is the call graph for this function:

static String de.mfuchs.xmlstorage.XMLHelper.unescape ( String  s,
char  b,
char  e 
) [static]

remove escape characters from the specified string

Definition at line 161 of file XMLHelper.java.

    {
    //  if (s.charAt(0) == b)
    //      s = s.substring(1);
    //
    //  if (s.length()>0 && end[-1]==e)
    //      s = s.substring(0, s.length()-1);
    
        if (s.charAt(0) == b)
            if (s.length()>1 && s.charAt(s.length()-1)==e)
                s = s.substring(1, s.length()-1);
    
        return s;
    }

The documentation for this class was generated from the following file: