001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.source;
018:
019: import java.net.MalformedURLException;
020: import java.net.URL;
021: import org.apache.avalon.framework.parameters.Parameters;
022: import org.apache.cocoon.ProcessingException;
023: import org.apache.cocoon.xml.XMLConsumer;
024: import org.xml.sax.ext.LexicalHandler;
025: import org.xml.sax.Attributes;
026: import org.xml.sax.ContentHandler;
027: import org.xml.sax.Locator;
028: import org.xml.sax.SAXException;
029: import org.xml.sax.helpers.AttributesImpl;
030:
031: /**
032: * This is an <code>XMLConsumer</code> which rewrites the stream
033: * according to the configuration.
034: * The configuration can have the following parameters:
035: * "rewriteURLMode" : The mode to rewrite the urls. Currently none and cocoon
036: * are supported.
037: * "cocoonURL" : The url all links are resolved to
038: * "urlParameterName" : The parameter name to use for links (all links are
039: * then "cocoonURL?urlParameterName=LINK"
040: * "baseURL" : The current URL to rewrite
041: *
042: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
043: * @version CVS $Id: URLRewriter.java 433543 2006-08-22 06:22:54Z crossley $
044: */
045: public final class URLRewriter implements XMLConsumer {
046:
047: public static final String PARAMETER_MODE = "rewriteURLMode";
048: public static final String MODE_NONE = "none";
049: public static final String MODE_COCOON = "cocoon";
050: public static final String PARAMETER_PARAMETER_NAME = "urlParameterName";
051: public static final String PARAMETER_URL = "baseURL";
052: public static final String PARAMETER_COCOON_URL = "cocoonURL";
053:
054: /** The <code>ContentHandler</code> */
055: private ContentHandler contentHandler;
056: /** The <code>LexicalHandler</code> */
057: private LexicalHandler lexicalHandler;
058: /** The mode:
059: * 0 : no rewriting
060: * 1 : cocoon */
061: private int mode;
062: /** The base url */
063: private String baseUrl;
064: /** The cocoon url */
065: private String cocoonUrl;
066:
067: /**
068: * Create a new rewriter
069: */
070: public URLRewriter(Parameters configuration,
071: ContentHandler contentHandler, LexicalHandler lexicalHandler)
072: throws ProcessingException {
073: try {
074: this .contentHandler = contentHandler;
075: this .lexicalHandler = lexicalHandler;
076: this .mode = 0;
077: if (configuration != null
078: && configuration.getParameter(PARAMETER_MODE, null) != null) {
079: if (configuration.getParameter(PARAMETER_MODE, null)
080: .equalsIgnoreCase(MODE_COCOON) == true) {
081: this .mode = 1;
082: this .baseUrl = configuration
083: .getParameter(PARAMETER_URL);
084: this .cocoonUrl = configuration
085: .getParameter(PARAMETER_COCOON_URL)
086: + '?'
087: + configuration
088: .getParameter(PARAMETER_PARAMETER_NAME)
089: + '=';
090: }
091: }
092: } catch (org.apache.avalon.framework.parameters.ParameterException local) {
093: throw new ProcessingException(
094: "URLRewriter: configuration exception.", local);
095: }
096: }
097:
098: /**
099: * Create a new rewriter
100: */
101: public URLRewriter(Parameters configuration,
102: ContentHandler contentHandler) throws ProcessingException {
103: this (
104: configuration,
105: contentHandler,
106: (contentHandler instanceof LexicalHandler ? (LexicalHandler) contentHandler
107: : null));
108: }
109:
110: /**
111: * SAX Event Handling
112: */
113: public void setDocumentLocator(Locator locator) {
114: contentHandler.setDocumentLocator(locator);
115: }
116:
117: /**
118: * SAX Event Handling
119: */
120: public void startDocument() throws SAXException {
121: contentHandler.startDocument();
122: }
123:
124: /**
125: * SAX Event Handling
126: */
127: public void endDocument() throws SAXException {
128: contentHandler.endDocument();
129: }
130:
131: /**
132: * SAX Event Handling
133: */
134: public void startPrefixMapping(String prefix, String uri)
135: throws SAXException {
136: contentHandler.startPrefixMapping(prefix, uri);
137: }
138:
139: /**
140: * SAX Event Handling
141: */
142: public void endPrefixMapping(String prefix) throws SAXException {
143: contentHandler.endPrefixMapping(prefix);
144: }
145:
146: /**
147: * SAX Event Handling
148: */
149: public void startElement(String namespace, String name, String raw,
150: Attributes attr) throws SAXException {
151: if (this .mode == 1) {
152: String attrname;
153: AttributesImpl newattr = null;
154: String value;
155:
156: for (int i = 0; i < attr.getLength(); i++) {
157: attrname = attr.getLocalName(i);
158: if (attrname.equals("href") == true
159: || attrname.equals("action") == true) {
160: if (newattr == null) {
161: newattr = new AttributesImpl(attr);
162: }
163: value = attr.getValue(i);
164: if (value.indexOf(':') == -1) {
165: try {
166: URL baseURL = new URL(
167: new URL(this .baseUrl), value);
168: value = baseURL.toExternalForm();
169: } catch (MalformedURLException local) {
170: value = attr.getValue(i);
171: }
172: }
173: newattr.setValue(i, this .cocoonUrl + value);
174: } else if (attrname.equals("src") == true
175: || attrname.equals("background") == true) {
176: if (newattr == null) {
177: newattr = new AttributesImpl(attr);
178: }
179: value = attr.getValue(i);
180: if (value.indexOf(':') == -1) {
181: try {
182: URL baseURL = new URL(
183: new URL(this .baseUrl), value);
184: value = baseURL.toExternalForm();
185: } catch (MalformedURLException local) {
186: value = attr.getValue(i);
187: }
188: }
189: newattr.setValue(i, value);
190: }
191: }
192: if (newattr != null) {
193: contentHandler.startElement(namespace, name, raw,
194: newattr);
195: return;
196: }
197: }
198: contentHandler.startElement(namespace, name, raw, attr);
199: }
200:
201: /**
202: * SAX Event Handling
203: */
204: public void endElement(String namespace, String name, String raw)
205: throws SAXException {
206: contentHandler.endElement(namespace, name, raw);
207: }
208:
209: /**
210: * SAX Event Handling
211: */
212: public void characters(char ary[], int start, int length)
213: throws SAXException {
214: contentHandler.characters(ary, start, length);
215: }
216:
217: /**
218: * SAX Event Handling
219: */
220: public void ignorableWhitespace(char ary[], int start, int length)
221: throws SAXException {
222: contentHandler.ignorableWhitespace(ary, start, length);
223: }
224:
225: /**
226: * SAX Event Handling
227: */
228: public void processingInstruction(String target, String data)
229: throws SAXException {
230: contentHandler.processingInstruction(target, data);
231: }
232:
233: /**
234: * SAX Event Handling
235: */
236: public void skippedEntity(String name) throws SAXException {
237: contentHandler.skippedEntity(name);
238: }
239:
240: /**
241: * SAX Event Handling
242: */
243: public void startDTD(String name, String public_id, String system_id)
244: throws SAXException {
245: if (lexicalHandler != null)
246: lexicalHandler.startDTD(name, public_id, system_id);
247: }
248:
249: /**
250: * SAX Event Handling
251: */
252: public void endDTD() throws SAXException {
253: if (lexicalHandler != null)
254: lexicalHandler.endDTD();
255: }
256:
257: /**
258: * SAX Event Handling
259: */
260: public void startEntity(String name) throws SAXException {
261: if (lexicalHandler != null)
262: lexicalHandler.startEntity(name);
263: }
264:
265: /**
266: * SAX Event Handling
267: */
268: public void endEntity(String name) throws SAXException {
269: if (lexicalHandler != null)
270: lexicalHandler.endEntity(name);
271: }
272:
273: /**
274: * SAX Event Handling
275: */
276: public void startCDATA() throws SAXException {
277: if (lexicalHandler != null)
278: lexicalHandler.startCDATA();
279: }
280:
281: /**
282: * SAX Event Handling
283: */
284: public void endCDATA() throws SAXException {
285: if (lexicalHandler != null)
286: lexicalHandler.endCDATA();
287: }
288:
289: /**
290: * SAX Event Handling
291: */
292: public void comment(char ary[], int start, int length)
293: throws SAXException {
294: if (this.lexicalHandler != null) {
295: lexicalHandler.comment(ary, start, length);
296: }
297: }
298:
299: }
|