001: /*
002: * Servlet.java
003: *
004: *
005: * Copyright (c) 2003 Rimfaxe ApS (www.rimfaxe.com).
006: * All rights reserved.
007: *
008: * This package is written by Lars Andersen <lars@rimfaxe.com>
009: * and licensed by Rimfaxe ApS.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * 3. The end-user documentation included with the redistribution, if
024: * any, must include the following acknowlegement:
025: * "This product includes software developed by Rimfaxe ApS
026: (www.rimfaxe.com)"
027: * Alternately, this acknowlegement may appear in the software itself,
028: * if and wherever such third-party acknowlegements normally appear.
029: *
030: * 4. The names "Rimfaxe", "Rimfaxe Software", "Lars Andersen" and
031: * "Rimfaxe WebServer" must not be used to endorse or promote products
032: * derived from this software without prior written permission. For written
033: * permission, please contact info@rimfaxe.com
034: *
035: * 5. Products derived from this software may not be called "Rimfaxe"
036: * nor may "Rimfaxe" appear in their names without prior written
037: * permission of the Rimfaxe ApS.
038: *
039: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
040: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
041: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
042: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
043: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
044: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
045: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
046: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
047: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
048: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: *
051: */
052:
053: package com.rimfaxe.webserver.webapp;
054:
055: import java.util.*;
056:
057: import org.w3c.dom.Attr;
058: import org.w3c.dom.Document;
059: import org.w3c.dom.NamedNodeMap;
060: import org.w3c.dom.Node;
061: import org.w3c.dom.NodeList;
062:
063: import com.rimfaxe.util.*;
064:
065: /**
066: *
067: * @author Lars Andersen
068: */
069: public class Servlet {
070:
071: String servletname = "";
072: String servletclass = "";
073:
074: ArrayList params = new ArrayList(10);
075:
076: int load_on_startup = -1;
077:
078: /** Creates a new instance of Servlet */
079: public Servlet(Node node) {
080: traverse(node);
081: }
082:
083: public String getKey() {
084: return servletname;
085: }
086:
087: public String getName() {
088: return servletname;
089: }
090:
091: public String getClassName() {
092: return servletclass;
093: }
094:
095: public Iterator getParameters() {
096: return params.iterator();
097: }
098:
099: public int getLoadOnStartup() {
100: return this .load_on_startup;
101: }
102:
103: private String traverse(Node node) {
104:
105: StringBuffer str = new StringBuffer();
106:
107: if (node == null) {
108: return "";
109: }
110: int type = node.getNodeType();
111: switch (type) {
112:
113: case Node.DOCUMENT_NODE: {
114: traverse(((Document) node).getDocumentElement());
115: break;
116: }
117:
118: case Node.ELEMENT_NODE: {
119: if (node.getNodeName().equalsIgnoreCase("servlet-name")) {
120: NodeList children = node.getChildNodes();
121: if (children != null) {
122: int len = children.getLength();
123: for (int i = 0; i < len; i++) {
124: String val = traverse(children.item(i));
125: servletname = val.trim();
126: }
127: }
128: } else if (node.getNodeName().equalsIgnoreCase(
129: "servlet-class")) {
130: NodeList children = node.getChildNodes();
131: if (children != null) {
132: int len = children.getLength();
133: for (int i = 0; i < len; i++) {
134: String val = traverse(children.item(i));
135: servletclass = val.trim();
136: }
137: }
138: } else if (node.getNodeName().equalsIgnoreCase(
139: "load-on-startup")) {
140: NodeList children = node.getChildNodes();
141: if (children != null) {
142: int len = children.getLength();
143: for (int i = 0; i < len; i++) {
144: String val = traverse(children.item(i));
145:
146: try {
147: Integer los_i = new Integer(val.trim());
148: this .load_on_startup = los_i.intValue();
149: } catch (NumberFormatException nfe) {
150: }
151:
152: }
153: }
154: } else if (node.getNodeName()
155: .equalsIgnoreCase("init-param")) {
156:
157: InitParameter ip = new InitParameter(node);
158: params.add(ip);
159: } else {
160: NodeList children = node.getChildNodes();
161: if (children != null) {
162: int len = children.getLength();
163: for (int i = 0; i < len; i++) {
164: String val = traverse(children.item(i));
165: }
166: }
167: }
168: break;
169: }
170:
171: case Node.TEXT_NODE: {
172: //if (node instanceof TextImpl)
173: //{
174: str.append(node.getNodeValue());
175: //}
176: break;
177: }
178: }
179: return str.toString();
180: }
181:
182: public String toXML() {
183: StringBuffer buf = new StringBuffer();
184:
185: buf.append(" <servlet> \n");
186: buf.append(" <servlet-name>" + servletname
187: + "</servlet-name> \n");
188: buf.append(" <servlet-class>" + servletclass
189: + "</servlet-class> \n");
190:
191: Object[] objarray = params.toArray();
192: for (int i = 0; i < objarray.length; i++) {
193: InitParameter ip = (InitParameter) objarray[i];
194: buf.append(ip.toXML());
195: }
196:
197: buf.append(" </servlet>\n");
198:
199: return "" + buf;
200: }
201:
202: public String toString() {
203: return servletname;
204: }
205: }
|