KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > transformation > HTMLEventLinkTransformer


1 /*
2  * Copyright 2004,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.portal.transformation;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.cocoon.ProcessingException;
23 import org.apache.cocoon.environment.SourceResolver;
24 import org.apache.cocoon.portal.coplet.CopletInstanceData;
25 import org.apache.cocoon.xml.AttributesImpl;
26 import org.apache.excalibur.source.SourceUtil;
27 import org.xml.sax.Attributes JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29
30 /**
31  * This transformer transforms html actions into events.
32  * The transformer listens for the element a and form. Links
33  * that only contain an anchor are ignored.
34  * In addition if a link has the attribute "external" with the value
35  * "true", the link is also ignored.
36  *
37  * TODO: Support target attribute
38  *
39  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
40  * @version CVS $Id: HTMLEventLinkTransformer.java 325892 2005-10-17 13:58:57Z cziegeler $
41  */

42 public class HTMLEventLinkTransformer
43 extends AbstractCopletTransformer {
44
45     /** The temporary attribute used to store the uri */
46     protected String JavaDoc attributeName;
47
48     /** The jxpath for the attribute */
49     protected String JavaDoc jxPath;
50
51     /* (non-Javadoc)
52      * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
53      */

54     public void setup(SourceResolver resolver,
55                       Map JavaDoc objectModel,
56                       String JavaDoc src,
57                       Parameters par)
58     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
59         super.setup(resolver, objectModel, src, par);
60         this.attributeName = par.getParameter("attribute-name", "application-uri");
61         this.jxPath = "temporaryAttributes/" + this.attributeName;
62     }
63
64     /* (non-Javadoc)
65      * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
66      */

67     public void startElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attr)
68     throws SAXException JavaDoc {
69         boolean processed = false;
70         if ("a".equals(name) ) {
71             boolean convert = false;
72             final boolean isRemoteAnchor = this.isRemoteAnchor(attr);
73             if ( isRemoteAnchor ) {
74                 convert = !this.isExternalLink(attr);
75             }
76             this.stack.push(convert ? Boolean.TRUE: Boolean.FALSE);
77             if ( convert ) {
78                 this.createAnchorEvent(attr);
79                 processed = true;
80             }
81         } else if ("form".equals(name) ) {
82             this.createFormEvent(attr);
83             processed = true;
84         }
85         if ( !processed ) {
86             super.startElement(uri, name, raw, attr);
87         }
88     }
89
90     /* (non-Javadoc)
91      * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
92      */

93     public void endElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw)
94     throws SAXException JavaDoc {
95         boolean processed = false;
96         if ( "a".equals(name) ) {
97             final Boolean JavaDoc converted = (Boolean JavaDoc)this.stack.pop();
98             if ( converted.booleanValue() ) {
99                 this.xmlConsumer.endElement(CopletTransformer.NAMESPACE_URI,
100                                             CopletTransformer.LINK_ELEM,
101                                             "coplet:" + CopletTransformer.LINK_ELEM);
102                 this.xmlConsumer.endPrefixMapping("coplet");
103                 processed = true;
104             }
105         } else if ( "form".equals(name) ) {
106             this.xmlConsumer.endElement(CopletTransformer.NAMESPACE_URI,
107                     CopletTransformer.LINK_ELEM,
108                     "coplet:" + CopletTransformer.LINK_ELEM);
109             this.xmlConsumer.endPrefixMapping("coplet");
110             processed = true;
111         }
112         if ( !processed ) {
113             super.endElement(uri, name, raw);
114         }
115     }
116
117     protected void createAnchorEvent(Attributes JavaDoc attributes)
118     throws SAXException JavaDoc {
119         AttributesImpl newAttributes = new AttributesImpl(attributes);
120         newAttributes.removeAttribute("href");
121         newAttributes.removeAttribute("external");
122         String JavaDoc link = attributes.getValue("href");
123
124         CopletInstanceData cid = this.getCopletInstanceData();
125         link = this.getLink((String JavaDoc)cid.getTemporaryAttribute(this.attributeName), link);
126
127         newAttributes.addCDATAAttribute("path", this.jxPath);
128         newAttributes.addCDATAAttribute("value", link);
129         newAttributes.addCDATAAttribute("coplet", cid.getId());
130         newAttributes.addCDATAAttribute("format", "html-link");
131         this.xmlConsumer.startPrefixMapping("coplet", CopletTransformer.NAMESPACE_URI);
132         this.xmlConsumer.startElement(CopletTransformer.NAMESPACE_URI,
133                                       CopletTransformer.LINK_ELEM,
134                                       "coplet:" + CopletTransformer.LINK_ELEM,
135                                       newAttributes);
136     }
137
138     protected void createFormEvent(Attributes JavaDoc attributes)
139     throws SAXException JavaDoc {
140         AttributesImpl newAttributes = new AttributesImpl(attributes);
141         newAttributes.removeAttribute("action");
142         String JavaDoc link = attributes.getValue("action");
143
144         CopletInstanceData cid = this.getCopletInstanceData();
145         link = this.getLink((String JavaDoc)cid.getTemporaryAttribute(this.attributeName), link);
146
147         newAttributes.addCDATAAttribute("path", this.jxPath);
148         newAttributes.addCDATAAttribute("value", link);
149         newAttributes.addCDATAAttribute("coplet", cid.getId());
150         newAttributes.addCDATAAttribute("format", "html-form");
151         if ( newAttributes.getIndex("method") == -1 ) {
152             newAttributes.addCDATAAttribute("method", "POST");
153         }
154
155         this.xmlConsumer.startPrefixMapping("coplet", CopletTransformer.NAMESPACE_URI);
156         this.xmlConsumer.startElement(CopletTransformer.NAMESPACE_URI,
157                 CopletTransformer.LINK_ELEM,
158                 "coplet:" + CopletTransformer.LINK_ELEM,
159                 newAttributes);
160
161     }
162
163     protected String JavaDoc getLink(String JavaDoc base, String JavaDoc link) {
164         final String JavaDoc v = SourceUtil.absolutize(base, link);
165         return v;
166     }
167
168     /**
169      * Determine if the element is an url and if the url points to some
170      * remote source.
171      *
172      * @param attributes the attributes of the element
173      * @return true if the href url is an anchor pointing to a remote source
174      */

175     protected boolean isRemoteAnchor(Attributes JavaDoc attributes) {
176         String JavaDoc link = attributes.getValue("href");
177
178         // no empty link to current document
179
if (link != null && link.trim().length() > 0) {
180             // check reference to document fragment
181
if (!link.trim().startsWith("#")) {
182                 return true;
183             }
184         }
185
186         return false;
187     }
188
189     /**
190      * a link in an external application is not transformed
191      * if there is an attribute external="true" in the link-element
192      * or if the link starts with "mailto:".
193      *
194      * @param attributes attributes of the node
195      * @return true if the attribute 'external' is 'true'
196      */

197     private boolean isExternalLink (Attributes JavaDoc attributes) {
198         final String JavaDoc external = attributes.getValue("external");
199         // links to external documents will be not transformed to portal links
200
if (external != null && external.trim().length() > 0
201             && external.trim().toLowerCase().equals ("true") ) {
202             return true;
203         }
204         final String JavaDoc link = attributes.getValue("href");
205         if ( link != null
206              && (link.startsWith("mailto:") || link.startsWith("javascript:") ) ) {
207             return true;
208         }
209         return false;
210     }
211     
212 }
213
Popular Tags