KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > URLRewriter


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.source;
17
18
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
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 JavaDoc;
25 import org.xml.sax.Attributes JavaDoc;
26 import org.xml.sax.ContentHandler JavaDoc;
27 import org.xml.sax.Locator JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.helpers.AttributesImpl JavaDoc;
30
31
32 /**
33  * This is an <code>XMLConsumer</code> which rewrites the stream
34  * according to the configuration.
35  * The configuration can have the following parameters:
36  * "rewriteURLMode" : The mode to rewrite the urls. Currently none and cocoon
37  * are supported.
38  * "cocoonURL" : The url all links are resolved to
39  * "urlParameterName" : The parameter name to use for links (all links are
40  * then "cocoonURL?urlParameterName=LINK"
41  * "baseURL" : The current URL to rewrite
42  *
43  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
44  * @version CVS $Id: URLRewriter.java 30932 2004-07-29 17:35:38Z vgritsenko $
45 */

46 public final class URLRewriter implements XMLConsumer {
47
48     public static final String JavaDoc PARAMETER_MODE = "rewriteURLMode";
49     public static final String JavaDoc MODE_NONE = "none";
50     public static final String JavaDoc MODE_COCOON = "cocoon";
51     public static final String JavaDoc PARAMETER_PARAMETER_NAME = "urlParameterName";
52     public static final String JavaDoc PARAMETER_URL = "baseURL";
53     public static final String JavaDoc PARAMETER_COCOON_URL = "cocoonURL";
54
55     /** The <code>ContentHandler</code> */
56     private ContentHandler JavaDoc contentHandler;
57     /** The <code>LexicalHandler</code> */
58     private LexicalHandler JavaDoc lexicalHandler;
59     /** The mode:
60      * 0 : no rewriting
61      * 1 : cocoon */

62     private int mode;
63     /** The base url */
64     private String JavaDoc baseUrl;
65     /** The cocoon url */
66     private String JavaDoc cocoonUrl;
67
68     /**
69      * Create a new rewriter
70      */

71     public URLRewriter(Parameters configuration,
72                        ContentHandler JavaDoc contentHandler,
73                        LexicalHandler JavaDoc 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     /**
94      * Create a new rewriter
95      */

96     public URLRewriter(Parameters configuration,
97                        ContentHandler JavaDoc contentHandler)
98     throws ProcessingException {
99         this(configuration, contentHandler,
100              (contentHandler instanceof LexicalHandler JavaDoc ? (LexicalHandler JavaDoc)contentHandler : null));
101     }
102
103     /**
104      * SAX Event Handling
105      */

106     public void setDocumentLocator(Locator JavaDoc locator) {
107         contentHandler.setDocumentLocator(locator);
108     }
109
110     /**
111      * SAX Event Handling
112      */

113     public void startDocument()
114     throws SAXException JavaDoc {
115         contentHandler.startDocument();
116     }
117
118     /**
119      * SAX Event Handling
120      */

121     public void endDocument()
122     throws SAXException JavaDoc {
123         contentHandler.endDocument();
124     }
125
126     /**
127      * SAX Event Handling
128      */

129     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
130     throws SAXException JavaDoc {
131         contentHandler.startPrefixMapping(prefix,uri);
132     }
133
134     /**
135      * SAX Event Handling
136      */

137     public void endPrefixMapping(String JavaDoc prefix)
138     throws SAXException JavaDoc {
139         contentHandler.endPrefixMapping(prefix);
140     }
141
142     /**
143      * SAX Event Handling
144      */

145     public void startElement(String JavaDoc namespace, String JavaDoc name, String JavaDoc raw,
146                          Attributes JavaDoc attr)
147     throws SAXException JavaDoc {
148         if (this.mode == 1) {
149             String JavaDoc attrname;
150             AttributesImpl JavaDoc newattr = null;
151             String JavaDoc 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 JavaDoc(attr);
159                     }
160                     value = attr.getValue(i);
161                     if (value.indexOf(':') == -1) {
162                         try {
163                             URL JavaDoc baseURL = new URL JavaDoc(new URL JavaDoc(this.baseUrl), value);
164                             value = baseURL.toExternalForm();
165                         } catch (MalformedURLException JavaDoc 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 JavaDoc(attr);
174                     }
175                     value = attr.getValue(i);
176                     if (value.indexOf(':') == -1) {
177                         try {
178                             URL JavaDoc baseURL = new URL JavaDoc(new URL JavaDoc(this.baseUrl), value);
179                             value = baseURL.toExternalForm();
180                         } catch (MalformedURLException JavaDoc 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     /**
196      * SAX Event Handling
197      */

198     public void endElement(String JavaDoc namespace, String JavaDoc name, String JavaDoc raw)
199     throws SAXException JavaDoc {
200         contentHandler.endElement(namespace,name,raw);
201     }
202
203     /**
204      * SAX Event Handling
205      */

206     public void characters(char ary[], int start, int length)
207     throws SAXException JavaDoc {
208         contentHandler.characters(ary,start,length);
209     }
210
211     /**
212      * SAX Event Handling
213      */

214     public void ignorableWhitespace(char ary[], int start, int length)
215     throws SAXException JavaDoc {
216         contentHandler.ignorableWhitespace(ary,start,length);
217     }
218
219     /**
220      * SAX Event Handling
221      */

222     public void processingInstruction(String JavaDoc target, String JavaDoc data)
223     throws SAXException JavaDoc {
224         contentHandler.processingInstruction(target,data);
225     }
226
227     /**
228      * SAX Event Handling
229      */

230     public void skippedEntity(String JavaDoc name)
231     throws SAXException JavaDoc {
232         contentHandler.skippedEntity(name);
233     }
234
235     /**
236      * SAX Event Handling
237      */

238     public void startDTD(String JavaDoc name, String JavaDoc public_id, String JavaDoc system_id)
239             throws SAXException JavaDoc {
240         if (lexicalHandler != null) lexicalHandler.startDTD(name,public_id,system_id);
241     }
242
243     /**
244      * SAX Event Handling
245      */

246     public void endDTD() throws SAXException JavaDoc {
247         if (lexicalHandler != null) lexicalHandler.endDTD();
248     }
249
250     /**
251      * SAX Event Handling
252      */

253     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
254         if (lexicalHandler != null) lexicalHandler.startEntity(name);
255     }
256
257     /**
258      * SAX Event Handling
259      */

260     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
261         if (lexicalHandler != null) lexicalHandler.endEntity(name);
262     }
263
264     /**
265      * SAX Event Handling
266      */

267     public void startCDATA() throws SAXException JavaDoc {
268         if (lexicalHandler != null) lexicalHandler.startCDATA();
269     }
270
271     /**
272      * SAX Event Handling
273      */

274     public void endCDATA() throws SAXException JavaDoc {
275         if (lexicalHandler != null) lexicalHandler.endCDATA();
276     }
277
278
279     /**
280      * SAX Event Handling
281      */

282     public void comment(char ary[], int start, int length)
283     throws SAXException JavaDoc {
284         if (this.lexicalHandler != null) {
285             lexicalHandler.comment(ary,start,length);
286         }
287     }
288
289 }
290
Popular Tags