1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.util.rewriter;
18
19
20
21 import java.io.Reader;
22
23
24 import java.net.MalformedURLException;
25
26
27 import javax.swing.text.html.HTML;
28 import javax.swing.text.MutableAttributeSet;
29
30 /***
31 *
32 * Sample of extending HTML Rewriter for your specific needs
33 *
34 *
35 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
36 * @version $Id: SampleRewriter.java,v 1.5 2004/02/23 03:18:59 jford Exp $
37 */
38
39 public class SampleRewriter extends HTMLRewriter
40 {
41 private boolean debug = false;
42 private String basePortalURL;
43 private String fullPortalURL;
44 private String sampleURL;
45
46 private String sessionID = "NONE";
47 private String formID = "NONE";
48 private boolean sampleEndFlag = false;
49
50 public String getSessionID()
51 {
52 return sessionID;
53 }
54
55 public String getFormID()
56 {
57 return formID;
58 }
59
60 public boolean getSampleEndFlag()
61 {
62 return sampleEndFlag;
63 }
64
65
66
67
68
69
70
71
72
73 public SampleRewriter(String basePortalURL, String fullPortalURL, String sampleURL )
74 {
75 this.basePortalURL = basePortalURL;
76 this.fullPortalURL = fullPortalURL;
77 this.sampleURL = sampleURL;
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public String rewrite(Reader input, String baseURL)
94 throws MalformedURLException
95 {
96 String rewrittenHTML = "";
97 this.basePortalURL = baseURL;
98
99 HTMLParserAdaptor parser = new SwingParserAdaptor(this);
100 rewrittenHTML = parser.run(input);
101
102 return rewrittenHTML;
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public String generateNewUrl(String targetURL, HTML.Tag tag, HTML.Attribute attribute)
128 {
129 if (debug)
130 System.out.println("[rewriter] Tag: " + tag.toString() + " Attribute: " + attribute.toString() + " targetURL: " + targetURL + " target = " + fullPortalURL + "]");
131
132
133
134 if (tag == HTML.Tag.FORM && attribute == HTML.Attribute.ACTION) {
135
136
137 int sessionLocation = targetURL.indexOf( "?sessionId" );
138 if (sessionLocation > -1) {
139 int equalsLocation = targetURL.indexOf( "=", sessionLocation );
140 if (equalsLocation > -1) {
141 int ampLocation = targetURL.indexOf( "&", equalsLocation );
142 if (ampLocation > -1) {
143 sessionID = targetURL.substring( equalsLocation + 1, ampLocation );
144 } else {
145 sessionID = targetURL.substring( equalsLocation + 1 );
146 }
147 }
148 }
149
150 if (sampleEndFlag) {
151
152 return basePortalURL;
153 } else {
154
155 return fullPortalURL;
156 }
157 }
158
159
160 return targetURL;
161 }
162
163
164
165
166
167
168 public boolean proxyAllTags()
169 {
170 return true;
171 }
172
173
174
175
176 public String exitStartTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
177 {
178 if (tag == HTML.Tag.FORM)
179 {
180 String inputTag = "<input type='hidden' name='sessionId' value='" + sessionID + "'/>";
181 return inputTag;
182 }
183 return null;
184 }
185
186
187
188
189
190 public boolean enterSimpleTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
191 {
192 if (tag == HTML.Tag.META)
193 {
194 Object o = attrs.getAttribute(HTML.Attribute.NAME);
195 if (o != null)
196 {
197 String s = o.toString();
198 if (s.equalsIgnoreCase("SampleEnd"))
199 {
200 sampleEndFlag = true;
201 }
202 }
203 }
204 return true;
205 }
206
207
208
209
210
211
212 public void convertTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
213 {
214 if (tag == HTML.Tag.FORM) {
215
216
217
218
219 attrs.addAttribute("NAME","SampleForm");
220 }
221
222
223 if (tag == HTML.Tag.INPUT)
224 {
225 Object o = attrs.getAttribute(HTML.Attribute.NAME);
226 if (o != null)
227 {
228 String s = o.toString();
229 if (s.equalsIgnoreCase("FormID"))
230 {
231 o = attrs.getAttribute(HTML.Attribute.VALUE);
232 if (o != null)
233 {
234 formID = o.toString();
235 }
236 }
237 }
238 }
239
240 }
241
242 }
243