001: // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
002: /*
003: * DOMUtil.java
004: *
005: * SUN PROPRIETARY/CONFIDENTIAL.
006: * This software is the proprietary information of Sun Microsystems, Inc.
007: * Use is subject to license terms.
008: *
009: */
010:
011: package com.sun.jbi.common;
012:
013: import org.w3c.dom.Document;
014: import org.w3c.dom.Element;
015: import org.w3c.dom.Node;
016: import org.w3c.dom.NodeList;
017: import org.w3c.dom.Text;
018:
019: import java.io.Writer;
020:
021: import java.util.ArrayList;
022:
023: import javax.xml.transform.OutputKeys;
024: import javax.xml.transform.Transformer;
025: import javax.xml.transform.TransformerFactory;
026: import javax.xml.transform.dom.DOMSource;
027: import javax.xml.transform.stream.StreamResult;
028:
029: /**
030: * This object provides utility methods to manipulate DOM tree.
031: *
032: * @author chikkala
033: */
034: public class DOMUtil {
035: /**
036: * static object to access the methods of this object.
037: */
038: public static final DOMUtil UTIL = new DOMUtil();
039:
040: /**
041: * Creates a new instance of DOMUtil.
042: */
043: public DOMUtil() {
044: }
045:
046: /**
047: * gets the element.
048: *
049: * @param aParentDocument Document for parent node
050: * @param aTagName String for tagname
051: *
052: * @return Element with tagname
053: */
054: public Element getElement(Document aParentDocument, String aTagName) {
055: NodeList nodeList = aParentDocument
056: .getElementsByTagName(aTagName);
057:
058: return (nodeList != null) ? (Element) nodeList.item(0) : null;
059: }
060:
061: /**
062: * gets the element.
063: *
064: * @param elem Element for parent node
065: * @param tagName String for tagname
066: *
067: * @return Element with tagname
068: */
069: public Element getElement(Element elem, String tagName) {
070: NodeList nl = elem.getElementsByTagName(tagName);
071: Element childElem = (Element) nl.item(0);
072:
073: return childElem;
074: }
075:
076: /**
077: * gets the element value.
078: *
079: * @param doc Document for parent node
080: * @param elemName String for element name
081: *
082: * @return Element value
083: */
084: public String getElementValue(Document doc, String elemName) {
085: String elemVal = null;
086:
087: Element elem = getElement(doc, elemName);
088: elemVal = getTextData(elem);
089:
090: return elemVal;
091: }
092:
093: /**
094: * use this util method to create/retrieve a Text Node in a element.
095: *
096: * @param aElement Element node
097: *
098: * @return Text node for text data
099: */
100: private Text getText(Element aElement) {
101: Node node = null;
102: aElement.normalize();
103: node = aElement.getFirstChild();
104:
105: if ((node == null) || !(node instanceof Text)) {
106: node = aElement.getOwnerDocument().createTextNode("");
107: aElement.appendChild(node);
108: }
109:
110: return (Text) node;
111: }
112:
113: /**
114: * use this util method to set a Text Data in a element.
115: *
116: * @param aElement Element for text node
117: * @param aData String contains text
118: */
119: public void setTextData(Element aElement, String aData) {
120: getText(aElement).setData(aData);
121: }
122:
123: /**
124: * use this util method to retrieve a Text Data in a element.
125: *
126: * @param aElement Element for text node
127: *
128: * @return String contains text
129: */
130: public String getTextData(Element aElement) {
131: return getText(aElement).getData();
132: }
133:
134: /**
135: * save document to stream.
136: *
137: * @param aDocument Document
138: * @param aWriter is what the aDocument is serialized to.
139: *
140: * @return String representation of Document
141: *
142: * @throws Exception If fails to construct a string.
143: */
144: public String DOM2String(Document aDocument, Writer aWriter)
145: throws Exception {
146: TransformerFactory transformerFactory = TransformerFactory
147: .newInstance();
148: Transformer transformer;
149:
150: try {
151: transformer = transformerFactory.newTransformer();
152: } catch (javax.xml.transform.TransformerConfigurationException e) {
153: e.printStackTrace();
154: transformer = null;
155: throw e;
156: }
157:
158: transformer.setOutputProperty(OutputKeys.METHOD, "xml");
159: transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
160: "no");
161: transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
162: transformer.setOutputProperty(
163: OutputKeys.CDATA_SECTION_ELEMENTS, "");
164: transformer.setOutputProperty(OutputKeys.INDENT, "no");
165:
166: try {
167: transformer.transform(new DOMSource(aDocument),
168: new StreamResult(aWriter));
169: } catch (javax.xml.transform.TransformerException e) {
170: e.printStackTrace();
171: throw e;
172: }
173:
174: return aWriter.toString();
175: }
176:
177: /**
178: * gets list of elements.
179: *
180: * @param aParentElement Element for parent
181: * @param aTagName String for tagname
182: *
183: * @return NodeList with tagname
184: */
185: public NodeList getElements(Element aParentElement, String aTagName) {
186: return aParentElement.getElementsByTagNameNS(aParentElement
187: .getNamespaceURI(), aTagName);
188: }
189:
190: /**
191: * gets set of elements.
192: *
193: * @param aParentDocument Document for parent node
194: * @param aTagName String for tagname
195: *
196: * @return NodeList with tagname
197: */
198: public NodeList getElements(Document aParentDocument,
199: String aTagName) {
200: return aParentDocument.getElementsByTagNameNS("*", aTagName);
201: }
202:
203: /**
204: * get the children of the same type element tag name.
205: *
206: * @param aElement Element for parent node
207: * @param aElementTagName String for tagname
208: *
209: * @return NodeList for list of children with the tagname
210: */
211: public NodeList getChildElements(Element aElement,
212: String aElementTagName) {
213: NodeList nodeList = aElement.getChildNodes();
214: NodeListImpl list = new NodeListImpl();
215: int count = nodeList.getLength();
216:
217: for (int i = 0; i < count; ++i) {
218: Node node = nodeList.item(i);
219:
220: if (node instanceof Element) {
221: String tagName = getElementName((Element) node);
222:
223: if (tagName.equals(aElementTagName)) {
224: list.add(node);
225: }
226: }
227: }
228:
229: return list;
230: }
231:
232: /**
233: * get Element Tag Name with striped prefix.
234: *
235: * @param aElement Element object
236: *
237: * @return String with stripped prefix
238: */
239: public String getElementName(Element aElement) {
240: String tagName = aElement.getTagName();
241:
242: return getName(tagName);
243: }
244:
245: /**
246: * strips the prefix of the name if present.
247: *
248: * @param aName String value of Name with prefix
249: *
250: * @return String for name after striping prefix
251: */
252: public String getName(String aName) {
253: int lastIdx = aName.lastIndexOf(':');
254:
255: if (lastIdx >= 0) {
256: return aName.substring(lastIdx + 1);
257: }
258:
259: return aName;
260: }
261:
262: /**
263: * NodeListImpl.
264: */
265: public static class NodeListImpl extends ArrayList implements
266: NodeList {
267: /**
268: * Default Constructor.
269: */
270: public NodeListImpl() {
271: super ();
272: }
273:
274: /**
275: * nodelist length.
276: *
277: * @return int for number of nodes in nodelist
278: */
279: public int getLength() {
280: return this .size();
281: }
282:
283: /**
284: * return a node.
285: *
286: * @param aIndex int for the index of the node
287: *
288: * @return Node at the index
289: */
290: public Node item(int aIndex) {
291: return (Node) this.get(aIndex);
292: }
293: }
294: }
|