KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JOnAS : Java(TM) OpenSource Application Server
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Initial Developer : Guillaume Sauthier
20  * --------------------------------------------------------------------------
21  * $Id: XMLUtils.java,v 1.2 2004/02/16 12:16:42 sauthieg Exp $
22  * --------------------------------------------------------------------------
23 */

24
25 package com.bull.eclipse.jonas.utils.xml;
26
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.util.jar.JarFile JavaDoc;
30
31 import javax.xml.parsers.DocumentBuilder JavaDoc;
32 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
33 import javax.xml.parsers.ParserConfigurationException JavaDoc;
34
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38 import org.w3c.dom.Text JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41 import com.bull.eclipse.jonas.utils.xml.desc.AppClientDTDs;
42 import com.bull.eclipse.jonas.utils.xml.desc.AppClientSchemas;
43 import com.bull.eclipse.jonas.utils.xml.desc.EarDTDs;
44 import com.bull.eclipse.jonas.utils.xml.desc.EarSchemas;
45 import com.bull.eclipse.jonas.utils.xml.desc.EjbjarDTDs;
46 import com.bull.eclipse.jonas.utils.xml.desc.EjbjarSchemas;
47 import com.bull.eclipse.jonas.utils.xml.desc.HibernateDTDs;
48 import com.bull.eclipse.jonas.utils.xml.desc.JonasAppClientDTDs;
49 import com.bull.eclipse.jonas.utils.xml.desc.JonasAppClientSchemas;
50 import com.bull.eclipse.jonas.utils.xml.desc.JonasEjbjarDTDs;
51 import com.bull.eclipse.jonas.utils.xml.desc.JonasEjbjarSchemas;
52 import com.bull.eclipse.jonas.utils.xml.desc.JonasWebAppDTDs;
53 import com.bull.eclipse.jonas.utils.xml.desc.JonasWebAppSchemas;
54 import com.bull.eclipse.jonas.utils.xml.desc.WebAppDTDs;
55 import com.bull.eclipse.jonas.utils.xml.desc.WebAppSchemas;
56
57
58 /**
59  * XML Utils Class. Holds methods for easier DOM parsing, XML modifications, ...
60  *
61  * @author Guillaume Sauthier
62  */

63 public class XMLUtils {
64     /** DOM factory */
65     private static DocumentBuilderFactory JavaDoc factory = null;
66
67     /**
68      * J2EE namespace
69      */

70     private static final String JavaDoc J2EE_NS = "http://java.sun.com/xml/ns/j2ee";
71
72     /**
73      * JONAS namespace
74      */

75     private static final String JavaDoc JONAS_NS = "http://www.objectweb.org/jonas/ns";
76
77     /** ejb-jar tag names */
78     private static final String JavaDoc EJB_JAR = "ejb-jar";
79     private static final String JavaDoc STANDARD_SESSION_BEAN = "session";
80     private static final String JavaDoc STANDARD_ENTITY_BEAN = "entity";
81     private static final String JavaDoc STANDARD_MESSAGE_BEAN = "message-driven";
82     private static final String JavaDoc STANDARD_EJB_NAME = "ejb-name";
83     private static final String JavaDoc PK_TYPE = "prim-key-class";
84
85     /** jonas-ejb-jar tag names */
86     private static final String JavaDoc JONAS_EJB_JAR = "jonas-ejb-jar";
87     private static final String JavaDoc SESSION_BEAN = "jonas-session";
88     private static final String JavaDoc ENTITY_BEAN = "jonas-entity";
89     private static final String JavaDoc MESSAGE_BEAN = "jonas-message-driven";
90     private static final String JavaDoc EJB_NAME = "ejb-name";
91     private static final String JavaDoc SERVLET = "servlet";
92     private static final String JavaDoc SERVLET_NAME = "servlet-name";
93     private static final String JavaDoc JONAS_SERVICE_REF = "jonas-service-ref";
94     private static final String JavaDoc SR_NAME = "service-ref-name";
95
96     /**
97      * Returns a new DocumentBuilderFactory.
98      *
99      * @param validating set the parser validating or not
100      *
101      * @return a new DocumentBuilderFactory.
102      */

103     private static DocumentBuilderFactory JavaDoc newFactory(boolean validating) {
104         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
105         factory.setNamespaceAware(true);
106         factory.setValidating(validating);
107         factory.setIgnoringElementContentWhitespace(true);
108         //factory.setAttribute("http://apache.org/xml/features/validation/schema",
109
// new Boolean(validating));
110
return factory;
111     }
112
113     /**
114      * Creates a new Document from a given InputStream.
115      *
116      * @param is the InputStream to be parsed
117      *
118      * @return the Document instance.
119      *
120      * @exception ParserConfigurationException
121      * @exception SAXException
122      * @exception IOException
123      */

124     public static Document JavaDoc newDocument(InputStream JavaDoc is, boolean validate)
125         throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
126         //MANU if(factory == null) {
127
factory = newFactory(validate);
128         //MANU }
129

130         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
131
132         // add entity resolver
133
if (factory.isValidating()) {
134             JEntityResolver jer = new JEntityResolver();
135             jer.addSchemas(new WebAppSchemas());
136             jer.addSchemas(new JonasWebAppSchemas());
137             jer.addSchemas(new JonasEjbjarSchemas());
138             jer.addSchemas(new EjbjarSchemas());
139             jer.addSchemas(new JonasAppClientSchemas());
140             jer.addSchemas(new AppClientSchemas());
141             jer.addSchemas(new EarSchemas());
142             
143             
144             jer.addDtds(new WebAppDTDs());
145             jer.addDtds(new JonasWebAppDTDs());
146             jer.addDtds(new EjbjarDTDs());
147             jer.addDtds(new JonasEjbjarDTDs());
148             jer.addDtds(new AppClientDTDs());
149             jer.addDtds(new JonasAppClientDTDs());
150             jer.addDtds(new EarDTDs());
151             jer.addDtds(new HibernateDTDs());
152             
153             builder.setEntityResolver(jer);
154         }
155
156         return builder.parse(is);
157     }
158
159     /**
160      * Returns the
161      * <code>session</code>/<code>entity</code>/<code>message-driven</code>
162      * XML Element with given name.
163      *
164      * @param base <code>jonas-ejb-jar</code> Element.
165      * @param bName the bean name to be found.
166      *
167      * @return the
168      * <code>session</code>/<code>entity</code>/<code>message-driven</code>
169      * XML Element.
170      */

171     public static Element JavaDoc getBeanElementInJOnASDesc(Element JavaDoc base, String JavaDoc bName) {
172         Element JavaDoc found = null;
173
174         // try Session Bean
175

176         //MANU NodeList sessions = base.getElementsByTagNameNS(JONAS_NS, SESSION_BEAN);
177
NodeList JavaDoc sessions = base.getElementsByTagName(SESSION_BEAN); //MANU
178

179         for(int i = 0; (i < sessions.getLength()) && (found == null); i++) {
180             Element JavaDoc session = (Element JavaDoc) sessions.item(i);
181             //MANU NodeList names = session.getElementsByTagNameNS(JONAS_NS, EJB_NAME);
182
NodeList JavaDoc names = session.getElementsByTagName(EJB_NAME); //MANU
183

184             // ejb-name mandatory and unique
185
Element JavaDoc name = (Element JavaDoc) names.item(0);
186
187             if(name.getFirstChild()
188                     .getNodeValue()
189                     .equals(bName)) {
190                 found = session;
191             }
192         }
193         // try Entity Bean
194
//MANU NodeList entities = base.getElementsByTagNameNS(J2EE_NS, STANDARD_ENTITY_BEAN);
195
NodeList JavaDoc entities = base.getElementsByTagName(STANDARD_ENTITY_BEAN); //MANU
196

197         for(int i = 0; (i < entities.getLength()) && (found == null); i++) {
198             Element JavaDoc entity = (Element JavaDoc) entities.item(i);
199             //MANU NodeList names = entity.getElementsByTagNameNS(J2EE_NS, STANDARD_EJB_NAME);
200
NodeList JavaDoc names = entity.getElementsByTagName(STANDARD_EJB_NAME); //MANU
201

202             // ejb-name mandatory and unique
203
Element JavaDoc name = (Element JavaDoc) names.item(0);
204
205             if(name.getFirstChild()
206                     .getNodeValue()
207                     .equals(bName)) {
208                 found = entity;
209             }
210         }
211
212         // try Message Driven Bean
213
//MANU NodeList messages = base.getElementsByTagNameNS(J2EE_NS, STANDARD_MESSAGE_BEAN);
214
NodeList JavaDoc messages = base.getElementsByTagName(STANDARD_MESSAGE_BEAN); //MANU
215

216         for(int i = 0; (i < messages.getLength()) && (found == null); i++) {
217             Element JavaDoc message = (Element JavaDoc) messages.item(i);
218             //MANU NodeList names = message.getElementsByTagNameNS(J2EE_NS, STANDARD_EJB_NAME);
219
NodeList JavaDoc names = message.getElementsByTagName(STANDARD_EJB_NAME); //MANU
220

221             // ejb-name mandatory and unique
222
Element JavaDoc name = (Element JavaDoc) names.item(0);
223
224             if(name.getFirstChild()
225                     .getNodeValue()
226                     .equals(bName)) {
227                 found = message;
228             }
229         }
230
231         return found;
232     }
233
234         /**
235          * Returns the
236          * <code>session</code>/<code>entity</code>/<code>message-driven</code>
237          * XML Element with given name.
238          *
239          * @param base <code>ejb-jar</code> Element.
240          * @param bName the bean name to be found.
241          *
242          * @return the
243          * <code>session</code>/<code>entity</code>/<code>message-driven</code>
244          * XML Element.
245          */

246         public static Element JavaDoc getBeanElementInStandardDesc(Element JavaDoc base, String JavaDoc bName) {
247             Element JavaDoc found = null;
248
249             // try Session Bean
250

251             //MANU NodeList sessions = base.getElementsByTagNameNS(J2EE_NS, STANDARD_SESSION_BEAN);
252
NodeList JavaDoc sessions = base.getElementsByTagName(STANDARD_SESSION_BEAN); //MANU
253

254             for(int i = 0; (i < sessions.getLength()) && (found == null); i++) {
255                 Element JavaDoc session = (Element JavaDoc) sessions.item(i);
256                 //MANU NodeList names = session.getElementsByTagNameNS(J2EE_NS, STANDARD_EJB_NAME);
257
NodeList JavaDoc names = session.getElementsByTagName(STANDARD_EJB_NAME); //MANU
258

259                 // ejb-name mandatory and unique
260
Element JavaDoc name = (Element JavaDoc) names.item(0);
261                 
262                 if(name.getFirstChild()
263                         .getNodeValue()
264                         .equals(bName)) {
265                     found = session;
266                 }
267             }
268
269         // try Entity Bean
270
//MANU NodeList entities = base.getElementsByTagNameNS(J2EE_NS, STANDARD_ENTITY_BEAN);
271
NodeList JavaDoc entities = base.getElementsByTagName(STANDARD_ENTITY_BEAN); //MANU
272

273         for(int i = 0; (i < entities.getLength()) && (found == null); i++) {
274             Element JavaDoc entity = (Element JavaDoc) entities.item(i);
275             //MANU NodeList names = entity.getElementsByTagNameNS(J2EE_NS, STANDARD_EJB_NAME);
276
NodeList JavaDoc names = entity.getElementsByTagName(STANDARD_EJB_NAME); //MANU
277

278             // ejb-name mandatory and unique
279
Element JavaDoc name = (Element JavaDoc) names.item(0);
280
281             if(name.getFirstChild()
282                     .getNodeValue()
283                     .equals(bName)) {
284                 found = entity;
285             }
286         }
287
288         // try Message Driven Bean
289
NodeList JavaDoc messages = base.getElementsByTagNameNS(J2EE_NS, STANDARD_MESSAGE_BEAN);
290
291         for(int i = 0; (i < messages.getLength()) && (found == null); i++) {
292             Element JavaDoc message = (Element JavaDoc) messages.item(i);
293             NodeList JavaDoc names = message.getElementsByTagNameNS(J2EE_NS, STANDARD_EJB_NAME);
294
295             // ejb-name mandatory and unique
296
Element JavaDoc name = (Element JavaDoc) names.item(0);
297
298             if(name.getFirstChild()
299                     .getNodeValue()
300                     .equals(bName)) {
301                 found = message;
302             }
303         }
304
305         return found;
306     }
307
308     /**
309      * Returns the matching <code>servlet</code> XML Element with given name.
310      *
311      * @param base <code>web-app</code> Element.
312      * @param sName the servlet name to be found.
313      *
314      * @return the matching <code>servlet</code> XML Element.
315      */

316     public static Element JavaDoc getServletElement(Element JavaDoc base, String JavaDoc sName) {
317         Element JavaDoc found = null;
318
319         // Search servlet List
320
//MANU NodeList servlets = base.getElementsByTagNameNS(J2EE_NS, SERVLET);
321

322         NodeList JavaDoc servlets = base.getElementsByTagName(SERVLET); //MANU
323

324         for(int i = 0; (i < servlets.getLength()) && (found == null); i++) {
325             Element JavaDoc servlet = (Element JavaDoc) servlets.item(i);
326             NodeList JavaDoc names =
327                 servlet.getElementsByTagName(SERVLET_NAME); //MANU
328

329             // servlet-name mandatory and unique
330
Element JavaDoc name = (Element JavaDoc) names.item(0);
331
332             if(name.getFirstChild()
333                     .getNodeValue()
334                     .equals(sName)) {
335                 found = servlet;
336             }
337         }
338
339         return found;
340     }
341
342     /**
343      * Returns the <code>jonas-service-ref</code> XML Element with given name.
344      *
345      * @param base <code>web-app</code> / bean Element containing
346      * <code>jonas-service-ref</code> Element(s).
347      * @param srName the service-ref name to be found.
348      *
349      * @return the <code>jonas-service-ref</code> XML Element.
350      */

351     public static Element JavaDoc getJonasServiceRef(Element JavaDoc base, String JavaDoc srName) {
352         Element JavaDoc found = null;
353
354         // Search jonas-service-ref list
355
NodeList JavaDoc jsrs =
356             base.getElementsByTagNameNS(JONAS_NS, JONAS_SERVICE_REF);
357
358         for(int i = 0; (i < jsrs.getLength()) && (found == null); i++) {
359             Element JavaDoc jsr = (Element JavaDoc) jsrs.item(i);
360             NodeList JavaDoc names = jsr.getElementsByTagNameNS(JONAS_NS, SR_NAME);
361
362             // service-ref-name mandatory and unique
363
Element JavaDoc name = (Element JavaDoc) names.item(0);
364
365             if(name.getFirstChild()
366                     .getNodeValue()
367                     .equals(srName)) {
368                 found = jsr;
369             }
370         }
371
372         return found;
373     }
374
375     /**
376      * default application.xml contains a fake ejb module needed to be parsed
377      * without error but not needed for a normal application. Than we remove it.
378      *
379      * @param doc application.xml document
380      */

381     public static void cleanDummyApplication(Document JavaDoc doc) {
382         Element JavaDoc root = doc.getDocumentElement();
383         NodeList JavaDoc nl = root.getElementsByTagNameNS(J2EE_NS, "module");
384         root.removeChild(nl.item(0));
385     }
386
387     /**
388      * Add an ejb module in an application Document
389      *
390      * @param app application.xml Document
391      * @param ejbjar EJBJar archive
392      */

393     public static void addEjb(Document JavaDoc app, JarFile JavaDoc ejbjar) {
394         Element JavaDoc module = app.createElementNS(J2EE_NS, "module");
395         Element JavaDoc ejb = app.createElementNS(J2EE_NS, "ejb");
396         Text JavaDoc ejbText = app.createTextNode(ejbjar.getName());
397         ejb.appendChild(ejbText);
398         module.appendChild(ejb);
399
400         insertModule(app, module);
401     }
402
403     /**
404      * Add a client module in an application Document
405      *
406      * @param app application.xml Document
407      * @param client Client archive
408      */

409     public static void addClient(Document JavaDoc app, JarFile JavaDoc client) {
410         Element JavaDoc module = app.createElementNS(J2EE_NS, "module");
411         Element JavaDoc clt = app.createElementNS(J2EE_NS, "java");
412         Text JavaDoc cltText = app.createTextNode(client.getName());
413         clt.appendChild(cltText);
414         module.appendChild(clt);
415
416         insertModule(app, module);
417     }
418
419     /**
420      * Add an web module in an application Document
421      *
422      * @param app application.xml Document
423      * @param webapp WebApp archive
424      * @param ctx context-root
425      */

426     public static void addWebApp(Document JavaDoc app, JarFile JavaDoc webapp, String JavaDoc ctx) {
427         Element JavaDoc module = app.createElementNS(J2EE_NS, "module");
428         Element JavaDoc web = app.createElementNS(J2EE_NS, "web");
429         Element JavaDoc webUri = app.createElementNS(J2EE_NS, "web-uri");
430         Element JavaDoc context = app.createElementNS(J2EE_NS, "context-root");
431
432         Text JavaDoc webText = app.createTextNode(webapp.getName());
433         Text JavaDoc ctxText = app.createTextNode(ctx);
434         webUri.appendChild(webText);
435         context.appendChild(ctxText);
436
437         web.appendChild(webUri);
438         web.appendChild(context);
439
440         module.appendChild(web);
441
442         insertModule(app, module);
443     }
444
445     /**
446      * Insert a module in application Document
447      *
448      * @param app application.xml Document
449      * @param module module Element
450      */

451     private static void insertModule(Document JavaDoc app, Element JavaDoc module) {
452         Element JavaDoc application = app.getDocumentElement();
453         Element JavaDoc first = findFirstSecurityRole(application);
454         application.insertBefore(module, first);
455     }
456
457     /**
458      * return the first found security-role Element (or null if not found)
459      *
460      * @param app application.xml Document
461      *
462      * @return the first found security-role Element (or null if not found)
463      */

464     private static Element JavaDoc findFirstSecurityRole(Element JavaDoc app) {
465         NodeList JavaDoc nl = app.getElementsByTagNameNS(J2EE_NS, "security-role");
466
467         if(nl.getLength() == 0) {
468             return null;
469         } else {
470             return (Element JavaDoc) nl.item(0);
471         }
472     }
473     
474 }
475
Popular Tags