KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > xml > XMLUtil


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.xml;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.xml.parsers.DocumentBuilder JavaDoc;
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28 import javax.xml.parsers.ParserConfigurationException JavaDoc;
29
30 import org.apache.commons.collections.Predicate;
31 import org.apache.cayenne.CayenneRuntimeException;
32 import org.w3c.dom.CharacterData JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36
37 /**
38  * Static utility methods to work with DOM trees.
39  *
40  * @since 1.2
41  * @author Andrus Adamchik
42  */

43 class XMLUtil {
44     
45     // note that per CAY-792, to be locale-safe the format must not contain literal parts
46
static final String JavaDoc DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss zzz";
47
48     static DocumentBuilderFactory JavaDoc sharedFactory;
49
50     /**
51      * Creates a new instance of DocumentBuilder using the default factory.
52      */

53     static DocumentBuilder JavaDoc newBuilder() throws CayenneRuntimeException {
54         if (sharedFactory == null) {
55             sharedFactory = DocumentBuilderFactory.newInstance();
56         }
57
58         try {
59             return sharedFactory.newDocumentBuilder();
60         }
61         catch (ParserConfigurationException JavaDoc e) {
62             throw new CayenneRuntimeException("Can't create DocumentBuilder", e);
63         }
64     }
65
66     /**
67      * Moves all children of the oldParent to the newParent
68      */

69     static List JavaDoc replaceParent(Node JavaDoc oldParent, Node JavaDoc newParent) {
70
71         List JavaDoc children = XMLUtil.getChildren(oldParent);
72
73         Iterator JavaDoc it = children.iterator();
74         while (it.hasNext()) {
75             Element JavaDoc child = (Element JavaDoc) it.next();
76             oldParent.removeChild(child);
77             newParent.appendChild(child);
78         }
79
80         return children;
81     }
82
83     /**
84      * Returns text content of a given Node.
85      */

86     static String JavaDoc getText(Node JavaDoc node) {
87
88         NodeList JavaDoc nodes = node.getChildNodes();
89         int len = nodes.getLength();
90
91         if (len == 0) {
92             return null;
93         }
94
95         StringBuffer JavaDoc text = new StringBuffer JavaDoc();
96         for (int i = 0; i < len; i++) {
97             Node JavaDoc child = nodes.item(i);
98
99             if (child instanceof CharacterData JavaDoc) {
100                 text.append(((CharacterData JavaDoc) child).getData());
101             }
102         }
103
104         return text.length() == 0 ? null : text.toString();
105     }
106
107     /**
108      * Returns the first element among the direct children that has a matching name.
109      */

110     static Element JavaDoc getChild(Node JavaDoc node, final String JavaDoc name) {
111         Predicate p = new Predicate() {
112
113             public boolean evaluate(Object JavaDoc object) {
114                 if (object instanceof Element JavaDoc) {
115                     Element JavaDoc e = (Element JavaDoc) object;
116                     return name.equals(e.getNodeName());
117                 }
118
119                 return false;
120             }
121         };
122
123         return (Element JavaDoc) firstMatch(node.getChildNodes(), p);
124     }
125
126     /**
127      * Returns all elements among the direct children that have a matching name.
128      */

129     static List JavaDoc getChildren(Node JavaDoc node, final String JavaDoc name) {
130         Predicate p = new Predicate() {
131
132             public boolean evaluate(Object JavaDoc object) {
133                 if (object instanceof Element JavaDoc) {
134                     Element JavaDoc e = (Element JavaDoc) object;
135                     return name.equals(e.getNodeName());
136                 }
137
138                 return false;
139             }
140         };
141
142         return allMatches(node.getChildNodes(), p);
143     }
144
145     /**
146      * Returns all children of a given Node that are Elements.
147      */

148     static List JavaDoc getChildren(Node JavaDoc node) {
149         Predicate p = new Predicate() {
150
151             public boolean evaluate(Object JavaDoc object) {
152                 return object instanceof Element JavaDoc;
153             }
154         };
155
156         return allMatches(node.getChildNodes(), p);
157     }
158
159     private static Node JavaDoc firstMatch(NodeList JavaDoc list, Predicate predicate) {
160         int len = list.getLength();
161
162         for (int i = 0; i < len; i++) {
163             Node JavaDoc node = list.item(i);
164             if (predicate.evaluate(node)) {
165                 return node;
166             }
167         }
168
169         return null;
170     }
171
172     private static List JavaDoc allMatches(NodeList JavaDoc list, Predicate predicate) {
173         int len = list.getLength();
174         List JavaDoc children = new ArrayList JavaDoc(len);
175
176         for (int i = 0; i < len; i++) {
177             Node JavaDoc node = list.item(i);
178             if (predicate.evaluate(node)) {
179                 children.add(node);
180             }
181         }
182
183         return children;
184     }
185 }
186
Popular Tags