com.opensymphony.util
Class XMLUtils

java.lang.Object
  extended by com.opensymphony.util.XMLUtils

public class XMLUtils
extends Object

XMLUtils is a bunch of quick access utility methods to common XML operations.

These include:

This class contains static methods only and is not meant to be instantiated. It also contains only basic (common) functions - for more control access appropriate API directly.

Version:
$Revision: 1.2 $
Author:
Joe Walnes, Hani Suleiman

Constructor Summary
XMLUtils()
           
 
Method Summary
static org.w3c.dom.Node cloneNode(org.w3c.dom.Node node, org.w3c.dom.Document target, boolean deep)
          Clone given Node into target Document.
static int getCacheSize()
          Accessor for the internal XSL transformer cache
static String getElementText(org.w3c.dom.Element element)
          Return the contained text within an Element.
static org.w3c.dom.Document newDocument()
          Create blank Document.
static org.w3c.dom.Document newDocument(String rootElementName)
          Create blank Document, and insert root element with given name.
static org.w3c.dom.Document parse(File file)
          Parse a File of XML into Document.
static org.w3c.dom.Document parse(org.xml.sax.InputSource in)
          Parse an InputSource of XML into Document.
static org.w3c.dom.Document parse(InputStream in)
          Parse an InputStream of XML into Document.
static org.w3c.dom.Document parse(Reader in)
          Parse a Reader of XML into Document.
static org.w3c.dom.Document parse(String xml)
          Parse a String containing XML data into a Document.
static org.w3c.dom.Document parse(URL url)
          Parse the contents of a URL's XML into Document.
static String print(org.w3c.dom.Document document)
          Pretty-print a Document back to String of XML.
static void print(org.w3c.dom.Document document, File file)
          Pretty-print a Document to File.
static void print(org.w3c.dom.Document document, OutputStream out)
          Pretty-print a Document to OutputStream.
static void print(org.w3c.dom.Document document, Writer out)
          Pretty-print a Document to Writer.
static void setCacheSize(int newCacheSize)
          Sets the internal cache size for XSL sheets
static org.w3c.dom.Document transform(org.w3c.dom.Document xml, org.w3c.dom.Document xsl)
          Perform XSL transformations using given Documents and return new Document.
static void transform(InputStream xml, InputStream xsl, OutputStream result)
          Perform XSL transformation.
static void transform(Reader xml, Reader xsl, Writer result)
          Perform XSL transformation.
static void transform(Reader xml, Reader xsl, Writer result, Map parameters)
          Perform XSL transformation, with params.
static void transform(Reader xml, Reader xsl, Writer result, Map parameters, String xslkey)
          This method applies an XSL sheet to an XML document.
static String transform(String xml, String xsl)
          Perform XSL transformation.
static org.w3c.dom.Node xpath(org.w3c.dom.Node base, String xpath)
          Return single Node from base Node using X-Path expression.
static org.w3c.dom.NodeList xpathList(org.w3c.dom.Node base, String xpath)
          Return multiple Nodes from base Node using X-Path expression.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtils

public XMLUtils()
Method Detail

getElementText

public static final String getElementText(org.w3c.dom.Element element)
Return the contained text within an Element. Returns null if no text found.


cloneNode

public static final org.w3c.dom.Node cloneNode(org.w3c.dom.Node node,
                                               org.w3c.dom.Document target,
                                               boolean deep)
                                        throws org.w3c.dom.DOMException
Clone given Node into target Document. If targe is null, same Document will be used. If deep is specified, all children below will also be cloned.

Throws:
org.w3c.dom.DOMException

newDocument

public static final org.w3c.dom.Document newDocument()
                                              throws javax.xml.parsers.ParserConfigurationException
Create blank Document.

Throws:
javax.xml.parsers.ParserConfigurationException

newDocument

public static final org.w3c.dom.Document newDocument(String rootElementName)
                                              throws javax.xml.parsers.ParserConfigurationException
Create blank Document, and insert root element with given name.

Throws:
javax.xml.parsers.ParserConfigurationException

parse

public static final org.w3c.dom.Document parse(org.xml.sax.InputSource in)
                                        throws javax.xml.parsers.ParserConfigurationException,
                                               IOException,
                                               org.xml.sax.SAXException
Parse an InputSource of XML into Document.

Throws:
javax.xml.parsers.ParserConfigurationException
IOException
org.xml.sax.SAXException

parse

public static final org.w3c.dom.Document parse(InputStream in)
                                        throws javax.xml.parsers.ParserConfigurationException,
                                               IOException,
                                               org.xml.sax.SAXException
Parse an InputStream of XML into Document.

Throws:
javax.xml.parsers.ParserConfigurationException
IOException
org.xml.sax.SAXException

parse

public static final org.w3c.dom.Document parse(Reader in)
                                        throws javax.xml.parsers.ParserConfigurationException,
                                               IOException,
                                               org.xml.sax.SAXException
Parse a Reader of XML into Document.

Throws:
javax.xml.parsers.ParserConfigurationException
IOException
org.xml.sax.SAXException

parse

public static final org.w3c.dom.Document parse(File file)
                                        throws javax.xml.parsers.ParserConfigurationException,
                                               IOException,
                                               org.xml.sax.SAXException
Parse a File of XML into Document.

Throws:
javax.xml.parsers.ParserConfigurationException
IOException
org.xml.sax.SAXException

parse

public static final org.w3c.dom.Document parse(URL url)
                                        throws javax.xml.parsers.ParserConfigurationException,
                                               IOException,
                                               org.xml.sax.SAXException
Parse the contents of a URL's XML into Document.

Throws:
javax.xml.parsers.ParserConfigurationException
IOException
org.xml.sax.SAXException

parse

public static final org.w3c.dom.Document parse(String xml)
                                        throws javax.xml.parsers.ParserConfigurationException,
                                               IOException,
                                               org.xml.sax.SAXException
Parse a String containing XML data into a Document. Note that String contains XML itself and is not URI.

Throws:
javax.xml.parsers.ParserConfigurationException
IOException
org.xml.sax.SAXException

print

public static final void print(org.w3c.dom.Document document,
                               Writer out)
                        throws IOException
Pretty-print a Document to Writer.

Throws:
IOException

print

public static final void print(org.w3c.dom.Document document,
                               OutputStream out)
                        throws IOException
Pretty-print a Document to OutputStream.

Throws:
IOException

print

public static final void print(org.w3c.dom.Document document,
                               File file)
                        throws IOException
Pretty-print a Document to File.

Throws:
IOException

print

public static final String print(org.w3c.dom.Document document)
                          throws IOException
Pretty-print a Document back to String of XML.

Throws:
IOException

transform

public static final void transform(Reader xml,
                                   Reader xsl,
                                   Writer result)
                            throws javax.xml.transform.TransformerException
Perform XSL transformation.

Throws:
javax.xml.transform.TransformerException

xpath

public static final org.w3c.dom.Node xpath(org.w3c.dom.Node base,
                                           String xpath)
                                    throws javax.xml.transform.TransformerException
Return single Node from base Node using X-Path expression.

Throws:
javax.xml.transform.TransformerException

xpathList

public static final org.w3c.dom.NodeList xpathList(org.w3c.dom.Node base,
                                                   String xpath)
                                            throws javax.xml.transform.TransformerException
Return multiple Nodes from base Node using X-Path expression.

Throws:
javax.xml.transform.TransformerException

setCacheSize

public static void setCacheSize(int newCacheSize)
Sets the internal cache size for XSL sheets

Parameters:
newCacheSize -

getCacheSize

public static int getCacheSize()
Accessor for the internal XSL transformer cache

Returns:
the cache size

transform

public static final void transform(Reader xml,
                                   Reader xsl,
                                   Writer result,
                                   Map parameters,
                                   String xslkey)
                            throws javax.xml.transform.TransformerException
This method applies an XSL sheet to an XML document.

2002/Apr/7, fixed bug 540875, first reported by Erik Weber, and added configurable cache size.

Parameters:
xml - the XML source
xsl - the XSL source
result - where to put the response
parameters - a map consisting of params for the transformer
xslkey - a key used to refer to the XSL
Throws:
javax.xml.transform.TransformerException

transform

public static final void transform(Reader xml,
                                   Reader xsl,
                                   Writer result,
                                   Map parameters)
                            throws javax.xml.transform.TransformerException
Perform XSL transformation, with params.

Throws:
javax.xml.transform.TransformerException

transform

public static final void transform(InputStream xml,
                                   InputStream xsl,
                                   OutputStream result)
                            throws javax.xml.transform.TransformerException
Perform XSL transformation.

Throws:
javax.xml.transform.TransformerException

transform

public static final String transform(String xml,
                                     String xsl)
                              throws javax.xml.transform.TransformerException
Perform XSL transformation. XML and XSL supplied in Strings and result returned as String. Note that inputs are actual XML/XSL data, not URIs.

Throws:
javax.xml.transform.TransformerException

transform

public static final org.w3c.dom.Document transform(org.w3c.dom.Document xml,
                                                   org.w3c.dom.Document xsl)
                                            throws javax.xml.parsers.ParserConfigurationException,
                                                   javax.xml.transform.TransformerException
Perform XSL transformations using given Documents and return new Document.

Throws:
javax.xml.parsers.ParserConfigurationException
javax.xml.transform.TransformerException

OSCore Project Page