KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2002,2004-2005 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 org.apache.cocoon.portal.coplet.CopletInstanceData;
19 import org.apache.cocoon.portal.event.impl.CopletLinkEvent;
20 import org.apache.cocoon.xml.AttributesImpl;
21 import org.xml.sax.Attributes JavaDoc;
22 import org.xml.sax.ContentHandler JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24
25 /**
26  * This transformer is used to replace links (URIs) from elements
27  * like <a HREF="URI"> or <form action="URI"> with portal
28  * event uris. Therefore the transformer searches for <eventlink>
29  * elements replaces the URI form the attribute which is specified within
30  * an attribute called "attribute" and renames the element as specified
31  * within an attribute called "element".
32  *
33  * Example:<br><br>
34  *
35  * <pre>
36  * &lt;root xmlns:ev="http://apache.org/cocoon/portal/eventlink/1.0"&gt;
37  * &lt;ev:eventlink HREF="http://eventlinkexample" element="a" attribute="href"&gt;linktext&lt;/ev:eventlink&gt;
38  * &lt;/root&gt;<br></pre>
39  *
40  * will be replaced with something like:<br><br>
41  *
42  * <pre>
43  * &lt;root&gt;
44  * &lt;a HREF="portal?cocoon-portal-event=8"&gt;linktext&lt;/a&gt;
45  * &lt;/root&gt;<br></pre>
46  *
47  * The transformer will create two CopletLinkEvents and insert corresponding links
48  * to them to the XML instead of "http://eventlinkexample". If such a link is pressed
49  * the corresponding CopletLinkEvent is sent to the Subscribers to be handled.<br>
50  * Please see also the documentation of superclass AbstractCopletTransformer for how
51  * the coplet instance data are acquired.
52  *
53  * @author <a HREF="mailto:gernot.koller@rizit.at">Gernot Koller</a>
54  *
55  * @version CVS $Id: NewEventLinkTransformer.java 344185 2005-11-14 19:40:03Z cziegeler $
56  */

57 public class NewEventLinkTransformer extends AbstractCopletTransformer {
58
59     /**
60      * The namespace URI to listen for.
61      */

62     public static final String JavaDoc NAMESPACE_URI =
63         "http://apache.org/cocoon/portal/eventlink/1.0";
64
65     /**
66      * The XML element name to listen for.
67      */

68     public static final String JavaDoc EVENT_ELEM = "eventlink";
69
70     /**
71      * An attribute's name of EVENT_ELEMENT.
72      */

73     public static final String JavaDoc ATTRIBUTE_ATTR = "attribute";
74
75     /**
76      * An attribute's name of EVENT_ELEMENT.
77      */

78     public static final String JavaDoc ELEMENT_ATTR = "element";
79
80     /**
81      * @see java.lang.Object#Object()
82      */

83     public NewEventLinkTransformer() {
84         this.defaultNamespaceURI = NAMESPACE_URI;
85     }
86
87     /**
88      * @throws SAXException when the eventlink element does not contain the necessary attributes
89      * "element" and "attribute", retrieving the LinkURI from the LinkService fails,
90      * or an unknown element within the namespaces in encountered.
91      * @see org.apache.cocoon.transformation.AbstractSAXTransformer#startTransformingElement(String, String, String, Attributes)
92      */

93     public void startTransformingElement(String JavaDoc uri,
94                                          String JavaDoc name,
95                                          String JavaDoc raw,
96                                          Attributes JavaDoc attributes)
97     throws SAXException JavaDoc {
98         if (!EVENT_ELEM.equals(name)) {
99             throw new SAXException JavaDoc("Unknown element encountered: " + name);
100         }
101
102         String JavaDoc attributeName = attributes.getValue(ATTRIBUTE_ATTR);
103         String JavaDoc elementName = attributes.getValue(ELEMENT_ATTR);
104
105         if (attributeName == null) {
106             throw new SAXException JavaDoc(
107                 "Element "
108                     + EVENT_ELEM
109                     + " must have an attribute "
110                     + ATTRIBUTE_ATTR
111                     + ".");
112         }
113
114         if (elementName == null) {
115             throw new SAXException JavaDoc(
116                 "Element "
117                     + EVENT_ELEM
118                     + " must have an attribute "
119                     + ELEMENT_ATTR
120                     + ".");
121         }
122
123         // remove ATTRIBUTE_ATTR, "coplet" and ELEMENT_ATTR from attributes
124
AttributesImpl newAttributes = this.getMutableAttributes(attributes);
125         newAttributes.removeAttribute(ELEMENT_ATTR);
126         newAttributes.removeAttribute(ATTRIBUTE_ATTR);
127         newAttributes.removeAttribute("coplet");
128
129         int index = newAttributes.getIndex(attributeName);
130         String JavaDoc link = newAttributes.getValue(index);
131
132         boolean formSpecialTreatment = false;
133         if ("form".equals(elementName)) {
134             //cut all query parameters from actions with method get, as these will be normaly ignored!
135
formSpecialTreatment = true;
136             if ("GET".equalsIgnoreCase(newAttributes.getValue("method"))
137                 && link.indexOf('?') > 0) {
138                 link = link.substring(0, link.indexOf('?'));
139             }
140         }
141
142         String JavaDoc portalAction = null;
143         String JavaDoc portalEvent = null;
144
145         // if attribute found that contains a link
146
if (link != null) {
147             CopletInstanceData cid = this.getCopletInstanceData(attributes.getValue("coplet"));
148             // create event link
149
CopletLinkEvent event = new CopletLinkEvent(cid, link);
150             String JavaDoc eventLink = this.portalService.getComponentManager().getLinkService().getLinkURI(event);
151
152             //form elements need hidden inputs to change request parameters
153
if (formSpecialTreatment) {
154                 int begin =
155                     eventLink.indexOf("cocoon-portal-action=")
156                         + "cocoon-portal-action=".length();
157                 int end = eventLink.indexOf('&', begin);
158                 if (end == -1) {
159                     end = eventLink.length();
160                 }
161
162                 portalAction = eventLink.substring(begin, end);
163
164                 begin =
165                     eventLink.indexOf("cocoon-portal-event=")
166                         + "cocoon-portal-event=".length();
167                 end = eventLink.indexOf('&', begin);
168                 if (end == -1) {
169                     end = eventLink.length();
170                 }
171                 portalEvent = eventLink.substring(begin, end);
172
173                 eventLink =
174                     eventLink.substring(0, eventLink.indexOf('?'));
175             }
176
177             // insert event link
178
newAttributes.setValue(index, eventLink);
179         }
180
181         this.stack.push(elementName);
182
183         contentHandler.startElement(
184             "",
185             elementName,
186             elementName,
187             newAttributes);
188
189         //generate hidden inputs to add request parameters to the form action
190
if (formSpecialTreatment) {
191             sendHiddenFields(contentHandler, portalAction, portalEvent);
192         }
193     }
194
195     /**
196      * With forms the uri in the action attribute cannot be enhanced with request parameters.
197      * Instead hidden input fields must be inserted into the SAX stream to add request parameters.
198      * This method sends two hidden inputs adding the "cocoon-portal-action" parameter and
199      * the "cocoon-portal-event" parameter.
200      * @param contentHandler the content handler recieving the SAX events
201      * @param portalAction value of the "cocoon-portal-action" parameter
202      * @param portalEvent value of the "cocoon-portal-event" parameter
203      * @throws SAXException if sending the SAX events failed
204      */

205     private void sendHiddenFields(ContentHandler JavaDoc contentHandler,
206                                   String JavaDoc portalAction,
207                                   String JavaDoc portalEvent)
208     throws SAXException JavaDoc {
209         AttributesImpl attributes = new AttributesImpl();
210         attributes.addAttribute("", "type", "type", "CDATA", "hidden");
211         attributes.addAttribute(
212             "",
213             "name",
214             "name",
215             "CDATA",
216             "cocoon-portal-action");
217         attributes.addAttribute("", "value", "value", "CDATA", portalAction);
218         contentHandler.startElement("", "input", "input", attributes);
219         contentHandler.endElement("", "input", "input");
220
221         attributes = new AttributesImpl();
222         attributes.addAttribute("", "type", "type", "CDATA", "hidden");
223         attributes.addAttribute(
224             "",
225             "name",
226             "name",
227             "CDATA",
228             "cocoon-portal-event");
229         attributes.addAttribute("", "value", "value", "CDATA", portalEvent);
230         contentHandler.startElement("", "input", "input", attributes);
231         contentHandler.endElement("", "input", "input");
232     }
233
234     /**
235      * @see org.apache.cocoon.transformation.AbstractSAXTransformer#endTransformingElement(String, String, String)
236      */

237     public void endTransformingElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw)
238     throws SAXException JavaDoc {
239         String JavaDoc elementName = (String JavaDoc) this.stack.pop();
240         contentHandler.endElement("", elementName, elementName);
241     }
242 }
243
Popular Tags