|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.waveset.util.XmlUtil
XML parsing and DOM manipulation utilities.
Field Summary | |
protected static XmlUtil |
_singleton
A handle to a singleton object for the getDefault method. |
static java.lang.String |
code_id
|
static java.lang.String |
DEFAULT_XML_ENCODING
The default encoding for XML is "UTF-8" . |
static char |
DOUBLE_QUOTE
******************************************************************* |
static char |
SINGLE_QUOTE
|
static java.lang.String |
XML_HEADER_FIRST_LINE
|
static java.lang.String |
XML_HEADER_VERSION
|
Constructor Summary | |
XmlUtil()
Construct a private utility object. |
Method Summary | |
static void |
addAttribute(java.lang.StringBuffer b,
java.lang.String name,
boolean value)
Adds a boolean attribute to the buffer. |
static void |
addAttribute(java.lang.StringBuffer b,
java.lang.String name,
java.util.Date value)
Adds a date attribute to the buffer. |
static void |
addAttribute(java.lang.StringBuffer b,
java.lang.String name,
double value)
Adds a double attribute to the buffer. |
static void |
addAttribute(java.lang.StringBuffer b,
java.lang.String name,
float value)
Adds a float attribute to the buffer. |
static void |
addAttribute(java.lang.StringBuffer b,
java.lang.String name,
int value)
Adds an integer attribute to the buffer. |
static void |
addAttribute(java.lang.StringBuffer b,
java.lang.String name,
long value)
Adds a long attribute to the buffer. |
static void |
addAttribute(java.lang.StringBuffer b,
java.lang.String name,
java.lang.Object obj)
Add an an attribute value using an arbitrary object, by calling its toString method. |
static void |
addAttribute(java.lang.StringBuffer b,
java.lang.String name,
java.lang.String value)
Adds an attribute name and value to a string buffer. |
static void |
addAttribute(java.lang.StringBuffer b,
java.lang.String name,
java.lang.String value,
boolean tolerateNullValue,
char preferredDelimiter)
Adds an attribute name and value to a string buffer. |
static void |
addCdata(java.lang.StringBuffer b,
int indent,
java.lang.String s,
boolean kludgeDoctype)
Build a CDATA marked section, escaping as necessary. |
static void |
addCdataContent(java.lang.StringBuffer b,
java.lang.String s)
|
static void |
addCdataContent(java.lang.StringBuffer b,
java.lang.String s,
boolean kludgeDoctype)
Add a string of CDATA content kludging around the DOCTYPE token. |
static void |
addContent(java.lang.StringBuffer b,
java.lang.Object o)
Replaces special characters in a string with XML character entities. |
static void |
addElement(java.lang.StringBuffer b,
int indent,
java.lang.String element,
java.lang.String content)
Adds an element with content to the buffer, being careful to escape content. |
static void |
addEndTag(java.lang.StringBuffer b,
int indent,
java.lang.String element)
Adds an element end tag with optional indentation. |
static void |
addIndent(java.lang.StringBuffer b,
int indent)
Internal method to add indentation to string buffers. |
static void |
addObject(java.lang.StringBuffer b,
int indent,
java.lang.String wrapper,
XmlObject obj)
Adds an object to the buffer, wrapped in some other elemenet. |
static void |
addOpenStartTag(java.lang.StringBuffer b,
int indent,
java.lang.String element)
Adds an open element start tag with optional indentation. |
static void |
addStartTag(java.lang.StringBuffer b,
int indent,
java.lang.String element,
boolean newline)
Adds a closed element start tag with optional indentation. |
static void |
addXmlHeader(java.lang.StringBuffer sb,
java.lang.String doctypeName)
Add the default header, need to be able to customize this better. |
static void |
addXmlHeader(java.lang.StringBuffer sb,
java.lang.String doctypeName,
java.lang.String encoding)
Add the default header, need to be able to customize this better. |
static boolean |
areSetEquivalent(org.w3c.dom.Node n1,
org.w3c.dom.Node n2)
Tests the two given nodes for set equivalency (meaning having equal content, but not necessarily in the same order). |
static boolean |
attributesMatch(org.w3c.dom.Node n1,
org.w3c.dom.Node n2)
For attributes only, tests the two given nodes for set equivalency (meaning having equal content, but not necessarily in the same order). |
static void |
closeStartTag(java.lang.StringBuffer b)
Completes an open element start tag. |
static void |
closeStartTagAndEndElement(java.lang.StringBuffer b)
Completes an open element start tag and the element in one move. |
static java.lang.String |
encodeAsHTML(java.lang.String s)
Make an XML string displayable for HTML. |
static java.lang.String |
encodeComment(java.lang.String s)
Takes what is expected to be an arbitrary blob of XML and processes it so that it can be embedded within an HTML comment. |
static java.lang.String |
escape(java.lang.String s)
Replaces special characters in a string with XML character entities. |
static void |
escape(java.lang.StringBuffer b,
java.lang.String s,
char delim)
Encode a string, assumed to be in a quoted attribute value. |
static java.lang.String |
escape(java.lang.String s,
char delim)
|
static java.lang.String |
escapeMessage(java.lang.String s)
Replace special characters in a string with XML character entities, preserving HTML formatting tags we think are "safe". |
static org.w3c.dom.Element |
findElement(org.w3c.dom.Node node,
java.lang.String name)
Search for an element with a given tag name, on or below the given node. |
static org.w3c.dom.Element |
findElement(org.w3c.dom.Node node,
java.lang.String elname,
java.lang.String attname,
java.lang.String attval)
Search for an element on or below a given node, that has a given tag name and an attribute with a given value. |
static java.lang.Object |
findObject(org.w3c.dom.Node node,
boolean ignoreEmpty)
Recurses til an text or localizable object is found. |
static org.w3c.dom.Text |
findText(org.w3c.dom.Node node,
boolean ignoreEmpty)
Locate the first text node at any level below the given node. |
static java.lang.String |
getAttribute(org.w3c.dom.Element e,
java.lang.String name)
Return the value of an attribute on an element. |
static boolean |
getBooleanAttribute(org.w3c.dom.Element e,
java.lang.String name)
Gets a boolean attribute. |
static boolean |
getBooleanAttribute(org.w3c.dom.Element e,
java.lang.String name,
boolean dflt)
Gets a boolean attribute with a specified default. |
static boolean |
getBooleanValue(java.lang.String value)
Interprets a string value as a boolean. |
static org.w3c.dom.Element |
getChildElement(org.w3c.dom.Node node)
Find the first child element of a node. |
static java.lang.String |
getContent(org.w3c.dom.Element e)
Return the content of the given element. |
static java.lang.String |
getContent(org.w3c.dom.Node e,
java.lang.String name)
Return the content of an element with the given tag on or below the given node. |
static java.lang.Object |
getContentObject(org.w3c.dom.Element e)
Return the content of the given element as an object, if it can be resolved as a recognized object. |
static java.util.Date |
getDateAttribute(org.w3c.dom.Element e,
java.lang.String name)
Gets a date attribute. |
static XmlUtil |
getDefault()
Get the public singleton utility object. |
static char |
getDelimiter(java.lang.String value)
|
static char |
getDelimiter(java.lang.String value,
char preferredDelim)
|
static double |
getDoubleAttribute(org.w3c.dom.Element e,
java.lang.String name)
Gets a double attribute. |
static double |
getDoubleAttribute(org.w3c.dom.Element e,
java.lang.String name,
double deflt)
Gets an double attribute, returning the specified default value if the attribute does not exist. |
static float |
getFloatAttribute(org.w3c.dom.Element e,
java.lang.String name)
Gets a float attribute. |
static float |
getFloatAttribute(org.w3c.dom.Element e,
java.lang.String name,
float dflt)
Gets a float attribute, returning a given default value if the attribute does not exist. |
static int |
getIntAttribute(org.w3c.dom.Element e,
java.lang.String name)
Gets an integer attribute. |
static int |
getIntAttribute(org.w3c.dom.Element e,
java.lang.String name,
int dflt)
Gets an integer attribute, returning a given default value if the attribute does not exist. |
static long |
getLongAttribute(org.w3c.dom.Element e,
java.lang.String name)
Gets a long attribute. |
static long |
getLongAttribute(org.w3c.dom.Element e,
java.lang.String name,
long deflt)
Gets an integer attribute, returning the specified default value if the attribute does not exist. |
static org.w3c.dom.Element |
getNextElement(org.w3c.dom.Node node)
Get the next right sibling that is an element. |
static java.lang.String |
getSpecifiedEncoding(java.lang.String xml)
Scans the specified string for an XML header line. |
static boolean |
isSafeAttributeValue(java.lang.String s)
Returns true if this string can be stored inside an XML attribute value without corruption. |
static void |
main(java.lang.String[] args)
Parses an XML file, and displays any error or warning messages. |
org.w3c.dom.Document |
parse(java.lang.String uri)
Parses the specified URI and returns the document. |
org.w3c.dom.Document |
parseString(java.lang.String xml)
Parses a string without validation and returns the Document. |
org.w3c.dom.Document |
parseString(java.lang.String xml,
boolean validation)
Parses a string with or without validation and returns the Document. |
static void |
println(java.lang.String msg)
|
static void |
saxParse(java.io.InputStream input,
org.xml.sax.ContentHandler handler)
Parses a stream using a SAX parser. |
static void |
saxParse(java.io.Reader input,
org.xml.sax.ContentHandler handler)
Parses a reader using a SAX parser. |
static void |
saxParse(java.lang.String uri,
org.xml.sax.ContentHandler handler)
Parses the specified URI using a SAX parser. |
static void |
saxParseString(java.lang.String xml,
org.xml.sax.ContentHandler handler)
Parses the specified using a SAX parser. |
static void |
saxParseString(java.lang.String xml,
org.xml.sax.ContentHandler handler,
org.xml.sax.ErrorHandler errorHandler)
Parses the specified using a SAX parser. |
static java.lang.String |
serialize(org.w3c.dom.Node node)
Serialize a node back to an XML string. |
static void |
serialize(java.lang.StringBuffer b,
org.w3c.dom.Node node)
Our own homebrew serializer. |
static void |
setContent(org.w3c.dom.Element e,
java.lang.String content)
Changes an element's content, either to null or to a string of text. |
static java.util.Map |
unpack(java.lang.String summary)
Given a string buffer containing an object summary, unpack it into an Map object. |
static boolean |
validXmlChar(char ch)
legal xml chars from http://www.xml.com/axml/testaxml.htm Char::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final java.lang.String code_id
public static final char DOUBLE_QUOTE
public static final char SINGLE_QUOTE
protected static XmlUtil _singleton
public static java.lang.String DEFAULT_XML_ENCODING
"UTF-8"
.
public static final java.lang.String XML_HEADER_VERSION
public static final java.lang.String XML_HEADER_FIRST_LINE
Constructor Detail |
public XmlUtil()
Method Detail |
public static XmlUtil getDefault()
public static java.lang.String getSpecifiedEncoding(java.lang.String xml)
public static void addXmlHeader(java.lang.StringBuffer sb, java.lang.String doctypeName)
public static void addXmlHeader(java.lang.StringBuffer sb, java.lang.String doctypeName, java.lang.String encoding)
public org.w3c.dom.Document parse(java.lang.String uri) throws WavesetException
WavesetException
public static void saxParse(java.lang.String uri, org.xml.sax.ContentHandler handler) throws WavesetException
WavesetException
public static void saxParse(java.io.InputStream input, org.xml.sax.ContentHandler handler) throws WavesetException
WavesetException
public static void saxParse(java.io.Reader input, org.xml.sax.ContentHandler handler) throws WavesetException
WavesetException
public static void saxParseString(java.lang.String xml, org.xml.sax.ContentHandler handler) throws WavesetException
WavesetException
public static void saxParseString(java.lang.String xml, org.xml.sax.ContentHandler handler, org.xml.sax.ErrorHandler errorHandler) throws WavesetException
WavesetException
public org.w3c.dom.Document parseString(java.lang.String xml, boolean validation) throws WavesetException
WavesetException
public org.w3c.dom.Document parseString(java.lang.String xml) throws WavesetException
WavesetException
public static java.lang.String getAttribute(org.w3c.dom.Element e, java.lang.String name)
public static org.w3c.dom.Element getChildElement(org.w3c.dom.Node node)
public static org.w3c.dom.Element getNextElement(org.w3c.dom.Node node)
public static org.w3c.dom.Element findElement(org.w3c.dom.Node node, java.lang.String name)
public static org.w3c.dom.Element findElement(org.w3c.dom.Node node, java.lang.String elname, java.lang.String attname, java.lang.String attval)
public static org.w3c.dom.Text findText(org.w3c.dom.Node node, boolean ignoreEmpty)
public static java.lang.Object findObject(org.w3c.dom.Node node, boolean ignoreEmpty)
public static java.lang.Object getContentObject(org.w3c.dom.Element e)
public static java.lang.String getContent(org.w3c.dom.Node e, java.lang.String name)
public static java.lang.String getContent(org.w3c.dom.Element e)
public static void setContent(org.w3c.dom.Element e, java.lang.String content)
public static java.lang.String serialize(org.w3c.dom.Node node) throws WavesetException
WavesetException
public static void serialize(java.lang.StringBuffer b, org.w3c.dom.Node node)
public static void println(java.lang.String msg)
public static java.lang.String escape(java.lang.String s)
public static java.lang.String escape(java.lang.String s, char delim)
public static java.lang.String escapeMessage(java.lang.String s)
escape
values that could have been specified by an attacker
as a parameter to the HTTP request.
However, this includes common parameters
such as "error"
and "message"
that by convention contain messages from other pages.
Escaping these messages neutralizes the HTML formatting
commonly included in messages.
public static java.lang.String encodeAsHTML(java.lang.String s)
public static java.lang.String encodeComment(java.lang.String s)
public static boolean isSafeAttributeValue(java.lang.String s)
public static void addAttribute(java.lang.StringBuffer b, java.lang.String name, java.lang.String value)
public static void addAttribute(java.lang.StringBuffer b, java.lang.String name, java.lang.String value, boolean tolerateNullValue, char preferredDelimiter)
public static char getDelimiter(java.lang.String value)
public static char getDelimiter(java.lang.String value, char preferredDelim)
preferredDelim
- - The delimiter we hope to get back.
All things being equal, this returns the suggested character--
e.g., single-quote--as delimiter. However, if the input value
contains more single-quote than double-quote characters,
this method returns the double-quote character.
public static void escape(java.lang.StringBuffer b, java.lang.String s, char delim)
public static void addAttribute(java.lang.StringBuffer b, java.lang.String name, java.lang.Object obj)
public static void addAttribute(java.lang.StringBuffer b, java.lang.String name, boolean value)
public static void addAttribute(java.lang.StringBuffer b, java.lang.String name, long value)
public static void addAttribute(java.lang.StringBuffer b, java.lang.String name, int value)
public static void addAttribute(java.lang.StringBuffer b, java.lang.String name, double value)
public static void addAttribute(java.lang.StringBuffer b, java.lang.String name, float value)
public static void addAttribute(java.lang.StringBuffer b, java.lang.String name, java.util.Date value)
public static boolean getBooleanAttribute(org.w3c.dom.Element e, java.lang.String name)
false
.
The value must be equal to the string "true" to be considered true.
KLUDGE: accept "1" as true for backward compatibility with some
older objects.
public static boolean getBooleanAttribute(org.w3c.dom.Element e, java.lang.String name, boolean dflt)
public static boolean getBooleanValue(java.lang.String value)
public static int getIntAttribute(org.w3c.dom.Element e, java.lang.String name)
public static int getIntAttribute(org.w3c.dom.Element e, java.lang.String name, int dflt)
public static long getLongAttribute(org.w3c.dom.Element e, java.lang.String name)
public static long getLongAttribute(org.w3c.dom.Element e, java.lang.String name, long deflt)
public static float getFloatAttribute(org.w3c.dom.Element e, java.lang.String name)
public static float getFloatAttribute(org.w3c.dom.Element e, java.lang.String name, float dflt)
public static double getDoubleAttribute(org.w3c.dom.Element e, java.lang.String name)
public static double getDoubleAttribute(org.w3c.dom.Element e, java.lang.String name, double deflt)
public static java.util.Date getDateAttribute(org.w3c.dom.Element e, java.lang.String name) throws WavesetException
WavesetException
public static boolean validXmlChar(char ch)
public static void addContent(java.lang.StringBuffer b, java.lang.Object o)
public static void addCdata(java.lang.StringBuffer b, int indent, java.lang.String s, boolean kludgeDoctype)
public static void addCdataContent(java.lang.StringBuffer b, java.lang.String s, boolean kludgeDoctype)
public static void addCdataContent(java.lang.StringBuffer b, java.lang.String s)
public static void addIndent(java.lang.StringBuffer b, int indent)
public static void addOpenStartTag(java.lang.StringBuffer b, int indent, java.lang.String element)
public static void closeStartTag(java.lang.StringBuffer b)
addOpenStartTag(b, indent, Whatever.ELEMENT);
XmlUtil.addAttribute(b,"attr1", _attr1);
XmlUtil.addAttribute(b,"attr2", _attr2);
if ( childElements==null || childElements.size()==0 ) {
XmlUtil.closeStartTagAndEndElement(b);
}
else {
closeStartTag(b);
...
// format child elements
...
XmlUtil.addEndTag(b, indent, Whatever.ELEMENT);
}
addOpenStartTag(java.lang.StringBuffer, int, java.lang.String)
public static void closeStartTagAndEndElement(java.lang.StringBuffer b)
addOpenStartTag(b, indent, Whatever.ELEMENT);
XmlUtil.addAttribute(b,"attr1", _attr1);
XmlUtil.addAttribute(b,"attr2", _attr2);
if ( childElements==null || childElements.size()==0 ) {
XmlUtil.closeStartTagAndEndElement(b);
}
else {
closeStartTag(b);
...
// format child elements
...
XmlUtil.addEndTag(b, indent, Whatever.ELEMENT);
}
addOpenStartTag(java.lang.StringBuffer, int, java.lang.String)
public static void addStartTag(java.lang.StringBuffer b, int indent, java.lang.String element, boolean newline)
public static void addEndTag(java.lang.StringBuffer b, int indent, java.lang.String element)
public static void addElement(java.lang.StringBuffer b, int indent, java.lang.String element, java.lang.String content)
public static void addObject(java.lang.StringBuffer b, int indent, java.lang.String wrapper, XmlObject obj)
public static void main(java.lang.String[] args)
public static boolean areSetEquivalent(org.w3c.dom.Node n1, org.w3c.dom.Node n2)
public static boolean attributesMatch(org.w3c.dom.Node n1, org.w3c.dom.Node n2)
public static java.util.Map unpack(java.lang.String summary)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |