001: package org.enhydra.server.conf;
002:
003: import java.io.FileNotFoundException;
004: import java.io.FileOutputStream;
005: import java.io.IOException;
006: import java.util.ArrayList;
007: import java.util.HashMap;
008: import java.util.Iterator;
009: import java.util.Set;
010:
011: import org.apache.xerces.parsers.DOMParser;
012: import org.apache.xml.serialize.LineSeparator;
013: import org.apache.xml.serialize.Method;
014: import org.apache.xml.serialize.OutputFormat;
015: import org.apache.xml.serialize.XMLSerializer;
016: import org.w3c.dom.Attr;
017: import org.w3c.dom.Document;
018: import org.w3c.dom.Element;
019: import org.w3c.dom.NamedNodeMap;
020: import org.w3c.dom.Node;
021: import org.w3c.dom.NodeList;
022: import org.xml.sax.SAXException;
023:
024: /**
025: * @author Tweety
026: *
027: * To change this generated comment edit the template variable "typecomment":
028: * Window>Preferences>Java>Templates.
029: * To enable and disable the creation of type comments go to
030: * Window>Preferences>Java>Code Generation.
031: */
032: public class EnhydraServerXML {
033:
034: //xml document
035: public Document document;
036:
037: //parser
038: DOMParser parser;
039:
040: //xml document name
041: private String xmlFileName;
042:
043: //Node that represents service tag with the name "EnhydraServer"
044: private Node serverNode;
045:
046: private String lineSep;
047:
048: private EnhydraServerXML() {
049: }
050:
051: public EnhydraServerXML(String fileName) {
052: this .xmlFileName = fileName;
053:
054: try {
055: parser = new DOMParser();
056: parser.parse(this .xmlFileName);
057: document = parser.getDocument();
058:
059: this .serverNode = this .getServerNode();
060: this .lineSep = this .determineLineSeparator();
061:
062: } catch (SAXException e) {
063: e.printStackTrace();
064: } catch (IOException e) {
065: e.printStackTrace();
066: }
067: }
068:
069: /*
070: * Determines which line separator to use in the xml document,
071: * on the basis of the operating system name.
072: */
073: String determineLineSeparator() {
074:
075: String os = System.getProperty("os.name");
076:
077: if (os.toLowerCase().indexOf("windows") != -1)
078: return LineSeparator.Windows;
079: if ((os.toLowerCase().indexOf("unix") != -1)
080: || (os.toLowerCase().indexOf("linux") != -1))
081: return LineSeparator.Unix;
082: //for all other systems set Web line separator ("\n")
083: return LineSeparator.Web;
084: }
085:
086: /*
087: * Saves the formatted xml document.
088: */
089: public void saveDocument() {
090:
091: try {
092:
093: FileOutputStream os = new FileOutputStream(this .xmlFileName);
094: OutputFormat of = new OutputFormat();
095:
096: //of.setIndenting(true);
097: of.setIndent(4);
098:
099: of.setMethod(Method.XML);
100: of.setPreserveSpace(true);
101:
102: XMLSerializer out = new XMLSerializer(os, of);
103: out.serialize(this .document);
104:
105: } catch (FileNotFoundException e) {
106: e.printStackTrace();
107: } catch (IOException e) {
108: e.printStackTrace();
109: }
110: }
111:
112: /*
113: * Returns array of HashMaps, where each HashMap contains
114: * attribute values (key anda value) of one application tag
115: * in the server tag node.
116: */
117: public HashMap[] getApplications() {
118: ArrayList hashArray = null;
119: try {
120: NodeList childs = this .serverNode.getChildNodes();
121: hashArray = new ArrayList(childs.getLength());
122:
123: for (int i = 0; i < childs.getLength(); i++) {
124: if (childs.item(i).getNodeName().equalsIgnoreCase(
125: "Application")) {
126: HashMap hash = new HashMap();
127: NamedNodeMap attributes = childs.item(i)
128: .getAttributes();
129: for (int j = 0; j < attributes.getLength(); j++)
130: hash.put(attributes.item(j).getNodeName(),
131: attributes.item(j).getNodeValue());
132: hashArray.add(hash);
133: }
134: }
135:
136: HashMap[] hashes = new HashMap[hashArray.size()];
137: for (int i = 0; i < hashArray.size(); i++)
138: hashes[i] = (HashMap) hashArray.get(i);
139:
140: return hashes;
141:
142: } catch (Exception e) {
143: return null;
144: }
145: }
146:
147: /*
148: * Adds new application tag node to the end of all applications in the server tag node.
149: * New application tag node attributes are contained in the hash argument.
150: */
151: public void addApplication(HashMap hash) {
152: try {
153: Element application = this .document
154: .createElement("application");
155: //application.appendChild(this.document.createTextNode(this.lineSep));
156: Set keys = hash.keySet();
157: Iterator it = keys.iterator();
158: while (it.hasNext()) {
159: String key = it.next().toString();
160: Attr attr = this .document.createAttribute(key);
161: attr.setNodeValue(hash.get(key).toString());
162: application.setAttributeNode(attr);
163: }
164: //attach application node to the end of all applications of the server tag node
165: this .serverNode.appendChild(this .document
166: .createTextNode(this .lineSep + "\t"));
167: this .serverNode.appendChild(application);
168: this .serverNode.appendChild(this .document
169: .createTextNode(this .lineSep));
170: } catch (Exception e) {
171: System.err.println("New application could not be added!");
172: }
173: }
174:
175: /*
176: * Removes the application tag node with the given context path.
177: */
178: public void removeApplication(String contextPath) {
179: try {
180: Node application = this .findApplication(contextPath);
181:
182: //determine spaces (ext nodes) before and after application node that is going to be removed
183: Node spaceBefore = null;
184: Node spaceAfter = null;
185: if (application.getPreviousSibling() != null
186: && application.getPreviousSibling().getNodeType() == Node.TEXT_NODE)
187: spaceBefore = application.getPreviousSibling();
188: if (application.getNextSibling() != null
189: && 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 e) {
201: System.err.println("Application with context path \""
202: + contextPath + "\" could not be removed!");
203: }
204: }
205:
206: //DACHA add 17.03.2003
207: /**
208: * Return attributes of <application> tag for given context path.
209: * @param contextPath value of <code>contextPath</code> attribute.
210: * @return attributes of <application> tag if exist, otherwise <code>null</code>.
211: */
212: public HashMap getApplication(String contextPath) {
213: Node appNode = findApplication(contextPath);
214: if (appNode == null) {
215: return null; //Application don't exist
216: }
217: HashMap hash = new HashMap();
218: NamedNodeMap attributes = appNode.getAttributes();
219: for (int j = 0; j < attributes.getLength(); j++)
220: hash.put(attributes.item(j).getNodeName(), attributes.item(
221: j).getNodeValue());
222: return hash;
223: }
224:
225: /*
226: * Returns node that represents server tag node (document root node).
227: */
228: private Node getServerNode() {
229: Element root = this .document.getDocumentElement(); //server tag
230: if (root.getNodeName().equalsIgnoreCase("server"))
231: return root;
232:
233: System.err.println("Tag node Server cannot be found !");
234: return null;
235: }
236:
237: /*
238: * Returns node that represents application tag with the given context path.
239: */
240: private Node findApplication(String contextPath) {
241: NodeList childs = this .serverNode.getChildNodes();
242: for (int i = 0; i < childs.getLength(); i++) {
243: if (childs.item(i).getNodeName().equalsIgnoreCase(
244: "application")) {
245: if (childs.item(i).getAttributes().getNamedItem(
246: "contextPath").getNodeValue().equalsIgnoreCase(
247: contextPath))
248: return childs.item(i);
249: }
250: }
251: return null;
252: }
253:
254: public static void main(String[] args) {
255: try {
256: EnhydraServerXML test = new EnhydraServerXML(
257: "EnhydraServer.xml");
258:
259: /*get*/
260: // HashMap[] hashes = test.getApplications();
261: // for(int i = 0; i < hashes.length; i++)
262: // System.out.println("hashes[" + i + "] = " + hashes[i].toString());
263: // test.saveDocument();
264: /*add*/
265: HashMap hash = new HashMap();
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: System.out.println("Happy end");
280: } catch (Exception e) {
281: System.out.println("NOOOOOOOOOO");
282: }
283: }
284: }
|