KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > AugmentTransformer


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.transformation;
17
18 import org.apache.avalon.framework.parameters.Parameters;
19 import org.apache.cocoon.ProcessingException;
20 import org.apache.cocoon.environment.ObjectModelHelper;
21 import org.apache.cocoon.environment.Request;
22 import org.apache.cocoon.environment.SourceResolver;
23 import org.xml.sax.Attributes JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.helpers.AttributesImpl JavaDoc;
26
27 import java.io.IOException JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31 * @cocoon.sitemap.component.documentation
32  * Augments all <code>href</code> attributes with the full path to
33  * the request. You can optionally specify the <code>mount</code>
34  * parameter.
35  *
36  * @cocoon.sitemap.component.name augment
37  * @cocoon.sitemap.component.logger sitemap.transformer.augment
38  *
39  * @author <a HREF="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
40  * @since October 10, 2001
41  * @version CVS $Id: AugmentTransformer.java 36466 2004-08-16 10:08:39Z cziegeler $
42  */

43 public class AugmentTransformer
44     extends AbstractTransformer {
45         
46     protected Map JavaDoc objectModel;
47     protected Request request;
48     protected String JavaDoc baseURI;
49   
50     public void setup(SourceResolver resolver,
51                       Map JavaDoc objectModel,
52                       String JavaDoc source,
53                       Parameters parameters)
54     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
55         this.objectModel = objectModel;
56         this.request = ObjectModelHelper.getRequest( this.objectModel );
57     
58         String JavaDoc mountPoint = parameters.getParameter("mount", null);
59         
60         StringBuffer JavaDoc uribuf = new StringBuffer JavaDoc();
61         boolean isSecure = this.request.isSecure();
62         int port = this.request.getServerPort();
63     
64         if (isSecure) {
65             uribuf.append("https://");
66         } else {
67             uribuf.append("http://");
68         }
69         uribuf.append(request.getServerName());
70     
71         if (isSecure) {
72             if (port != 443) {
73                 uribuf.append(":").append(port);
74             }
75         } else {
76             if (port != 80) {
77                 uribuf.append(":").append(port);
78             }
79         }
80         if (mountPoint == null) {
81             String JavaDoc requestedURI = this.request.getRequestURI();
82             requestedURI = requestedURI.substring(0, requestedURI.lastIndexOf("/"));
83             uribuf.append(requestedURI);
84             uribuf.append("/");
85         } else {
86             uribuf.append(request.getContextPath());
87             uribuf.append("/");
88             uribuf.append(mountPoint);
89         }
90         this.baseURI = uribuf.toString();
91     }
92
93     public void startElement(String JavaDoc uri,
94                              String JavaDoc name,
95                              String JavaDoc qname,
96                              Attributes JavaDoc attrs)
97     throws SAXException JavaDoc {
98         AttributesImpl JavaDoc newAttrs = null;
99     
100         for (int i = 0, size = attrs.getLength(); i < size; i++) {
101             String JavaDoc attrName = attrs.getLocalName(i);
102             if (attrName.equals("href")) {
103                 String JavaDoc value = attrs.getValue(i);
104
105                 // Don't touch the attribute if it's an absolute URL
106
if (value.startsWith("http:") || value.startsWith("https:")) {
107                     continue;
108                 }
109
110                 if (newAttrs == null) {
111                     newAttrs = new AttributesImpl JavaDoc(attrs);
112                 }
113
114                 String JavaDoc newValue = baseURI + value;
115                 newAttrs.setValue(i, newValue);
116             }
117         }
118
119         if (newAttrs == null) {
120             super.startElement(uri, name, qname, attrs);
121         } else {
122             super.startElement(uri, name, qname, newAttrs);
123         }
124     }
125
126     /**
127      * Recyclable
128      */

129     public void recycle() {
130         this.objectModel = null;
131         this.request = null;
132         this.baseURI = null;
133         super.recycle();
134     }
135 }
136
Popular Tags