| 1 20 package org.apache.cactus.integration.ant.deployment.webapp; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.DocumentType ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 32 40 public class WebXml 41 { 42 43 45 49 private static final WebXmlTag[] ELEMENT_ORDER = { 50 WebXmlTag.ICON, 51 WebXmlTag.DISPLAY_NAME, 52 WebXmlTag.DESCRIPTION, 53 WebXmlTag.DISTRIBUTABLE, 54 WebXmlTag.FILTER, 55 WebXmlTag.FILTER_MAPPING, 56 WebXmlTag.LISTENER, 57 WebXmlTag.SERVLET, 58 WebXmlTag.SERVLET_MAPPING, 59 WebXmlTag.SESSION_CONFIG, 60 WebXmlTag.MIME_MAPPING, 61 WebXmlTag.WELCOME_FILE_LIST, 62 WebXmlTag.ERROR_PAGE, 63 WebXmlTag.TAGLIB, 64 WebXmlTag.RESOURCE_ENV_REF, 65 WebXmlTag.RESOURCE_REF, 66 WebXmlTag.SECURITY_CONSTRAINT, 67 WebXmlTag.LOGIN_CONFIG, 68 WebXmlTag.SECURITY_ROLE, 69 WebXmlTag.ENV_ENTRY, 70 WebXmlTag.EJB_REF, 71 WebXmlTag.EJB_LOCAL_REF, 72 }; 73 74 76 79 private final Document document; 80 81 84 private final Element rootElement; 85 86 88 94 public WebXml(Document theDocument) 95 { 96 this.document = theDocument; 97 this.rootElement = theDocument.getDocumentElement(); 98 } 99 100 102 108 public final Document getDocument() 109 { 110 return this.document; 111 } 112 113 118 public final WebXmlVersion getVersion() 119 { 120 DocumentType docType = this.document.getDoctype(); 121 if (docType != null) 122 { 123 return WebXmlVersion.valueOf(docType); 124 } 125 return null; 126 } 127 128 134 public final void addFilter(String theFilterName, String theFilterClass) 135 { 136 if (theFilterName == null) 137 { 138 throw new NullPointerException (); 139 } 140 if (hasFilter(theFilterName)) 141 { 142 throw new IllegalStateException ("Filter '" + theFilterName 143 + "' already defined"); 144 } 145 Element filterElement = 146 this.document.createElement(WebXmlTag.FILTER.getTagName()); 147 filterElement.appendChild( 148 createNestedText(WebXmlTag.FILTER_NAME, theFilterName)); 149 filterElement.appendChild( 150 createNestedText(WebXmlTag.FILTER_CLASS, theFilterClass)); 151 addElement(WebXmlTag.FILTER, filterElement); 152 } 153 154 160 public final void addContextParam(Element theContextParam) 161 { 162 checkElement(theContextParam, WebXmlTag.CONTEXT_PARAM); 163 164 String paramName = 165 getNestedText(theContextParam, WebXmlTag.PARAM_NAME); 166 if (paramName == null) 167 { 168 throw new IllegalArgumentException ( 169 "Not a valid context-param name element"); 170 } 171 172 String paramValue = 173 getNestedText(theContextParam, WebXmlTag.PARAM_VALUE); 174 if (paramValue == null) 175 { 176 throw new IllegalArgumentException ( 177 "Not a valid context-param value element"); 178 } 179 180 if (hasContextParam(paramName)) 181 { 182 throw new IllegalStateException ("Context param '" + paramName 183 + "' already defined"); 184 } 185 addElement(WebXmlTag.CONTEXT_PARAM, theContextParam); 186 } 187 188 193 public final void addFilter(Element theFilter) 194 { 195 checkElement(theFilter, WebXmlTag.FILTER); 196 String filterName = getNestedText(theFilter, WebXmlTag.FILTER_NAME); 197 if (filterName == null) 198 { 199 throw new IllegalArgumentException ("Not a valid filter element"); 200 } 201 if (hasFilter(filterName)) 202 { 203 throw new IllegalStateException ("Filter '" + filterName 204 + "' already defined"); 205 } 206 addElement(WebXmlTag.FILTER, theFilter); 207 } 208 209 216 public final void addFilterInitParam(String theFilterName, 217 String theParamName, String theParamValue) 218 { 219 Element filterElement = getFilter(theFilterName); 220 if (filterElement == null) 221 { 222 throw new IllegalStateException ("Filter '" + theFilterName 223 + "' not defined"); 224 } 225 addInitParam(filterElement, theParamName, theParamValue); 226 } 227 228 234 public final void addFilterMapping(String theFilterName, 235 String theUrlPattern) 236 { 237 if (!hasFilter(theFilterName)) 238 { 239 throw new IllegalStateException ("Filter '" + theFilterName 240 + "' not defined"); 241 } 242 Element filterMappingElement = 243 this.document.createElement(WebXmlTag.FILTER_MAPPING.getTagName()); 244 filterMappingElement.appendChild( 245 createNestedText(WebXmlTag.FILTER_NAME, theFilterName)); 246 filterMappingElement.appendChild( 247 createNestedText(WebXmlTag.URL_PATTERN, theUrlPattern)); 248 addElement(WebXmlTag.FILTER_MAPPING, filterMappingElement); 249 } 250 251 259 public final Element getFilter(String theFilterName) 260 { 261 if (theFilterName == null) 262 { 263 throw new NullPointerException (); 264 } 265 Iterator filterElements = getElements(WebXmlTag.FILTER); 266 while (filterElements.hasNext()) 267 { 268 Element filterElement = (Element ) filterElements.next(); 269 if (theFilterName.equals(getNestedText( 270 filterElement, WebXmlTag.FILTER_NAME))) 271 { 272 return filterElement; 273 } 274 } 275 return null; 276 } 277 278 286 public final Element getContextParam(String theParamName) 287 { 288 if (theParamName == null) 289 { 290 throw new NullPointerException (); 291 } 292 Iterator contextParamElements = getElements(WebXmlTag.CONTEXT_PARAM); 293 while (contextParamElements.hasNext()) 294 { 295 Element contextParamElement = (Element ) contextParamElements.next(); 296 if (theParamName.equals(getNestedText( 297 contextParamElement, WebXmlTag.PARAM_NAME))) 298 { 299 return contextParamElement; 300 } 301 } 302 return null; 303 } 304 305 310 public final String getContextParamName(Element theContextParam) 311 { 312 return getNestedText(theContextParam, WebXmlTag.PARAM_NAME); 313 } 314 315 322 public final Iterator getFilterNamesForClass(String theClassName) 323 { 324 if (theClassName == null) 325 { 326 throw new NullPointerException (); 327 } 328 Iterator filterElements = getElements(WebXmlTag.FILTER); 329 List filterNames = new ArrayList (); 330 while (filterElements.hasNext()) 331 { 332 Element filterElement = (Element ) filterElements.next(); 333 if (theClassName.equals(getNestedText( 334 filterElement, WebXmlTag.FILTER_CLASS))) 335 { 336 filterNames.add(getNestedText( 337 filterElement, WebXmlTag.FILTER_NAME)); 338 } 339 } 340 return filterNames.iterator(); 341 } 342 343 350 public final String getFilterInitParam(String theFilterName, 351 String theParamName) 352 { 353 return getInitParam(getFilter(theFilterName), theParamName); 354 } 355 356 364 public final Iterator getFilterInitParamNames(String theFilterName) 365 { 366 return getInitParamNames(getFilter(theFilterName)); 367 } 368 369 378 public final Iterator getFilterMappings(String theFilterName) 379 { 380 if (theFilterName == null) 381 { 382 throw new NullPointerException (); 383 } 384 List filterMappings = new ArrayList (); 385 Iterator filterMappingElements = getElements(WebXmlTag.FILTER_MAPPING); 386 while (filterMappingElements.hasNext()) 387 { 388 Element filterMappingElement = (Element ) 389 filterMappingElements.next(); 390 if (theFilterName.equals(getNestedText( 391 filterMappingElement, WebXmlTag.FILTER_NAME))) 392 { 393 String urlPattern = getNestedText( 394 filterMappingElement, WebXmlTag.URL_PATTERN); 395 if (urlPattern != null) 396 { 397 filterMappings.add(urlPattern); 398 } 399 } 400 } 401 return filterMappings.iterator(); 402 } 403 404 410 public final Iterator getFilterNames() 411 { 412 List filterNames = new ArrayList (); 413 Iterator filterElements = getElements(WebXmlTag.FILTER); 414 while (filterElements.hasNext()) 415 { 416 Element filterElement = (Element ) filterElements.next(); 417 String filterName = 418 getNestedText(filterElement, WebXmlTag.FILTER_NAME); 419 if (filterName != null) 420 { 421 filterNames.add(filterName); 422 } 423 } 424 return filterNames.iterator(); 425 } 426 427 435 public final boolean hasContextParam(String theParamName) 436 { 437 return (getContextParam(theParamName) != null); 438 } 439 440 448 public final boolean hasFilter(String theFilterName) 449 { 450 return (getFilter(theFilterName) != null); 451 } 452 453 460 public final void addJspFile(String theServletName, String theJspFile) 461 { 462 if (theServletName == null) 463 { 464 throw new NullPointerException (); 465 } 466 if (hasFilter(theServletName)) 467 { 468 throw new IllegalStateException ("Servlet '" + theServletName 469 + "' already defined"); 470 } 471 Element servletElement = 472 this.document.createElement(WebXmlTag.SERVLET.getTagName()); 473 servletElement.appendChild( 474 createNestedText(WebXmlTag.SERVLET_NAME, theServletName)); 475 servletElement.appendChild( 476 createNestedText(WebXmlTag.JSP_FILE, theJspFile)); 477 addElement(WebXmlTag.SERVLET, servletElement); 478 } 479 480 486 public final void addServlet(String theServletName, String theServletClass) 487 { 488 if (theServletName == null) 489 { 490 throw new NullPointerException (); 491 } 492 if (hasServlet(theServletName)) 493 { 494 throw new IllegalStateException ("Servlet '" + theServletName 495 + "' already defined"); 496 } 497 Element servletElement = 498 this.document.createElement(WebXmlTag.SERVLET.getTagName()); 499 servletElement.appendChild( 500 createNestedText(WebXmlTag.SERVLET_NAME, theServletName)); 501 servletElement.appendChild( 502 createNestedText(WebXmlTag.SERVLET_CLASS, theServletClass)); 503 addElement(WebXmlTag.SERVLET, servletElement); 504 } 505 506 511 public final void addServlet(Element theServlet) 512 { 513 checkElement(theServlet, WebXmlTag.SERVLET); 514 String servletName = getNestedText(theServlet, WebXmlTag.SERVLET_NAME); 515 if (servletName == null) 516 { 517 throw new IllegalArgumentException ("Not a valid servlet element"); 518 } 519 if (hasServlet(servletName)) 520 { 521 throw new IllegalStateException ("Servlet '" + servletName 522 + "' already defined"); 523 } 524 addElement(WebXmlTag.SERVLET, theServlet); 525 } 526 527 534 public final void addServletInitParam(String theServletName, 535 String theParamName, String theParamValue) 536 { 537 Element servletElement = getServlet(theServletName); 538 if (servletElement == null) 539 { 540 throw new IllegalStateException ("Servlet '" + theServletName 541 + "' not defined"); 542 } 543 addInitParam(servletElement, theParamName, theParamValue); 544 } 545 546 552 public final void addServletMapping(String theServletName, 553 String theUrlPattern) 554 { 555 if (!hasServlet(theServletName)) 556 { 557 throw new IllegalStateException ("Servlet '" + theServletName 558 + "' not defined"); 559 } 560 Element servletMappingElement = 561 this.document.createElement(WebXmlTag.SERVLET_MAPPING.getTagName()); 562 servletMappingElement.appendChild( 563 createNestedText(WebXmlTag.SERVLET_NAME, theServletName)); 564 servletMappingElement.appendChild( 565 createNestedText(WebXmlTag.URL_PATTERN, theUrlPattern)); 566 addElement(WebXmlTag.SERVLET_MAPPING, servletMappingElement); 567 } 568 569 577 public final Element getServlet(String theServletName) 578 { 579 if (theServletName == null) 580 { 581 throw new NullPointerException (); 582 } 583 Iterator servletElements = getElements(WebXmlTag.SERVLET); 584 while (servletElements.hasNext()) 585 { 586 Element servletElement = (Element ) servletElements.next(); 587 if (theServletName.equals(getNestedText( 588 servletElement, WebXmlTag.SERVLET_NAME))) 589 { 590 return servletElement; 591 } 592 } 593 return null; 594 } 595 596 604 public final String getServletInitParam(String theServletName, 605 String theParamName) 606 { 607 return getInitParam(getServlet(theServletName), theParamName); 608 } 609 610 618 public final Iterator getServletInitParamNames(String theServletName) 619 { 620 return getInitParamNames(getServlet(theServletName)); 621 } 622 623 632 public final Iterator getServletMappings(String theServletName) 633 { 634 if (theServletName == null) 635 { 636 throw new NullPointerException (); 637 } 638 List servletMappings = new ArrayList (); 639 Iterator servletMappingElements = 640 getElements(WebXmlTag.SERVLET_MAPPING); 641 while (servletMappingElements.hasNext()) 642 { 643 Element servletMappingElement = (Element ) 644 servletMappingElements.next(); 645 if (theServletName.equals(getNestedText( 646 servletMappingElement, WebXmlTag.SERVLET_NAME))) 647 { 648 String urlPattern = getNestedText( 649 servletMappingElement, WebXmlTag.URL_PATTERN); 650 if (urlPattern != null) 651 { 652 servletMappings.add(urlPattern); 653 } 654 } 655 } 656 return servletMappings.iterator(); 657 } 658 659 665 public final Iterator getServletNames() 666 { 667 List servletNames = new ArrayList (); 668 Iterator servletElements = getElements(WebXmlTag.SERVLET); 669 while (servletElements.hasNext()) 670 { 671 Element servletElement = (Element ) servletElements.next(); 672 String servletName = 673 getNestedText(servletElement, WebXmlTag.SERVLET_NAME); 674 if (servletName != null) 675 { 676 servletNames.add(servletName); 677 } 678 } 679 return servletNames.iterator(); 680 } 681 682 689 public final Iterator getServletNamesForClass(String theClassName) 690 { 691 if (theClassName == null) 692 { 693 throw new NullPointerException (); 694 } 695 Iterator servletElements = getElements(WebXmlTag.SERVLET); 696 List servletNames = new ArrayList (); 697 while (servletElements.hasNext()) 698 { 699 Element servletElement = (Element ) servletElements.next(); 700 if (theClassName.equals(getNestedText( 701 servletElement, WebXmlTag.SERVLET_CLASS))) 702 { 703 servletNames.add(getNestedText( 704 servletElement, WebXmlTag.SERVLET_NAME)); 705 } 706 } 707 return servletNames.iterator(); 708 } 709 710 718 public final Iterator getServletNamesForJspFile(String theJspFile) 719 { 720 if (theJspFile == null) 721 { 722 throw new NullPointerException (); 723 } 724 Iterator servletElements = getElements(WebXmlTag.SERVLET); 725 List servletNames = new ArrayList (); 726 while (servletElements.hasNext()) 727 { 728 Element servletElement = (Element ) servletElements.next(); 729 if (theJspFile.equals(getNestedText( 730 servletElement, WebXmlTag.JSP_FILE))) 731 { 732 servletNames.add(getNestedText( 733 servletElement, WebXmlTag.SERVLET_NAME)); 734 } 735 } 736 return servletNames.iterator(); 737 } 738 739 747 public final boolean hasServlet(String theServletName) 748 { 749 return (getServlet(theServletName) != null); 750 } 751 752 760 public final void addSecurityConstraint(String theWebResourceName, 761 String theUrlPattern, List theRoles) 762 { 763 if ((theWebResourceName == null) || (theUrlPattern == null) 764 || (theRoles == null)) 765 { 766 throw new NullPointerException (); 767 } 768 if (hasSecurityConstraint(theUrlPattern)) 769 { 770 throw new IllegalStateException ("Security constraint for URL " 771 + "pattern " + theUrlPattern + " already defined"); 772 } 773 Element securityConstraintElement = 774 this.document.createElement( 775 WebXmlTag.SECURITY_CONSTRAINT.getTagName()); 776 Element webResourceCollectionElement = 777 this.document.createElement( 778 WebXmlTag.WEB_RESOURCE_COLLECTION.getTagName()); 779 webResourceCollectionElement.appendChild( 780 createNestedText(WebXmlTag.WEB_RESOURCE_NAME, theWebResourceName)); 781 webResourceCollectionElement.appendChild( 782 createNestedText(WebXmlTag.URL_PATTERN, theUrlPattern)); 783 securityConstraintElement.appendChild(webResourceCollectionElement); 784 Element authConstraintElement = 785 this.document.createElement(WebXmlTag.AUTH_CONSTRAINT.getTagName()); 786 for (Iterator i = theRoles.iterator(); i.hasNext();) 787 { 788 authConstraintElement.appendChild(
|