001: /*
002: * ContentTemplate.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 1999-2000 Sun Microsystems, Inc.
007: *
008: * Sun Public License Notice
009: *
010: * The contents of this file are subject to the Sun Public License Version
011: * 1.0 (the "License"). You may not use this file except in compliance with
012: * the License. A copy of the License is included as the file "license.terms",
013: * and also available at http://www.sun.com/
014: *
015: * The Original Code is from:
016: * Brazil project web application Framework release 1.1.
017: * The Initial Developer of the Original Code is: suhler.
018: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s): cstevens, suhler.
022: *
023: * Version: 1.16
024: * Created by suhler on 99/06/28
025: * Last modified by suhler on 00/12/27 12:22:42
026: */
027:
028: package sunlabs.brazil.template;
029:
030: import java.util.Dictionary;
031:
032: /**
033: * Template class for extracting content out of remote html page
034: * This class is used by the TemplateHandler, for extracting
035: * the "content" out of html documents for later integration with
036: * a look-and-feel template using one or more of:
037: * {@link PropsTemplate},
038: * {@link BSLTemplate},
039: * or
040: * {@link sunlabs.brazil.filter.ReplaceFilter},
041: *
042: * The plan is to snag the title and the content, and put them into
043: * request properties. The resultant processed output will be
044: * discarded. The following properties are gathered:
045: * <dl class=other>
046: * <dt>title <dd> The document title
047: * <dt>bodyArgs <dd> The attributes to the body tag, if any
048: * <dt>content <dd> The body, delimited by content.../content>.
049: * The text inside multiple content ... /content pairs
050: * are concatenated together.
051: * <dt>script <dd> All "script"..."/script" tags found in the document head
052: * <dt>style <dd> All "style"..."/style" tags found in the document head
053: * <dt>meta-[name] <dd> Every meta tag "name" and "content"
054: * <dt>link-[rel] <dd> Every link tag "rel" and "href"
055: * <dt>user-agent <dd> The origin user agent
056: * <dt>referer <dd> The user agent referrer (if any)
057: * <dt>last-modified <dd> The document last modified time (if any) in std format
058: * <dt>content-length <dd> The document content length, as fetched from the origin server
059: * </dl>
060: *
061: * @author Stephen Uhler
062: * @version %V% 1.16
063: */
064:
065: public class ContentTemplate extends Template {
066: boolean inHead = true;
067:
068: /**
069: * Toss everything up to and including this entity.
070: */
071:
072: public void tag_title(RewriteContext hr) {
073: hr.reset();
074: }
075:
076: /**
077: * Gather up the title - no tags allowed between title .... /title.
078: */
079:
080: public void tag_slash_title(RewriteContext hr) {
081: hr.request.props.put("title", hr.toString().trim());
082: hr.reset();
083: }
084:
085: /**
086: * Append all "script" code while in the head section.
087: */
088:
089: public void tag_script(RewriteContext hr) {
090: if (inHead) {
091: boolean save = hr.accumulate(false);
092: hr.nextToken();
093: String script = hr.request.props.getProperty("script", "")
094: + hr.getBody();
095: hr.request.props.put("script", script);
096: hr.accumulate(save);
097: hr.reset();
098: }
099: }
100:
101: /**
102: * Append all "style" code while in the head section.
103: */
104:
105: public void tag_style(RewriteContext hr) {
106: if (inHead) {
107: boolean save = hr.accumulate(false);
108: hr.nextToken();
109: String style = hr.request.props.getProperty("style", "")
110: + hr.getBody();
111: hr.request.props.put("style", style);
112: hr.accumulate(save);
113: hr.reset();
114: }
115: }
116:
117: /**
118: * Mark end of head section. All "script" content in the "body"
119: * is left alone.
120: */
121:
122: public void tag_slash_head(RewriteContext hr) {
123: inHead = false;
124: }
125:
126: /**
127: * toss everything up to and including here, but turn on
128: * content accumulation.
129: */
130:
131: public void tag_content(RewriteContext hr) {
132: hr.reset();
133: hr.accumulate(true);
134: }
135:
136: /**
137: * Grab the "body" attributes, and toss all output to this point.
138: */
139:
140: public void tag_body(RewriteContext hr) {
141: inHead = false;
142: String bodyArgs = hr.getArgs();
143: if (bodyArgs != null) {
144: hr.request.props.put("bodyArgs", bodyArgs);
145: }
146: hr.reset();
147: }
148:
149: /**
150: * Save the content gathered so far, and turn off content accumulation.
151: */
152:
153: public void tag_slash_content(RewriteContext hr) {
154: String content = hr.request.props.getProperty("content", "")
155: + hr.toString();
156: hr.request.props.put("content", content);
157: hr.accumulate(false);
158: }
159:
160: /**
161: * If no content tags are present, use the entire "body" instead.
162: */
163:
164: public void tag_slash_body(RewriteContext hr) {
165: if (!hr.request.props.containsKey("content")) {
166: hr.request.props.put("content", hr.toString());
167: hr.accumulate(false);
168: }
169: }
170:
171: /**
172: * Extract data out of meta tags into the properties.
173: * For "http-equiv" tags, set the corrosponding http respones header.
174: */
175:
176: public void tag_meta(RewriteContext hr) {
177: String name = hr.get("name");
178: String equiv = hr.get("http-equiv");
179: String content = hr.get("content");
180: if ((name != null) && (content != null)) {
181: hr.request.props.put("meta-" + name, content);
182: } else if ((equiv != null) && (content != null)) {
183: hr.request.addHeader(equiv, content);
184: }
185: }
186:
187: /**
188: * Extract data out of link tags into the properties.
189: * Prefix the "rel" attribute with "link-" to use as the
190: * property name.
191: */
192:
193: public void tag_link(RewriteContext hr) {
194: String type = hr.get("rel");
195: String href = hr.get("href");
196: if ((type != null) && (href != null)) {
197: hr.request.props.put("link-" + type, href);
198: }
199: }
200:
201: /**
202: * Extract useful properties out of the http mime headers.
203: */
204:
205: public boolean done(RewriteContext hr) {
206: tag_slash_body(hr);
207:
208: transfer("user-agent", hr.request.headers, hr.request.props);
209: transfer("referer", hr.request.headers, hr.request.props);
210: transfer("last-modified", hr.request.responseHeaders,
211: hr.request.props);
212: transfer("content-length", hr.request.responseHeaders,
213: hr.request.props);
214: return true;
215: }
216:
217: /**
218: * Transfer an item to another hash table, if it exists
219: */
220:
221: private boolean transfer(String key, Dictionary src, Dictionary dst) {
222: Object obj = src.get(key);
223: if (obj != null) {
224: dst.put(key, obj);
225: return true;
226: } else {
227: return false;
228: }
229: }
230: }
|