1 package net.trajano.twiff.internal.webxml; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.ArrayList; 6 import java.util.Collections; 7 import java.util.HashMap; 8 import java.util.List; 9 import java.util.Map; 10 import javax.servlet.ServletConfig; 11 import org.apache.commons.digester.Digester; 12 import org.xml.sax.SAXException; 13 14 /*** 15 * This processes the web.xml file. It is used to get the servlet mappings. 16 * 17 * @author Archimedes Trajano 18 */ 19 public final class WebXmlAdapter { 20 /*** 21 * Map of servlet classes to sevlet names. 22 */ 23 private final Map<String, List<String>> servletClassToNameMap; 24 25 /*** 26 * Map of servlet names to sevlet mappings. 27 */ 28 private final Map<String, List<String>> servletNameToMappingMap; 29 30 /*** 31 * Constructs the adapter from the <code>web.xml</code> file in the 32 * servlet context used by the servlet config. 33 * 34 * @param servletConfig 35 * @throws IOException 36 * @throws SAXException 37 */ 38 public WebXmlAdapter(final ServletConfig servletConfig) throws IOException, SAXException { 39 this(servletConfig.getServletContext().getResourceAsStream("/WEB-INF/web.xml")); 40 } 41 42 /*** 43 * Constructs the adapter using the specified web.xml location. 44 * 45 * @param webXml 46 * web.xml input stream 47 * @throws SAXException 48 * @throws IOException 49 */ 50 public WebXmlAdapter(final InputStream webXml) throws IOException, SAXException { 51 servletClassToNameMap = new HashMap<String, List<String>>(); 52 servletNameToMappingMap = new HashMap<String, List<String>>(); 53 Digester digester = new Digester(); 54 digester.setValidating(false); 55 digester.setNamespaceAware(false); 56 digester.push(this); 57 digester.addCallMethod("web-app/servlet", "addServlet", 2); 58 digester.addCallParam("web-app/servlet/servlet-name", 0); 59 digester.addCallParam("web-app/servlet/servlet-class", 1); 60 digester.addCallMethod("web-app/servlet-mapping", "addServletMapping", 2); 61 digester.addCallParam("web-app/servlet-mapping/servlet-name", 0); 62 digester.addCallParam("web-app/servlet-mapping/url-pattern", 1); 63 digester.parse(webXml); 64 } 65 66 /*** 67 * @param servletName 68 * @param servletClass 69 */ 70 public void addServlet(final String servletName, final String servletClass) { 71 List<String> list = servletClassToNameMap.get(servletClass); 72 if (list == null) { 73 list = new ArrayList<String>(); 74 servletClassToNameMap.put(servletClass, list); 75 } 76 list.add(servletName); 77 } 78 79 /*** 80 * @param servletName 81 * @param mapping 82 */ 83 public void addServletMapping(final String servletName, final String mapping) { 84 List<String> list = servletNameToMappingMap.get(servletName); 85 if (list == null) { 86 list = new ArrayList<String>(); 87 servletNameToMappingMap.put(servletName, list); 88 } 89 list.add(mapping); 90 } 91 92 /*** 93 * This gets the mappings used by a servlet by class. It will get all the 94 * mappings for any specified servlet that uses the class. 95 * 96 * @param servletClass 97 * name of a servlet class 98 * @return array of mappings for a servlet. 99 */ 100 public List<String> getMappingsForServlet(final Class servletClass) { 101 List<String> list = getServletNamesForClass(servletClass); 102 List<String> returnList = new ArrayList(list.size()); 103 for (String name : list) { 104 returnList.addAll(getMappingsForServletName(name)); 105 } 106 return Collections.unmodifiableList(returnList); 107 } 108 109 /*** 110 * This gets the first mapping used by a servlet by class. 111 * 112 * @param servletClass 113 * name of a servlet class 114 * @return mapping for a servlet. 115 */ 116 public String getMappingForServlet(final Class servletClass) { 117 return getMappingsForServlet(servletClass).get(0); 118 } 119 120 /*** 121 * @param servletName 122 * @return array of mappings for a servlet. 123 */ 124 public List<String> getMappingsForServletName(final String servletName) { 125 return Collections.unmodifiableList(servletNameToMappingMap.get(servletName)); 126 } 127 128 /*** 129 * @param servletClass 130 * @return array of servlet names. 131 */ 132 public List<String> getServletNamesForClass(final Class servletClass) { 133 return Collections.unmodifiableList(servletClassToNameMap.get(servletClass.getName())); 134 } 135 }