KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > webapp > webxml > WebXmlParser


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25 import org.w3c.dom.NodeList JavaDoc;
26 import org.xml.sax.EntityResolver JavaDoc;
27 import org.xml.sax.InputSource JavaDoc;
28
29 import javax.faces.FacesException;
30 import javax.faces.context.ExternalContext;
31 import javax.xml.parsers.DocumentBuilder JavaDoc;
32 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35
36 /**
37  * @author Manfred Geiler (latest modification by $Author: mmarinschek $)
38  * @version $Revision: 1.6 $ $Date: 2004/10/27 11:47:26 $
39  */

40 public class WebXmlParser
41 {
42     private static final Log log = LogFactory.getLog(WebXmlParser.class);
43
44     /*
45     private static final String JAXP_SCHEMA_LANGUAGE =
46         "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
47     private static final String W3C_XML_SCHEMA =
48         "http://www.w3.org/2001/XMLSchema";
49         */

50
51     private static final String JavaDoc WEB_XML_PATH = "/WEB-INF/web.xml";
52     private static final String JavaDoc DEFAULT_ENCODING = "ISO-8859-1";
53
54     private static final String JavaDoc WEB_APP_2_2_J2EE_SYSTEM_ID = "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd";
55     private static final String JavaDoc WEB_APP_2_2_SYSTEM_ID = "http://java.sun.com/dtd/web-app_2_2.dtd";
56     private static final String JavaDoc WEB_APP_2_2_RESOURCE = "javax/servlet/resources/web-app_2_2.dtd";
57
58     private static final String JavaDoc WEB_APP_2_3_SYSTEM_ID = "http://java.sun.com/dtd/web-app_2_3.dtd";
59     private static final String JavaDoc 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 JavaDoc dbf = DocumentBuilderFactory.newInstance();
77             dbf.setIgnoringElementContentWhitespace(true);
78             dbf.setIgnoringComments(true);
79             dbf.setNamespaceAware(true);
80 // dbf.setValidating(true); // Needed false to allow use of version 2.4 spec (there should be a way to do this better)
81
// dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
82

83             DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
84             db.setEntityResolver(new _EntityResolver());
85             db.setErrorHandler(new MyFacesErrorHandler(log));
86
87             InputSource JavaDoc is = createContextInputSource(null, WEB_XML_PATH);
88
89             Document JavaDoc document = db.parse(is);
90
91             Element JavaDoc 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 JavaDoc e)
103         {
104             log.fatal("Unable to parse web.xml", e);
105             throw new FacesException(e);
106         }
107     }
108
109
110     private InputSource JavaDoc createContextInputSource(String JavaDoc publicId, String JavaDoc systemId)
111     {
112         InputStream JavaDoc inStream = _context.getResourceAsStream(systemId);
113         if (inStream == null)
114         {
115             // there is no such entity
116
return null;
117         }
118         InputSource JavaDoc is = new InputSource JavaDoc(inStream);
119         is.setPublicId(publicId);
120         is.setSystemId(systemId);
121         is.setEncoding(DEFAULT_ENCODING);
122         return is;
123     }
124
125     private InputSource JavaDoc createClassloaderInputSource(String JavaDoc publicId, String JavaDoc systemId)
126     {
127         InputStream JavaDoc inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(systemId);
128         if (inStream == null)
129         {
130             // there is no such entity
131
return null;
132         }
133         InputSource JavaDoc is = new InputSource JavaDoc(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 JavaDoc
141     {
142         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws IOException JavaDoc
143         {
144             if (systemId == null)
145             {
146                 throw new UnsupportedOperationException JavaDoc("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                 //Load DTD from servlet.jar
153
return createClassloaderInputSource(publicId, WebXmlParser.WEB_APP_2_2_RESOURCE);
154             }
155             else if (systemId.equals(WebXmlParser.WEB_APP_2_3_SYSTEM_ID))
156             {
157                 //Load DTD from servlet.jar
158
return createClassloaderInputSource(publicId, WebXmlParser.WEB_APP_2_3_RESOURCE);
159             }
160             else
161             {
162                 //Load additional entities from web context
163
return createContextInputSource(publicId, systemId);
164             }
165         }
166
167     }
168
169
170     private void readWebApp(Element JavaDoc webAppElem)
171     {
172         NodeList JavaDoc nodeList = webAppElem.getChildNodes();
173         for (int i = 0, len = nodeList.getLength(); i < len; i++)
174         {
175             Node JavaDoc n = nodeList.item(i);
176             if (n.getNodeType() == Node.ELEMENT_NODE)
177             {
178                 if (n.getNodeName().equals("servlet"))
179                 {
180                     readServlet((Element JavaDoc)n);
181                 }
182                 if (n.getNodeName().equals("servlet-mapping"))
183                 {
184                     readServletMapping((Element JavaDoc)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 JavaDoc servletElem)
195     {
196         String JavaDoc servletName = null;
197         String JavaDoc servletClass = null;
198         NodeList JavaDoc nodeList = servletElem.getChildNodes();
199         for (int i = 0, len = nodeList.getLength(); i < len; i++)
200         {
201             Node JavaDoc 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 JavaDoc)n);
207                 }
208                 else if (n.getNodeName().equals("servlet-class"))
209                 {
210                     servletClass = XmlUtils.getElementText((Element JavaDoc)n).trim();
211                 }
212                 else if (n.getNodeName().equals("description") || n.getNodeName().equals("load-on-startup"))
213                 {
214                     //ignore
215
}
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 JavaDoc servletMappingElem)
231     {
232         String JavaDoc servletName = null;
233         String JavaDoc urlPattern = null;
234         NodeList JavaDoc nodeList = servletMappingElem.getChildNodes();
235         for (int i = 0, len = nodeList.getLength(); i < len; i++)
236         {
237             Node JavaDoc 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 JavaDoc)n);
243                 }
244                 else if (n.getNodeName().equals("url-pattern"))
245                 {
246                     urlPattern = org.apache.myfaces.util.xml.XmlUtils.getElementText((Element JavaDoc)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