001: /*
002: * HTMLextractor.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.xml.extractor;
054:
055: import org.w3c.dom.Attr;
056: import org.w3c.dom.Document;
057: import org.w3c.dom.NamedNodeMap;
058: import org.w3c.dom.Node;
059: import org.w3c.dom.NodeList;
060:
061: import java.util.*;
062: import com.rimfaxe.util.*;
063:
064: /**
065: *
066: * @author Lars Andersen
067: * @version
068: */
069: public class HTMLextractor extends Extractor {
070: String fs = System.getProperty("file.separator");
071:
072: int alt = 1;
073: String body = "";
074:
075: RimfaxeVector params;
076: RimfaxeVector errors = new RimfaxeVector();
077:
078: /** Creates new HTMLextractor for site*/
079: public HTMLextractor(com.rimfaxe.xml.datamodel.Container data,
080: com.rimfaxe.xml.datamodel.Rules rules, String xmlcontent,
081: String id, RimfaxeVector params) {
082: this .params = params;
083: if (params.contains("error")) {
084: String error = ((com.rimfaxe.http.Parameter) params
085: .find("error")).getValue();
086: com.rimfaxe.util.RimfaxeStringTokenizer tkz = new com.rimfaxe.util.RimfaxeStringTokenizer(
087: error, ",", false);
088: while (tkz.hasMoreTokens()) {
089: errors.addElement(tkz.nextToken());
090: }
091: }
092:
093: StringBuffer content = new StringBuffer();
094:
095: //String DTDdir = com.rimfaxe.balancer.ObjectStore.getConfiguration().getHome()+"/admin/dtd/";
096: //com.rimfaxe.util.File file = new com.rimfaxe.util.File(DTDdir+"html.dtd");
097: //System.out.println("Read DTD -> "+DTDdir+"html.dtd");
098: //content.append(file.read());
099:
100: content.append("<?xml version='1.0'?>\n\n");
101: content.append(xmlcontent);
102:
103: setContent(id, content.toString());
104:
105: evaluator = new Condition(rules.makeRuleVec());
106:
107: addData(data);
108:
109: if (document == null)
110: com.rimfaxe.util.Log.log("Document==null");
111: body = traverse(document);
112: }
113:
114: public String processTag(String name, NamedNodeMap attrs,
115: String body, boolean simple) {
116: if (name.equalsIgnoreCase("document")) {
117: return "" + body;
118: }
119: if (name.equalsIgnoreCase("title")) {
120: return "<title> \n" + body + "\n</title>\n";
121: }
122: if (name.equalsIgnoreCase("list_even")) {
123: if (alt == 2) {
124: alt = 1;
125: return "" + body;
126: } else {
127: alt = 2;
128: return "";
129: }
130: }
131: if (name.equalsIgnoreCase("list_odd")) {
132: if (alt == 1) {
133: return "" + body;
134: } else {
135: return "";
136: }
137: }
138: if (name.equalsIgnoreCase("entity")) {
139: String id = "" + attrs.getNamedItem("id").getNodeValue();
140: return "&" + id + ";";
141: }
142: if (name.equalsIgnoreCase("a"))
143: return anchor(attrs, body);
144: if (name.equalsIgnoreCase("input"))
145: return inputfield(attrs);
146: if (name.equalsIgnoreCase("form"))
147: return form(attrs, body);
148: if (name.equalsIgnoreCase("space"))
149: return " ";
150: if (name.equalsIgnoreCase("newline"))
151: return "\n";
152:
153: if (name.equalsIgnoreCase("fileseparator"))
154: return fs;
155:
156: if (name.equalsIgnoreCase("amp"))
157: return "&";
158: if (name.equalsIgnoreCase("br"))
159: return "<br> \n";
160: if (name.equalsIgnoreCase("comment"))
161: return "<!--" + body + "-->";
162:
163: if (name.equalsIgnoreCase("errorblock")) {
164: String id = "" + attrs.getNamedItem("id").getNodeValue();
165: if (errors.contains(id))
166: return body;
167: else
168: return "";
169: }
170:
171: if (name.equalsIgnoreCase("errormessage")) {
172:
173: String id = "" + attrs.getNamedItem("id").getNodeValue();
174:
175: if (params.contains("error_" + id)) {
176: try {
177:
178: String msg = ((com.rimfaxe.http.Parameter) params
179: .find("error_" + id)).getValue();
180:
181: return msg;
182: } catch (NullPointerException npe) {
183:
184: }
185: }
186: return "";
187: }
188:
189: return genericTag(name, attrs, body);
190: }
191:
192: public String inputfield(NamedNodeMap attrs) {
193: StringBuffer buf = new StringBuffer();
194:
195: buf.append("<input ");
196:
197: Node type_node = attrs.getNamedItem("type");
198: Node name_node = attrs.getNamedItem("name");
199: Node checked_node = attrs.getNamedItem("checked");
200: Node value_node = attrs.getNamedItem("value");
201:
202: String type = "unknown";
203: if (type_node != null) {
204: type = attrs.getNamedItem("type").getNodeValue();
205: buf.append("type=\"" + type + "\" ");
206: }
207:
208: String name = "unknown";
209: if (name_node != null) {
210: name = attrs.getNamedItem("name").getNodeValue();
211: //System.out.println("input box -> "+name);
212: buf.append("name=\"" + name + "\" ");
213: }
214:
215: String value = "unknown";
216: if (value_node != null) {
217: value = attrs.getNamedItem("value").getNodeValue();
218: buf.append("value=\"" + value + "\" ");
219: } else {
220: if (params.contains(name)) {
221: String val = ((com.rimfaxe.http.Parameter) params
222: .find(name)).getValue();
223: buf.append("value=\"" + val + "\" ");
224: }
225: }
226:
227: if (params.contains(name)) {
228: if (type.equalsIgnoreCase("checkbox")) {
229: buf.append("CHECKED ");
230: } else if (type.equalsIgnoreCase("radio")) {
231: String val = ((com.rimfaxe.http.Parameter) params
232: .find(name)).getValue();
233: if (val.equalsIgnoreCase(value))
234: buf.append("CHECKED ");
235: }
236: } else {
237: if (checked_node != null) {
238: //System.out.println("Checked input box");
239: buf.append("CHECKED ");
240: }
241: }
242:
243: buf.append("/>");
244: return "" + buf;
245: }
246:
247: public String anchor(NamedNodeMap attrs, String body) {
248: StringBuffer res = new StringBuffer();
249:
250: String href = "";
251: String dest = "";
252: String action = "";
253:
254: Node href_node = attrs.getNamedItem("href");
255: Node dest_node = attrs.getNamedItem("goto");
256: Node action_node = attrs.getNamedItem("action");
257:
258: if (href_node != null)
259: href = attrs.getNamedItem("href").getNodeValue();
260: if (dest_node != null)
261: dest = attrs.getNamedItem("goto").getNodeValue();
262: if (action_node != null)
263: action = attrs.getNamedItem("action").getNodeValue();
264:
265: if (action.equalsIgnoreCase("params")) {
266: String params = attrs.getNamedItem("params").getNodeValue();
267: com.rimfaxe.xml.datamodel.Container c = (com.rimfaxe.xml.datamodel.Container) stack
268: .peek();
269: StringBuffer buf = new StringBuffer();
270: if (href.indexOf("?") == -1)
271: buf.append(href + "?");
272: else
273: buf.append(href + "&");
274: com.rimfaxe.util.RimfaxeStringTokenizer tkz = new com.rimfaxe.util.RimfaxeStringTokenizer(
275: params, ",", false);
276: while (tkz.hasMoreTokens()) {
277: String p = tkz.nextToken();
278: buf.append(p + "=" + c.getLiteral(p));
279: if (tkz.hasMoreTokens())
280: buf.append("&");
281: }
282:
283: href = "" + buf;
284: }
285:
286: return "<a style=\"text-decoration: none;\" href=\"/rimfaxe-loadbalancer-admin"
287: + href + "\">" + body + "</a>";
288: }
289:
290: public String form(NamedNodeMap attrs, String body) {
291: String action = attrs.getNamedItem("action").getNodeValue();
292: String method = attrs.getNamedItem("method").getNodeValue();
293:
294: return genericTag("form", attrs, body);
295: }
296:
297: public String getBody() {
298: return body;
299: }
300: }
|