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