Package org.eigenbase.xom
Class XMLUtil
java.lang.Object
org.eigenbase.xom.XMLUtil
- Direct Known Subclasses:
XOMUtil
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic 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 valstatic void
printAtt
(PrintWriter pw, String name, int val) Print an XML attribute name and value for int valstatic void
printAtt
(PrintWriter pw, String name, String val) Print an XML attribute name and value for string valstatic void
printPCDATA
(PrintWriter pw, String data) Quote a string, and write to aPrintWriter
.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 aPrintWriter
.static String
Quote a string so that it can be included as an XML attribute value.static String
Return an XML attribute/value pair for boolean valstatic String
Return an XML attribute/value pair for int valstatic String
Return an XML attribute/value pair for String valstatic 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.
-
Constructor Details
-
XMLUtil
public XMLUtil()
-
-
Method Details
-
stringHasXMLSpecials
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
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
Quote a string, and write to aPrintWriter
.For example,
"a string"
becomesinvalid input: '<'![CDATA[a string]]>
. If the string contains ']]>' (which commonly occurs when wrapping other XML documents), we give up on usinginvalid input: '<'![CDATA[
...]]>
, and just encode the string. For example,"A string with ]]> in it"
becomes"A string with ]]> in it"
. -
quotePCDATA
Quote a string.- See Also:
-
printPCDATA
Quote a string in an element and a CDATA, and write to aPrintWriter
. For example, ittag
is "Value", then"a string"
becomesinvalid input: '<Value'>invalid input: '<'![CDATA[a string]]>invalid input: '<'/Value>.
- Parameters:
newline
- whether to print a newline after the element- See Also:
-
printPCDATA
-
quoteAtt
Quote a string so that it can be included as an XML attribute value. -
quoteAtt
Return an XML attribute/value pair for String val -
quoteAtt
Return an XML attribute/value pair for int val -
quoteAtt
Return an XML attribute/value pair for boolean val -
printAtt
Quote a string so that it can be included as an XML attribute value. -
printAtt
Print an XML attribute name and value for string val -
printAtt
Print an XML attribute name and value for int val -
printAtt
Print an XML attribute name and value for boolean val -
getFirstTagName
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.
-