Class WebXmlUtils


  • public class WebXmlUtils
    extends Object
    Utilities to extract data from a web.xml file.
    Author:
    Guus der Kinderen, guus.der.kinderen@gmail.com
    • Constructor Detail

      • WebXmlUtils

        public WebXmlUtils()
    • Method Detail

      • asDocument

        public static org.dom4j.Document asDocument​(File webXML)
                                             throws org.dom4j.DocumentException,
                                                    SAXException
        Throws:
        org.dom4j.DocumentException
        SAXException
      • getServletNames

        public static List<String> getServletNames​(org.dom4j.Document webXml)
        Retrieves the names of all servlets from a web.xml document. Returns a list that contains 'sessioncreate' and 'sessiondestroy' from this web.xml document: <servlet> <servlet-name>sessioncreate</servlet-name> <servlet-class>SessionCreateServlet</servlet-class> </servlet> <servlet> <servlet-name>sessiondestroy</servlet-name> <servlet-class>SessionDestroyServlet</servlet-class> </servlet>
        Parameters:
        webXml - web.xml document, parsed as XML (cannot be null)
        Returns:
        The name of the filter class, or null when no such class was defined.
      • getFilterNames

        public static List<String> getFilterNames​(org.dom4j.Document webXml)
        Retrieves the names of all filters from a web.xml document. Returns a list that contains 'message' from this web.xml document: <filter> <filter-name>message</filter-name> <filter-class>com.acme.filter.MessageFilter</filter-class> </filter>
        Parameters:
        webXml - web.xml document, parsed as XML (cannot be null)
        Returns:
        The name of the filter class, or null when no such class was defined.
      • getServletClassName

        public static String getServletClassName​(org.dom4j.Document webXml,
                                                 String servletName)
        Retrieves the class name for a particular servlet from a web.xml document. Returns 'SessionCreateServlet' for 'sessioncreate' of this web.xml document: <servlet> <servlet-name>sessioncreate</servlet-name> <servlet-class>SessionCreateServlet</servlet-class> </servlet> <servlet> <servlet-name>sessiondestroy</servlet-name> <servlet-class>SessionDestroyServlet</servlet-class> </servlet>
        Parameters:
        webXml - web.xml document, parsed as XML (cannot be null)
        servletName - The name of the servlet (cannot be null or empty).
        Returns:
        The name of the filter class, or null when no such class was defined.
      • getFilterClassName

        public static String getFilterClassName​(org.dom4j.Document webXml,
                                                String filterName)
        Retrieves the class name for a particular filter from a web.xml document. Returns 'com.acme.filter.MessageFilter' for 'message' of this web.xml document: <filter> <filter-name>message</filter-name> <filter-class>com.acme.filter.MessageFilter</filter-class> </filter>
        Parameters:
        webXml - web.xml document, parsed as XML (cannot be null)
        filterName - The name of the filter (cannot be null or empty).
        Returns:
        The name of the filter class, or null when no such class was defined.
      • getServletInitParams

        public static Map<String,​String> getServletInitParams​(org.dom4j.Document webXml,
                                                                    String servletName)
        Retrieves a map of init param/values for a particular servlet from a web.xml document. For filter 'message' of this web.xml document, returns a map with two entries: foo-> bar, test->value. <servlet> <servlet-name>sessioncreate</servlet-name> <servlet-class>SessionCreateServlet</servlet-class> <init-param> <param-name>foo</param-name> <param-value>bar</param-value> </init-param> <init-param> <param-name>test</param-name> <param-value>value</param-value> </init-param> </servlet> Parameters with no or empty name are ignored. When multiple parameters have the same name, only one of them is returned.
        Parameters:
        webXml - web.xml document, parsed as XML (cannot be null)
        servletName - The name of the servlet (cannot be null or empty).
        Returns:
        A map (possibly empty, but never null).
      • getFilterInitParams

        public static Map<String,​String> getFilterInitParams​(org.dom4j.Document webXml,
                                                                   String filterName)
        Retrieves a map of init param/values for a particular filter from a web.xml document. For filter 'message' of this web.xml document, returns a map with two entries: foo-> bar, test->value. <filter> <filter-name>message</filter-name> <filter-class>com.acme.filter.MessageFilter</filter-class> <init-param> <param-name>foo</param-name> <param-value>bar</param-value> </init-param> <init-param> <param-name>test</param-name> <param-value>value</param-value> </init-param> </filter> Parameters with no or empty name are ignored. When multiple parameters have the same name, only one of them is returned.
        Parameters:
        webXml - web.xml document, parsed as XML (cannot be null)
        filterName - The name of the filter (cannot be null or empty).
        Returns:
        A map (possibly empty, but never null).
      • getServletUrlPatterns

        public static Set<String> getServletUrlPatterns​(org.dom4j.Document webXml,
                                                        String servletName)
        Retrieves all URL patterns that apply to a specific servlet.
        Parameters:
        webXml - web.xml document, parsed as XML (cannot be null)
        servletName - The name of the servlet (cannot be null or empty).
        Returns:
        A collection (possibly empty, but never null).
      • getFilterUrlPatterns

        public static Set<String> getFilterUrlPatterns​(org.dom4j.Document webXml,
                                                       String filterName)
        Retrieves all URL patterns that apply to a specific filter.
        Parameters:
        webXml - web.xml document, parsed as XML (cannot be null)
        filterName - The name of the filter (cannot be null or empty).
        Returns:
        A collection (possibly empty, but never null).