KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > connector > utils > Wrapper


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2005 - Bull SA
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Nicolas Tachker (ScalAgent)
21  * Contributor(s):
22  */

23 package org.objectweb.joram.client.connector.utils;
24
25 import java.io.*;
26 import java.util.*;
27
28 import javax.xml.transform.*;
29 import javax.xml.transform.dom.DOMSource JavaDoc;
30 import javax.xml.transform.stream.StreamResult JavaDoc;
31
32 import javax.xml.parsers.DocumentBuilder JavaDoc;
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34
35 import org.xml.sax.Attributes JavaDoc;
36 import org.w3c.dom.*;
37
38
39 /**
40  * XML Wrapper.
41  */

42 public class Wrapper {
43   private boolean debug = false;
44   private Map map = null;
45
46   public Wrapper(boolean debug) {
47     this.debug = debug;
48   }
49
50   /**
51    *
52    * @param reader Reader
53    * @exception Exception unspecialized error
54    */

55   public String JavaDoc parse(InputStream in) throws Exception JavaDoc {
56     StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
57     DocumentBuilderFactory JavaDoc docBuilderFactory = DocumentBuilderFactory.newInstance();
58     DocumentBuilder JavaDoc docBuilder = docBuilderFactory.newDocumentBuilder();
59     Document doc = docBuilder.parse(in);
60
61     NodeList nl = doc.getElementsByTagName("resourceadapter");
62     for (int i = 0; i < nl.getLength(); i++) {
63       Node node = nl.item(i);
64       buff.append(explore(node));
65     }
66     return buff.toString();
67   }
68
69   public String JavaDoc explore(Node node) throws Exception JavaDoc {
70 // if (debug)
71
// System.out.println("Wrapper.explore(" + node + ")");
72

73     StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
74     NodeList nl = node.getChildNodes();
75     for (int i = 0; i < nl.getLength(); i++) {
76       Node n = nl.item(i);
77       if (n.getNodeName().equals("config-property")) {
78         buff.append(extractPropertyInfo(n.getChildNodes()));
79       } else {
80         if (n.getNodeName().equals("resourceadapter-class")
81             || n.getNodeName().equals("managedconnectionfactory-class")) {
82           String JavaDoc nodeName = n.getFirstChild().getNodeValue();
83           buff.append("[");
84           buff.append(nodeName);
85           buff.append("]");
86           buff.append("\n");
87         }
88         buff.append(explore(n));
89       }
90     }
91     return buff.toString();
92   }
93
94   public String JavaDoc extractPropertyInfo(NodeList nodeList) throws Exception JavaDoc {
95     String JavaDoc name = null;
96     String JavaDoc value = null;
97     for (int i = 0; i < nodeList.getLength(); i++) {
98       Node n = nodeList.item(i);
99       if (n.getNodeName().equals("config-property-name")) {
100         name = n.getFirstChild().getNodeValue();
101       } else if (n.getNodeName().equals("config-property-value")) {
102         value = n.getFirstChild().getNodeValue();
103       }
104     }
105
106     StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
107     buff.append(" ");
108     buff.append(name);
109     buff.append("\t");
110     if (value != null)
111       buff.append(value);
112     buff.append("\n");
113     return buff.toString();
114   }
115
116
117  /**
118    *
119    * @param reader Reader
120    * @param map update by this properties
121    * @exception Exception unspecialized error
122    */

123   public String JavaDoc update(InputStream in, Map map) throws Exception JavaDoc {
124     this.map = map;
125     DocumentBuilderFactory JavaDoc docBuilderFactory = DocumentBuilderFactory.newInstance();
126     DocumentBuilder JavaDoc docBuilder = docBuilderFactory.newDocumentBuilder();
127     Document doc = docBuilder.parse(in);
128
129     NodeList nl = doc.getElementsByTagName("resourceadapter");
130     for (int i = 0; i < nl.getLength(); i++) {
131       Node node = nl.item(i);
132       browse(node);
133     }
134
135     Transformer transformer =
136       TransformerFactory.newInstance().newTransformer();
137     Source JavaDoc source = new DOMSource JavaDoc(doc);
138
139     CharArrayWriter writer = new CharArrayWriter();
140     Result JavaDoc output = new StreamResult JavaDoc(writer);
141     transformer.transform(source, output);
142     writer.flush();
143
144     return writer.toString();
145   }
146
147   public void browse(Node node) throws Exception JavaDoc {
148     if (debug)
149       System.out.println("Wrapper.browse(" + node + ")");
150     NodeList nl = node.getChildNodes();
151     for (int i = 0; i < nl.getLength(); i++) {
152       Node n = nl.item(i);
153       String JavaDoc nodeValue = n.getNodeValue();
154       if (nodeValue != null && map.containsKey(nodeValue)) {
155         if (debug)
156           System.out.println("NodeName=" + nodeValue);
157         explore(n.getParentNode().getParentNode(),nodeValue);
158       } else if (nodeValue != null
159                  && !nodeValue.equals("config-property")) {
160         continue;
161       } else {
162         browse(n);
163       }
164     }
165   }
166
167   public void explore(Node node, String JavaDoc key) throws Exception JavaDoc {
168     if (debug)
169       System.out.println(">>>> Wrapper.explore(" + node + "," + key + ")");
170     NodeList nl = node.getChildNodes();
171     for (int i = 0; i < nl.getLength(); i++) {
172       Node n = nl.item(i);
173       String JavaDoc nodeName = n.getNodeName();
174       if (nodeName.equals("config-property")) {
175         setPropertiesValue(n.getChildNodes(),key);
176       } else if (nodeName.equals("outbound-resourceadapter")
177                  || nodeName.equals("inbound-resourceadapter")) {
178         continue;
179       } else {
180         explore(n,key);
181       }
182     }
183     if (debug)
184       System.out.println("<<<<<< Wrapper.explore node = " + node);
185   }
186
187
188   public void setPropertiesValue(NodeList nodeList, String JavaDoc key) throws Exception JavaDoc {
189     if (debug)
190       System.out.println("Wrapper.setPropertiesValue(" + nodeList + "," + key + ")");
191     String JavaDoc name = null;
192     String JavaDoc value = null;
193     Map prop = (Map) map.get(key);
194     for (int i = 0; i < nodeList.getLength(); i++) {
195       Node n = nodeList.item(i);
196       if (n.getNodeName().equals("config-property-name")) {
197         name = n.getFirstChild().getNodeValue();
198       } else if (n.getNodeName().equals("config-property-value")
199                  && prop.containsKey(name)) {
200         n.getFirstChild().setNodeValue((String JavaDoc) prop.get(name));
201       }
202     }
203     if (debug)
204       System.out.println("Wrapper.setPropertiesValue name=" + name + ", value=" + value);
205   }
206 }
207
Popular Tags