001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.utils;
007:
008: import java.io.IOException;
009: import java.io.StringWriter;
010:
011: import javax.xml.transform.Transformer;
012: import javax.xml.transform.TransformerConfigurationException;
013: import javax.xml.transform.TransformerException;
014: import javax.xml.transform.TransformerFactory;
015: import javax.xml.transform.dom.DOMSource;
016: import javax.xml.transform.sax.SAXResult;
017:
018: import org.jasig.portal.serialize.OutputFormat;
019: import org.jasig.portal.serialize.XMLSerializer;
020: import org.w3c.dom.Document;
021: import org.w3c.dom.Element;
022: import org.w3c.dom.Node;
023: import org.xml.sax.ContentHandler;
024:
025: /**
026: * This utility provides useful XML helper methods.
027: *
028: * @author Ken Weiner, kweiner@unicon.net
029: * @version $Revision: 34797 $
030: */
031: public class XML {
032: /**
033: * Gets the text value of an Element. For example, if an element nameElement
034: * looks like this: <name>Fred</name>, then getElementText(nameElement) would
035: * return "Fred". An empty String is returned in the case that there is no text
036: * under the element.
037: * @param e the Element with a text value
038: * @return the the text value of the element
039: */
040: public static String getElementText(Element e) {
041: String val = "";
042: for (Node n = e.getFirstChild(); n != null; n = n
043: .getNextSibling()) {
044: if (n.getNodeType() == Node.TEXT_NODE) {
045: val = n.getNodeValue();
046: break;
047: }
048: }
049: return val;
050: }
051:
052: /**
053: * Gets the text value of a child Element. For example, if an element nameElement
054: * looks like this: <name><first>Fred</first><last>Flinstone</last></name>, then
055: * getChildElementText(nameElement, "first") would return "Fred". An empty String
056: * is returned in the case that there is no text under the child Element.
057: * @param e the Element to search under
058: * @param childElementName the name of the child Element
059: * @return the text value of the child element
060: */
061: public static String getChildElementText(Element e,
062: String childElementName) {
063: String val = "";
064: for (Node n = e.getFirstChild(); n != null; n = n
065: .getNextSibling()) {
066: if (n.getNodeType() == Node.ELEMENT_NODE
067: && n.getNodeName() != null
068: && n.getNodeName().equals(childElementName)) {
069: Element childElement = (Element) n;
070: val = getElementText(childElement);
071: }
072: }
073: return val;
074: }
075:
076: /**
077: * Gets the contents of an XML Document or Element as a nicely formatted string.
078: * This method is useful for debugging.
079: * @param node the Node to print; must be of type Document or Element
080: * @return a nicely formatted String suitable for printing
081: */
082: public static String serializeNode(Node node) {
083: OutputFormat format = new OutputFormat();
084: format.setOmitXMLDeclaration(true);
085: format.setIndenting(true);
086: return serializeNode(node, format);
087: }
088:
089: /**
090: * Gets the contents of an XML Document or Element as a formatted string.
091: * This method is useful for debugging.
092: * @param node the Node to print; must be of type Document or Element
093: * @param format controls the formatting of the string
094: * @return a nicely formatted String suitable for printing
095: */
096: public static String serializeNode(Node node, OutputFormat format) {
097: String returnString = null;
098: StringWriter outString = new StringWriter();
099: XMLSerializer xsl = new XMLSerializer(outString, format);
100: try {
101: if (node.getNodeType() == Node.DOCUMENT_NODE) {
102: xsl.serialize((Document) node);
103: returnString = outString.toString();
104: } else if (node.getNodeType() == Node.ELEMENT_NODE) {
105: xsl.serialize((Element) node);
106: returnString = outString.toString();
107: } else {
108: returnString = "The node you passed to getNodeAsString() must be of type org.w3c.dom.Document or org.w3c.dom.Element in order to be serialized.";
109: }
110: } catch (IOException ioe) {
111: returnString = "Error occurred while trying to serialize node: "
112: + ioe.getMessage();
113: }
114:
115: return returnString;
116: }
117:
118: /**
119: * This is only being kept around for backward compatibility. Callers
120: * should now be using Document.cloneNode(true).
121: * @param olddoc the original document
122: * @return a clone of the original document with preserved ID tables
123: */
124: public static Document cloneDocument(Document olddoc) {
125: return (Document) olddoc.cloneNode(true);
126: }
127:
128: /**
129: * Outputs a dom document into a sax stream.
130: *
131: * @param dom a dom <code>Node</code> value
132: * @param sax a sax <code>ContentHandler</code> value
133: */
134: public static void dom2sax(Node dom, ContentHandler sax)
135: throws TransformerConfigurationException,
136: TransformerException {
137: TransformerFactory tFactory = TransformerFactory.newInstance();
138: Transformer emptytr = tFactory.newTransformer();
139: emptytr.transform(new DOMSource(dom), new SAXResult(sax));
140: }
141: }
|