KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > server > conf > EnhydraServerXML


1 package org.enhydra.server.conf;
2
3 import java.io.FileNotFoundException JavaDoc;
4 import java.io.FileOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Set JavaDoc;
10
11 import org.apache.xerces.parsers.DOMParser;
12 import org.apache.xml.serialize.LineSeparator;
13 import org.apache.xml.serialize.Method;
14 import org.apache.xml.serialize.OutputFormat;
15 import org.apache.xml.serialize.XMLSerializer;
16 import org.w3c.dom.Attr JavaDoc;
17 import org.w3c.dom.Document JavaDoc;
18 import org.w3c.dom.Element JavaDoc;
19 import org.w3c.dom.NamedNodeMap JavaDoc;
20 import org.w3c.dom.Node JavaDoc;
21 import org.w3c.dom.NodeList JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 /**
25  * @author Tweety
26  *
27  * To change this generated comment edit the template variable "typecomment":
28  * Window>Preferences>Java>Templates.
29  * To enable and disable the creation of type comments go to
30  * Window>Preferences>Java>Code Generation.
31  */

32 public class EnhydraServerXML {
33
34
35     //xml document
36
public Document JavaDoc document;
37
38     //parser
39
DOMParser parser;
40
41     //xml document name
42
private String JavaDoc xmlFileName;
43
44     //Node that represents service tag with the name "EnhydraServer"
45
private Node JavaDoc serverNode;
46
47     private String JavaDoc lineSep;
48
49
50
51     private EnhydraServerXML() {
52     }
53
54     public EnhydraServerXML(String JavaDoc fileName) {
55         this.xmlFileName = fileName;
56
57         try {
58             parser = new DOMParser();
59             parser.parse(this.xmlFileName);
60             document = parser.getDocument();
61
62             this.serverNode = this.getServerNode();
63             this.lineSep = this.determineLineSeparator();
64
65         } catch (SAXException JavaDoc e) {
66             e.printStackTrace();
67         } catch (IOException JavaDoc e) {
68             e.printStackTrace();
69         }
70     }
71
72
73     /*
74      * Determines which line separator to use in the xml document,
75      * on the basis of the operating system name.
76      */

77     String JavaDoc determineLineSeparator() {
78
79         String JavaDoc os = System.getProperty("os.name");
80
81         if(os.toLowerCase().indexOf("windows") != -1)
82             return LineSeparator.Windows;
83         if( (os.toLowerCase().indexOf("unix") != -1) || (os.toLowerCase().indexOf("linux") != -1) )
84             return LineSeparator.Unix;
85         //for all other systems set Web line separator ("\n")
86
return LineSeparator.Web;
87     }
88
89
90
91     /*
92      * Saves the formatted xml document.
93      */

94     public void saveDocument() {
95
96         try {
97
98             FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(this.xmlFileName);
99             OutputFormat of = new OutputFormat();
100
101             //of.setIndenting(true);
102
of.setIndent(4);
103
104             of.setMethod(Method.XML);
105             of.setPreserveSpace(true);
106
107             XMLSerializer out = new XMLSerializer(os, of);
108             out.serialize(this.document);
109
110         } catch (FileNotFoundException JavaDoc e) {
111             e.printStackTrace();
112         } catch (IOException JavaDoc e) {
113             e.printStackTrace();
114         }
115     }
116
117
118
119
120     /*
121      * Returns array of HashMaps, where each HashMap contains
122      * attribute values (key anda value) of one application tag
123      * in the server tag node.
124      */

125     public HashMap JavaDoc[] getApplications() {
126         ArrayList JavaDoc hashArray = null;
127         try {
128             NodeList JavaDoc childs = this.serverNode.getChildNodes();
129             hashArray = new ArrayList JavaDoc(childs.getLength());
130
131             for(int i = 0; i < childs.getLength(); i++) {
132                 if(childs.item(i).getNodeName().equalsIgnoreCase("Application")) {
133                     HashMap JavaDoc hash = new HashMap JavaDoc();
134                     NamedNodeMap JavaDoc attributes = childs.item(i).getAttributes();
135                     for(int j = 0; j < attributes.getLength(); j++)
136                         hash.put(attributes.item(j).getNodeName(), attributes.item(j).getNodeValue());
137                     hashArray.add(hash);
138                 }
139             }
140
141             HashMap JavaDoc[]hashes = new HashMap JavaDoc[hashArray.size()];
142             for(int i = 0; i < hashArray.size(); i++)
143                 hashes[i] = (HashMap JavaDoc)hashArray.get(i);
144
145             return hashes;
146
147         } catch(Exception JavaDoc e) {
148             return null;
149         }
150     }
151
152     /*
153      * Adds new application tag node to the end of all applications in the server tag node.
154      * New application tag node attributes are contained in the hash argument.
155      */

156     public void addApplication(HashMap JavaDoc hash) {
157         try {
158             Element JavaDoc application = this.document.createElement("application");
159             //application.appendChild(this.document.createTextNode(this.lineSep));
160
Set JavaDoc keys = hash.keySet();
161             Iterator JavaDoc it = keys.iterator();
162             while(it.hasNext()) {
163                 String JavaDoc key = it.next().toString();
164                 Attr JavaDoc attr = this.document.createAttribute(key);
165                 attr.setNodeValue(hash.get(key).toString());
166                 application.setAttributeNode(attr);
167             }
168             //attach application node to the end of all applications of the server tag node
169
this.serverNode.appendChild(this.document.createTextNode(this.lineSep + "\t"));
170             this.serverNode.appendChild(application);
171             this.serverNode.appendChild(this.document.createTextNode(this.lineSep));
172         } catch(Exception JavaDoc e) {
173             System.err.println("New application could not be added!");
174         }
175     }
176
177     /*
178      * Removes the application tag node with the given context path.
179      */

180     public void removeApplication(String JavaDoc contextPath) {
181         try {
182             Node JavaDoc application = this.findApplication(contextPath);
183
184             //determine spaces (ext nodes) before and after application node that is going to be removed
185
Node JavaDoc spaceBefore = null;
186             Node JavaDoc spaceAfter = null;
187             if(application.getPreviousSibling() != null && application.getPreviousSibling().getNodeType() == Node.TEXT_NODE)
188                 spaceBefore = application.getPreviousSibling();
189             if(application.getNextSibling() != null && application.getNextSibling().getNodeType() == Node.TEXT_NODE)
190                 spaceAfter = application.getNextSibling();
191
192             //remove application node
193
this.serverNode.removeChild(application);
194
195             //remove spaces
196
if(spaceBefore != null)
197                 this.serverNode.removeChild(spaceBefore);
198             if(spaceAfter != null)
199                 this.serverNode.removeChild(spaceAfter);
200         } catch(Exception JavaDoc e) {
201             System.err.println("Application with context path \"" + contextPath + "\" could not be removed!");
202         }
203     }
204     //DACHA add 17.03.2003
205
/**
206      * Return attributes of <application> tag for given context path.
207      * @param contextPath value of <code>contextPath</code> attribute.
208      * @return attributes of <application> tag if exist, otherwise <code>null</code>.
209      */

210     public HashMap JavaDoc getApplication(String JavaDoc contextPath){
211         Node JavaDoc appNode = findApplication(contextPath);
212         if (appNode == null){
213             return null; //Application don't exist
214
}
215         HashMap JavaDoc hash = new HashMap JavaDoc();
216         NamedNodeMap JavaDoc attributes = appNode.getAttributes();
217         for(int j = 0; j < attributes.getLength(); j++)
218             hash.put(attributes.item(j).getNodeName(), attributes.item(j).getNodeValue());
219         return hash;
220     }
221
222     /*
223      * Returns node that represents server tag node (document root node).
224      */

225     private Node JavaDoc getServerNode() {
226         Element JavaDoc root = this.document.getDocumentElement(); //server tag
227
if(root.getNodeName().equalsIgnoreCase("server"))
228             return root;
229
230         System.err.println("Tag node Server cannot be found !");
231         return null;
232     }
233
234
235     /*
236      * Returns node that represents application tag with the given context path.
237      */

238     private Node JavaDoc findApplication(String JavaDoc contextPath) {
239         NodeList JavaDoc childs = this.serverNode.getChildNodes();
240         for(int i = 0; i < childs.getLength(); i++) {
241             if(childs.item(i).getNodeName().equalsIgnoreCase("application")) {
242                 if(childs.item(i).getAttributes().getNamedItem("contextPath").getNodeValue().equalsIgnoreCase(contextPath))
243                     return childs.item(i);
244             }
245         }
246         return null;
247     }
248
249
250
251
252
253
254     public static void main(String JavaDoc[] args) {
255         try {
256             EnhydraServerXML test = new EnhydraServerXML("EnhydraServer.xml");
257
258 /*get*/
259 // HashMap[] hashes = test.getApplications();
260
// for(int i = 0; i < hashes.length; i++)
261
// System.out.println("hashes[" + i + "] = " + hashes[i].toString());
262
// test.saveDocument();
263

264 /*add*/
265             HashMap JavaDoc hash = new HashMap JavaDoc();
266             hash.put("name", "zzz application");
267             hash.put("contextPath", "cp");
268             hash.put("connections", "2233, 4455");
269             hash.put("running", "false");
270             hash.put("URLPath", "C:/...");
271             hash.put("description", "zzz app");
272             test.addApplication(hash);
273
274             test.saveDocument();
275
276 /*remove*/
277 // test.removeApplication("cp");
278
// test.saveDocument();
279

280             System.out.println("Happy end");
281         } catch(Exception JavaDoc e) {
282             System.out.println("NOOOOOOOOOO");
283         }
284     }
285 }
286
Popular Tags