1 16 package org.apache.myfaces.webapp.webxml; 17 18 import org.apache.myfaces.util.xml.MyFacesErrorHandler; 19 import org.apache.myfaces.util.xml.XmlUtils; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.w3c.dom.Document ; 23 import org.w3c.dom.Element ; 24 import org.w3c.dom.Node ; 25 import org.w3c.dom.NodeList ; 26 import org.xml.sax.EntityResolver ; 27 import org.xml.sax.InputSource ; 28 29 import javax.faces.FacesException; 30 import javax.faces.context.ExternalContext; 31 import javax.xml.parsers.DocumentBuilder ; 32 import javax.xml.parsers.DocumentBuilderFactory ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 36 40 public class WebXmlParser 41 { 42 private static final Log log = LogFactory.getLog(WebXmlParser.class); 43 44 50 51 private static final String WEB_XML_PATH = "/WEB-INF/web.xml"; 52 private static final String DEFAULT_ENCODING = "ISO-8859-1"; 53 54 private static final String WEB_APP_2_2_J2EE_SYSTEM_ID = "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"; 55 private static final String WEB_APP_2_2_SYSTEM_ID = "http://java.sun.com/dtd/web-app_2_2.dtd"; 56 private static final String WEB_APP_2_2_RESOURCE = "javax/servlet/resources/web-app_2_2.dtd"; 57 58 private static final String WEB_APP_2_3_SYSTEM_ID = "http://java.sun.com/dtd/web-app_2_3.dtd"; 59 private static final String WEB_APP_2_3_RESOURCE = "javax/servlet/resources/web-app_2_3.dtd"; 60 61 62 private ExternalContext _context; 63 private WebXml _webXml; 64 65 public WebXmlParser(ExternalContext context) 66 { 67 _context = context; 68 } 69 70 public WebXml parse() 71 { 72 _webXml = new WebXml(); 73 74 try 75 { 76 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 77 dbf.setIgnoringElementContentWhitespace(true); 78 dbf.setIgnoringComments(true); 79 dbf.setNamespaceAware(true); 80 83 DocumentBuilder db = dbf.newDocumentBuilder(); 84 db.setEntityResolver(new _EntityResolver()); 85 db.setErrorHandler(new MyFacesErrorHandler(log)); 86 87 InputSource is = createContextInputSource(null, WEB_XML_PATH); 88 89 Document document = db.parse(is); 90 91 Element webAppElem = document.getDocumentElement(); 92 if (webAppElem == null || 93 !webAppElem.getNodeName().equals("web-app")) 94 { 95 throw new FacesException("No valid web-app root element found!"); 96 } 97 98 readWebApp(webAppElem); 99 100 return _webXml; 101 } 102 catch (Exception e) 103 { 104 log.fatal("Unable to parse web.xml", e); 105 throw new FacesException(e); 106 } 107 } 108 109 110 private InputSource createContextInputSource(String publicId, String systemId) 111 { 112 InputStream inStream = _context.getResourceAsStream(systemId); 113 if (inStream == null) 114 { 115 return null; 117 } 118 InputSource is = new InputSource (inStream); 119 is.setPublicId(publicId); 120 is.setSystemId(systemId); 121 is.setEncoding(DEFAULT_ENCODING); 122 return is; 123 } 124 125 private InputSource createClassloaderInputSource(String publicId, String systemId) 126 { 127 InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(systemId); 128 if (inStream == null) 129 { 130 return null; 132 } 133 InputSource is = new InputSource (inStream); 134 is.setPublicId(publicId); 135 is.setSystemId(systemId); 136 is.setEncoding(DEFAULT_ENCODING); 137 return is; 138 } 139 140 private class _EntityResolver implements EntityResolver 141 { 142 public InputSource resolveEntity(String publicId, String systemId) throws IOException 143 { 144 if (systemId == null) 145 { 146 throw new UnsupportedOperationException ("systemId must not be null"); 147 } 148 149 if (systemId.equals(WebXmlParser.WEB_APP_2_2_SYSTEM_ID) || 150 systemId.equals(WebXmlParser.WEB_APP_2_2_J2EE_SYSTEM_ID)) 151 { 152 return createClassloaderInputSource(publicId, WebXmlParser.WEB_APP_2_2_RESOURCE); 154 } 155 else if (systemId.equals(WebXmlParser.WEB_APP_2_3_SYSTEM_ID)) 156 { 157 return createClassloaderInputSource(publicId, WebXmlParser.WEB_APP_2_3_RESOURCE); 159 } 160 else 161 { 162 return createContextInputSource(publicId, systemId); 164 } 165 } 166 167 } 168 169 170 private void readWebApp(Element webAppElem) 171 { 172 NodeList nodeList = webAppElem.getChildNodes(); 173 for (int i = 0, len = nodeList.getLength(); i < len; i++) 174 { 175 Node n = nodeList.item(i); 176 if (n.getNodeType() == Node.ELEMENT_NODE) 177 { 178 if (n.getNodeName().equals("servlet")) 179 { 180 readServlet((Element )n); 181 } 182 if (n.getNodeName().equals("servlet-mapping")) 183 { 184 readServletMapping((Element )n); 185 } 186 } 187 else 188 { 189 if (log.isDebugEnabled()) log.debug("Ignored node '" + n.getNodeName() + "' of type " + n.getNodeType()); 190 } 191 } 192 } 193 194 private void readServlet(Element servletElem) 195 { 196 String servletName = null; 197 String servletClass = null; 198 NodeList nodeList = servletElem.getChildNodes(); 199 for (int i = 0, len = nodeList.getLength(); i < len; i++) 200 { 201 Node n = nodeList.item(i); 202 if (n.getNodeType() == Node.ELEMENT_NODE) 203 { 204 if (n.getNodeName().equals("servlet-name")) 205 { 206 servletName = XmlUtils.getElementText((Element )n); 207 } 208 else if (n.getNodeName().equals("servlet-class")) 209 { 210 servletClass = XmlUtils.getElementText((Element )n).trim(); 211 } 212 else if (n.getNodeName().equals("description") || n.getNodeName().equals("load-on-startup")) 213 { 214 } 216 else 217 { 218 if (log.isWarnEnabled()) log.warn("Ignored element '" + n.getNodeName() + "' as child of '" + servletElem.getNodeName() + "'."); 219 } 220 } 221 else 222 { 223 if (log.isDebugEnabled()) log.debug("Ignored node '" + n.getNodeName() + "' of type " + n.getNodeType()); 224 } 225 } 226 _webXml.addServlet(servletName, servletClass); 227 } 228 229 230 private void readServletMapping(Element servletMappingElem) 231 { 232 String servletName = null; 233 String urlPattern = null; 234 NodeList nodeList = servletMappingElem.getChildNodes(); 235 for (int i = 0, len = nodeList.getLength(); i < len; i++) 236 { 237 Node n = nodeList.item(i); 238 if (n.getNodeType() == Node.ELEMENT_NODE) 239 { 240 if (n.getNodeName().equals("servlet-name")) 241 { 242 servletName = org.apache.myfaces.util.xml.XmlUtils.getElementText((Element )n); 243 } 244 else if (n.getNodeName().equals("url-pattern")) 245 { 246 urlPattern = org.apache.myfaces.util.xml.XmlUtils.getElementText((Element )n).trim(); 247 } 248 else 249 { 250 if (log.isWarnEnabled()) log.warn("Ignored element '" + n.getNodeName() + "' as child of '" + servletMappingElem.getNodeName() + "'."); 251 } 252 } 253 else 254 { 255 if (log.isDebugEnabled()) log.debug("Ignored node '" + n.getNodeName() + "' of type " + n.getNodeType()); 256 } 257 } 258 urlPattern = urlPattern.trim(); 259 _webXml.addServletMapping(servletName, urlPattern); 260 } 261 262 } 263 | Popular Tags |