1 16 package org.apache.cocoon.components.source; 17 18 19 import java.net.MalformedURLException ; 20 import java.net.URL ; 21 import org.apache.avalon.framework.parameters.Parameters; 22 import org.apache.cocoon.ProcessingException; 23 import org.apache.cocoon.xml.XMLConsumer; 24 import org.xml.sax.ext.LexicalHandler ; 25 import org.xml.sax.Attributes ; 26 import org.xml.sax.ContentHandler ; 27 import org.xml.sax.Locator ; 28 import org.xml.sax.SAXException ; 29 import org.xml.sax.helpers.AttributesImpl ; 30 31 32 46 public final class URLRewriter implements XMLConsumer { 47 48 public static final String PARAMETER_MODE = "rewriteURLMode"; 49 public static final String MODE_NONE = "none"; 50 public static final String MODE_COCOON = "cocoon"; 51 public static final String PARAMETER_PARAMETER_NAME = "urlParameterName"; 52 public static final String PARAMETER_URL = "baseURL"; 53 public static final String PARAMETER_COCOON_URL = "cocoonURL"; 54 55 56 private ContentHandler contentHandler; 57 58 private LexicalHandler lexicalHandler; 59 62 private int mode; 63 64 private String baseUrl; 65 66 private String cocoonUrl; 67 68 71 public URLRewriter(Parameters configuration, 72 ContentHandler contentHandler, 73 LexicalHandler lexicalHandler) 74 throws ProcessingException { 75 try { 76 this.contentHandler = contentHandler; 77 this.lexicalHandler = lexicalHandler; 78 this.mode = 0; 79 if (configuration != null 80 && configuration.getParameter(PARAMETER_MODE, null) != null) { 81 if (configuration.getParameter(PARAMETER_MODE, null).equalsIgnoreCase(MODE_COCOON) == true) { 82 this.mode = 1; 83 this.baseUrl = configuration.getParameter(PARAMETER_URL); 84 this.cocoonUrl = configuration.getParameter(PARAMETER_COCOON_URL) + 85 '?' + configuration.getParameter(PARAMETER_PARAMETER_NAME) + '='; 86 } 87 } 88 } catch (org.apache.avalon.framework.parameters.ParameterException local) { 89 throw new ProcessingException("URLRewriter: configuration exception.", local); 90 } 91 } 92 93 96 public URLRewriter(Parameters configuration, 97 ContentHandler contentHandler) 98 throws ProcessingException { 99 this(configuration, contentHandler, 100 (contentHandler instanceof LexicalHandler ? (LexicalHandler )contentHandler : null)); 101 } 102 103 106 public void setDocumentLocator(Locator locator) { 107 contentHandler.setDocumentLocator(locator); 108 } 109 110 113 public void startDocument() 114 throws SAXException { 115 contentHandler.startDocument(); 116 } 117 118 121 public void endDocument() 122 throws SAXException { 123 contentHandler.endDocument(); 124 } 125 126 129 public void startPrefixMapping(String prefix, String uri) 130 throws SAXException { 131 contentHandler.startPrefixMapping(prefix,uri); 132 } 133 134 137 public void endPrefixMapping(String prefix) 138 throws SAXException { 139 contentHandler.endPrefixMapping(prefix); 140 } 141 142 145 public void startElement(String namespace, String name, String raw, 146 Attributes attr) 147 throws SAXException { 148 if (this.mode == 1) { 149 String attrname; 150 AttributesImpl newattr = null; 151 String value; 152 153 for(int i = 0; i < attr.getLength(); i++) { 154 attrname = attr.getLocalName(i); 155 if (attrname.equals("href") == true 156 || attrname.equals("action") == true) { 157 if (newattr == null) { 158 newattr = new AttributesImpl (attr); 159 } 160 value = attr.getValue(i); 161 if (value.indexOf(':') == -1) { 162 try { 163 URL baseURL = new URL (new URL (this.baseUrl), value); 164 value = baseURL.toExternalForm(); 165 } catch (MalformedURLException local) { 166 value = attr.getValue(i); 167 } 168 } 169 newattr.setValue(i, this.cocoonUrl + value); 170 } else if (attrname.equals("src") == true 171 || attrname.equals("background") == true) { 172 if (newattr == null) { 173 newattr = new AttributesImpl (attr); 174 } 175 value = attr.getValue(i); 176 if (value.indexOf(':') == -1) { 177 try { 178 URL baseURL = new URL (new URL (this.baseUrl), value); 179 value = baseURL.toExternalForm(); 180 } catch (MalformedURLException local) { 181 value = attr.getValue(i); 182 } 183 } 184 newattr.setValue(i, value); 185 } 186 } 187 if (newattr != null) { 188 contentHandler.startElement(namespace, name, raw, newattr); 189 return; 190 } 191 } 192 contentHandler.startElement(namespace,name,raw,attr); 193 } 194 195 198 public void endElement(String namespace, String name, String raw) 199 throws SAXException { 200 contentHandler.endElement(namespace,name,raw); 201 } 202 203 206 public void characters(char ary[], int start, int length) 207 throws SAXException { 208 contentHandler.characters(ary,start,length); 209 } 210 211 214 public void ignorableWhitespace(char ary[], int start, int length) 215 throws SAXException { 216 contentHandler.ignorableWhitespace(ary,start,length); 217 } 218 219 222 public void processingInstruction(String target, String data) 223 throws SAXException { 224 contentHandler.processingInstruction(target,data); 225 } 226 227 230 public void skippedEntity(String name) 231 throws SAXException { 232 contentHandler.skippedEntity(name); 233 } 234 235 238 public void startDTD(String name, String public_id, String system_id) 239 throws SAXException { 240 if (lexicalHandler != null) lexicalHandler.startDTD(name,public_id,system_id); 241 } 242 243 246 public void endDTD() throws SAXException { 247 if (lexicalHandler != null) lexicalHandler.endDTD(); 248 } 249 250 253 public void startEntity(String name) throws SAXException { 254 if (lexicalHandler != null) lexicalHandler.startEntity(name); 255 } 256 257 260 public void endEntity(String name) throws SAXException { 261 if (lexicalHandler != null) lexicalHandler.endEntity(name); 262 } 263 264 267 public void startCDATA() throws SAXException { 268 if (lexicalHandler != null) lexicalHandler.startCDATA(); 269 } 270 271 274 public void endCDATA() throws SAXException { 275 if (lexicalHandler != null) lexicalHandler.endCDATA(); 276 } 277 278 279 282 public void comment(char ary[], int start, int length) 283 throws SAXException { 284 if (this.lexicalHandler != null) { 285 lexicalHandler.comment(ary,start,length); 286 } 287 } 288 289 } 290 | Popular Tags |