1 24 25 package org.objectweb.jonas_lib.genbase.utils; 26 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 30 import javax.xml.parsers.DocumentBuilder ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import javax.xml.parsers.ParserConfigurationException ; 33 34 import org.w3c.dom.Attr ; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.NodeList ; 38 import org.w3c.dom.Text ; 39 import org.xml.sax.SAXException ; 40 41 import org.objectweb.jonas_client.deployment.lib.JonasAppClientDTDs; 42 import org.objectweb.jonas_client.deployment.lib.JonasAppClientSchemas; 43 44 import org.objectweb.jonas_ear.deployment.lib.EarDTDs; 45 import org.objectweb.jonas_ear.deployment.lib.EarSchemas; 46 47 import org.objectweb.jonas_ejb.deployment.lib.JonasEjbjarDTDs; 48 import org.objectweb.jonas_ejb.deployment.lib.JonasEjbjarSchemas; 49 50 import org.objectweb.jonas_lib.deployment.digester.JEntityResolver; 51 import org.objectweb.jonas_lib.genbase.archive.Archive; 52 53 import org.objectweb.jonas_web.deployment.lib.JonasWebAppDTDs; 54 import org.objectweb.jonas_web.deployment.lib.JonasWebAppSchemas; 55 import org.objectweb.jonas_web.deployment.lib.WebAppDTDs; 56 import org.objectweb.jonas_web.deployment.lib.WebAppSchemas; 57 58 import org.objectweb.jonas_ws.deployment.lib.JonasWsSchemas; 59 import org.objectweb.jonas_ws.deployment.lib.WsSchemas; 60 import org.objectweb.jonas_ws.wsgen.NoJ2EEWebservicesException; 61 62 66 public class XMLUtils { 67 68 69 private static DocumentBuilderFactory factory = null; 70 71 74 private static final String J2EE_NS = "http://java.sun.com/xml/ns/j2ee"; 75 76 79 private static final String JONAS_NS = "http://www.objectweb.org/jonas/ns"; 80 81 84 private static final String XMLNS_NS = "http://www.w3.org/2000/xmlns/"; 85 86 89 private static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance"; 90 91 94 private static final String SESSION_BEAN = "jonas-session"; 95 96 99 private static final String ENTITY_BEAN = "jonas-entity"; 100 101 104 private static final String MESSAGE_BEAN = "jonas-message-driven"; 105 106 109 private static final String EJB_NAME = "ejb-name"; 110 111 114 private static final String SERVLET = "servlet"; 115 116 119 private static final String SERVLET_NAME = "servlet-name"; 120 121 124 private static final String JONAS_SERVICE_REF = "jonas-service-ref"; 125 126 129 private static final String SR_NAME = "service-ref-name"; 130 131 134 private XMLUtils() { 135 } 136 137 142 private static DocumentBuilderFactory newFactory(boolean validating) { 143 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 144 factory.setNamespaceAware(true); 145 factory.setValidating(validating); 146 setFactoryValidation (factory, validating); 147 148 return factory; 149 } 150 151 157 private static void setFactoryValidation (DocumentBuilderFactory factory, boolean validating) { 158 factory.setValidating(validating); 159 factory.setIgnoringElementContentWhitespace(validating); 161 factory.setAttribute("http://apache.org/xml/features/validation/schema", new Boolean (validating)); 162 } 163 164 165 179 public static Document newDocument(InputStream is, String name, boolean isDTDsAllowed) throws ParserConfigurationException , SAXException , IOException , NoJ2EEWebservicesException { 180 181 return newDocument (is, name, isDTDsAllowed, true); 182 } 183 184 199 public static Document newDocument(InputStream is, String name, boolean isDTDsAllowed, boolean validate) 200 throws NoJ2EEWebservicesException, ParserConfigurationException , SAXException , IOException { 201 202 if (factory == null) { 203 factory = newFactory(validate); 204 } 205 206 if (factory.isValidating() != validate) { 207 setFactoryValidation(factory, validate); 208 } 209 210 DocumentBuilder builder = factory.newDocumentBuilder(); 211 212 if (factory.isValidating()) { 214 JEntityResolver jer = new JEntityResolver(); 215 jer.addSchemas(new WebAppSchemas()); 217 jer.addSchemas(new JonasWebAppSchemas()); 218 jer.addSchemas(new JonasEjbjarSchemas()); 219 jer.addSchemas(new JonasAppClientSchemas()); 220 jer.addSchemas(new EarSchemas()); 221 jer.addSchemas(new WsSchemas()); 222 jer.addSchemas(new JonasWsSchemas()); 223 224 jer.addDtds(new WebAppDTDs()); 226 jer.addDtds(new JonasWebAppDTDs()); 227 jer.addDtds(new JonasEjbjarDTDs()); 228 jer.addDtds(new JonasAppClientDTDs()); 229 jer.addDtds(new EarDTDs()); 230 231 builder.setEntityResolver(jer); 232 } 233 Document doc = builder.parse(is); 234 is.close(); 236 if (doc.getDoctype() != null && !isDTDsAllowed) { 237 throw new NoJ2EEWebservicesException(name + " use a DTD. Only XML Schema are accepted for J2EE 1.4 webservices"); 240 } 241 return doc; 242 } 243 244 245 253 public static Element getBeanElement(Element base, String bName) { 254 Element found = null; 255 256 NodeList sessions = base.getElementsByTagNameNS(JONAS_NS, SESSION_BEAN); 258 259 for (int i = 0; (i < sessions.getLength()) && (found == null); i++) { 260 Element session = (Element ) sessions.item(i); 261 NodeList names = session.getElementsByTagNameNS(JONAS_NS, EJB_NAME); 262 263 Element name = (Element ) names.item(0); 265 266 if (name.getFirstChild().getNodeValue().equals(bName)) { 267 found = session; 268 } 269 } 270 271 NodeList entities = base.getElementsByTagNameNS(JONAS_NS, ENTITY_BEAN); 273 274 for (int i = 0; (i < entities.getLength()) && (found == null); i++) { 275 Element entity = (Element ) entities.item(i); 276 NodeList names = entity.getElementsByTagNameNS(JONAS_NS, EJB_NAME); 277 278 Element name = (Element ) names.item(0); 280 281 if (name.getFirstChild().getNodeValue().equals(bName)) { 282 found = entity; 283 } 284 } 285 286 NodeList messages = base.getElementsByTagNameNS(JONAS_NS, MESSAGE_BEAN); 288 289 for (int i = 0; (i < messages.getLength()) && (found == null); i++) { 290 Element message = (Element ) messages.item(i); 291 NodeList names = message.getElementsByTagNameNS(JONAS_NS, EJB_NAME); 292 293 Element name = (Element ) names.item(0); 295 296 if (name.getFirstChild().getNodeValue().equals(bName)) { 297 found = message; 298 } 299 } 300 301 return found; 302 } 303 304 310 public static Element getServletElement(Element base, String sName) { 311 Element found = null; 312 313 NodeList servlets = base.getElementsByTagNameNS(J2EE_NS, SERVLET); 315 316 for (int i = 0; (i < servlets.getLength()) && (found == null); i++) { 317 Element servlet = (Element ) servlets.item(i); 318 NodeList names = servlet.getElementsByTagNameNS(J2EE_NS, SERVLET_NAME); 319 320 Element name = (Element ) names.item(0); 322 323 if (name.getFirstChild().getNodeValue().equals(sName)) { 324 found = servlet; 325 } 326 } 327 328 return found; 329 } 330 331 338 public static Element getJonasServiceRef(Element base, String srName) { 339 Element found = null; 340 341 if (base != null) { 343 NodeList jsrs = base.getElementsByTagNameNS(JONAS_NS, JONAS_SERVICE_REF); 344 345 for (int i = 0; (i < jsrs.getLength()) && (found == null); i++) { 346 Element jsr = (Element ) jsrs.item(i); 347 NodeList names = jsr.getElementsByTagNameNS(JONAS_NS, SR_NAME); 348 349 Element name = (Element ) names.item(0); 351 352 if (name.getFirstChild().getNodeValue().equals(srName)) { 353 found = jsr; 354 } 355 } 356 } 357 358 return found; 359 } 360 361 366 public static void cleanDummyApplication(Document doc) { 367 Element root = doc.getDocumentElement(); 368 NodeList nl = root.getElementsByTagNameNS(J2EE_NS, "module"); 369 root.removeChild(nl.item(0)); 370 } 371 372 377 public static void addEjb(Document app, Archive ejbjar) { 378 Element module = app.createElementNS(J2EE_NS, "module"); 379 Element ejb = app.createElementNS(J2EE_NS, "ejb"); 380 Text ejbText = app.createTextNode(ejbjar.getRootFile().getName()); 381 ejb.appendChild(ejbText); 382 module.appendChild(ejb); 383 384 insertModule(app, module); 385 } 386 387 392 public static void addClient(Document app, Archive client) { 393 Element module = app.createElementNS(J2EE_NS, "module"); 394 Element clt = app.createElementNS(J2EE_NS, "java"); 395 Text cltText = app.createTextNode(client.getRootFile().getName()); 396 clt.appendChild(cltText); 397 module.appendChild(clt); 398 399 insertModule(app, module); 400 } 401 402 408 public static void addWebApp(Document app, Archive webapp, String ctx) { 409 Element module = app.createElementNS(J2EE_NS, "module"); 410 Element web = app.createElementNS(J2EE_NS, "web"); 411 Element webUri = app.createElementNS(J2EE_NS, "web-uri"); 412 Element context = app.createElementNS(J2EE_NS, "context-root"); 413 414 Text webText = app.createTextNode(webapp.getName()); 415 Text ctxText = app.createTextNode(ctx); 416 webUri.appendChild(webText); 417 context.appendChild(ctxText); 418 419 web.appendChild(webUri); 420 web.appendChild(context); 421 422 module.appendChild(web); 423 424 insertModule(app, module); 425 } 426 427 432 private static void insertModule(Document app, Element module) { 433 Element application = app.getDocumentElement(); 434 Element first = findFirstSecurityRole(application); 435 application.insertBefore(module, first); 436 } 437 438 443 private static Element findFirstSecurityRole(Element app) { 444 NodeList nl = app.getElementsByTagNameNS(J2EE_NS, "security-role"); 445 446 if (nl.getLength() == 0) { 447 return null; 448 } else { 449 return (Element ) nl.item(0); 450 } 451 } 452 453 457 public static Document newJonasClient() { 458 459 Document root = createDocument(); 460 461 Element jonasClient = root.createElementNS(JONAS_NS, "jonas-client"); 463 464 int index = JonasAppClientSchemas.JONAS_APPCLIENT_SCHEMAS.length - 1; 466 String schema = JonasAppClientSchemas.JONAS_APPCLIENT_SCHEMAS[index]; 467 addNamespaces(jonasClient, schema); 468 root.appendChild(jonasClient); 469 470 return root; 471 } 472 473 476 public static Document newJonasWeb() { 477 478 Document root = createDocument(); 479 480 Element jonasWebApp = root.createElementNS(JONAS_NS, "jonas-web-app"); 482 483 int index = JonasWebAppSchemas.JONAS_WEBAPP_SCHEMAS.length - 1; 485 String schema = JonasWebAppSchemas.JONAS_WEBAPP_SCHEMAS[index]; 486 addNamespaces(jonasWebApp, schema); 487 root.appendChild(jonasWebApp); 488 489 return root; 490 } 491 492 495 private static Document createDocument() { 496 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 497 factory.setNamespaceAware(true); 498 DocumentBuilder builder; 499 try { 500 builder = factory.newDocumentBuilder(); 501 } catch (ParserConfigurationException e) { 502 throw new RuntimeException (e); 503 } 504 return builder.newDocument(); 505 } 506 507 512 private static void addNamespaces(Element e, String schema) { 513 Document root = e.getOwnerDocument(); 514 Attr xmlns = root.createAttributeNS(XMLNS_NS, "xmlns"); 516 xmlns.setValue(JONAS_NS); 517 518 Attr xsi = root.createAttributeNS(XMLNS_NS, "xmlns:xsi"); 520 xsi.setValue(XSI_NS); 521 522 Attr schemaLocation = root.createAttributeNS(XSI_NS, "xsi:schemaLocation"); 525 schemaLocation.setValue(JONAS_NS + " " + JONAS_NS + "/" + schema); 526 527 e.setAttributeNodeNS(xmlns); 528 e.setAttributeNodeNS(xsi); 529 e.setAttributeNodeNS(schemaLocation); 530 531 } 532 533 } | Popular Tags |