Package org.eigenbase.xom

## Class XMLUtil

- java.lang.Object
- - org.eigenbase.xom.XMLUtil

Direct Known Subclasses:
  `XOMUtil`

    public class XMLUtil
    extends java.lang.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
Version:
  $Id: //open/util/resgen/src/org/eigenbase/xom/XMLUtil.java#5 $
Author:
  jhyde

### Constructor Summary

Constructors
`XMLUtil()`

### Method Summary

All Methods Static Methods Concrete Methods
`static java.lang.String` `getFirstTagName​(java.io.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​(java.io.PrintWriter pw, java.lang.String val)` Quote a string so that it can be included as an XML attribute value.
`static void` `printAtt​(java.io.PrintWriter pw, java.lang.String name, boolean val)` Print an XML attribute name and value for boolean val
`static void` `printAtt​(java.io.PrintWriter pw, java.lang.String name, int val)` Print an XML attribute name and value for int val
`static void` `printAtt​(java.io.PrintWriter pw, java.lang.String name, java.lang.String val)` Print an XML attribute name and value for string val
`static void` `printPCDATA​(java.io.PrintWriter pw, java.lang.String data)` Quote a string, and write to a `PrintWriter`.
`static void` `printPCDATA​(java.io.PrintWriter pw, java.lang.String tag, java.lang.String data)`
`static void` `printPCDATA​(java.io.PrintWriter pw, java.lang.String tag, java.lang.String data, boolean newline)` Quote a string in an element and a CDATA, and write to a `PrintWriter`.
`static java.lang.String` `quoteAtt​(java.lang.String val)` Quote a string so that it can be included as an XML attribute value.
`static java.lang.String` `quoteAtt​(java.lang.String name, boolean val)` Return an XML attribute/value pair for boolean val
`static java.lang.String` `quoteAtt​(java.lang.String name, int val)` Return an XML attribute/value pair for int val
`static java.lang.String` `quoteAtt​(java.lang.String name, java.lang.String val)` Return an XML attribute/value pair for String val
`static java.lang.String` `quotePCDATA​(java.lang.String data)` Quote a string.
`static void` `stringEncodeXML​(java.lang.String input, java.io.PrintWriter out)` Encode a String for XML output, displaying it to a PrintWriter.
`static boolean` `stringHasXMLSpecials​(java.lang.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​(java.lang.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​(java.lang.String input,
                                       java.io.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​(java.io.PrintWriter pw,
                                   java.lang.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 java.lang.String quotePCDATA​(java.lang.String data)

Quote a string.

See Also:
  `printPCDATA(PrintWriter,String)`

#### printPCDATA

    public static void printPCDATA​(java.io.PrintWriter pw,
                                   java.lang.String tag,
                                   java.lang.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​(java.io.PrintWriter pw,
                                   java.lang.String tag,
                                   java.lang.String data)

#### quoteAtt

    public static java.lang.String quoteAtt​(java.lang.String val)

Quote a string so that it can be included as an XML attribute value.

#### quoteAtt

    public static java.lang.String quoteAtt​(java.lang.String name,
                                            java.lang.String val)

Return an XML attribute/value pair for String val

#### quoteAtt

    public static java.lang.String quoteAtt​(java.lang.String name,
                                            int val)

Return an XML attribute/value pair for int val

#### quoteAtt

    public static java.lang.String quoteAtt​(java.lang.String name,
                                            boolean val)

Return an XML attribute/value pair for boolean val

#### printAtt

    public static void printAtt​(java.io.PrintWriter pw,
                                java.lang.String val)

Quote a string so that it can be included as an XML attribute value.

#### printAtt

    public static void printAtt​(java.io.PrintWriter pw,
                                java.lang.String name,
                                java.lang.String val)

Print an XML attribute name and value for string val

#### printAtt

    public static void printAtt​(java.io.PrintWriter pw,
                                java.lang.String name,
                                int val)

Print an XML attribute name and value for int val

#### printAtt

    public static void printAtt​(java.io.PrintWriter pw,
                                java.lang.String name,
                                boolean val)

Print an XML attribute name and value for boolean val

#### getFirstTagName

    public static java.lang.String getFirstTagName​(java.io.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.