001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.controls.system.ejb.internal;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.w3c.dom.Attr;
025: import org.w3c.dom.Element;
026: import org.w3c.dom.Node;
027: import org.w3c.dom.NodeList;
028: import org.w3c.dom.Text;
029:
030: /**
031: * Some simple DOM utility methods.
032: */
033: final class DomUtils {
034:
035: /**
036: * <p>Returns the first child element with the given name. Returns
037: * <code>null</code> if not found.</p>
038: *
039: * @param parent parent element
040: * @param name name of the child element
041: * @return child element
042: */
043: static Element getChildElementByName(Element parent, String name) {
044: NodeList children = parent.getChildNodes();
045:
046: for (int i = 0; i < children.getLength(); i++) {
047: Node node = children.item(i);
048: if (node.getNodeType() == Node.ELEMENT_NODE) {
049: Element element = (Element) node;
050: if (element.getTagName().equals(name)) {
051: return element;
052: }
053: }
054: }
055:
056: return null;
057: }
058:
059: /**
060: * <p>Returns a list of child elements with the given
061: * name. Returns an empty list if there are no such child
062: * elements.</p>
063: *
064: * @param parent parent element
065: * @param name name of the child element
066: * @return child elements
067: */
068: static List getChildElementsByName(Element parent, String name) {
069: List elements = new ArrayList();
070:
071: NodeList children = parent.getChildNodes();
072:
073: for (int i = 0; i < children.getLength(); i++) {
074: Node node = children.item(i);
075: if (node.getNodeType() == Node.ELEMENT_NODE) {
076: Element element = (Element) node;
077: if (element.getTagName().equals(name)) {
078: elements.add(element);
079: }
080: }
081: }
082:
083: return elements;
084: }
085:
086: /**
087: * <p>Returns the text value of a child element. Returns
088: * <code>null</code> if there is no child element found.</p>
089: *
090: * @param parent parent element
091: * @param name name of the child element
092: * @return text value
093: */
094: static String getChildElementText(Element parent, String name) {
095: // Get children
096: List list = DomUtils.getChildElementsByName(parent, name);
097:
098: if (list.size() == 1) {
099: Element child = (Element) list.get(0);
100:
101: StringBuffer buf = new StringBuffer();
102:
103: NodeList children = child.getChildNodes();
104: for (int i = 0; i < children.getLength(); i++) {
105: Node node = children.item(i);
106: if (node.getNodeType() == Node.TEXT_NODE
107: || node.getNodeType() == Node.CDATA_SECTION_NODE) {
108: Text text = (Text) node;
109: buf.append(text.getData().trim());
110: }
111: }
112:
113: return buf.toString();
114: } else {
115: return null;
116: }
117: }
118:
119: /**
120: * <p>Returns the text value of a child element. Returns
121: * <code>null</code> if there is no child element found.</p>
122: *
123: * @param element element
124: * @return text value
125: */
126: static String getElementText(Element element) {
127: StringBuffer buf = new StringBuffer();
128:
129: NodeList children = element.getChildNodes();
130: for (int i = 0; i < children.getLength(); i++) {
131: Node node = children.item(i);
132: if (node.getNodeType() == Node.TEXT_NODE
133: || node.getNodeType() == Node.CDATA_SECTION_NODE) {
134: Text text = (Text) node;
135: buf.append(text.getData().trim());
136: }
137: }
138:
139: return buf.toString();
140: }
141:
142: /**
143: * <p>Returns an array of text values of a child element. Returns
144: * <code>null</code> if there is no child element found.</p>
145: *
146: * @param parent parent element
147: * @param name name of the child element
148: * @return text value
149: */
150: static String[] getChildElementTextArr(Element parent, String name) {
151: // Get all the elements
152: List children = getChildElementsByName(parent, name);
153:
154: String str[] = new String[children.size()];
155:
156: for (int i = 0; i < children.size(); i++) {
157: Node child = (Node) children.get(i);
158:
159: StringBuffer buf = new StringBuffer();
160:
161: NodeList nodes = child.getChildNodes();
162: for (int j = 0; j < nodes.getLength(); j++) {
163: Node node = nodes.item(j);
164: if (node.getNodeType() == Node.TEXT_NODE
165: || node.getNodeType() == Node.CDATA_SECTION_NODE) {
166: Text text = (Text) node;
167: buf.append(text.getData().trim());
168: }
169: }
170:
171: str[i] = buf.toString();
172: }
173:
174: return str;
175: }
176:
177: /**
178: * <p>Retutns the value of the named attribute of the given
179: * element. If there is no such attribute, returns null.</p>
180: *
181: * @param element element
182: * @param name name
183: * @return value
184: */
185: static String getAttributeValue(Element element, String name) {
186: Attr attribute = element.getAttributeNode(name);
187: if (attribute == null) {
188: return null;
189: } else {
190: return attribute.getValue();
191: }
192: }
193: }
|