1 32 33 package websphinx; 34 35 import java.io.*; 36 import java.net.URL ; 37 import java.net.MalformedURLException ; 38 import java.util.Hashtable ; 39 40 58 public class LinkTransformer extends HTMLTransformer { 59 protected Hashtable map; 60 protected URL base = null; 61 boolean emitBaseElement = false; 62 63 boolean needToEmitBase = false; 64 65 69 public LinkTransformer (String filename) throws IOException { 70 super (filename); 71 } 72 73 79 public LinkTransformer (String filename, boolean seekable) throws IOException { 80 super (filename, seekable); 81 } 82 83 87 public LinkTransformer (OutputStream out) { 88 super (out); 89 } 90 91 95 public LinkTransformer (HTMLTransformer next) { 96 super (next); 97 } 98 99 108 public URL getBase () { 109 return base; 110 } 111 112 121 public synchronized void setBase (URL base) { 122 this.base = base; 123 } 124 125 131 public boolean getEmitBaseElement () { 132 return emitBaseElement; 133 } 134 135 141 public synchronized void setEmitBaseElement (boolean emitBase) { 142 emitBaseElement = emitBase; 143 } 144 145 152 public String lookup (URL base, URL url) { 153 if (map != null) { 154 Object obj = map.get (url); 155 if (obj instanceof URL ) 156 return base != null 157 ? Link.relativeTo (base, (URL )obj) 158 : obj.toString (); 159 else if (obj instanceof String ) 160 return base != null 161 ? Link.relativeTo (base, (String )obj) 162 : obj.toString (); 163 } 164 165 return base != null 166 ? Link.relativeTo (base, url) 167 : url.toString (); 168 } 169 170 177 public synchronized void map (URL url, String href) { 178 if (map == null) 179 map = new Hashtable (); 180 map.put (url, href); 181 } 182 183 190 public synchronized void map (URL url, URL newURL) { 191 if (map == null) 192 map = new Hashtable (); 193 map.put (url, newURL); 194 } 195 196 201 public boolean isMapped (URL url) { 202 return map != null && map.containsKey (url); 203 } 204 205 214 public synchronized void writePage (Page page) throws IOException { 215 needToEmitBase = emitBaseElement && base != null; 216 super.writePage (page); 217 needToEmitBase = false; 218 } 219 220 225 protected void handleElement (Element elem) throws IOException { 226 Tag tag = elem.getStartTag (); 227 String tagName = elem.getTagName (); 228 229 if (needToEmitBase && tag.isBodyTag ()) { 230 emit ("<BASE HREF=\"" + base.toString () + "\">"); 231 needToEmitBase = false; 232 } 233 234 if (elem instanceof Link) 235 handleLink ((Link)elem); 236 else if (tagName == Tag.BASE) 237 handleBase (elem); 238 else if (needToEmitBase && tagName == Tag.HEAD) { 239 emit (elem.getStartTag ()); 241 transformContents (elem); 242 if (needToEmitBase) { 243 emit ("<BASE HREF=\"" + base.toString () + "\">"); 244 needToEmitBase = false; 245 } 246 if (elem.getEndTag () != null) 247 emit (elem.getEndTag ()); 248 } 249 else 250 super.handleElement (elem); 251 } 252 253 259 protected void handleLink (Link link) throws IOException { 260 emit ( link.replaceHref (lookup (base, link.getURL())) ); 261 transformContents (link); 262 if (link.getEndTag () != null) 263 emit (link.getEndTag ()); 264 } 265 266 273 protected void handleBase (Element elem) throws IOException { 274 Tag tag = elem.getStartTag (); 275 if (needToEmitBase) { 276 emit (tag.replaceHTMLAttribute ("href", base.toString())); 277 needToEmitBase = false; 278 } 279 else if (tag.hasHTMLAttribute ("href") 280 && tag.countHTMLAttributes () > 1) 281 emit (tag.removeHTMLAttribute ("href")); 283 } 285 286 302 303 } 304 | Popular Tags |