KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > util > XMLUtils


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.ioc.util;
8
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import javax.xml.parsers.DocumentBuilder JavaDoc;
18 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
19
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24 import org.w3c.dom.Text JavaDoc;
25
26 /**
27  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
28  */

29
30 public class XMLUtils {
31
32     private XMLUtils() {
33
34     }
35
36     private final static DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
37
38     static {
39         dbf.setValidating(false);
40         dbf.setNamespaceAware(true);
41         dbf.setValidating(false);
42
43     };
44
45     static ThreadLocal JavaDoc dbcache = new ThreadLocal JavaDoc() {
46         protected Object JavaDoc initialValue() {
47             try {
48                 DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
49                 db.setEntityResolver(new DTDEntityResolver());
50                 return db;
51             } catch (Exception JavaDoc ex) {
52                 ex.printStackTrace();
53                 return null;
54             }
55         }
56     };
57
58     public static DocumentBuilder JavaDoc getDocumentBuilder() {
59         return (DocumentBuilder JavaDoc) dbcache.get();
60     }
61     /**
62      * get child node of the node
63      *
64      * @param node
65      * @param tagName
66      * @return
67      */

68     public static Node JavaDoc getChildNodeOf(Node JavaDoc node, String JavaDoc tagName) {
69         for(Node JavaDoc temp = node.getFirstChild(); temp != null; temp = temp.getNextSibling())
70             if(temp.getNodeType() == Node.ELEMENT_NODE
71                     && tagName.equals(temp.getNodeName())) {
72                 return temp;
73             }
74         return null;
75     }
76
77     public static String JavaDoc getChildNodeValueOf(Node JavaDoc node, String JavaDoc tagName) {
78         if(tagName.equals(node.getNodeName())) {
79             return getValueOf(node);
80         }
81         for(Node JavaDoc temp = node.getFirstChild(); temp != null; temp = temp.getNextSibling()) {
82             if(temp.getNodeType() == Node.ELEMENT_NODE && tagName.equals(temp.getNodeName())) {
83                 return getValueOf(temp);
84             }
85         }
86         return null;
87     }
88
89     public static final String JavaDoc getValueOf(Node JavaDoc node) {
90         if(node == null) {
91             return null;
92         }
93         else if(node instanceof Text JavaDoc) {
94             return node.getNodeValue().trim();
95         }
96         else if(node instanceof Element JavaDoc) {
97             ((Element JavaDoc) node).normalize();
98             Node JavaDoc temp = node.getFirstChild();
99             if(temp != null && (temp instanceof Text JavaDoc))
100                 return temp.getNodeValue().trim();
101             else
102                 return "";
103         }
104         else {
105             return node.getNodeValue().trim();
106         }
107     }
108
109     public static final String JavaDoc getAtrributeValueOf(Node JavaDoc node, String JavaDoc attribute) {
110         Node JavaDoc _node = node.getAttributes().getNamedItem(attribute);
111         return getValueOf(_node);
112     }
113
114     public static Iterator JavaDoc getElementsByTagName(Element JavaDoc element, String JavaDoc tag) {
115         ArrayList JavaDoc children = new ArrayList JavaDoc();
116         if(element != null && tag != null) {
117             NodeList JavaDoc nodes = element.getElementsByTagName(tag);
118             for(int i = 0; i < nodes.getLength(); i++) {
119                 Node JavaDoc child = nodes.item(i);
120 // System.out.println("Name: " + child.getNodeName() + ", Value: " + child.getFirstChild().getNodeValue());
121
children.add((Element JavaDoc) child);
122             }
123         }
124         return children.iterator();
125     }
126
127     public static Iterator JavaDoc getElementsByTagNames(Element JavaDoc element, String JavaDoc[] tags) {
128         List JavaDoc children = new ArrayList JavaDoc();
129         if(element != null && tags != null) {
130             List JavaDoc tagList = Arrays.asList(tags);
131             NodeList JavaDoc nodes = element.getChildNodes();
132             for(int i = 0; i < nodes.getLength(); i++) {
133                 Node JavaDoc child = nodes.item(i);
134                 if(child.getNodeType() == Node.ELEMENT_NODE
135                         && tagList.contains(((Element JavaDoc) child).getTagName())) {
136                     children.add((Element JavaDoc) child);
137 // System.out.println("Name: " + child.getNodeName() + ", Value: " + child.getFirstChild().getNodeValue());
138
}
139             }
140         }
141         return children.iterator();
142     }
143
144     /**
145      * get the xml root document of the xml descriptor
146      *
147      * @param url the xml descriptor url
148      * @return
149      */

150
151     public static Document JavaDoc getDocument(URL JavaDoc url) throws Exception JavaDoc {
152         InputStream JavaDoc is = null;
153         try {
154             is = new BufferedInputStream JavaDoc(url.openStream());
155             return getDocumentBuilder().parse(is);
156         }
157         finally {
158             if(is != null) {
159                 try {
160                     is.close();
161                 }
162                 catch(IOException JavaDoc e) {
163
164                 }
165             }
166         }
167     }
168
169     public static void main(String JavaDoc[] args) {
170
171     }
172 }
173
174
Popular Tags