001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app.util;
031:
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: import org.w3c.dom.Element;
036: import org.w3c.dom.Node;
037: import org.w3c.dom.NodeList;
038: import org.w3c.dom.Text;
039:
040: /**
041: * A utility class which provides methods for working with a W3C DOM.
042: */
043: public class DomUtil {
044:
045: /**
046: * Determines whether a specific boolean flag is set on an element.
047: *
048: * @param element The element to analyze.
049: * @param attributeName The name of the boolean 'flag' attribute.
050: * @return True if the value of the attribute is 'true', false if it is
051: * not or if the attribute does not exist.
052: */
053: public static boolean getBooleanAttribute(Element element,
054: String attributeName) {
055: String value = element.getAttribute(attributeName);
056: if (value == null) {
057: return false;
058: } else if (value.equals("true")) {
059: return true;
060: } else {
061: return false;
062: }
063: }
064:
065: /**
066: * Retrieves the first immediate child element of the specified element
067: * whose name matches the provided <code>name</code> parameter.
068: *
069: * @param parentElement The element to search.
070: * @param name The name of the child element.
071: * @return The child element, or null if none was found.
072: */
073: public static Element getChildElementByTagName(
074: Element parentElement, String name) {
075: NodeList nodes = parentElement.getChildNodes();
076: int length = nodes.getLength();
077: for (int index = 0; index < length; ++index) {
078: if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
079: && name.equals(nodes.item(index).getNodeName())) {
080: return (Element) nodes.item(index);
081: }
082: }
083: return null;
084: }
085:
086: /**
087: * Retrieves the first immediate child element of the specified element
088: * whose name matches the provided <code>name</code> parameter.
089: *
090: * @param parentElement The element to search.
091: * @param namespaceURI The namespace URI of the child element.
092: * @param localName The name of the child element.
093: * @return The child element, or null if none was found.
094: */
095: public static Element getChildElementByTagNameNS(
096: Element parentElement, String namespaceURI, String localName) {
097: NodeList nodes = parentElement.getChildNodes();
098: int length = nodes.getLength();
099: for (int index = 0; index < length; ++index) {
100: if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
101: && namespaceURI.equals(nodes.item(index)
102: .getNamespaceURI())
103: && localName
104: .equals(nodes.item(index).getNodeName())) {
105: return (Element) nodes.item(index);
106: }
107: }
108: return null;
109: }
110:
111: /**
112: * Retrieves all immediate child elements of the specified element whose
113: * names match the provided <code>name</code> parameter.
114: *
115: * @param parentElement The element to search.
116: * @param name The name of the child element.
117: * @return An array of matching child elements.
118: */
119: public static Element[] getChildElementsByTagName(
120: Element parentElement, String name) {
121: List children = new ArrayList();
122: NodeList nodes = parentElement.getChildNodes();
123: int length = nodes.getLength();
124: for (int index = 0; index < length; ++index) {
125: if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
126: && name.equals(nodes.item(index).getNodeName())) {
127: children.add(nodes.item(index));
128: }
129: }
130: Element[] childElements = new Element[children.size()];
131: return (Element[]) children.toArray(childElements);
132: }
133:
134: /**
135: * Retrieves all immediate child elements of the specified element whose
136: * names match the provided <code>name</code> parameter.
137: *
138: * @param parentElement The element to search.
139: * @param namespaceURI The namespace URI of the child element.
140: * @param localName The name of the child element.
141: * @return An array of matching child elements.
142: */
143: public static Element[] getChildElementsByTagNameNS(
144: Element parentElement, String namespaceURI, String localName) {
145: List children = new ArrayList();
146: NodeList nodes = parentElement.getChildNodes();
147: int length = nodes.getLength();
148: for (int index = 0; index < length; ++index) {
149: if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
150: && namespaceURI.equals(nodes.item(index)
151: .getNamespaceURI())
152: && localName
153: .equals(nodes.item(index).getNodeName())) {
154: children.add(nodes.item(index));
155: }
156: }
157: Element[] childElements = new Element[children.size()];
158: return (Element[]) children.toArray(childElements);
159: }
160:
161: /**
162: * Counts the number of immediate child elements of the specified element
163: * whose names match the provided <code>name</code> parameter.
164: *
165: * @param parentElement The element to analyze.
166: * @param name The name of the child element.
167: * @return The number of matching child elements.
168: */
169: public static int getChildElementCountByTagName(
170: Element parentElement, String name) {
171: NodeList nodes = parentElement.getChildNodes();
172: int length = nodes.getLength();
173: int count = 0;
174: for (int index = 0; index < length; ++index) {
175: if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
176: && name.equals(nodes.item(index).getNodeName())) {
177: ++count;
178: }
179: }
180: return count;
181: }
182:
183: /**
184: * Counts the number of immediate child elements of the specified element
185: * whose names match the provided <code>name</code> parameter.
186: *
187: * @param parentElement The element to analyze.
188: * @param namespaceURI The namespace URI of the child element.
189: * @param localName The name of the child element.
190: * @return The number of matching child elements.
191: */
192: public static int getChildElementCountByTagNameNS(
193: Element parentElement, String namespaceURI, String localName) {
194: NodeList nodes = parentElement.getChildNodes();
195: int length = nodes.getLength();
196: int count = 0;
197: for (int index = 0; index < length; ++index) {
198: if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
199: && namespaceURI.equals(nodes.item(index)
200: .getNamespaceURI())
201: && localName
202: .equals(nodes.item(index).getNodeName())) {
203: ++count;
204: }
205: }
206: return count;
207: }
208:
209: /**
210: * Returns the text content of a DOM <code>Element</code>.
211: *
212: * @param element The <code>Element</code> to analyze.
213: */
214: public static String getElementText(Element element) {
215: NodeList children = element.getChildNodes();
216: int childCount = children.getLength();
217: for (int index = 0; index < childCount; ++index) {
218: if (children.item(index) instanceof Text) {
219: Text text = (Text) children.item(index);
220: return text.getData();
221: }
222: }
223: return null;
224: }
225:
226: /**
227: * Sets the text content of a DOM <code>Element</code>.
228: *
229: * @param element The <code>Element</code> to modify.
230: * @param value The new text value.
231: */
232: public static void setElementText(Element element, String value) {
233: NodeList children = element.getChildNodes();
234: int childCount = children.getLength();
235: for (int index = 0; index < childCount; ++index) {
236: if (children.item(index) instanceof Text) {
237: Text text = (Text) children.item(index);
238: text.setData(value);
239: return;
240: }
241: }
242: Text text = element.getOwnerDocument().createTextNode(value);
243: element.appendChild(text);
244: }
245:
246: /** Non-instantiable class. */
247: private DomUtil() {
248: }
249: }
|