01: /*
02: * NoImageTemplate.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.7
24: * Created by suhler on 99/06/18
25: * Last modified by suhler on 00/07/07 17:01:52
26: */
27:
28: package sunlabs.brazil.template;
29:
30: /**
31: * SAMPLE Template class for removing all images
32: * from a web page, and replacing them with their alt strings
33: * This class is used by the TemplateHandler
34: * Each image is replaced by a text string defined by the server property
35: * "template", which the first "%" replaced by the contents of the
36: * alt attribute.
37: *
38: * @author Stephen Uhler
39: * @version %V% NoImageTemplate.java
40: */
41:
42: public class NoImageTemplate extends Template {
43: private String template = "[<b>%</b>]";
44: private boolean filterOther; // only filter non-local images
45: private String mine;
46: private int index;
47:
48: /**
49: * Save a reference to our request properties
50: * We'll use it some day to tailor the image display
51: */
52:
53: public boolean init(RewriteContext hr) {
54: template = hr.request.props.getProperty(hr.prefix + "template",
55: template);
56: filterOther = (null != hr.request.props.getProperty(hr.prefix
57: + "nonlocal", null));
58: mine = hr.request.serverUrl();
59: index = template.indexOf("%");
60: // System.out.println("mine=" + mine + " filter = " + filterOther);
61:
62: return true;
63: }
64:
65: /**
66: * Convert the html tag img into text using the alt string
67: * @param h The name/value pairs
68: * src=
69: * alt=<...>
70: */
71:
72: public void hr_img(RewriteContext hr) {
73:
74: // Only nuke non-local images - XXX for testing
75:
76: if (filterOther) {
77: String src = hr.get("src");
78: if (src.indexOf("http://") < 0 || src.indexOf(mine) >= 0) {
79: return;
80: }
81: // System.out.println( "Stripping image: " + src);
82: }
83:
84: String alt = hr.get("alt");
85: if (alt == null) {
86: alt = "image";
87: }
88: if (index >= 0) {
89: hr.append(template.substring(0, index) + alt
90: + template.substring(index + 1));
91: } else {
92: hr.append(template);
93: }
94: }
95: }
|