KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > utils > xml > UpdateXmlFile


1 /*
2  * Created on 24 juin 2004
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package com.bull.eclipse.jonas.utils.xml;
8
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.FileNotFoundException JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 import javax.xml.parsers.ParserConfigurationException JavaDoc;
16
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 import com.bull.eclipse.jonas.JonasLauncherPlugin;
25 import com.bull.eclipse.jonas.JonasProject;
26 import com.bull.eclipse.jonas.utils.TemplateDirUtil;
27
28 /**
29  * @author riase
30  *
31  * To change the template for this generated type comment go to
32  * Window>Preferences>Java>Code Generation>Code and Comments
33  */

34 public class UpdateXmlFile {
35     
36     public static void updateWebXmlFileWithAxisServlet(File JavaDoc webXmlFile) {
37         File JavaDoc xmlFileTemp = new File JavaDoc(TemplateDirUtil.getTemplateDir() + File.separator + "ws" + File.separator + "axis.web.xml");
38         try {
39             FileInputStream JavaDoc is = new FileInputStream JavaDoc(webXmlFile);
40             Document JavaDoc docXmlWeb = XMLUtils.newDocument(is,true);
41             
42             Document JavaDoc docXmlTemp = XMLUtils.newDocument(new FileInputStream JavaDoc(xmlFileTemp),false);
43
44             // On teste si les servlet Axis sont déjà déclarés
45
NodeList JavaDoc nlSC = docXmlWeb.getElementsByTagName("servlet-class");
46             for(int i = 0;i < nlSC.getLength(); i++) {
47                 if (nlSC.item(i).getFirstChild().getNodeValue().equals("org.apache.axis.transport.http.AxisServlet") ||
48                 nlSC.item(i).getFirstChild().getNodeValue().equals("org.apache.axis.transport.http.AdminServlet") ||
49                 nlSC.item(i).getFirstChild().getNodeValue().equals("org.apache.axis.monitor.SOAPMonitorService"))
50                     return;
51                     
52             }
53
54             // Insertion des tags servlets de Axis a partir du fichier axis.web.xml
55
// On récupère la liste des tags servlets
56
NodeList JavaDoc nl1 = docXmlTemp.getElementsByTagName("servlet");
57             
58             Node JavaDoc servletAxisNode = null;
59
60             // On teste les tags pouvant être présents dans le web.xml
61
// On commence par servlet-mapping et on descend jusqu'a en trouver un
62
Node JavaDoc webNode = getNodeInWebFile(docXmlWeb);
63                 
64
65             for(int i = 0;i < nl1.getLength(); i++) {
66                 servletAxisNode = docXmlWeb.importNode(nl1.item(i),true);
67                 if (webNode.getNodeName().equals("web-app"))
68                     docXmlWeb.getDocumentElement().appendChild(servletAxisNode);
69                 else
70                     docXmlWeb.getDocumentElement().insertBefore(servletAxisNode,webNode);
71             }
72             
73             NodeList JavaDoc nl2 = docXmlTemp.getElementsByTagName("servlet-mapping");
74             for(int i = 0;i < nl2.getLength(); i++) {
75                 servletAxisNode = docXmlWeb.importNode(nl2.item(i),true);
76                 if (webNode.getNodeName().equals("web-app"))
77                     docXmlWeb.getDocumentElement().appendChild(servletAxisNode);
78                 else
79                     docXmlWeb.getDocumentElement().insertBefore(servletAxisNode,webNode);
80             }
81
82             XMLSerializer xManu = XMLSerializer.getInstance(webNode.getOwnerDocument());
83             FileOutputStream JavaDoc fo = new FileOutputStream JavaDoc(webXmlFile);
84             xManu.serialize(fo);
85         } catch (FileNotFoundException JavaDoc e) {
86             // TODO Auto-generated catch block
87
e.printStackTrace();
88         } catch (ParserConfigurationException JavaDoc e) {
89             // TODO Auto-generated catch block
90
e.printStackTrace();
91         } catch (SAXException JavaDoc e) {
92             // TODO Auto-generated catch block
93
e.printStackTrace();
94         } catch (IOException JavaDoc e) {
95             // TODO Auto-generated catch block
96
e.printStackTrace();
97         }
98     
99
100     }
101
102     public static void updateServerConfigXmlFile(String JavaDoc packName, JonasProject jp, String JavaDoc jarName) {
103         File JavaDoc serverConfigFile = new File JavaDoc(jp.getWebInfFolder().getLocation().append("server-config.wsdd").toOSString());
104         File JavaDoc deployFile = new File JavaDoc(jp.getProject().getLocation().append(jp.getSrcEJBDir() + File.separator + packName + File.separator + "deploy.wsdd").toOSString());
105         //File outputServerConfigFile = new File(jp.getWebInfFolder().getLocation().append("server-config.wsdd").toOSString());
106
try {
107             Document JavaDoc docXmlSC = XMLUtils.newDocument(new FileInputStream JavaDoc(serverConfigFile),false);
108             Document JavaDoc docXmlDeploy = XMLUtils.newDocument(new FileInputStream JavaDoc(deployFile),false);
109             
110             NodeList JavaDoc nlSC = docXmlSC.getElementsByTagName("service");
111             JonasLauncherPlugin.log("service node = " + nlSC.item(0));
112             // Insertion du tag service
113
Node JavaDoc nl1 = docXmlDeploy.getElementsByTagName("service").item(0);
114             Node JavaDoc scUpdate = null;
115             scUpdate = docXmlSC.importNode(nl1,true);
116             JonasLauncherPlugin.log("service node = " + nl1);
117             boolean serviceExistant = false;
118             XMLSerializer xManu = null;
119             for(int i = 0;i < nlSC.getLength(); i++) {
120                 JonasLauncherPlugin.log("Node = " + nlSC.item(i));
121                 NamedNodeMap JavaDoc nnm = nlSC.item(i).getAttributes();
122                 String JavaDoc value = nnm.item(0).getNodeValue();
123                 if (nnm.item(0).getNodeValue().equals(jarName + "EndPoint")) {
124                     // Cas où le service avait deja été déclaré
125
JonasLauncherPlugin.log("On passe dans service deja existant");
126                     Node JavaDoc replaceNode = nlSC.item(i);
127                     docXmlSC.getDocumentElement().replaceChild(scUpdate,replaceNode);
128                     xManu = XMLSerializer.getInstance(replaceNode.getOwnerDocument());
129                     serviceExistant = true;
130                 }
131             }
132
133             if (!serviceExistant) {
134                 JonasLauncherPlugin.log("Service non existant");
135                 Node JavaDoc transportNode = docXmlSC.getElementsByTagName("transport").item(0);
136                 docXmlSC.getDocumentElement().insertBefore(scUpdate,transportNode);
137                 xManu = XMLSerializer.getInstance(transportNode.getOwnerDocument());
138             }
139
140             FileOutputStream JavaDoc fo = new FileOutputStream JavaDoc(serverConfigFile);
141             xManu.serialize(fo);
142         } catch (FileNotFoundException JavaDoc e) {
143             // TODO Auto-generated catch block
144
e.printStackTrace();
145         } catch (ParserConfigurationException JavaDoc e) {
146             // TODO Auto-generated catch block
147
e.printStackTrace();
148         } catch (SAXException JavaDoc e) {
149             // TODO Auto-generated catch block
150
e.printStackTrace();
151         } catch (IOException JavaDoc e) {
152             // TODO Auto-generated catch block
153
e.printStackTrace();
154         } catch (Exception JavaDoc e) {
155             e.printStackTrace();
156         }
157     
158
159     }
160
161
162     public static void updateEjbStandardXmlFile(File JavaDoc ejbStanXmlFile, File JavaDoc xmlFile) {
163         try {
164             FileInputStream JavaDoc is = new FileInputStream JavaDoc(ejbStanXmlFile);
165             Document JavaDoc docXmlEjb = XMLUtils.newDocument(is,true);
166             Document JavaDoc docXmlTemp = XMLUtils.newDocument(new FileInputStream JavaDoc(xmlFile),false);
167
168
169             
170
171             // Test if session EJB is already declared
172
// In this cas we do nothing
173
NodeList JavaDoc nlSC = docXmlEjb.getElementsByTagName("ejb-name");
174             String JavaDoc ejbSessionName = docXmlTemp.getElementsByTagName("ejb-name").item(0).getFirstChild().getNodeValue();
175             for(int i = 0;i < nlSC.getLength(); i++) {
176                 String JavaDoc test = nlSC.item(i).getFirstChild().getNodeValue();
177                 if (nlSC.item(i).getFirstChild().getNodeValue().equals(ejbSessionName))
178                         return;
179                     
180             }
181
182 // Insertion des tags ejb, a partir du fichier xmlFile
183
Node JavaDoc session = docXmlTemp.getElementsByTagName("session").item(0);
184             Node JavaDoc ejbSessionNode = docXmlEjb.getElementsByTagName("entity").item(0);
185             Node JavaDoc sessionNode = docXmlEjb.importNode(session,true);
186             docXmlEjb.getDocumentElement().getElementsByTagName("enterprise-beans").item(0).insertBefore(sessionNode, ejbSessionNode);
187
188 // Insertion des tags de transaction, a partir du fichier xmlFile
189
Node JavaDoc transaction = docXmlTemp.getElementsByTagName("container-transaction").item(0);
190             Node JavaDoc ejbTransactionNode = docXmlEjb.getElementsByTagName("container-transaction").item(0);
191             Node JavaDoc transactionNode = docXmlEjb.importNode(transaction,true);
192             docXmlEjb.getDocumentElement().getElementsByTagName("assembly-descriptor").item(0).insertBefore(transactionNode, ejbTransactionNode);
193
194             XMLSerializer xManu = XMLSerializer.getInstance(ejbSessionNode.getOwnerDocument());
195             FileOutputStream JavaDoc fo = new FileOutputStream JavaDoc(ejbStanXmlFile);
196             xManu.serialize(fo);
197         } catch (FileNotFoundException JavaDoc e) {
198             // TODO Auto-generated catch block
199
e.printStackTrace();
200         } catch (ParserConfigurationException JavaDoc e) {
201             // TODO Auto-generated catch block
202
e.printStackTrace();
203         } catch (SAXException JavaDoc e) {
204             // TODO Auto-generated catch block
205
e.printStackTrace();
206         } catch (IOException JavaDoc e) {
207             // TODO Auto-generated catch block
208
e.printStackTrace();
209         }
210     
211
212     }
213
214
215     public static void updateEjbJOnASXmlFile(File JavaDoc ejbJOnASXmlFile, File JavaDoc xmlFile) {
216         try {
217             FileInputStream JavaDoc is = new FileInputStream JavaDoc(ejbJOnASXmlFile);
218             Document JavaDoc docXmlEjb = XMLUtils.newDocument(is,true);
219             Document JavaDoc docXmlTemp = XMLUtils.newDocument(new FileInputStream JavaDoc(xmlFile),false);
220
221
222             
223
224             // Test if session EJB is already declared
225
// In this cas we do nothing
226
NodeList JavaDoc nlSC = docXmlEjb.getElementsByTagName("ejb-name");
227             String JavaDoc ejbSessionName = docXmlTemp.getElementsByTagName("ejb-name").item(0).getFirstChild().getNodeValue();
228             for(int i = 0;i < nlSC.getLength(); i++) {
229                 String JavaDoc test = nlSC.item(i).getFirstChild().getNodeValue();
230                 if (nlSC.item(i).getFirstChild().getNodeValue().equals(ejbSessionName))
231                         return;
232                     
233             }
234
235 // Insertion des tags ejb, a partir du fichier xmlFile
236
Node JavaDoc session = docXmlTemp.getElementsByTagName("jonas-session").item(0);
237             Node JavaDoc ejbSessionNode = docXmlEjb.getElementsByTagName("jonas-entity").item(0);
238             Node JavaDoc sessionNode = docXmlEjb.importNode(session,true);
239             docXmlEjb.getDocumentElement().insertBefore(sessionNode, ejbSessionNode);
240
241             XMLSerializer xManu = XMLSerializer.getInstance(ejbSessionNode.getOwnerDocument());
242             FileOutputStream JavaDoc fo = new FileOutputStream JavaDoc(ejbJOnASXmlFile);
243             xManu.serialize(fo);
244         } catch (FileNotFoundException JavaDoc e) {
245             // TODO Auto-generated catch block
246
e.printStackTrace();
247         } catch (ParserConfigurationException JavaDoc e) {
248             // TODO Auto-generated catch block
249
e.printStackTrace();
250         } catch (SAXException JavaDoc e) {
251             // TODO Auto-generated catch block
252
e.printStackTrace();
253         } catch (IOException JavaDoc e) {
254             // TODO Auto-generated catch block
255
e.printStackTrace();
256         }
257     
258
259     }
260
261     private static Node JavaDoc getNodeInWebFile(Document JavaDoc webXmlFile) {
262         // On teste les tags pouvant être présents dans le web.xml
263
// On commence par servlet-mapping et on descend jusqu'a en trouver un
264
Node JavaDoc webNode = webXmlFile.getElementsByTagName("servlet-mapping").item(0);
265         if (webNode == null)
266             webNode = webXmlFile.getElementsByTagName("session-config").item(0);
267         if (webNode == null)
268             webNode = webXmlFile.getElementsByTagName("mime-mapping").item(0);
269         if (webNode == null)
270             webNode = webXmlFile.getElementsByTagName("welcome-file-list").item(0);
271         if (webNode == null)
272             webNode = webXmlFile.getElementsByTagName("error-page").item(0);
273         if (webNode == null)
274             webNode = webXmlFile.getElementsByTagName("taglib").item(0);
275         if (webNode == null)
276             webNode = webXmlFile.getElementsByTagName("resource-env-ref").item(0);
277         if (webNode == null)
278             webNode = webXmlFile.getElementsByTagName("resource-ref").item(0);
279         if (webNode == null)
280             webNode = webXmlFile.getElementsByTagName("security-constraint").item(0);
281         if (webNode == null)
282             webNode = webXmlFile.getElementsByTagName("login-config").item(0);
283         if (webNode == null)
284             webNode = webXmlFile.getElementsByTagName("security-role").item(0);
285         if (webNode == null)
286             webNode = webXmlFile.getElementsByTagName("env-entry").item(0);
287         if (webNode == null)
288             webNode = webXmlFile.getElementsByTagName("ejb-ref").item(0);
289         if (webNode == null)
290             webNode = webXmlFile.getElementsByTagName("ejb-local-ref").item(0);
291         if (webNode == null)
292             webNode = webXmlFile.getElementsByTagName("web-app").item(0);
293         
294         return webNode;
295     }
296
297
298     public static void updateHbmXmlFile(File JavaDoc hbmXmlFile, File JavaDoc xmlNode) {
299         try {
300             FileInputStream JavaDoc is = new FileInputStream JavaDoc(hbmXmlFile);
301             Document JavaDoc docXmlHbm = XMLUtils.newDocument(is,true);
302             Document JavaDoc docXmlTemp = XMLUtils.newDocument(new FileInputStream JavaDoc(xmlNode),false);
303
304             Node JavaDoc compositeTemp = docXmlTemp.getElementsByTagName("composite-id").item(0);
305             Node JavaDoc hbmComposite = docXmlHbm.getElementsByTagName("composite-id").item(0);
306             Node JavaDoc compositeInHBM = docXmlHbm.importNode(compositeTemp,true);
307             docXmlHbm.getDocumentElement().getElementsByTagName("class").item(0).replaceChild(compositeInHBM, hbmComposite);
308
309             XMLSerializer xManu = XMLSerializer.getInstance(hbmComposite.getOwnerDocument());
310             FileOutputStream JavaDoc fo = new FileOutputStream JavaDoc(hbmXmlFile);
311             xManu.serialize(fo);
312         } catch (FileNotFoundException JavaDoc e) {
313             // TODO Auto-generated catch block
314
e.printStackTrace();
315         } catch (ParserConfigurationException JavaDoc e) {
316             // TODO Auto-generated catch block
317
e.printStackTrace();
318         } catch (SAXException JavaDoc e) {
319             // TODO Auto-generated catch block
320
e.printStackTrace();
321         } catch (IOException JavaDoc e) {
322             // TODO Auto-generated catch block
323
e.printStackTrace();
324         }
325     
326
327     }
328
329 }
330
Popular Tags