001: package com.quadcap.http.server22;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.Reader;
042: import java.io.IOException;
043:
044: import java.util.Hashtable;
045:
046: import javax.servlet.ServletException;
047:
048: import org.xml.sax.AttributeList;
049: import org.xml.sax.DocumentHandler;
050: import org.xml.sax.ErrorHandler;
051: import org.xml.sax.InputSource;
052: import org.xml.sax.Parser;
053: import org.xml.sax.Locator;
054: import org.xml.sax.SAXException;
055: import org.xml.sax.SAXParseException;
056:
057: import org.xml.sax.helpers.ParserFactory;
058:
059: import com.quadcap.text.sax.Handler;
060: import com.quadcap.util.Debug;
061:
062: /**
063: * Parser for Servlet 2.2 Web Deployment Descriptor.
064: *
065: * @author Stan Bailes
066: */
067: public class DDParser extends Handler {
068: WebApplication app;
069: WebServlet servlet;
070: int state = INIT;
071:
072: final static int INIT = 0;
073: final static int APP = 1;
074: final static int SERVLET = 2;
075:
076: /**
077: * Construct a new deployment descriptor parser
078: */
079: public DDParser() throws SAXException {
080: super ();
081: }
082:
083: /**
084: * Parse the specified deployment descriptor in the context of the
085: * specified web application.
086: *
087: * @param dd the inputstream containing the deployment descriptor
088: * @param app the web application being constructed
089: */
090: public void parse(Reader dd, WebApplication app)
091: throws SAXException {
092: this .app = app;
093: this .state = INIT;
094: super .parse(dd, app);
095: }
096:
097: /**
098: * SAX parser callback function called for the end of an element.
099: *
100: * @param name the name of this element
101: * @exception SAXException may be thrown
102: */
103: /*{com.quadcap.http.server22.DDParser.xml}
104: *
105: * <el><name>web-app</name>
106: * <p>The <b>web-app</b> element is the root element of the
107: * document used to describe and configure a web application.</p>
108: * <el><name>context-param</name>
109: * <p>Application initialization parameters, available via
110: * <code>ServletContext.getInitParameter(String)</code>
111: * are specified here.</p>
112: * <el><name>param-name</name>
113: * <p>Specifies the name of an initialization parameter.</p>
114: * </el>
115: * <el><name>param-value</name>
116: * <p>Specifies the value of an initialization parameter.</p>
117: * </el>
118: * <el><name>session-timeout</name>
119: * <p>Specifies the time period after which inactive sessions
120: * will be closed.</p>
121: * </el>
122: * <el><name>servlet-mapping</name>
123: * <p>Used to tell the container how to map incoming requests
124: * to servlets, based on exact path match, prefix path match,
125: * or extension match.</p>
126: * <el><name>url-pattern</name>
127: * <p>Specifies the pattern to match.</p>
128: * </el>
129: * <el><name>servlet-name</name>
130: * <p>Specifies the name of the servlet to invoke on URIs
131: * which match the specified pattern.</p>
132: * </el>
133: * </el>
134: * <el><name>mime-mapping</name>
135: * <p> Used to tell the container which MIME types are
136: * associated
137: * with particular file extensions.</p>
138: * <el><name>extension</name>
139: * <p>The file name extension (without the '.')</p>
140: * </el>
141: * <el><name>mime-type</name>
142: * <p>The MIME type to associate with the specified file
143: * extension.</p>
144: * </el>
145: * </el>
146: * <el><name>display-name</name>
147: * <p>The name associated with the Web Application when
148: * displayed in the administrative UI.</p>
149: * </el>
150: * <el><name>error-page</name>
151: * <p>The page to be invoked if exceptions are thrown by
152: * servlets in the application</p>
153: * </el>
154: * <el><name>servlet</name>
155: * <p>Define a servlet to be part of this Web Application</p>
156: * <el><name>servlet-name</name>
157: * <p>Specify the name of the servlet. This name isn't
158: * really significant except in that it uniquely identifies
159: * the servlet for the purpose of references to the
160: * servlet in this document (e.g.
161: * <b>web-app/servlet-mapping/servlet-name</b>)</p>
162: * </el>
163: * <el><name>servlet-class</name>
164: * <p>Specify the name of the Java class which implements
165: * the servlet.</p>
166: * </el>
167: * <el><name>init-param</name>
168: * <p>Specify servlet initialization parameters, accessible
169: * to the servlet via
170: * <code>ServletConfig.getInitParameter()</code></p>
171: * <el><name>param-name</name>
172: * <p>Specifies the name of an initialization
173: * parameter.</p>
174: * </el>
175: * <el><name>param-value</name>
176: * <p>Specifies the value of an initialization
177: * parameter.</p>
178: * </el>
179: * </el>
180: * <el><name>load-on-startup</name>
181: * <p>If specified, this causes the servlet to be
182: * initialized
183: * when the web application is loaded, in the order
184: * specified by the integer value of this element, lower
185: * numbers being loaded first.</p>
186: * </el>
187: * </el>
188: * </el>
189: * </el>
190: */
191: public void endElement(String name) throws SAXException {
192: switch (state) {
193: case SERVLET:
194: if (name.equals("servlet")) {
195: app.addServlet(servlet);
196: servlet = null;
197: state = APP;
198: } else if (name.equals("servlet-name")) {
199: servlet.setServletName(consumeData());
200: } else if (name.equals("servlet-class")) {
201: servlet.setServletClass(consumeData());
202: } else if (name.equals("init-param")) {
203: servlet.addInitParam(consume("param-name"),
204: consume("param-value"));
205: } else if (name.equals("load-on-startup")) {
206: servlet.setLoadOnStartup(Integer
207: .parseInt(consumeData()));
208: } else {
209: env.put(name, consumeData());
210: }
211: break;
212: case APP:
213: if (name.equals("context-param")) {
214: app.addInitParam(consume("param-name"),
215: consume("param-value"));
216: } else if (name.equals("session-timeout")) {
217: app.setSessionTimeout(60 * Integer
218: .parseInt(consumeData()));
219: } else if (name.equals("context-param")) {
220: app.addInitParam(consume("param-name"),
221: consume("param-value"));
222: } else if (name.equals("servlet-mapping")) {
223: app.addServletMapping(consume("servlet-name"),
224: consume("url-pattern"));
225: } else if (name.equals("error-page")) {
226: app.setErrorPage(consumeData());
227: } else if (name.equals("mime-mapping")) {
228: app.addMimeMapping(consume("extension"),
229: consume("mime-type"));
230: } else if (name.equals("display-name")) {
231: app.setDisplayName(consumeData());
232: } else if (name.equals("welcome-file")) {
233: app.addWelcomeFile(consumeData());
234: } else {
235: env.put(name, consumeData());
236: }
237: break;
238: }
239: }
240:
241: /**
242: * SAX parser callback for the start of an element.
243: *
244: * @param name the element name
245: * @param attrs the element's attributes
246: *
247: * @exception SAXException may be thrown
248: */
249: public void startElement(String name, AttributeList attrs)
250: throws SAXException {
251: data.setLength(0);
252: if (name.equals("servlet")) {
253: servlet = new WebServlet();
254: servlet.setWebApplication(app);
255: state = SERVLET;
256: } else if (name.equals("web-app")) {
257: state = APP;
258: }
259: }
260:
261: }
|