0001: /*
0002: * ========================================================================
0003: *
0004: * Copyright 2003-2005 The Apache Software Foundation.
0005: *
0006: * Licensed under the Apache License, Version 2.0 (the "License");
0007: * you may not use this file except in compliance with the License.
0008: * You may obtain a copy of the License at
0009: *
0010: * http://www.apache.org/licenses/LICENSE-2.0
0011: *
0012: * Unless required by applicable law or agreed to in writing, software
0013: * distributed under the License is distributed on an "AS IS" BASIS,
0014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015: * See the License for the specific language governing permissions and
0016: * limitations under the License.
0017: *
0018: * ========================================================================
0019: */
0020: package org.apache.cactus.integration.ant.deployment.webapp;
0021:
0022: import java.util.ArrayList;
0023: import java.util.Iterator;
0024: import java.util.List;
0025:
0026: import org.w3c.dom.Document;
0027: import org.w3c.dom.DocumentType;
0028: import org.w3c.dom.Element;
0029: import org.w3c.dom.Node;
0030: import org.w3c.dom.NodeList;
0031:
0032: /**
0033: * Encapsulates the DOM representation of a web deployment descriptor
0034: * <code>web.xml</code> to provide convenience methods for easy access and
0035: * manipulation.
0036: *
0037: * @since Cactus 1.5
0038: * @version $Id: WebXml.java 239137 2005-02-10 19:51:34Z vmassol $
0039: */
0040: public class WebXml {
0041: // Private Constants -------------------------------------------------------
0042:
0043: /**
0044: * Specifies the order in which the top-level elements must appear in the
0045: * descriptor, according to the DTD.
0046: */
0047: private static final WebXmlTag[] ELEMENT_ORDER = { WebXmlTag.ICON,
0048: WebXmlTag.DISPLAY_NAME, WebXmlTag.DESCRIPTION,
0049: WebXmlTag.DISTRIBUTABLE, WebXmlTag.FILTER,
0050: WebXmlTag.FILTER_MAPPING, WebXmlTag.LISTENER,
0051: WebXmlTag.SERVLET, WebXmlTag.SERVLET_MAPPING,
0052: WebXmlTag.SESSION_CONFIG, WebXmlTag.MIME_MAPPING,
0053: WebXmlTag.WELCOME_FILE_LIST, WebXmlTag.ERROR_PAGE,
0054: WebXmlTag.TAGLIB, WebXmlTag.RESOURCE_ENV_REF,
0055: WebXmlTag.RESOURCE_REF, WebXmlTag.SECURITY_CONSTRAINT,
0056: WebXmlTag.LOGIN_CONFIG, WebXmlTag.SECURITY_ROLE,
0057: WebXmlTag.ENV_ENTRY, WebXmlTag.EJB_REF,
0058: WebXmlTag.EJB_LOCAL_REF, };
0059:
0060: // Instance Variables ------------------------------------------------------
0061:
0062: /**
0063: * The DOM representation of the deployment descriptor.
0064: */
0065: private final Document document;
0066:
0067: /**
0068: * The root element of the descriptor.
0069: */
0070: private final Element rootElement;
0071:
0072: // Constructors ------------------------------------------------------------
0073:
0074: /**
0075: * Constructor.
0076: *
0077: * @param theDocument The DOM document representing the parsed deployment
0078: * descriptor
0079: */
0080: public WebXml(Document theDocument) {
0081: this .document = theDocument;
0082: this .rootElement = theDocument.getDocumentElement();
0083: }
0084:
0085: // Public Methods ----------------------------------------------------------
0086:
0087: /**
0088: * Returns the DOM document representing the deployment descriptor. The
0089: * document will contain any modifications made through this instance.
0090: *
0091: * @return The document representing the deploy descriptor
0092: */
0093: public final Document getDocument() {
0094: return this .document;
0095: }
0096:
0097: /**
0098: * Returns the servlet API version.
0099: *
0100: * @return The version
0101: */
0102: public final WebXmlVersion getVersion() {
0103: DocumentType docType = this .document.getDoctype();
0104: if (docType != null) {
0105: return WebXmlVersion.valueOf(docType);
0106: }
0107: return null;
0108: }
0109:
0110: /**
0111: * Adds a new servlet filter to the descriptor.
0112: *
0113: * @param theFilterName The name of the filter to add
0114: * @param theFilterClass The name of the class implementing the filter
0115: */
0116: public final void addFilter(String theFilterName,
0117: String theFilterClass) {
0118: if (theFilterName == null) {
0119: throw new NullPointerException();
0120: }
0121: if (hasFilter(theFilterName)) {
0122: throw new IllegalStateException("Filter '" + theFilterName
0123: + "' already defined");
0124: }
0125: Element filterElement = this .document
0126: .createElement(WebXmlTag.FILTER.getTagName());
0127: filterElement.appendChild(createNestedText(
0128: WebXmlTag.FILTER_NAME, theFilterName));
0129: filterElement.appendChild(createNestedText(
0130: WebXmlTag.FILTER_CLASS, theFilterClass));
0131: addElement(WebXmlTag.FILTER, filterElement);
0132: }
0133:
0134: /**
0135: * Adds a new context-param element to the descriptor.
0136: *
0137: * @param theContextParam The element representing the context-param
0138: * definition
0139: */
0140: public final void addContextParam(Element theContextParam) {
0141: checkElement(theContextParam, WebXmlTag.CONTEXT_PARAM);
0142:
0143: String paramName = getNestedText(theContextParam,
0144: WebXmlTag.PARAM_NAME);
0145: if (paramName == null) {
0146: throw new IllegalArgumentException(
0147: "Not a valid context-param name element");
0148: }
0149:
0150: String paramValue = getNestedText(theContextParam,
0151: WebXmlTag.PARAM_VALUE);
0152: if (paramValue == null) {
0153: throw new IllegalArgumentException(
0154: "Not a valid context-param value element");
0155: }
0156:
0157: if (hasContextParam(paramName)) {
0158: throw new IllegalStateException("Context param '"
0159: + paramName + "' already defined");
0160: }
0161: addElement(WebXmlTag.CONTEXT_PARAM, theContextParam);
0162: }
0163:
0164: /**
0165: * Adds a new servlet filter to the descriptor.
0166: *
0167: * @param theFilter The element representing the filter definition
0168: */
0169: public final void addFilter(Element theFilter) {
0170: checkElement(theFilter, WebXmlTag.FILTER);
0171: String filterName = getNestedText(theFilter,
0172: WebXmlTag.FILTER_NAME);
0173: if (filterName == null) {
0174: throw new IllegalArgumentException(
0175: "Not a valid filter element");
0176: }
0177: if (hasFilter(filterName)) {
0178: throw new IllegalStateException("Filter '" + filterName
0179: + "' already defined");
0180: }
0181: addElement(WebXmlTag.FILTER, theFilter);
0182: }
0183:
0184: /**
0185: * Adds an initialization parameter to the specified filter.
0186: *
0187: * @param theFilterName The name of the filter
0188: * @param theParamName The name of the parameter
0189: * @param theParamValue The parameter value
0190: */
0191: public final void addFilterInitParam(String theFilterName,
0192: String theParamName, String theParamValue) {
0193: Element filterElement = getFilter(theFilterName);
0194: if (filterElement == null) {
0195: throw new IllegalStateException("Filter '" + theFilterName
0196: + "' not defined");
0197: }
0198: addInitParam(filterElement, theParamName, theParamValue);
0199: }
0200:
0201: /**
0202: * Adds a filter mapping to the descriptor.
0203: *
0204: * @param theFilterName The name of the filter
0205: * @param theUrlPattern The URL pattern the filter should be mapped to
0206: */
0207: public final void addFilterMapping(String theFilterName,
0208: String theUrlPattern) {
0209: if (!hasFilter(theFilterName)) {
0210: throw new IllegalStateException("Filter '" + theFilterName
0211: + "' not defined");
0212: }
0213: Element filterMappingElement = this .document
0214: .createElement(WebXmlTag.FILTER_MAPPING.getTagName());
0215: filterMappingElement.appendChild(createNestedText(
0216: WebXmlTag.FILTER_NAME, theFilterName));
0217: filterMappingElement.appendChild(createNestedText(
0218: WebXmlTag.URL_PATTERN, theUrlPattern));
0219: addElement(WebXmlTag.FILTER_MAPPING, filterMappingElement);
0220: }
0221:
0222: /**
0223: * Returns the element that contains the definition of a specific servlet
0224: * filter, or <code>null</code> if a filter of the specified name is not
0225: * defined in the descriptor.
0226: *
0227: * @param theFilterName The name of the servlet filter
0228: * @return The DOM element representing the filter definition
0229: */
0230: public final Element getFilter(String theFilterName) {
0231: if (theFilterName == null) {
0232: throw new NullPointerException();
0233: }
0234: Iterator filterElements = getElements(WebXmlTag.FILTER);
0235: while (filterElements.hasNext()) {
0236: Element filterElement = (Element) filterElements.next();
0237: if (theFilterName.equals(getNestedText(filterElement,
0238: WebXmlTag.FILTER_NAME))) {
0239: return filterElement;
0240: }
0241: }
0242: return null;
0243: }
0244:
0245: /**
0246: * Returns the element that contains the definition of a specific context
0247: * param, or <code>null</code> if a context param of the specified name
0248: * is not defined in the descriptor.
0249: *
0250: * @param theParamName The context param name
0251: * @return The DOM element representing the context param definition
0252: */
0253: public final Element getContextParam(String theParamName) {
0254: if (theParamName == null) {
0255: throw new NullPointerException();
0256: }
0257: Iterator contextParamElements = getElements(WebXmlTag.CONTEXT_PARAM);
0258: while (contextParamElements.hasNext()) {
0259: Element contextParamElement = (Element) contextParamElements
0260: .next();
0261: if (theParamName.equals(getNestedText(contextParamElement,
0262: WebXmlTag.PARAM_NAME))) {
0263: return contextParamElement;
0264: }
0265: }
0266: return null;
0267: }
0268:
0269: /**
0270: * @param theContextParam the context param element from which to extract
0271: * the name
0272: * @return the name of the passed context param element
0273: */
0274: public final String getContextParamName(Element theContextParam) {
0275: return getNestedText(theContextParam, WebXmlTag.PARAM_NAME);
0276: }
0277:
0278: /**
0279: * Returns a list of names of filters that are mapped to the specified
0280: * class.
0281: *
0282: * @param theClassName The fully qualified name of the filter class
0283: * @return An iterator over the names of the filters mapped to the class
0284: */
0285: public final Iterator getFilterNamesForClass(String theClassName) {
0286: if (theClassName == null) {
0287: throw new NullPointerException();
0288: }
0289: Iterator filterElements = getElements(WebXmlTag.FILTER);
0290: List filterNames = new ArrayList();
0291: while (filterElements.hasNext()) {
0292: Element filterElement = (Element) filterElements.next();
0293: if (theClassName.equals(getNestedText(filterElement,
0294: WebXmlTag.FILTER_CLASS))) {
0295: filterNames.add(getNestedText(filterElement,
0296: WebXmlTag.FILTER_NAME));
0297: }
0298: }
0299: return filterNames.iterator();
0300: }
0301:
0302: /**
0303: * Returns the value of an initialization parameter of the specified filter.
0304: *
0305: * @param theFilterName The name of the servlet filter
0306: * @param theParamName The name of the initialization parameter
0307: * @return The parameter value
0308: */
0309: public final String getFilterInitParam(String theFilterName,
0310: String theParamName) {
0311: return getInitParam(getFilter(theFilterName), theParamName);
0312: }
0313:
0314: /**
0315: * Returns the names of the initialization parameters of the specified
0316: * servlet filter.
0317: *
0318: * @param theFilterName The name of the servlet filter of which the
0319: * parameter names should be retrieved
0320: * @return An iterator over the ordered list of parameter names
0321: */
0322: public final Iterator getFilterInitParamNames(String theFilterName) {
0323: return getInitParamNames(getFilter(theFilterName));
0324: }
0325:
0326: /**
0327: * Returns the URL-patterns that the specified filter is mapped to in an
0328: * ordered list. If there are no mappings for the specified filter, an
0329: * iterator over an empty list is returned.
0330: *
0331: * @param theFilterName The name of the servlet filter of which the
0332: * mappings should be retrieved
0333: * @return An iterator over the ordered list of URL-patterns
0334: */
0335: public final Iterator getFilterMappings(String theFilterName) {
0336: if (theFilterName == null) {
0337: throw new NullPointerException();
0338: }
0339: List filterMappings = new ArrayList();
0340: Iterator filterMappingElements = getElements(WebXmlTag.FILTER_MAPPING);
0341: while (filterMappingElements.hasNext()) {
0342: Element filterMappingElement = (Element) filterMappingElements
0343: .next();
0344: if (theFilterName.equals(getNestedText(
0345: filterMappingElement, WebXmlTag.FILTER_NAME))) {
0346: String urlPattern = getNestedText(filterMappingElement,
0347: WebXmlTag.URL_PATTERN);
0348: if (urlPattern != null) {
0349: filterMappings.add(urlPattern);
0350: }
0351: }
0352: }
0353: return filterMappings.iterator();
0354: }
0355:
0356: /**
0357: * Returns the names of all filters defined in the deployment descriptor.
0358: * The names are returned as an iterator over an ordered list.
0359: *
0360: * @return The filter names
0361: */
0362: public final Iterator getFilterNames() {
0363: List filterNames = new ArrayList();
0364: Iterator filterElements = getElements(WebXmlTag.FILTER);
0365: while (filterElements.hasNext()) {
0366: Element filterElement = (Element) filterElements.next();
0367: String filterName = getNestedText(filterElement,
0368: WebXmlTag.FILTER_NAME);
0369: if (filterName != null) {
0370: filterNames.add(filterName);
0371: }
0372: }
0373: return filterNames.iterator();
0374: }
0375:
0376: /**
0377: * Returns whether a context param by the specified name is defined in the
0378: * deployment descriptor.
0379: *
0380: * @param theParamName The name of the context param
0381: * @return <code>true</code> if the context param is defined,
0382: * <code>false</code> otherwise
0383: */
0384: public final boolean hasContextParam(String theParamName) {
0385: return (getContextParam(theParamName) != null);
0386: }
0387:
0388: /**
0389: * Returns whether a servlet filter by the specified name is defined in the
0390: * deployment descriptor.
0391: *
0392: * @param theFilterName The name of the filter
0393: * @return <code>true</code> if the filter is defined, <code>false</code>
0394: * otherwise
0395: */
0396: public final boolean hasFilter(String theFilterName) {
0397: return (getFilter(theFilterName) != null);
0398: }
0399:
0400: /**
0401: * Adds a mapped JSP file to the descriptor.
0402: *
0403: * @param theServletName The name of the servlet to add
0404: * @param theJspFile The path to the JSP file relative to the root of the
0405: * web application
0406: */
0407: public final void addJspFile(String theServletName,
0408: String theJspFile) {
0409: if (theServletName == null) {
0410: throw new NullPointerException();
0411: }
0412: if (hasFilter(theServletName)) {
0413: throw new IllegalStateException("Servlet '"
0414: + theServletName + "' already defined");
0415: }
0416: Element servletElement = this .document
0417: .createElement(WebXmlTag.SERVLET.getTagName());
0418: servletElement.appendChild(createNestedText(
0419: WebXmlTag.SERVLET_NAME, theServletName));
0420: servletElement.appendChild(createNestedText(WebXmlTag.JSP_FILE,
0421: theJspFile));
0422: addElement(WebXmlTag.SERVLET, servletElement);
0423: }
0424:
0425: /**
0426: * Adds a new servlet to the descriptor.
0427: *
0428: * @param theServletName The name of the servlet to add
0429: * @param theServletClass The name of the class implementing the servlet
0430: */
0431: public final void addServlet(String theServletName,
0432: String theServletClass) {
0433: if (theServletName == null) {
0434: throw new NullPointerException();
0435: }
0436: if (hasServlet(theServletName)) {
0437: throw new IllegalStateException("Servlet '"
0438: + theServletName + "' already defined");
0439: }
0440: Element servletElement = this .document
0441: .createElement(WebXmlTag.SERVLET.getTagName());
0442: servletElement.appendChild(createNestedText(
0443: WebXmlTag.SERVLET_NAME, theServletName));
0444: servletElement.appendChild(createNestedText(
0445: WebXmlTag.SERVLET_CLASS, theServletClass));
0446: addElement(WebXmlTag.SERVLET, servletElement);
0447: }
0448:
0449: /**
0450: * Adds a new servlet to the descriptor.
0451: *
0452: * @param theServlet The element representing the servlet definition
0453: */
0454: public final void addServlet(Element theServlet) {
0455: checkElement(theServlet, WebXmlTag.SERVLET);
0456: String servletName = getNestedText(theServlet,
0457: WebXmlTag.SERVLET_NAME);
0458: if (servletName == null) {
0459: throw new IllegalArgumentException(
0460: "Not a valid servlet element");
0461: }
0462: if (hasServlet(servletName)) {
0463: throw new IllegalStateException("Servlet '" + servletName
0464: + "' already defined");
0465: }
0466: addElement(WebXmlTag.SERVLET, theServlet);
0467: }
0468:
0469: /**
0470: * Adds an initialization parameter to the specified servlet.
0471: *
0472: * @param theServletName The name of the filter
0473: * @param theParamName The name of the parameter
0474: * @param theParamValue The parameter value
0475: */
0476: public final void addServletInitParam(String theServletName,
0477: String theParamName, String theParamValue) {
0478: Element servletElement = getServlet(theServletName);
0479: if (servletElement == null) {
0480: throw new IllegalStateException("Servlet '"
0481: + theServletName + "' not defined");
0482: }
0483: addInitParam(servletElement, theParamName, theParamValue);
0484: }
0485:
0486: /**
0487: * Adds a run-as declaration to the specified servlet.
0488: *
0489: * @param theServletName the name of the servlet to manipulate
0490: * @param theRoleName the role name that the servlet should be running as
0491: */
0492: public final void addServletRunAsRoleName(String theServletName,
0493: String theRoleName) {
0494: Element servlet = getServlet(theServletName);
0495: Element runAsElement = this .document
0496: .createElement(WebXmlTag.RUN_AS.getTagName());
0497: runAsElement.appendChild(createNestedText(WebXmlTag.ROLE_NAME,
0498: theRoleName));
0499: servlet.appendChild(runAsElement);
0500: }
0501:
0502: /**
0503: * Adds a servlet mapping to the descriptor.
0504: *
0505: * @param theServletName The name of the servlet
0506: * @param theUrlPattern The URL pattern the servlet should be mapped to
0507: */
0508: public final void addServletMapping(String theServletName,
0509: String theUrlPattern) {
0510: if (!hasServlet(theServletName)) {
0511: throw new IllegalStateException("Servlet '"
0512: + theServletName + "' not defined");
0513: }
0514: Element servletMappingElement = this .document
0515: .createElement(WebXmlTag.SERVLET_MAPPING.getTagName());
0516: servletMappingElement.appendChild(createNestedText(
0517: WebXmlTag.SERVLET_NAME, theServletName));
0518: servletMappingElement.appendChild(createNestedText(
0519: WebXmlTag.URL_PATTERN, theUrlPattern));
0520: addElement(WebXmlTag.SERVLET_MAPPING, servletMappingElement);
0521: }
0522:
0523: /**
0524: * Returns the element that contains the definition of a specific servlet,
0525: * or <code>null</code> if a servlet of the specified name is not defined
0526: * in the descriptor.
0527: *
0528: * @param theServletName The name of the servlet
0529: * @return The DOM element representing the servlet definition
0530: */
0531: public final Element getServlet(String theServletName) {
0532: if (theServletName == null) {
0533: throw new NullPointerException();
0534: }
0535: Iterator servletElements = getElements(WebXmlTag.SERVLET);
0536: while (servletElements.hasNext()) {
0537: Element servletElement = (Element) servletElements.next();
0538: if (theServletName.equals(getNestedText(servletElement,
0539: WebXmlTag.SERVLET_NAME))) {
0540: return servletElement;
0541: }
0542: }
0543: return null;
0544: }
0545:
0546: /**
0547: * Returns the value of an initialization parameter of the specified
0548: * servlet.
0549: *
0550: * @param theServletName The name of the servlet
0551: * @param theParamName The name of the initialization parameter
0552: * @return The parameter value
0553: */
0554: public final String getServletInitParam(String theServletName,
0555: String theParamName) {
0556: return getInitParam(getServlet(theServletName), theParamName);
0557: }
0558:
0559: /**
0560: * Returns the names of the initialization parameters of the specified
0561: * servlet.
0562: *
0563: * @param theServletName The name of the servlet of which the parameter
0564: * names should be retrieved
0565: * @return An iterator over the ordered list of parameter names
0566: */
0567: public final Iterator getServletInitParamNames(String theServletName) {
0568: return getInitParamNames(getServlet(theServletName));
0569: }
0570:
0571: /**
0572: * Returns the role name that the servlet is running as.
0573: *
0574: * @param theServletName The name of the servlet of which the role name
0575: * should be retrieved
0576: * @return the roleName or null if non is specified
0577: */
0578: public final String getServletRunAsRoleName(String theServletName) {
0579: if (theServletName == null) {
0580: throw new NullPointerException();
0581: }
0582: String roleName = null;
0583: Element servlet = getServlet(theServletName);
0584: NodeList nodeList = servlet
0585: .getElementsByTagName(WebXmlTag.RUN_AS.getTagName());
0586: if (nodeList != null) {
0587: Element e = (Element) nodeList.item(0);
0588: if (e != null) {
0589: roleName = getNestedText(e, WebXmlTag.ROLE_NAME);
0590: }
0591: }
0592:
0593: return roleName;
0594: }
0595:
0596: /**
0597: * Returns the URL-patterns that the specified servlet is mapped to in an
0598: * ordered list. If there are no mappings for the specified servlet, an
0599: * iterator over an empty list is returned.
0600: *
0601: * @param theServletName The name of the servlet of which the mappings
0602: * should be retrieved
0603: * @return An iterator over the ordered list of URL-patterns
0604: */
0605: public final Iterator getServletMappings(String theServletName) {
0606: if (theServletName == null) {
0607: throw new NullPointerException();
0608: }
0609: List servletMappings = new ArrayList();
0610: Iterator servletMappingElements = getElements(WebXmlTag.SERVLET_MAPPING);
0611: while (servletMappingElements.hasNext()) {
0612: Element servletMappingElement = (Element) servletMappingElements
0613: .next();
0614: if (theServletName.equals(getNestedText(
0615: servletMappingElement, WebXmlTag.SERVLET_NAME))) {
0616: String urlPattern = getNestedText(
0617: servletMappingElement, WebXmlTag.URL_PATTERN);
0618: if (urlPattern != null) {
0619: servletMappings.add(urlPattern);
0620: }
0621: }
0622: }
0623: return servletMappings.iterator();
0624: }
0625:
0626: /**
0627: * Returns the names of all servlets defined in the deployment descriptor.
0628: * The names are returned as an iterator over an ordered list.
0629: *
0630: * @return The servlet names
0631: */
0632: public final Iterator getServletNames() {
0633: List servletNames = new ArrayList();
0634: Iterator servletElements = getElements(WebXmlTag.SERVLET);
0635: while (servletElements.hasNext()) {
0636: Element servletElement = (Element) servletElements.next();
0637: String servletName = getNestedText(servletElement,
0638: WebXmlTag.SERVLET_NAME);
0639: if (servletName != null) {
0640: servletNames.add(servletName);
0641: }
0642: }
0643: return servletNames.iterator();
0644: }
0645:
0646: /**
0647: * Returns a list of names of servlets that are mapped to the specified
0648: * class.
0649: *
0650: * @param theClassName The fully qualified name of the servlet class
0651: * @return An iterator over the names of the servlets mapped to the class
0652: */
0653: public final Iterator getServletNamesForClass(String theClassName) {
0654: if (theClassName == null) {
0655: throw new NullPointerException();
0656: }
0657: Iterator servletElements = getElements(WebXmlTag.SERVLET);
0658: List servletNames = new ArrayList();
0659: while (servletElements.hasNext()) {
0660: Element servletElement = (Element) servletElements.next();
0661: if (theClassName.equals(getNestedText(servletElement,
0662: WebXmlTag.SERVLET_CLASS))) {
0663: servletNames.add(getNestedText(servletElement,
0664: WebXmlTag.SERVLET_NAME));
0665: }
0666: }
0667: return servletNames.iterator();
0668: }
0669:
0670: /**
0671: * Returns a list of names of servlets that are mapped to the specified
0672: * JSP file.
0673: *
0674: * @param theJspFile The path to the JSP file, relative to the root of the
0675: * web-application
0676: * @return An iterator over the names of the servlets mapped to the JSP file
0677: */
0678: public final Iterator getServletNamesForJspFile(String theJspFile) {
0679: if (theJspFile == null) {
0680: throw new NullPointerException();
0681: }
0682: Iterator servletElements = getElements(WebXmlTag.SERVLET);
0683: List servletNames = new ArrayList();
0684: while (servletElements.hasNext()) {
0685: Element servletElement = (Element) servletElements.next();
0686: if (theJspFile.equals(getNestedText(servletElement,
0687: WebXmlTag.JSP_FILE))) {
0688: servletNames.add(getNestedText(servletElement,
0689: WebXmlTag.SERVLET_NAME));
0690: }
0691: }
0692: return servletNames.iterator();
0693: }
0694:
0695: /**
0696: * Returns whether a servlet by the specified name is defined in the
0697: * deployment descriptor.
0698: *
0699: * @param theServletName The name of the servlet
0700: * @return <code>true</code> if the servlet is defined, <code>false</code>
0701: * otherwise
0702: */
0703: public final boolean hasServlet(String theServletName) {
0704: return (getServlet(theServletName) != null);
0705: }
0706:
0707: /**
0708: * Creates and adds a security-constraint to the descriptor.
0709: *
0710: * @param theWebResourceName The name of the web resource collection to
0711: * protect
0712: * @param theUrlPattern The URL pattern to apply the constraint to
0713: * @param theRoles The list of authorized roles
0714: */
0715: public final void addSecurityConstraint(String theWebResourceName,
0716: String theUrlPattern, List theRoles) {
0717: if ((theWebResourceName == null) || (theUrlPattern == null)
0718: || (theRoles == null)) {
0719: throw new NullPointerException();
0720: }
0721: if (hasSecurityConstraint(theUrlPattern)) {
0722: throw new IllegalStateException(
0723: "Security constraint for URL " + "pattern "
0724: + theUrlPattern + " already defined");
0725: }
0726: Element securityConstraintElement = this .document
0727: .createElement(WebXmlTag.SECURITY_CONSTRAINT
0728: .getTagName());
0729: Element webResourceCollectionElement = this .document
0730: .createElement(WebXmlTag.WEB_RESOURCE_COLLECTION
0731: .getTagName());
0732: webResourceCollectionElement.appendChild(createNestedText(
0733: WebXmlTag.WEB_RESOURCE_NAME, theWebResourceName));
0734: webResourceCollectionElement.appendChild(createNestedText(
0735: WebXmlTag.URL_PATTERN, theUrlPattern));
0736: securityConstraintElement
0737: .appendChild(webResourceCollectionElement);
0738: Element authConstraintElement = this .document
0739: .createElement(WebXmlTag.AUTH_CONSTRAINT.getTagName());
0740: for (Iterator i = theRoles.iterator(); i.hasNext();) {
0741: authConstraintElement.appendChild(createNestedText(
0742: WebXmlTag.ROLE_NAME, (String) i.next()));
0743: }
0744: securityConstraintElement.appendChild(authConstraintElement);
0745: addElement(WebXmlTag.SECURITY_CONSTRAINT,
0746: securityConstraintElement);
0747: }
0748:
0749: /**
0750: * Returns the element that contains the security constraint defined for the
0751: * specified URL pattern.
0752: *
0753: * @param theUrlPattern The URL pattern
0754: * @return The DOM element representing the security constraint
0755: */
0756: public final Element getSecurityConstraint(String theUrlPattern) {
0757: if (theUrlPattern == null) {
0758: throw new NullPointerException();
0759: }
0760: Iterator securityConstraintElements = getElements(WebXmlTag.SECURITY_CONSTRAINT);
0761: while (securityConstraintElements.hasNext()) {
0762: Element securityConstraintElement = (Element) securityConstraintElements
0763: .next();
0764: Iterator webResourceCollectionElements = getNestedElements(
0765: securityConstraintElement,
0766: WebXmlTag.WEB_RESOURCE_COLLECTION);
0767: if (webResourceCollectionElements.hasNext()) {
0768: Element webResourceCollectionElement = (Element) webResourceCollectionElements
0769: .next();
0770: if (theUrlPattern.equals(getNestedText(
0771: webResourceCollectionElement,
0772: WebXmlTag.URL_PATTERN))) {
0773: return securityConstraintElement;
0774: }
0775: }
0776: }
0777: return null;
0778: }
0779:
0780: /**
0781: * Returns whether a security constraint has been mapped to the specified
0782: * URL pattern.
0783: *
0784: * @param theUrlPattern The URL patterm
0785: * @return <code>true</code> if a security constraint is defined,
0786: * <code>false</code> otherwise
0787: */
0788: public final boolean hasSecurityConstraint(String theUrlPattern) {
0789: return (getSecurityConstraint(theUrlPattern) != null);
0790: }
0791:
0792: /**
0793: * Returns whether the descriptor has a login configuration.
0794: *
0795: * @return <code>true</code> if a login config is defined,
0796: * <code>false</code> otherwise
0797: */
0798: public final boolean hasLoginConfig() {
0799: return (getLoginConfig() != null);
0800: }
0801:
0802: /**
0803: * Returns whether the descriptor has a login configuration.
0804: *
0805: * @return <code>true</code> if a login config is defined,
0806: * <code>false</code> otherwise
0807: */
0808: public final Element getLoginConfig() {
0809: Iterator loginConfigElements = getElements(WebXmlTag.LOGIN_CONFIG);
0810: if (loginConfigElements.hasNext()) {
0811: return (Element) loginConfigElements.next();
0812: }
0813: return null;
0814: }
0815:
0816: /**
0817: * Returns the authorization method defined by the login configuration.
0818: *
0819: * @return The authorization method
0820: */
0821: public final String getLoginConfigAuthMethod() {
0822: return getNestedText(getLoginConfig(), WebXmlTag.AUTH_METHOD);
0823: }
0824:
0825: /**
0826: * Sets the login configuration.
0827: *
0828: * @param theAuthMethod The authentication method (for example, BASIC)
0829: * @param theRealmName The name of the realm
0830: */
0831: public final void setLoginConfig(String theAuthMethod,
0832: String theRealmName) {
0833: if ((theRealmName == null) || (theAuthMethod == null)) {
0834: throw new NullPointerException();
0835: }
0836: Element loginConfigElement = document
0837: .createElement(WebXmlTag.LOGIN_CONFIG.getTagName());
0838: loginConfigElement.appendChild(createNestedText(
0839: WebXmlTag.AUTH_METHOD, theAuthMethod));
0840: loginConfigElement.appendChild(createNestedText(
0841: WebXmlTag.REALM_NAME, theRealmName));
0842: replaceElement(WebXmlTag.LOGIN_CONFIG, loginConfigElement);
0843: }
0844:
0845: /**
0846: * Adds a new security role to the descriptor.
0847: *
0848: * @param theRoleName The name of the role to add
0849: */
0850: public final void addSecurityRole(String theRoleName) {
0851: if (theRoleName == null) {
0852: throw new NullPointerException();
0853: }
0854: if (hasSecurityRole(theRoleName)) {
0855: throw new IllegalStateException("Security role '"
0856: + theRoleName + "' already defined");
0857: }
0858: Element securityRoleElement = this .document
0859: .createElement(WebXmlTag.SECURITY_ROLE.getTagName());
0860: securityRoleElement.appendChild(createNestedText(
0861: WebXmlTag.ROLE_NAME, theRoleName));
0862: addElement(WebXmlTag.SECURITY_ROLE, securityRoleElement);
0863: }
0864:
0865: /**
0866: * Returns the element that contains the specified security role, or
0867: * <code>null</code> if the role is not defined in the descriptor.
0868: *
0869: * @param theRoleName The name of the role
0870: * @return The DOM element representing the security role
0871: */
0872: public final Element getSecurityRole(String theRoleName) {
0873: if (theRoleName == null) {
0874: throw new NullPointerException();
0875: }
0876: Iterator securityRoleElements = getElements(WebXmlTag.SECURITY_ROLE);
0877: while (securityRoleElements.hasNext()) {
0878: Element securityRoleElement = (Element) securityRoleElements
0879: .next();
0880: if (theRoleName.equals(getNestedText(securityRoleElement,
0881: WebXmlTag.ROLE_NAME))) {
0882: return securityRoleElement;
0883: }
0884: }
0885: return null;
0886: }
0887:
0888: /**
0889: * Returns a list of the security role names defined in the deployment
0890: * descriptor
0891: *
0892: * @return An iterator over the list of security role names, or an empty
0893: * iterator if no security roles are defined in the descriptor
0894: */
0895: public final Iterator getSecurityRoleNames() {
0896: List securityRoleNames = new ArrayList();
0897: Iterator securityRoleElements = getElements(WebXmlTag.SECURITY_ROLE);
0898: while (securityRoleElements.hasNext()) {
0899: Element securityRoleElement = (Element) securityRoleElements
0900: .next();
0901: String securityRoleName = getNestedText(
0902: securityRoleElement, WebXmlTag.ROLE_NAME);
0903: if (securityRoleName != null) {
0904: securityRoleNames.add(securityRoleName);
0905: }
0906: }
0907: return securityRoleNames.iterator();
0908: }
0909:
0910: /**
0911: * Returns whether a specific security role has been defined.
0912: *
0913: * @param theRoleName The name of the role
0914: * @return <code>true</code> if the security role is defined,
0915: * <code>false</code> otherwise
0916: */
0917: public final boolean hasSecurityRole(String theRoleName) {
0918: return (getSecurityRole(theRoleName) != null);
0919: }
0920:
0921: /**
0922: * Returns an iterator over the elements that match the specified tag.
0923: *
0924: * @param theTag The descriptor tag of which the elements should be
0925: * returned
0926: * @return An iterator over the elements matching the tag, in the order
0927: * they occur in the descriptor
0928: */
0929: public final Iterator getElements(WebXmlTag theTag) {
0930: List elements = new ArrayList();
0931: NodeList nodeList = this .rootElement
0932: .getElementsByTagName(theTag.getTagName());
0933: for (int i = 0; i < nodeList.getLength(); i++) {
0934: elements.add(nodeList.item(i));
0935: }
0936: return elements.iterator();
0937: }
0938:
0939: /**
0940: * Adds an element of the specified tag to the descriptor.
0941: *
0942: * @param theTag The descriptor tag
0943: * @param theElement The element to add
0944: */
0945: public final void addElement(WebXmlTag theTag, Element theElement) {
0946: checkElement(theElement, theTag);
0947: if (!theTag.isMultipleAllowed()
0948: && getElements(theTag).hasNext()) {
0949: throw new IllegalStateException(
0950: "The tag '"
0951: + theTag
0952: + "' may not occur more than once in the descriptor");
0953: }
0954: Node importedNode = this .document.importNode(theElement, true);
0955: Node refNode = getInsertionPointFor(theTag);
0956: this .rootElement.insertBefore(importedNode, refNode);
0957: }
0958:
0959: /**
0960: * Replaces all elements of the specified tag with the provided element.
0961: *
0962: * @param theTag The descriptor tag
0963: * @param theElement The element to replace the current elements with
0964: */
0965: public final void replaceElement(WebXmlTag theTag,
0966: Element theElement) {
0967: Iterator elements = getElements(theTag);
0968: while (elements.hasNext()) {
0969: Element element = (Element) elements.next();
0970: element.getParentNode().removeChild(element);
0971: }
0972: addElement(theTag, theElement);
0973: }
0974:
0975: // Private Methods ---------------------------------------------------------
0976:
0977: /**
0978: * Adds an initialization parameter to the specified filter or servlet.
0979: *
0980: * @param theElement The filter or servlet element to which the
0981: * initialization parameter should be added
0982: * @param theParamName The name of the parameter
0983: * @param theParamValue The parameter value
0984: */
0985: private void addInitParam(Element theElement, String theParamName,
0986: String theParamValue) {
0987: Element initParamElement = this .document
0988: .createElement(WebXmlTag.INIT_PARAM.getTagName());
0989: initParamElement.appendChild(createNestedText(
0990: WebXmlTag.PARAM_NAME, theParamName));
0991: initParamElement.appendChild(createNestedText(
0992: WebXmlTag.PARAM_VALUE, theParamValue));
0993: Iterator loadOnStartupElements = getNestedElements(theElement,
0994: WebXmlTag.LOAD_ON_STARTUP);
0995: if (loadOnStartupElements.hasNext()) {
0996: theElement.insertBefore(initParamElement,
0997: (Element) loadOnStartupElements.next());
0998: } else {
0999: theElement.appendChild(initParamElement);
1000: }
1001: }
1002:
1003: /**
1004: * Checks an element whether its name matches the specified name.
1005: *
1006: * @param theElement The element to check
1007: * @param theExpectedTag The expected tag name
1008: * @throws IllegalArgumentException If the element name doesn't match
1009: */
1010: private void checkElement(Element theElement,
1011: WebXmlTag theExpectedTag) throws IllegalArgumentException {
1012: if (!theExpectedTag.getTagName().equals(
1013: theElement.getNodeName())) {
1014: throw new IllegalArgumentException("Not a '"
1015: + theExpectedTag + "' element");
1016: }
1017: }
1018:
1019: /**
1020: * Returns an iterator over the child elements of the specified element that
1021: * match the specified tag.
1022: *
1023: * @param theParent The element of which the nested elements should be
1024: * retrieved
1025: * @param theTag The descriptor tag of which the elements should be
1026: * returned
1027: * @return An iterator over the elements matching the tag, in the order
1028: * they occur in the descriptor
1029: */
1030: private Iterator getNestedElements(Element theParent,
1031: WebXmlTag theTag) {
1032: List elements = new ArrayList();
1033: NodeList nodeList = theParent.getElementsByTagName(theTag
1034: .getTagName());
1035: for (int i = 0; i < nodeList.getLength(); i++) {
1036: elements.add(nodeList.item(i));
1037: }
1038: return elements.iterator();
1039: }
1040:
1041: /**
1042: * Creates an element that contains nested text.
1043: *
1044: * @param theTag The tag to create an instance of
1045: * @param theText The text that should be nested in the element
1046: * @return The created DOM element
1047: */
1048: private Element createNestedText(WebXmlTag theTag, String theText) {
1049: Element element = this .document.createElement(theTag
1050: .getTagName());
1051: element.appendChild(this .document.createTextNode(theText));
1052: return element;
1053: }
1054:
1055: /**
1056: * Returns the value of an initialization parameter of the specified filter
1057: * or servlet.
1058: *
1059: * @param theElement The filter or servlet element that contains the
1060: * initialization parameters
1061: * @param theParamName The name of the initialization parameter
1062: * @return The parameter value
1063: */
1064: private String getInitParam(Element theElement, String theParamName) {
1065: if (theElement != null) {
1066: NodeList initParamElements = theElement
1067: .getElementsByTagName(WebXmlTag.INIT_PARAM
1068: .getTagName());
1069: for (int i = 0; i < initParamElements.getLength(); i++) {
1070: Element initParamElement = (Element) initParamElements
1071: .item(i);
1072: String paramName = getNestedText(initParamElement,
1073: WebXmlTag.PARAM_NAME);
1074: if (theParamName.equals(paramName)) {
1075: return getNestedText(initParamElement,
1076: WebXmlTag.PARAM_VALUE);
1077: }
1078: }
1079: }
1080: return null;
1081: }
1082:
1083: /**
1084: * Returns the names of the initialization parameters of the specified
1085: * filter or servlet.
1086: *
1087: * @param theElement The filter or servlet element that contains the
1088: * initialization parameters
1089: * @return An iterator over the ordered list of parameter names
1090: */
1091: private Iterator getInitParamNames(Element theElement) {
1092: List initParamNames = new ArrayList();
1093: if (theElement != null) {
1094: NodeList initParamElements = theElement
1095: .getElementsByTagName(WebXmlTag.INIT_PARAM
1096: .getTagName());
1097: for (int i = 0; i < initParamElements.getLength(); i++) {
1098: Element initParamElement = (Element) initParamElements
1099: .item(i);
1100: String paramName = getNestedText(initParamElement,
1101: WebXmlTag.PARAM_NAME);
1102: if (paramName != null) {
1103: initParamNames.add(paramName);
1104: }
1105: }
1106: }
1107: return initParamNames.iterator();
1108: }
1109:
1110: /**
1111: * Returns the node before which the specified tag should be inserted, or
1112: * <code>null</code> if the node should be inserted at the end of the
1113: * descriptor.
1114: *
1115: * @param theTag The tag that should be inserted
1116: * @return The node before which the tag can be inserted
1117: */
1118: private Node getInsertionPointFor(WebXmlTag theTag) {
1119: for (int i = 0; i < ELEMENT_ORDER.length; i++) {
1120: if (ELEMENT_ORDER[i] == theTag) {
1121: for (int j = i + 1; j < ELEMENT_ORDER.length; j++) {
1122: NodeList elements = this .rootElement
1123: .getElementsByTagName(ELEMENT_ORDER[j]
1124: .getTagName());
1125: if (elements.getLength() > 0) {
1126: Node result = elements.item(0);
1127: Node previous = result.getPreviousSibling();
1128: while ((previous != null)
1129: && ((previous.getNodeType() == Node.COMMENT_NODE) || (previous
1130: .getNodeType() == Node.TEXT_NODE))) {
1131: result = previous;
1132: previous = result.getPreviousSibling();
1133: }
1134: return result;
1135: }
1136: }
1137: break;
1138: }
1139: }
1140: return null;
1141: }
1142:
1143: /**
1144: * Returns the text nested inside a child element of the specified element.
1145: *
1146: * @param theElement The element of which the nested text should be
1147: * returned
1148: * @param theTag The descriptor tag in which the text is nested
1149: * @return The text nested in the element
1150: */
1151: private String getNestedText(Element theElement, WebXmlTag theTag) {
1152: NodeList nestedElements = theElement
1153: .getElementsByTagName(theTag.getTagName());
1154: if (nestedElements.getLength() > 0) {
1155: Node nestedText = nestedElements.item(0).getFirstChild();
1156: if (nestedText != null) {
1157: return nestedText.getNodeValue();
1158: }
1159: }
1160: return null;
1161: }
1162:
1163: }
|