01: /*
02: * IncludeTemplate.java
03: *
04: * Brazil project web application Framework,
05: * export version: 1.1
06: * Copyright (c) 1999-2000 Sun Microsystems, Inc.
07: *
08: * Sun Public License Notice
09: *
10: * The contents of this file are subject to the Sun Public License Version
11: * 1.0 (the "License"). You may not use this file except in compliance with
12: * the License. A copy of the License is included as the file "license.terms",
13: * and also available at http://www.sun.com/
14: *
15: * The Original Code is from:
16: * Brazil project web application Framework release 1.1.
17: * The Initial Developer of the Original Code is: suhler.
18: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
19: * All Rights Reserved.
20: *
21: * Contributor(s): cstevens, suhler.
22: *
23: * Version: 1.9
24: * Created by suhler on 99/05/06
25: * Last modified by suhler on 00/12/11 13:29:55
26: */
27:
28: package sunlabs.brazil.template;
29:
30: import sunlabs.brazil.util.http.HttpInputStream;
31: import sunlabs.brazil.util.http.HttpRequest;
32:
33: import java.io.ByteArrayOutputStream;
34: import java.io.IOException;
35:
36: /**
37: * Template class for substituting html pages into an html page.
38: * This class is used by the TemplateHandler. This does not perform
39: * traditional server-side include processing, whose normal purpose is to
40: * include standard headers and footers. That functionallity is provided
41: * instead by including the content into the template, and not the other
42: * way around.
43: * <p>
44: * This include incorporates entire pages from other sites.
45: *
46: * @author Stephen Uhler
47: * @version %V% IncludeTemplate.java
48: */
49:
50: public class IncludeTemplate extends Template {
51: /**
52: * Convert the html tag "include" in to text for an included
53: * html page.
54: * Attributes processed
55: * <dl class=attributes>
56: * <dt>href<dd>Absolute url to fetch, and intert here
57: * <dt>alt<dd>Text to insert if URL can't be obtained.
58: * </dl>
59: * Example: <code><include href=http://www.foo.com/index.html></code>
60: */
61:
62: public void tag_include(RewriteContext hr) {
63: String href = hr.get("href");
64: String alt = hr.get("alt");
65: if (alt == null) {
66: alt = "";
67: }
68:
69: HttpRequest target = new HttpRequest(href);
70: try {
71: HttpInputStream in = target.getInputStream();
72: ByteArrayOutputStream out = new ByteArrayOutputStream();
73: in.copyTo(out);
74:
75: hr.append(out.toString());
76: } catch (IOException e) {
77: hr.append("<!-- " + e + " -->" + alt);
78: }
79: }
80:
81: /**
82: * Treat comments of the form:
83: * <code><!#include ...></code> as if they were include tags.
84: */
85:
86: public void comment(RewriteContext hr) {
87: if (hr.getBody().startsWith("#include") == false) {
88: return;
89: }
90: tag_include(hr);
91: }
92: }
|