Package org.apache.axis.utils
Class XMLUtils
- java.lang.Object
-
- org.apache.axis.utils.XMLUtils
-
public class XMLUtils extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classXMLUtils.ParserErrorHandler
-
Field Summary
Fields Modifier and Type Field Description protected static booleanenableParserReusestatic java.lang.StringhttpAuthCharEncodingprotected static org.apache.commons.logging.Loglog
-
Constructor Summary
Constructors Constructor Description XMLUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.w3c.dom.Element[]asElementArray(java.util.List list)Converts a List with org.w3c.dom.Element objects to an Array with org.w3c.dom.Element objects.static java.lang.Stringbase64encode(byte[] bytes)static voidDocumentToStream(org.w3c.dom.Document doc, java.io.OutputStream out)static java.lang.StringDocumentToString(org.w3c.dom.Document doc)turn a whole DOM document into XMLstatic voidDocumentToWriter(org.w3c.dom.Document doc, java.io.Writer writer)static voidElementToStream(org.w3c.dom.Element element, java.io.OutputStream out)static java.lang.StringElementToString(org.w3c.dom.Element element)turn an element into an XML fragmentstatic voidElementToWriter(org.w3c.dom.Element element, java.io.Writer writer)static org.w3c.dom.NodefindNode(org.w3c.dom.Node node, QName name)Find a Node with a given QNamestatic java.lang.StringgetChildCharacterData(org.w3c.dom.Element parentEl)Concat all the text and cdata node children of this elem and return the resulting text.static javax.xml.parsers.DocumentBuildergetDocumentBuilder()Gets a DocumentBuilderstatic org.xml.sax.InputSourcegetEmptyInputSource()static java.lang.StringgetEncoding()Get the current encoding in effectstatic java.lang.StringgetEncoding(MessageContext msgContext)Get the current encoding in effectstatic java.lang.StringgetEncoding(Message message, MessageContext msgContext)static java.lang.StringgetEncoding(Message message, MessageContext msgContext, XMLEncoder defaultEncoder)static QNamegetFullQNameFromString(java.lang.String str, org.w3c.dom.Node e)Return a QName when passed a string like "foo:bar" by mapping the "foo" prefix to a namespace in the context of the given Node.static java.lang.StringgetInnerXMLString(org.w3c.dom.Element element)get the inner XML inside an element as a string.static org.xml.sax.InputSourcegetInputSourceFromURI(java.lang.String uri)Utility to get the bytes uri.static java.lang.StringgetNamespace(java.lang.String prefix, org.w3c.dom.Node e)static java.lang.StringgetNamespace(java.lang.String prefix, org.w3c.dom.Node e, org.w3c.dom.Node stopNode)Searches for the namespace URI of the given prefix in the given DOM range.static java.lang.StringgetPrefix(java.lang.String uri, org.w3c.dom.Node e)static QNamegetQNameFromString(java.lang.String str, org.w3c.dom.Node e)Return a QName when passed a string like "foo:bar" by mapping the "foo" prefix to a namespace in the context of the given Node.static javax.xml.parsers.SAXParsergetSAXParser()Get a SAX parser instance from the JAXP factory.static java.lang.StringgetStringForQName(QName qname, org.w3c.dom.Element e)Return a string for a particular QName, mapping a new prefix if necessary.static XMLEncodergetXMLEncoder(java.lang.String encoding)Get the XMLEncoder for specific encodingstatic XMLEncodergetXMLEncoder(MessageContext msgContext)Get the current XMLEncoderstatic voidinitSAXFactory(java.lang.String factoryClassName, boolean namespaceAware, boolean validating)Initialize the SAX parser factory.static org.w3c.dom.DocumentnewDocument()Get an empty new Documentstatic org.w3c.dom.DocumentnewDocument(java.io.InputStream inp)Get a new Document read from the input streamstatic org.w3c.dom.DocumentnewDocument(java.lang.String uri)Get a new Document read from the indicated uristatic org.w3c.dom.DocumentnewDocument(java.lang.String uri, java.lang.String username, java.lang.String password)Create a new document from the given URI, use the username and password if the URI requires authentication.static org.w3c.dom.DocumentnewDocument(org.xml.sax.InputSource inp)Get a new Document read from the input sourcestatic voidnormalize(org.w3c.dom.Node node)Trim all new lines from text nodes.static voidPrettyDocumentToStream(org.w3c.dom.Document doc, java.io.OutputStream out)static java.lang.StringPrettyDocumentToString(org.w3c.dom.Document doc)static voidPrettyDocumentToWriter(org.w3c.dom.Document doc, java.io.Writer writer)static voidPrettyElementToStream(org.w3c.dom.Element element, java.io.OutputStream out)static voidPrettyElementToWriter(org.w3c.dom.Element element, java.io.Writer writer)static voidprivateElementToWriter(org.w3c.dom.Element element, java.io.Writer writer, boolean omitXMLDecl, boolean pretty)static voidreleaseDocumentBuilder(javax.xml.parsers.DocumentBuilder db)Releases a DocumentBuilderstatic voidreleaseSAXParser(javax.xml.parsers.SAXParser parser)Return a SAX parser for reuse.static org.xml.sax.InputSourcesourceToInputSource(javax.xml.transform.Source source)Utility to get the bytes uristatic org.w3c.dom.ElementStringToElement(java.lang.String namespace, java.lang.String name, java.lang.String string)Convert a simple string to an element with a text nodestatic java.lang.Stringtrim(java.lang.String str)static java.lang.StringxmlEncodeString(java.lang.String orig)Encode a string appropriately for XML.
-
-
-
Field Detail
-
log
protected static org.apache.commons.logging.Log log
-
httpAuthCharEncoding
public static final java.lang.String httpAuthCharEncoding
- See Also:
- Constant Field Values
-
enableParserReuse
protected static boolean enableParserReuse
-
-
Method Detail
-
xmlEncodeString
public static java.lang.String xmlEncodeString(java.lang.String orig)
Encode a string appropriately for XML.- Parameters:
orig- the String to encode- Returns:
- a String in which XML special chars are repalced by entities
-
getXMLEncoder
public static XMLEncoder getXMLEncoder(MessageContext msgContext)
Get the current XMLEncoder- Returns:
- XMLEncoder
-
getXMLEncoder
public static XMLEncoder getXMLEncoder(java.lang.String encoding)
Get the XMLEncoder for specific encoding- Returns:
- XMLEncoder
-
getEncoding
public static java.lang.String getEncoding(MessageContext msgContext)
Get the current encoding in effect- Returns:
- string
-
getEncoding
public static java.lang.String getEncoding()
Get the current encoding in effect- Returns:
- string
-
initSAXFactory
public static void initSAXFactory(java.lang.String factoryClassName, boolean namespaceAware, boolean validating)Initialize the SAX parser factory.- Parameters:
factoryClassName- The (optional) class name of the desired SAXParserFactory implementation. Will be assigned to the system property javax.xml.parsers.SAXParserFactory unless this property is already set. Ifnull, leaves current setting alone.namespaceAware- true if we want a namespace-aware parservalidating- true if we want a validating parser
-
getDocumentBuilder
public static javax.xml.parsers.DocumentBuilder getDocumentBuilder() throws javax.xml.parsers.ParserConfigurationExceptionGets a DocumentBuilder- Returns:
- DocumentBuilder
- Throws:
javax.xml.parsers.ParserConfigurationException
-
releaseDocumentBuilder
public static void releaseDocumentBuilder(javax.xml.parsers.DocumentBuilder db)
Releases a DocumentBuilder- Parameters:
db-
-
getSAXParser
public static javax.xml.parsers.SAXParser getSAXParser()
Get a SAX parser instance from the JAXP factory.- Returns:
- a SAXParser instance.
-
releaseSAXParser
public static void releaseSAXParser(javax.xml.parsers.SAXParser parser)
Return a SAX parser for reuse.- Parameters:
parser- A SAX parser that is available for reuse
-
newDocument
public static org.w3c.dom.Document newDocument() throws javax.xml.parsers.ParserConfigurationExceptionGet an empty new Document- Returns:
- Document
- Throws:
javax.xml.parsers.ParserConfigurationException- if construction problems occur
-
newDocument
public static org.w3c.dom.Document newDocument(org.xml.sax.InputSource inp) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOExceptionGet a new Document read from the input source- Returns:
- Document
- Throws:
javax.xml.parsers.ParserConfigurationException- if construction problems occurorg.xml.sax.SAXException- if the document has xml sax problemsjava.io.IOException- if i/o exceptions occur
-
newDocument
public static org.w3c.dom.Document newDocument(java.io.InputStream inp) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOExceptionGet a new Document read from the input stream- Returns:
- Document
- Throws:
javax.xml.parsers.ParserConfigurationException- if construction problems occurorg.xml.sax.SAXException- if the document has xml sax problemsjava.io.IOException- if i/o exceptions occur
-
newDocument
public static org.w3c.dom.Document newDocument(java.lang.String uri) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOExceptionGet a new Document read from the indicated uri- Returns:
- Document
- Throws:
javax.xml.parsers.ParserConfigurationException- if construction problems occurorg.xml.sax.SAXException- if the document has xml sax problemsjava.io.IOException- if i/o exceptions occur
-
newDocument
public static org.w3c.dom.Document newDocument(java.lang.String uri, java.lang.String username, java.lang.String password) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOExceptionCreate a new document from the given URI, use the username and password if the URI requires authentication.- Parameters:
uri- the resource to getusername- basic auth usernamepassword- basic auth password- Throws:
javax.xml.parsers.ParserConfigurationException- if construction problems occurorg.xml.sax.SAXException- if the document has xml sax problemsjava.io.IOException- if i/o exceptions occur
-
ElementToString
public static java.lang.String ElementToString(org.w3c.dom.Element element)
turn an element into an XML fragment- Parameters:
element-- Returns:
- stringified element
-
DocumentToString
public static java.lang.String DocumentToString(org.w3c.dom.Document doc)
turn a whole DOM document into XML- Parameters:
doc- DOM document- Returns:
- string representation of the document, including XML declaration
-
PrettyDocumentToString
public static java.lang.String PrettyDocumentToString(org.w3c.dom.Document doc)
-
privateElementToWriter
public static void privateElementToWriter(org.w3c.dom.Element element, java.io.Writer writer, boolean omitXMLDecl, boolean pretty)
-
ElementToStream
public static void ElementToStream(org.w3c.dom.Element element, java.io.OutputStream out)
-
PrettyElementToStream
public static void PrettyElementToStream(org.w3c.dom.Element element, java.io.OutputStream out)
-
ElementToWriter
public static void ElementToWriter(org.w3c.dom.Element element, java.io.Writer writer)
-
PrettyElementToWriter
public static void PrettyElementToWriter(org.w3c.dom.Element element, java.io.Writer writer)
-
DocumentToStream
public static void DocumentToStream(org.w3c.dom.Document doc, java.io.OutputStream out)
-
PrettyDocumentToStream
public static void PrettyDocumentToStream(org.w3c.dom.Document doc, java.io.OutputStream out)
-
DocumentToWriter
public static void DocumentToWriter(org.w3c.dom.Document doc, java.io.Writer writer)
-
PrettyDocumentToWriter
public static void PrettyDocumentToWriter(org.w3c.dom.Document doc, java.io.Writer writer)
-
StringToElement
public static org.w3c.dom.Element StringToElement(java.lang.String namespace, java.lang.String name, java.lang.String string)Convert a simple string to an element with a text node- Parameters:
namespace- - element namespacename- - element namestring- - value of the text node- Returns:
- element - an XML Element, null if no element was created
-
getInnerXMLString
public static java.lang.String getInnerXMLString(org.w3c.dom.Element element)
get the inner XML inside an element as a string. This is done by converting the XML to its string representation, then extracting the subset between beginning and end tags.- Parameters:
element-- Returns:
- textual body of the element, or null for no inner body
-
getPrefix
public static java.lang.String getPrefix(java.lang.String uri, org.w3c.dom.Node e)
-
getNamespace
public static java.lang.String getNamespace(java.lang.String prefix, org.w3c.dom.Node e, org.w3c.dom.Node stopNode)Searches for the namespace URI of the given prefix in the given DOM range. The namespace is not searched in parent of the "stopNode". This is usefull to get all the needed namespaces when you need to ouput only a subtree of a DOM document.- Parameters:
prefix- the prefix to finde- the starting nodestopNode- null to search in all the document or a parent node where the search must stop.- Returns:
- null if no namespace is found, or the namespace URI.
-
getNamespace
public static java.lang.String getNamespace(java.lang.String prefix, org.w3c.dom.Node e)
-
getQNameFromString
public static QName getQNameFromString(java.lang.String str, org.w3c.dom.Node e)
Return a QName when passed a string like "foo:bar" by mapping the "foo" prefix to a namespace in the context of the given Node.- Returns:
- a QName generated from the given string representation
-
getFullQNameFromString
public static QName getFullQNameFromString(java.lang.String str, org.w3c.dom.Node e)
Return a QName when passed a string like "foo:bar" by mapping the "foo" prefix to a namespace in the context of the given Node. If default namespace is found it is returned as part of the QName.- Returns:
- a QName generated from the given string representation
-
getStringForQName
public static java.lang.String getStringForQName(QName qname, org.w3c.dom.Element e)
Return a string for a particular QName, mapping a new prefix if necessary.
-
getChildCharacterData
public static java.lang.String getChildCharacterData(org.w3c.dom.Element parentEl)
Concat all the text and cdata node children of this elem and return the resulting text. (by Matt Duftler)- Parameters:
parentEl- the element whose cdata/text node values are to be combined.- Returns:
- the concatanated string.
-
getInputSourceFromURI
public static org.xml.sax.InputSource getInputSourceFromURI(java.lang.String uri)
Utility to get the bytes uri. Does NOT handle authenticated URLs, use getInputSourceFromURI(uri, username, password)- Parameters:
uri- the resource to get- See Also:
getInputSourceFromURI(String uri, String username, String password)
-
sourceToInputSource
public static org.xml.sax.InputSource sourceToInputSource(javax.xml.transform.Source source)
Utility to get the bytes uri- Parameters:
source- the resource to get
-
base64encode
public static final java.lang.String base64encode(byte[] bytes)
-
getEmptyInputSource
public static org.xml.sax.InputSource getEmptyInputSource()
-
findNode
public static org.w3c.dom.Node findNode(org.w3c.dom.Node node, QName name)Find a Node with a given QName- Parameters:
node- parent nodename- QName of the child we need to find- Returns:
- child node
-
normalize
public static void normalize(org.w3c.dom.Node node)
Trim all new lines from text nodes.- Parameters:
node-
-
trim
public static java.lang.String trim(java.lang.String str)
-
asElementArray
public static org.w3c.dom.Element[] asElementArray(java.util.List list)
Converts a List with org.w3c.dom.Element objects to an Array with org.w3c.dom.Element objects.- Parameters:
list- List containing org.w3c.dom.Element objects- Returns:
- Element[] Array with org.w3c.dom.Element objects
-
getEncoding
public static java.lang.String getEncoding(Message message, MessageContext msgContext)
-
getEncoding
public static java.lang.String getEncoding(Message message, MessageContext msgContext, XMLEncoder defaultEncoder)
-
-