00001
00025 package org.objectweb.jonas_lib.genbase.utils;
00026
00027 import java.io.IOException;
00028 import java.io.InputStream;
00029
00030 import javax.xml.parsers.DocumentBuilder;
00031 import javax.xml.parsers.DocumentBuilderFactory;
00032 import javax.xml.parsers.ParserConfigurationException;
00033
00034 import org.w3c.dom.Attr;
00035 import org.w3c.dom.Document;
00036 import org.w3c.dom.Element;
00037 import org.w3c.dom.NodeList;
00038 import org.w3c.dom.Text;
00039 import org.xml.sax.SAXException;
00040
00041 import org.objectweb.jonas_client.deployment.lib.JonasAppClientDTDs;
00042 import org.objectweb.jonas_client.deployment.lib.JonasAppClientSchemas;
00043
00044 import org.objectweb.jonas_ear.deployment.lib.EarDTDs;
00045 import org.objectweb.jonas_ear.deployment.lib.EarSchemas;
00046
00047 import org.objectweb.jonas_ejb.deployment.lib.JonasEjbjarDTDs;
00048 import org.objectweb.jonas_ejb.deployment.lib.JonasEjbjarSchemas;
00049
00050 import org.objectweb.jonas_lib.deployment.digester.JEntityResolver;
00051 import org.objectweb.jonas_lib.genbase.archive.Archive;
00052
00053 import org.objectweb.jonas_web.deployment.lib.JonasWebAppDTDs;
00054 import org.objectweb.jonas_web.deployment.lib.JonasWebAppSchemas;
00055 import org.objectweb.jonas_web.deployment.lib.WebAppDTDs;
00056 import org.objectweb.jonas_web.deployment.lib.WebAppSchemas;
00057
00058 import org.objectweb.jonas_ws.deployment.lib.WsSchemas;
00059 import org.objectweb.jonas_ws.wsgen.NoJ2EEWebservicesException;
00060
00065 public class XMLUtils {
00066
00068 private static DocumentBuilderFactory factory = null;
00069
00073 private static final String J2EE_NS = "http://java.sun.com/xml/ns/j2ee";
00074
00078 private static final String JONAS_NS = "http://www.objectweb.org/jonas/ns";
00079
00083 private static final String XMLNS_NS = "http://www.w3.org/2000/xmlns/";
00084
00088 private static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance";
00089
00093 private static final String SESSION_BEAN = "jonas-session";
00094
00098 private static final String ENTITY_BEAN = "jonas-entity";
00099
00103 private static final String MESSAGE_BEAN = "jonas-message-driven";
00104
00108 private static final String EJB_NAME = "ejb-name";
00109
00113 private static final String SERVLET = "servlet";
00114
00118 private static final String SERVLET_NAME = "servlet-name";
00119
00123 private static final String JONAS_SERVICE_REF = "jonas-service-ref";
00124
00128 private static final String SR_NAME = "service-ref-name";
00129
00133 private XMLUtils() {
00134 }
00135
00141 private static DocumentBuilderFactory newFactory(boolean validating) {
00142 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
00143 factory.setNamespaceAware(true);
00144 factory.setValidating(validating);
00145
00146 factory.setIgnoringElementContentWhitespace(validating);
00147 factory.setAttribute("http://apache.org/xml/features/validation/schema", new Boolean(validating));
00148
00149 return factory;
00150 }
00151
00166 public static Document newDocument(InputStream is, String name, boolean isDTDsAllowed) throws ParserConfigurationException, SAXException, IOException, NoJ2EEWebservicesException {
00167 if (factory == null) {
00168 factory = newFactory(true);
00169 }
00170
00171 DocumentBuilder builder = factory.newDocumentBuilder();
00172
00173
00174 if (factory.isValidating()) {
00175 JEntityResolver jer = new JEntityResolver();
00176
00177 jer.addSchemas(new WebAppSchemas());
00178 jer.addSchemas(new JonasWebAppSchemas());
00179 jer.addSchemas(new JonasEjbjarSchemas());
00180 jer.addSchemas(new JonasAppClientSchemas());
00181 jer.addSchemas(new EarSchemas());
00182 jer.addSchemas(new WsSchemas());
00183
00184
00185 jer.addDtds(new WebAppDTDs());
00186 jer.addDtds(new JonasWebAppDTDs());
00187 jer.addDtds(new JonasEjbjarDTDs());
00188 jer.addDtds(new JonasAppClientDTDs());
00189 jer.addDtds(new EarDTDs());
00190
00191 builder.setEntityResolver(jer);
00192 }
00193 Document doc = builder.parse(is);
00194
00195 is.close();
00196 if (doc.getDoctype() != null && !isDTDsAllowed) {
00197
00198
00199 throw new NoJ2EEWebservicesException(name + " use a DTD. Only XML Schema are accepted for J2EE 1.4 webservices");
00200 }
00201 return doc;
00202 }
00203
00212 public static Element getBeanElement(Element base, String bName) {
00213 Element found = null;
00214
00215
00216 NodeList sessions = base.getElementsByTagNameNS(JONAS_NS, SESSION_BEAN);
00217
00218 for (int i = 0; (i < sessions.getLength()) && (found == null); i++) {
00219 Element session = (Element) sessions.item(i);
00220 NodeList names = session.getElementsByTagNameNS(JONAS_NS, EJB_NAME);
00221
00222
00223 Element name = (Element) names.item(0);
00224
00225 if (name.getFirstChild().getNodeValue().equals(bName)) {
00226 found = session;
00227 }
00228 }
00229
00230
00231 NodeList entities = base.getElementsByTagNameNS(JONAS_NS, ENTITY_BEAN);
00232
00233 for (int i = 0; (i < entities.getLength()) && (found == null); i++) {
00234 Element entity = (Element) entities.item(i);
00235 NodeList names = entity.getElementsByTagNameNS(JONAS_NS, EJB_NAME);
00236
00237
00238 Element name = (Element) names.item(0);
00239
00240 if (name.getFirstChild().getNodeValue().equals(bName)) {
00241 found = entity;
00242 }
00243 }
00244
00245
00246 NodeList messages = base.getElementsByTagNameNS(JONAS_NS, MESSAGE_BEAN);
00247
00248 for (int i = 0; (i < messages.getLength()) && (found == null); i++) {
00249 Element message = (Element) messages.item(i);
00250 NodeList names = message.getElementsByTagNameNS(JONAS_NS, EJB_NAME);
00251
00252
00253 Element name = (Element) names.item(0);
00254
00255 if (name.getFirstChild().getNodeValue().equals(bName)) {
00256 found = message;
00257 }
00258 }
00259
00260 return found;
00261 }
00262
00269 public static Element getServletElement(Element base, String sName) {
00270 Element found = null;
00271
00272
00273 NodeList servlets = base.getElementsByTagNameNS(J2EE_NS, SERVLET);
00274
00275 for (int i = 0; (i < servlets.getLength()) && (found == null); i++) {
00276 Element servlet = (Element) servlets.item(i);
00277 NodeList names = servlet.getElementsByTagNameNS(J2EE_NS, SERVLET_NAME);
00278
00279
00280 Element name = (Element) names.item(0);
00281
00282 if (name.getFirstChild().getNodeValue().equals(sName)) {
00283 found = servlet;
00284 }
00285 }
00286
00287 return found;
00288 }
00289
00297 public static Element getJonasServiceRef(Element base, String srName) {
00298 Element found = null;
00299
00300
00301 if (base != null) {
00302 NodeList jsrs = base.getElementsByTagNameNS(JONAS_NS, JONAS_SERVICE_REF);
00303
00304 for (int i = 0; (i < jsrs.getLength()) && (found == null); i++) {
00305 Element jsr = (Element) jsrs.item(i);
00306 NodeList names = jsr.getElementsByTagNameNS(JONAS_NS, SR_NAME);
00307
00308
00309 Element name = (Element) names.item(0);
00310
00311 if (name.getFirstChild().getNodeValue().equals(srName)) {
00312 found = jsr;
00313 }
00314 }
00315 }
00316
00317 return found;
00318 }
00319
00325 public static void cleanDummyApplication(Document doc) {
00326 Element root = doc.getDocumentElement();
00327 NodeList nl = root.getElementsByTagNameNS(J2EE_NS, "module");
00328 root.removeChild(nl.item(0));
00329 }
00330
00336 public static void addEjb(Document app, Archive ejbjar) {
00337 Element module = app.createElementNS(J2EE_NS, "module");
00338 Element ejb = app.createElementNS(J2EE_NS, "ejb");
00339 Text ejbText = app.createTextNode(ejbjar.getRootFile().getName());
00340 ejb.appendChild(ejbText);
00341 module.appendChild(ejb);
00342
00343 insertModule(app, module);
00344 }
00345
00351 public static void addClient(Document app, Archive client) {
00352 Element module = app.createElementNS(J2EE_NS, "module");
00353 Element clt = app.createElementNS(J2EE_NS, "java");
00354 Text cltText = app.createTextNode(client.getRootFile().getName());
00355 clt.appendChild(cltText);
00356 module.appendChild(clt);
00357
00358 insertModule(app, module);
00359 }
00360
00367 public static void addWebApp(Document app, Archive webapp, String ctx) {
00368 Element module = app.createElementNS(J2EE_NS, "module");
00369 Element web = app.createElementNS(J2EE_NS, "web");
00370 Element webUri = app.createElementNS(J2EE_NS, "web-uri");
00371 Element context = app.createElementNS(J2EE_NS, "context-root");
00372
00373 Text webText = app.createTextNode(webapp.getName());
00374 Text ctxText = app.createTextNode(ctx);
00375 webUri.appendChild(webText);
00376 context.appendChild(ctxText);
00377
00378 web.appendChild(webUri);
00379 web.appendChild(context);
00380
00381 module.appendChild(web);
00382
00383 insertModule(app, module);
00384 }
00385
00391 private static void insertModule(Document app, Element module) {
00392 Element application = app.getDocumentElement();
00393 Element first = findFirstSecurityRole(application);
00394 application.insertBefore(module, first);
00395 }
00396
00402 private static Element findFirstSecurityRole(Element app) {
00403 NodeList nl = app.getElementsByTagNameNS(J2EE_NS, "security-role");
00404
00405 if (nl.getLength() == 0) {
00406 return null;
00407 } else {
00408 return (Element) nl.item(0);
00409 }
00410 }
00411
00416 public static Document newJonasClient() {
00417
00418 Document root = createDocument();
00419
00420
00421 Element jonasClient = root.createElementNS(JONAS_NS, "jonas-client");
00422
00423
00424 int index = JonasAppClientSchemas.JONAS_APPCLIENT_SCHEMAS.length - 1;
00425 String schema = JonasAppClientSchemas.JONAS_APPCLIENT_SCHEMAS[index];
00426 addNamespaces(jonasClient, schema);
00427 root.appendChild(jonasClient);
00428
00429 return root;
00430 }
00431
00435 public static Document newJonasWeb() {
00436
00437 Document root = createDocument();
00438
00439
00440 Element jonasWebApp = root.createElementNS(JONAS_NS, "jonas-web-app");
00441
00442
00443 int index = JonasWebAppSchemas.JONAS_WEBAPP_SCHEMAS.length - 1;
00444 String schema = JonasWebAppSchemas.JONAS_WEBAPP_SCHEMAS[index];
00445 addNamespaces(jonasWebApp, schema);
00446 root.appendChild(jonasWebApp);
00447
00448 return root;
00449 }
00450
00454 private static Document createDocument() {
00455 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
00456 factory.setNamespaceAware(true);
00457 DocumentBuilder builder;
00458 try {
00459 builder = factory.newDocumentBuilder();
00460 } catch (ParserConfigurationException e) {
00461 throw new RuntimeException(e);
00462 }
00463 return builder.newDocument();
00464 }
00465
00471 private static void addNamespaces(Element e, String schema) {
00472 Document root = e.getOwnerDocument();
00473
00474 Attr xmlns = root.createAttributeNS(XMLNS_NS, "xmlns");
00475 xmlns.setValue(JONAS_NS);
00476
00477
00478 Attr xsi = root.createAttributeNS(XMLNS_NS, "xmlns:xsi");
00479 xsi.setValue(XSI_NS);
00480
00481
00482
00483 Attr schemaLocation = root.createAttributeNS(XSI_NS, "xsi:schemaLocation");
00484 schemaLocation.setValue(JONAS_NS + " " + JONAS_NS + "/" + schema);
00485
00486 e.setAttributeNodeNS(xmlns);
00487 e.setAttributeNodeNS(xsi);
00488 e.setAttributeNodeNS(schemaLocation);
00489
00490 }
00491
00492 }