![]() |
Quadcap Embeddable Server |
00001 package com.quadcap.http.server22; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.Reader; 00042 import java.io.IOException; 00043 00044 import java.util.Hashtable; 00045 00046 import javax.servlet.ServletException; 00047 00048 import org.xml.sax.AttributeList; 00049 import org.xml.sax.DocumentHandler; 00050 import org.xml.sax.ErrorHandler; 00051 import org.xml.sax.InputSource; 00052 import org.xml.sax.Parser; 00053 import org.xml.sax.Locator; 00054 import org.xml.sax.SAXException; 00055 import org.xml.sax.SAXParseException; 00056 00057 import org.xml.sax.helpers.ParserFactory; 00058 00059 import com.quadcap.text.sax.Handler; 00060 import com.quadcap.util.Debug; 00061 00062 /** 00063 * Parser for Servlet 2.2 Web Deployment Descriptor. 00064 * 00065 * @author Stan Bailes 00066 */ 00067 public class DDParser extends Handler { 00068 WebApplication app; 00069 WebServlet servlet; 00070 int state = INIT; 00071 00072 final static int INIT = 0; 00073 final static int APP = 1; 00074 final static int SERVLET = 2; 00075 00076 /** 00077 * Construct a new deployment descriptor parser 00078 */ 00080 super(); 00081 } 00082 00083 /** 00084 * Parse the specified deployment descriptor in the context of the 00085 * specified web application. 00086 * 00087 * @param dd the inputstream containing the deployment descriptor 00088 * @param app the web application being constructed 00089 */ 00090 public void parse(Reader dd, WebApplication app) 00092 { 00093 this.app = app; 00094 this.state = INIT; 00095 super.parse(dd, app); 00096 } 00097 00098 /** 00099 * SAX parser callback function called for the end of an element. 00100 * 00101 * @param name the name of this element 00102 * @exception SAXException may be thrown 00103 */ 00104 /*{com.quadcap.http.server22.DDParser.xml} 00105 * 00106 * <el><name>web-app</name> 00107 * <p>The <b>web-app</b> element is the root element of the 00108 * document used to describe and configure a web application.</p> 00109 * <el><name>context-param</name> 00110 * <p>Application initialization parameters, available via 00111 * <code>ServletContext.getInitParameter(String)</code> 00112 * are specified here.</p> 00113 * <el><name>param-name</name> 00114 * <p>Specifies the name of an initialization parameter.</p> 00115 * </el> 00116 * <el><name>param-value</name> 00117 * <p>Specifies the value of an initialization parameter.</p> 00118 * </el> 00119 * <el><name>session-timeout</name> 00120 * <p>Specifies the time period after which inactive sessions 00121 * will be closed.</p> 00122 * </el> 00123 * <el><name>servlet-mapping</name> 00124 * <p>Used to tell the container how to map incoming requests 00125 * to servlets, based on exact path match, prefix path match, 00126 * or extension match.</p> 00127 * <el><name>url-pattern</name> 00128 * <p>Specifies the pattern to match.</p> 00129 * </el> 00130 * <el><name>servlet-name</name> 00131 * <p>Specifies the name of the servlet to invoke on URIs 00132 * which match the specified pattern.</p> 00133 * </el> 00134 * </el> 00135 * <el><name>mime-mapping</name> 00136 * <p> Used to tell the container which MIME types are 00137 * associated 00138 * with particular file extensions.</p> 00139 * <el><name>extension</name> 00140 * <p>The file name extension (without the '.')</p> 00141 * </el> 00142 * <el><name>mime-type</name> 00143 * <p>The MIME type to associate with the specified file 00144 * extension.</p> 00145 * </el> 00146 * </el> 00147 * <el><name>display-name</name> 00148 * <p>The name associated with the Web Application when 00149 * displayed in the administrative UI.</p> 00150 * </el> 00151 * <el><name>error-page</name> 00152 * <p>The page to be invoked if exceptions are thrown by 00153 * servlets in the application</p> 00154 * </el> 00155 * <el><name>servlet</name> 00156 * <p>Define a servlet to be part of this Web Application</p> 00157 * <el><name>servlet-name</name> 00158 * <p>Specify the name of the servlet. This name isn't 00159 * really significant except in that it uniquely identifies 00160 * the servlet for the purpose of references to the 00161 * servlet in this document (e.g. 00162 * <b>web-app/servlet-mapping/servlet-name</b>)</p> 00163 * </el> 00164 * <el><name>servlet-class</name> 00165 * <p>Specify the name of the Java class which implements 00166 * the servlet.</p> 00167 * </el> 00168 * <el><name>init-param</name> 00169 * <p>Specify servlet initialization parameters, accessible 00170 * to the servlet via 00171 * <code>ServletConfig.getInitParameter()</code></p> 00172 * <el><name>param-name</name> 00173 * <p>Specifies the name of an initialization 00174 * parameter.</p> 00175 * </el> 00176 * <el><name>param-value</name> 00177 * <p>Specifies the value of an initialization 00178 * parameter.</p> 00179 * </el> 00180 * </el> 00181 * <el><name>load-on-startup</name> 00182 * <p>If specified, this causes the servlet to be 00183 * initialized 00184 * when the web application is loaded, in the order 00185 * specified by the integer value of this element, lower 00186 * numbers being loaded first.</p> 00187 * </el> 00188 * </el> 00189 * </el> 00190 * </el> 00191 */ 00193 switch (state) { 00194 case SERVLET: 00195 if (name.equals("servlet")) { 00196 app.addServlet(servlet); 00197 servlet = null; 00198 state = APP; 00199 } else if (name.equals("servlet-name")) { 00200 servlet.setServletName(consumeData()); 00201 } else if (name.equals("servlet-class")) { 00202 servlet.setServletClass(consumeData()); 00203 } else if (name.equals("init-param")) { 00204 servlet.addInitParam(consume("param-name"), 00205 consume("param-value")); 00206 } else if (name.equals("load-on-startup")) { 00207 servlet.setLoadOnStartup(Integer.parseInt(consumeData())); 00208 } else { 00209 env.put(name, consumeData()); 00210 } 00211 break; 00212 case APP: 00213 if (name.equals("context-param")) { 00214 app.addInitParam(consume("param-name"), 00215 consume("param-value")); 00216 } else if (name.equals("session-timeout")) { 00217 app.setSessionTimeout(60 * Integer.parseInt(consumeData())); 00218 } else if (name.equals("context-param")) { 00219 app.addInitParam(consume("param-name"), 00220 consume("param-value")); 00221 } else if (name.equals("servlet-mapping")) { 00222 app.addServletMapping(consume("servlet-name"), 00223 consume("url-pattern")); 00224 } else if (name.equals("error-page")) { 00225 app.setErrorPage(consumeData()); 00226 } else if (name.equals("mime-mapping")) { 00227 app.addMimeMapping(consume("extension"), 00228 consume("mime-type")); 00229 } else if (name.equals("display-name")) { 00230 app.setDisplayName(consumeData()); 00231 } else if (name.equals("welcome-file")) { 00232 app.addWelcomeFile(consumeData()); 00233 } else { 00234 env.put(name, consumeData()); 00235 } 00236 break; 00237 } 00238 } 00239 00240 /** 00241 * SAX parser callback for the start of an element. 00242 * 00243 * @param name the element name 00244 * @param attrs the element's attributes 00245 * 00246 * @exception SAXException may be thrown 00247 */ 00250 { 00251 data.setLength(0); 00252 if (name.equals("servlet")) { 00253 servlet = new WebServlet(); 00254 servlet.setWebApplication(app); 00255 state = SERVLET; 00256 } else if (name.equals("web-app")) { 00257 state = APP; 00258 } 00259 } 00260 00261 }