KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > ant > AntXMLUtil


1 package org.enhydra.kelp.ant;
2
3
4 import javax.xml.parsers.ParserConfigurationException JavaDoc;
5 import javax.xml.transform.Result JavaDoc;
6 import javax.xml.transform.Source JavaDoc;
7 import javax.xml.transform.Transformer JavaDoc;
8 import javax.xml.transform.TransformerConfigurationException JavaDoc;
9 import javax.xml.transform.TransformerException JavaDoc;
10 import javax.xml.transform.TransformerFactory JavaDoc;
11 import javax.xml.transform.TransformerFactoryConfigurationError JavaDoc;
12 import javax.xml.transform.dom.DOMSource JavaDoc;
13 import javax.xml.transform.stream.StreamResult JavaDoc;
14
15 import java.io.File JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.io.PrintStream JavaDoc;
21
22 import org.w3c.dom.DOMImplementation JavaDoc;
23 import org.w3c.dom.Document JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.NamedNodeMap JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29
30 import org.apache.crimson.tree.TextNode;
31
32 import org.apache.xml.serialize.OutputFormat;
33 import org.apache.xml.serialize.XMLSerializer;
34 import org.apache.xerces.parsers.DOMParser;
35 import org.apache.xml.serialize.Method;
36
37 /**
38  * @author Tweety
39  *
40  * To change this generated comment edit the template variable "typecomment":
41  * Window>Preferences>Java>Templates.
42  * To enable and disable the creation of type comments go to
43  * Window>Preferences>Java>Code Generation.
44  */

45 public class AntXMLUtil {
46
47
48     //xml document
49
public Document JavaDoc document;
50
51
52     //xml document name
53
private String JavaDoc xmlFileName;
54
55
56
57     AntXMLUtil() throws Exception JavaDoc {
58         this("");
59     }
60
61
62     AntXMLUtil(String JavaDoc fileName) throws Exception JavaDoc {
63
64         this.xmlFileName = fileName;
65         try{
66             DOMParser parser = new DOMParser();
67             parser.parse(xmlFileName);
68             document = parser.getDocument();
69
70         }catch(Exception JavaDoc e){
71             throw e;
72         }
73
74     }
75
76
77
78     /*
79      * @return Node that represents project tag
80      */

81     public Node JavaDoc getProjectNode() {
82         NodeList JavaDoc list = document.getElementsByTagName("project");
83         if(list.getLength() > 0)
84             return list.item(0);
85         else
86             return null;
87     }
88
89     /*
90      * @param propertyName name attribute of the property tag
91      * @return Node that represents property tag
92      */

93     public Node JavaDoc getProjectProperty(String JavaDoc propertyName) {
94         Node JavaDoc project = this.getProjectNode();
95         NodeList JavaDoc childs = project.getChildNodes();
96         for(int i = 0; i < childs.getLength(); i++) {
97             if(childs.item(i).getNodeName().equals("property")) {
98                 Node JavaDoc nameAttribute = childs.item(i).getAttributes().getNamedItem("name");
99                 if(nameAttribute != null && nameAttribute.getNodeValue().equals(propertyName))
100                     return childs.item(i);
101             }
102         }
103         return null;
104     }
105
106
107     /*
108      * @param targetName: name attribute of the searched target
109      * @return Node that represents target with given name
110      */

111     public Node JavaDoc getTargetByName(String JavaDoc targetName) {
112         NodeList JavaDoc allTargets = document.getElementsByTagName("target");
113         for(int i = 0; i < allTargets.getLength(); i++) {
114             String JavaDoc nameValue = allTargets.item(i).getAttributes().getNamedItem("name").getNodeValue();
115
116             if(nameValue.equals(targetName))
117                 return allTargets.item(i);
118         }
119         return null;
120     }
121
122
123     /*
124      * This method finds child Node in given subtree, with given name
125      * @param subRoot: root node
126      * @param childName: child tag name
127      */

128     public Node JavaDoc getChildByTagName(Node JavaDoc subRoot, String JavaDoc tagName) {
129         NodeList JavaDoc childs = subRoot.getChildNodes();
130             for(int i = 0; i < childs.getLength(); i++) {
131                 if(childs.item(i).getNodeName().equals(tagName)) {
132                     return childs.item(i);
133                 }
134             }
135         return null;
136     }
137
138     /*
139      * This method finds child attribute Node in given subtree, with given name
140      * @param subRoot: root node
141      * @param attributeName: name of attribute
142      */

143     public Node JavaDoc getAttributeByName(Node JavaDoc subRoot, String JavaDoc attributeName) {
144         return subRoot.getAttributes().getNamedItem(attributeName);
145     }
146
147     /*
148      * This method finds patternset Node with given id
149      * @param id: id of searched patternset
150      */

151     public Node JavaDoc getPatternsetById(String JavaDoc id) {
152         Node JavaDoc projectNode = this.getProjectNode();
153         NodeList JavaDoc childs = projectNode.getChildNodes();
154         for(int i = 0; i < childs.getLength(); i++) {
155             if(childs.item(i).getNodeName().equals("patternset")) {
156                 if(childs.item(i).getAttributes().getNamedItem("id").getNodeValue().equals(id))
157                     return childs.item(i);
158             }
159         }
160
161         return null;
162     }
163
164     /*
165      * This method finds filterset Node with given id
166      * @param id: id of searched filterset
167      */

168     public Node JavaDoc getFiltersetById(String JavaDoc id) {
169         Node JavaDoc projectNode = this.getProjectNode();
170         NodeList JavaDoc childs = projectNode.getChildNodes();
171         for(int i = 0; i < childs.getLength(); i++) {
172             if(childs.item(i).getNodeName().equals("filterset")) {
173                 if(childs.item(i).getAttributes().getNamedItem("id").getNodeValue().equalsIgnoreCase(id))
174                     return childs.item(i);
175             }
176         }
177
178         return null;
179     }
180
181     public boolean saveDocument() {
182
183             try{
184                 FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(xmlFileName);
185                 OutputFormat of = new OutputFormat(); //xerces apache.xml.serializer
186
of.setIndenting(true);
187                 of.setIndent(4);
188                 of.setMethod(Method.XML);
189                 of.setPreserveSpace(true);
190                 XMLSerializer out = new XMLSerializer(os,of);
191                 //IOException
192
out.serialize(document);
193             } catch (Exception JavaDoc e) {
194             e.printStackTrace();
195                       return false;
196             }
197             return true;
198     }
199
200 }
201
Popular Tags