org.eigenbase.xom
Class XMLUtil

java.lang.Object
  extended by org.eigenbase.xom.XMLUtil
Direct Known Subclasses:
XOMUtil

public class XMLUtil
extends Object

Utilities for dealing with XML data. These methods must NOT depend upon any XML parser or object model (MSXML, DOM, SAX, etc.)

Since:
3 October, 2001
Author:
jhyde
 

Constructor Summary
XMLUtil()
           
 
Method Summary
static String getFirstTagName(Reader xml)
          Retrieve the name of the first tag in the XML document specified by the given Reader, without parsing the full file/string.
static void printAtt(PrintWriter pw, String val)
          Quote a string so that it can be included as an XML attribute value.
static void printAtt(PrintWriter pw, String name, boolean val)
          Print an XML attribute name and value for boolean val
static void printAtt(PrintWriter pw, String name, int val)
          Print an XML attribute name and value for int val
static void printAtt(PrintWriter pw, String name, String val)
          Print an XML attribute name and value for string val
static void printPCDATA(PrintWriter pw, String data)
          Quote a string, and write to a PrintWriter.
static void printPCDATA(PrintWriter pw, String tag, String data)
           
static void printPCDATA(PrintWriter pw, String tag, String data, boolean newline)
          Quote a string in an element and a CDATA, and write to a PrintWriter.
static String quoteAtt(String val)
          Quote a string so that it can be included as an XML attribute value.
static String quoteAtt(String name, boolean val)
          Return an XML attribute/value pair for boolean val
static String quoteAtt(String name, int val)
          Return an XML attribute/value pair for int val
static String quoteAtt(String name, String val)
          Return an XML attribute/value pair for String val
static String quotePCDATA(String data)
          Quote a string.
static void stringEncodeXML(String input, PrintWriter out)
          Encode a String for XML output, displaying it to a PrintWriter.
static boolean stringHasXMLSpecials(String input)
          Determine if a String contains any XML special characters, return true if it does.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtil

public XMLUtil()
Method Detail

stringHasXMLSpecials

public static boolean stringHasXMLSpecials(String input)
Determine if a String contains any XML special characters, return true if it does. If this function returns true, the string will need to be encoded either using the stringEncodeXML function above or using a CDATA section. Note that MSXML has a nasty bug whereby whitespace characters outside of a CDATA section are lost when parsing. To avoid hitting this bug, this method treats many whitespace characters as "special".

Parameters:
input - the String to scan for XML special characters.
Returns:
true if the String contains any such characters.

stringEncodeXML

public static void stringEncodeXML(String input,
                                   PrintWriter out)
Encode a String for XML output, displaying it to a PrintWriter. The String to be encoded is displayed, except that special characters are converted into entities.

Parameters:
input - a String to convert.
out - a PrintWriter to which to write the results.

printPCDATA

public static void printPCDATA(PrintWriter pw,
                               String data)
Quote a string, and write to a PrintWriter.

For example, "a string" becomes <![CDATA[a string]]>. If the string contains ']]>' (which commonly occurs when wrapping other XML documents), we give up on using <![CDATA[ ... ]]>, and just encode the string. For example, "A string with ]]> in it" becomes "A string with ]]&gt; in it".


quotePCDATA

public static String quotePCDATA(String data)
Quote a string.

See Also:
printPCDATA(PrintWriter,String)

printPCDATA

public static void printPCDATA(PrintWriter pw,
                               String tag,
                               String data,
                               boolean newline)
Quote a string in an element and a CDATA, and write to a PrintWriter. For example, it tag is "Value", then "a string" becomes <Value><![CDATA[a string]]></Value>.

Parameters:
newline - whether to print a newline after the element
See Also:
printPCDATA(PrintWriter,String)

printPCDATA

public static void printPCDATA(PrintWriter pw,
                               String tag,
                               String data)

quoteAtt

public static String quoteAtt(String val)
Quote a string so that it can be included as an XML attribute value.


quoteAtt

public static String quoteAtt(String name,
                              String val)
Return an XML attribute/value pair for String val


quoteAtt

public static String quoteAtt(String name,
                              int val)
Return an XML attribute/value pair for int val


quoteAtt

public static String quoteAtt(String name,
                              boolean val)
Return an XML attribute/value pair for boolean val


printAtt

public static void printAtt(PrintWriter pw,
                            String val)
Quote a string so that it can be included as an XML attribute value.


printAtt

public static void printAtt(PrintWriter pw,
                            String name,
                            String val)
Print an XML attribute name and value for string val


printAtt

public static void printAtt(PrintWriter pw,
                            String name,
                            int val)
Print an XML attribute name and value for int val


printAtt

public static void printAtt(PrintWriter pw,
                            String name,
                            boolean val)
Print an XML attribute name and value for boolean val


getFirstTagName

public static String getFirstTagName(Reader xml)
Retrieve the name of the first tag in the XML document specified by the given Reader, without parsing the full file/string. This function is useful to identify the DocType of an XML document before parsing, possibly to send the document off to different pieces of code. For performance reasons, the function attempts to read as little of the file or string as possible before making its decision about the first tag. Leading comments are ignored.

Parameters:
xml - a Reader containing an XML document.
Returns:
the first tag name, as a String, or null if no first tag can be found.

SourceForge.net_Logo