KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > server22 > DDParser


1 package com.quadcap.http.server22;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.Reader JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 import java.util.Hashtable JavaDoc;
45
46 import javax.servlet.ServletException JavaDoc;
47
48 import org.xml.sax.AttributeList JavaDoc;
49 import org.xml.sax.DocumentHandler JavaDoc;
50 import org.xml.sax.ErrorHandler JavaDoc;
51 import org.xml.sax.InputSource JavaDoc;
52 import org.xml.sax.Parser JavaDoc;
53 import org.xml.sax.Locator JavaDoc;
54 import org.xml.sax.SAXException JavaDoc;
55 import org.xml.sax.SAXParseException JavaDoc;
56
57 import org.xml.sax.helpers.ParserFactory JavaDoc;
58
59 import com.quadcap.text.sax.Handler;
60 import com.quadcap.util.Debug;
61
62 /**
63  * Parser for Servlet 2.2 Web Deployment Descriptor.
64  *
65  * @author Stan Bailes
66  */

67 public class DDParser extends Handler JavaDoc {
68     WebApplication app;
69     WebServlet servlet;
70     int state = INIT;
71     
72     final static int INIT = 0;
73     final static int APP = 1;
74     final static int SERVLET = 2;
75     
76     /**
77      * Construct a new deployment descriptor parser
78      */

79     public DDParser() throws SAXException JavaDoc {
80         super();
81     }
82
83     /**
84      * Parse the specified deployment descriptor in the context of the
85      * specified web application.
86      *
87      * @param dd the inputstream containing the deployment descriptor
88      * @param app the web application being constructed
89      */

90     public void parse(Reader JavaDoc dd, WebApplication app)
91         throws SAXException JavaDoc
92     {
93         this.app = app;
94         this.state = INIT;
95         super.parse(dd, app);
96     }
97
98     /**
99      * SAX parser callback function called for the end of an element.
100      *
101      * @param name the name of this element
102      * @exception SAXException may be thrown
103      */

104     /*{com.quadcap.http.server22.DDParser.xml}
105      *
106      * <el><name>web-app</name>
107      * <p>The <b>web-app</b> element is the root element of the
108      * document used to describe and configure a web application.</p>
109      * <el><name>context-param</name>
110      * <p>Application initialization parameters, available via
111      * <code>ServletContext.getInitParameter(String)</code>
112      * are specified here.</p>
113      * <el><name>param-name</name>
114      * <p>Specifies the name of an initialization parameter.</p>
115      * </el>
116      * <el><name>param-value</name>
117      * <p>Specifies the value of an initialization parameter.</p>
118      * </el>
119      * <el><name>session-timeout</name>
120      * <p>Specifies the time period after which inactive sessions
121      * will be closed.</p>
122      * </el>
123      * <el><name>servlet-mapping</name>
124      * <p>Used to tell the container how to map incoming requests
125      * to servlets, based on exact path match, prefix path match,
126      * or extension match.</p>
127      * <el><name>url-pattern</name>
128      * <p>Specifies the pattern to match.</p>
129      * </el>
130      * <el><name>servlet-name</name>
131      * <p>Specifies the name of the servlet to invoke on URIs
132      * which match the specified pattern.</p>
133      * </el>
134      * </el>
135      * <el><name>mime-mapping</name>
136      * <p> Used to tell the container which MIME types are
137      * associated
138      * with particular file extensions.</p>
139      * <el><name>extension</name>
140      * <p>The file name extension (without the '.')</p>
141      * </el>
142      * <el><name>mime-type</name>
143      * <p>The MIME type to associate with the specified file
144      * extension.</p>
145      * </el>
146      * </el>
147      * <el><name>display-name</name>
148      * <p>The name associated with the Web Application when
149      * displayed in the administrative UI.</p>
150      * </el>
151      * <el><name>error-page</name>
152      * <p>The page to be invoked if exceptions are thrown by
153      * servlets in the application</p>
154      * </el>
155      * <el><name>servlet</name>
156      * <p>Define a servlet to be part of this Web Application</p>
157      * <el><name>servlet-name</name>
158      * <p>Specify the name of the servlet. This name isn't
159      * really significant except in that it uniquely identifies
160      * the servlet for the purpose of references to the
161      * servlet in this document (e.g.
162      * <b>web-app/servlet-mapping/servlet-name</b>)</p>
163      * </el>
164      * <el><name>servlet-class</name>
165      * <p>Specify the name of the Java class which implements
166      * the servlet.</p>
167      * </el>
168      * <el><name>init-param</name>
169      * <p>Specify servlet initialization parameters, accessible
170      * to the servlet via
171      * <code>ServletConfig.getInitParameter()</code></p>
172      * <el><name>param-name</name>
173      * <p>Specifies the name of an initialization
174      * parameter.</p>
175      * </el>
176      * <el><name>param-value</name>
177      * <p>Specifies the value of an initialization
178      * parameter.</p>
179      * </el>
180      * </el>
181      * <el><name>load-on-startup</name>
182      * <p>If specified, this causes the servlet to be
183      * initialized
184      * when the web application is loaded, in the order
185      * specified by the integer value of this element, lower
186      * numbers being loaded first.</p>
187      * </el>
188      * </el>
189      * </el>
190      * </el>
191      */

192     public void endElement(String JavaDoc name) throws SAXException JavaDoc {
193         switch (state) {
194         case SERVLET:
195             if (name.equals("servlet")) {
196                 app.addServlet(servlet);
197                 servlet = null;
198                 state = APP;
199             } else if (name.equals("servlet-name")) {
200                 servlet.setServletName(consumeData());
201             } else if (name.equals("servlet-class")) {
202                 servlet.setServletClass(consumeData());
203             } else if (name.equals("init-param")) {
204                 servlet.addInitParam(consume("param-name"),
205                                      consume("param-value"));
206             } else if (name.equals("load-on-startup")) {
207                 servlet.setLoadOnStartup(Integer.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.parseInt(consumeData()));
218             } else if (name.equals("context-param")) {
219                 app.addInitParam(consume("param-name"),
220                                  consume("param-value"));
221             } else if (name.equals("servlet-mapping")) {
222                 app.addServletMapping(consume("servlet-name"),
223                                       consume("url-pattern"));
224             } else if (name.equals("error-page")) {
225                 app.setErrorPage(consumeData());
226             } else if (name.equals("mime-mapping")) {
227                 app.addMimeMapping(consume("extension"),
228                                    consume("mime-type"));
229             } else if (name.equals("display-name")) {
230                 app.setDisplayName(consumeData());
231             } else if (name.equals("welcome-file")) {
232                 app.addWelcomeFile(consumeData());
233             } else {
234                 env.put(name, consumeData());
235             }
236             break;
237         }
238     }
239
240     /**
241      * SAX parser callback for the start of an element.
242      *
243      * @param name the element name
244      * @param attrs the element's attributes
245      *
246      * @exception SAXException may be thrown
247      */

248     public void startElement(String JavaDoc name, AttributeList JavaDoc attrs)
249     throws SAXException JavaDoc
250     {
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 }
262
Popular Tags