1 16 17 package org.apache.jetspeed.util.rewriter; 18 19 20 import java.io.Reader ; 22 23 import java.net.MalformedURLException ; 25 26 import javax.swing.text.html.HTML ; 28 import javax.swing.text.MutableAttributeSet ; 29 30 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 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 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 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 if (tag == HTML.Tag.FORM && attribute == HTML.Attribute.ACTION) { 135 136 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 return basePortalURL; 153 } else { 154 return fullPortalURL; 156 } 157 } 158 159 return targetURL; 161 } 162 163 168 public boolean proxyAllTags() 169 { 170 return true; 171 } 172 173 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 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 211 212 public void convertTagEvent(HTML.Tag tag, MutableAttributeSet attrs) 213 { 214 if (tag == HTML.Tag.FORM) { 215 attrs.addAttribute("NAME","SampleForm"); 220 } 221 222 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 244 | Popular Tags |