|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.aitools.programd.util.XMLKit
public class XMLKit
A collection of XML utilities.
| Field Summary | |
|---|---|
static String |
CDATA_END
CDATA end marker. |
static String |
CDATA_START
CDATA start marker. |
static String |
COMMENT_END
Comment end marker. |
static String |
COMMENT_START
Comment start marker. |
protected static String |
EQUAL_QUOTE
A common string we search for when parsing attributes in tags. |
protected static char |
QUOTE_MARK
A quote mark, for convenience. |
protected static DocumentBuilder |
utilBuilder
A DocumentBuilder for producing new documents. |
protected static Document |
utilDoc
A document for producing new elements. |
protected static String |
WHITESPACE_REGEX
The regex for whitespace. |
| Constructor Summary | |
|---|---|
XMLKit()
|
|
| Method Summary | |
|---|---|
static String |
convertXMLUnicodeEntities(String input)
Converts XML Unicode character entities into their character equivalents within a given string. |
static int |
elementCount(NodeList list)
Returns the number of elements in the nodelist and its descendants. |
static String |
escapeXMLChars(char[] ch,
int start,
int length)
Like escapeXMLChars(String), but takes an array of chars
instead of a String. |
static String |
escapeXMLChars(String input)
Replaces the following characters with their "escaped" equivalents: |
static String[] |
filterViaHTMLTags(String input)
Breaks a message into multiple lines at an HTML <br/>, except if it comes at the beginning of the message, or ending HTML </p>. |
static String |
filterWhitespace(String input)
Filters all whitespace: line separators and multiple consecutive spaces are replaced with a single space, and any leading or trailing whitespace characters are removed. |
static String |
filterXML(String input)
Removes all characters that are not considered XML characters from the input. |
static List<Element> |
getAllElementsNamed(Element element,
String name)
Returns the all elements with the given name that are children of the given element, or null if there is no such element. |
static String |
getChildText(Element element,
String childName)
Gets the text of the named child from of the given element. |
static String |
getDeclaredXMLEncoding(InputStream in)
Returns the declared encoding string from the XML resource supposedly connected to a given InputStream, or the system default if none is found. |
static DocumentBuilder |
getDocumentBuilder(URL schemaLocation,
String schemaDescription)
Sets up a SAX parser that is schema-aware, processes XIncludes, and is set to use the schema at the given location. |
static List<Element> |
getElementChildrenOf(Element element)
Returns the element children of the given element. |
static Element |
getFirstElementChildOf(Element element)
Returns the first element child of the given element. |
static Element |
getFirstElementIn(NodeList list)
Returns the first element member (if there is one) of the given nodelist. |
static Element |
getFirstElementNamed(Element element,
String name)
Returns the first element with the given name that is a child of the given element, or null if there is no such element. |
static SAXParser |
getSAXParser(URL schemaLocation,
String schemaDescription)
Sets up a SAX parser that is schema-aware, processes XIncludes, and is set to use the schema at the given location. |
static Schema |
getSchema(URL schemaLocation,
String schemaDescription)
Attempts to get the schema at the given location. |
static String |
getSpaces(int count)
|
static Document |
parseAsDocumentFragment(String text)
|
static String |
removeMarkup(String input)
Removes all tags from a string (retains character content of tags, however). |
static String |
renderEmptyElement(Element element,
boolean includeNamespaceAttribute)
Renders a given element as an empty element, including a namespace declaration, if requested. |
static String |
renderEndTag(Element element)
Renders a given element as an end tag. |
static String |
renderStartTag(Element element,
boolean includeNamespaceAttribute)
Renders a given element as a start tag, including a namespace declaration, if requested. |
static String |
renderStartTag(String elementName,
Attributes attributes,
boolean includeNamespaceAttribute,
String namespaceURI)
Renders a given element name and set of attributes as a start tag, including a namespace declaration, if requested. |
static String |
renderXML(NodeList list,
boolean indent)
Formats XML from a node list into a nicely indented multi-line string (if indent is true), or just a long string (if
indent is false). |
static String |
renderXML(NodeList list,
boolean includeNamespaceAttribute,
boolean indent)
Formats XML from a node list into a nicely indented multi-line string (if indent is true), or just a long string (if
indent is false). |
static String |
renderXML(NodeList list,
int level,
boolean atStart,
boolean includeNamespaceAttribute,
boolean indent)
Formats XML from a node list into a nicely indented multi-line string (if indent is true), or just a long string (if
indent is false). |
static String |
renderXML(String content,
boolean includeNamespaceAttribute,
boolean indent)
Formats XML from a single long string into a nicely indented multi-line string (if indent is true), or just a long
string (if indent is false). |
static String |
unescapeXMLChars(String input)
Replaces the following "escape" strings with their character equivalents: |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final String CDATA_START
public static final String CDATA_END
public static final String COMMENT_START
public static final String COMMENT_END
protected static final String EQUAL_QUOTE
protected static final char QUOTE_MARK
protected static final String WHITESPACE_REGEX
protected static DocumentBuilder utilBuilder
protected static Document utilDoc
| Constructor Detail |
|---|
public XMLKit()
| Method Detail |
|---|
public static String unescapeXMLChars(String input)
Replaces the following "escape" strings with their character equivalents:
- & with &
- < with <
- > with >
- ' with '
- " with "
input - the string on which to perform the replacement
public static String escapeXMLChars(String input)
Replaces the following characters with their "escaped" equivalents:
- & with &
- < with <
- > with >
- ' with '
- " with "
input - the string on which to perform the replacement
public static String escapeXMLChars(char[] ch,
int start,
int length)
escapeXMLChars(String), but takes an array of chars
instead of a String. This might be faster (but should be tested).
public static String filterXML(String input)
input - the input to filter
public static String convertXMLUnicodeEntities(String input)
Converts XML Unicode character entities into their character equivalents within a given string.
This will handle entities in the form &#xxxx;
(decimal character code, where xxxx is a valid character code),
or &#xxxxx (hexadecimal character code, where
xxxx is a valid character code).
input - the string to process
public static String getDeclaredXMLEncoding(InputStream in)
throws IOException
in - the input stream
IOException - if there was a problem reading the input streampublic static Document parseAsDocumentFragment(String text)
text - a document fragment
public static String renderXML(String content,
boolean includeNamespaceAttribute,
boolean indent)
indent is true), or just a long
string (if indent is false).
content - the XML content to formatincludeNamespaceAttribute - whether to include the namespace
attributeindent - whether to render the string in an indented, multiline
fashion
public static String renderXML(NodeList list,
boolean indent)
indent is true), or just a long string (if
indent is false). This is a convenience method
that assumes that we should include namespace attributes.
list - the list of XML nodesindent - whether to render the string in an indented, multiline
fashion
public static String renderXML(NodeList list,
boolean includeNamespaceAttribute,
boolean indent)
indent is true), or just a long string (if
indent is false).
list - the list of XML nodesincludeNamespaceAttribute - whether to include the namespace attributeindent - whether to render the string in an indented, multiline
fashion
public static String renderXML(NodeList list,
int level,
boolean atStart,
boolean includeNamespaceAttribute,
boolean indent)
indent is true), or just a long string (if
indent is false).
list - the list of XML nodeslevel - the level (for indenting; no meaning if indenting is off)atStart - whether the whole XML string is at its beginningincludeNamespaceAttribute - whether to include the namespace
attributeindent - whether to render the string in an indented, multiline
fashion
public static String filterWhitespace(String input)
throws StringIndexOutOfBoundsException
Filters all whitespace: line separators and multiple consecutive spaces
are replaced with a single space, and any leading or trailing whitespace
characters are removed. Any data enclosed in
<![CDATA[ ]]> sections, however, is
left as-is (including the CDATA markers).
input - the input to filter
StringIndexOutOfBoundsException - if there is malformed text in the
input.public static int elementCount(NodeList list)
list - a list of nodes
public static String[] filterViaHTMLTags(String input)
Breaks a message into multiple lines at an HTML <br/>, except if it comes at the beginning of the message, or ending HTML </p>. Other tags are just removed.
Generally used to format output nicely for a console.
input - the string to break
public static String removeMarkup(String input)
input - the string from which to remove markup
public static String renderStartTag(Element element,
boolean includeNamespaceAttribute)
element - the element to renderincludeNamespaceAttribute - whether to include the namespace
attribute
public static String renderStartTag(String elementName,
Attributes attributes,
boolean includeNamespaceAttribute,
String namespaceURI)
elementName - the name of the element to renderattributes - the attributes to includeincludeNamespaceAttribute - whether or not to include the namespace
attributenamespaceURI - the namespace URI
public static String renderEmptyElement(Element element,
boolean includeNamespaceAttribute)
element - the element to renderincludeNamespaceAttribute - whether to include the namespace
attribute
public static String renderEndTag(Element element)
element - the element to render
public static String getSpaces(int count)
count - the number of spaces to return
public static SAXParser getSAXParser(URL schemaLocation,
String schemaDescription)
schemaLocation - location of the schema to useschemaDescription - short (one word or so) description of the schema
public static DocumentBuilder getDocumentBuilder(URL schemaLocation,
String schemaDescription)
schemaLocation - location of the schema to useschemaDescription - short (one word or so) description of the schema
public static Schema getSchema(URL schemaLocation,
String schemaDescription)
schemaLocation - location of the schema to useschemaDescription - short (one word or so) description of the schema
public static List<Element> getElementChildrenOf(Element element)
element - the element whose children are wanted
public static Element getFirstElementChildOf(Element element)
element - the element whose child is wanted
public static Element getFirstElementIn(NodeList list)
list - the nodes to scan
public static List<Element> getAllElementsNamed(Element element,
String name)
element - the element whose children should be examinedname - the name of the element desired
public static Element getFirstElementNamed(Element element,
String name)
element - the element whose children should be examinedname - the name of the element desired
public static String getChildText(Element element,
String childName)
element - childName -
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||