KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Stack JavaDoc;
19
20 import org.apache.cocoon.portal.LinkService;
21 import org.apache.cocoon.portal.coplet.CopletInstanceData;
22 import org.apache.cocoon.portal.event.impl.CopletLinkEvent;
23 import org.w3c.dom.DocumentFragment JavaDoc;
24 import org.xml.sax.Attributes JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.helpers.AttributesImpl JavaDoc;
27
28 /**
29  * This transformer searches for event descriptions in the XML.
30  * For each one an event is created and the event link is inserted into the XML
31  * instead of the description.<br><br>
32  *
33  * Example:<br><br>
34  *
35  * <pre>&lt;root xmlns:event="http://apache.org/cocoon/portal/eventlink/1.0"&gt;
36  * &lt;event:event attribute="href"&gt;
37  * &lt;a HREF="http://eventlinkexample"/&gt;
38  * &lt;/event:event&gt;
39  * &lt;event:event element="uri"&gt;
40  * &lt;link&gt;&lt;uri&gt;http://eventlinkexample&lt;/uri&gt;&lt;/link&gt;
41  * &lt;/event:event&gt;
42  * &lt;/root&gt;<br></pre>
43  *
44  * The transformer will create two CopletLinkEvents and insert corresponding links
45  * to them to the XML instead of "http://eventlinkexample". If such a link is pressed
46  * the corresponding CopletLinkEvent is sent to the Subscribers to be handled.<br>
47  * Please see also the documentation of superclass AbstractCopletTransformer for how
48  * the coplet instance data are acquired.
49  *
50  * @author <a HREF="mailto:bluetkemeier@s-und-n.de">Bj&ouml;rn L&uuml;tkemeier</a>
51  * @version $Id: EventLinkTransformer.java 227171 2005-08-03 06:08:26Z cziegeler $
52  */

53 public class EventLinkTransformer extends AbstractCopletTransformer {
54
55     /**
56      * The namespace URI to listen for.
57      */

58     public static final String JavaDoc NAMESPACE_URI = "http://apache.org/cocoon/portal/eventlink/1.0";
59
60     /**
61      * The XML element name to listen for.
62      */

63     public static final String JavaDoc EVENT_ELEM = "event";
64
65     /**
66      * An attribute's name of EVENT_ELEMENT.
67      */

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

73     public static final String JavaDoc ELEMENT_ATTR = "element";
74
75     /**
76      * Used to signal whether the transformer is inside an EVENT_ELEM tag.
77      */

78     private boolean insideEvent = false;
79
80     /**
81      * The attribute defining the link inside an EVENT_ELEM tag.
82      */

83     private String JavaDoc attributeName;
84
85     /**
86      * The element defining the link inside an EVENT_ELEM tag.
87      */

88     private String JavaDoc elementName;
89
90     /**
91      * Used to store elements' attributes between startTransformingElement and endTransformingElement.
92      */

93     private Stack JavaDoc attrStack = new Stack JavaDoc();
94
95     /**
96      * Overridden from superclass.
97      */

98     public void recycle() {
99         this.insideEvent = false;
100         this.attributeName = null;
101         this.elementName = null;
102         this.attrStack.clear();
103
104         super.recycle();
105     }
106
107     /**
108      * Overridden from superclass.
109      */

110     public void startElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attr)
111     throws SAXException JavaDoc {
112
113         if (uri.equals(NAMESPACE_URI) && name.equals(EVENT_ELEM)) {
114             if (this.insideEvent) {
115                 throw new SAXException JavaDoc("Elements "+EVENT_ELEM+" must not be nested.");
116             }
117             this.insideEvent = true;
118
119             // get element or attribute name that contains links
120
this.attributeName = attr.getValue(ATTRIBUTE_ATTR);
121             this.elementName = attr.getValue(ELEMENT_ATTR);
122
123             // at least one of them must be set
124
if (this.attributeName == null && this.elementName == null) {
125                 throw new SAXException JavaDoc("Element "+EVENT_ELEM+" must have one of attributes "+ATTRIBUTE_ATTR+" and "+ELEMENT_ATTR+".");
126             }
127         } else {
128             if (this.insideEvent) {
129                 // store attributes for endTransformingElement
130
this.attrStack.push(new AttributesImpl JavaDoc(attr));
131
132                 /* Record element content. In case of an element we asume, that no
133                  * children exist but only text content, since the text content shall
134                  * be the link. Therefore we do startTextRecording. Otherwise we
135                  * record the whole subtree.
136                  */

137                 if (this.elementName != null && name.equals(this.elementName)) {
138                     this.startTextRecording();
139                 } else {
140                     this.startRecording();
141                 }
142             } else {
143                 super.startElement(uri, name, raw, attr);
144             }
145         }
146     }
147
148     /**
149      * Overridden from superclass.
150      */

151     public void endElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw)
152     throws SAXException JavaDoc {
153
154         if (uri.equals(NAMESPACE_URI) && name.equals(EVENT_ELEM)) {
155             this.attributeName = null;
156             this.elementName = null;
157             this.insideEvent = false;
158         } else {
159             if (this.insideEvent) {
160                 AttributesImpl JavaDoc attr = (AttributesImpl JavaDoc)this.attrStack.pop();
161
162                 // process attribute that contains link
163
if (this.attributeName != null) {
164                     int index = attr.getIndex(this.attributeName);
165                     String JavaDoc link = attr.getValue(index);
166
167                     // if attribute found that contains a link
168
if (link != null) {
169                         CopletInstanceData cid = this.getCopletInstanceData(attr.getValue("coplet"));
170                         LinkService linkService = this.portalService.getComponentManager().getLinkService();
171
172                         // create event link
173
CopletLinkEvent event = new CopletLinkEvent(cid, link);
174                         String JavaDoc eventLink = linkService.getLinkURI(event);
175
176                         // insert event link
177
attr.setValue(index, eventLink);
178                     }
179                 }
180
181                 String JavaDoc eventLink = null;
182                 DocumentFragment JavaDoc fragment = null;
183
184                 // process element that contains link
185
if (this.elementName != null && name.equals(this.elementName)) {
186                     String JavaDoc link = this.endTextRecording();
187
188                     CopletInstanceData cid = this.getCopletInstanceData();
189                     LinkService linkService = this.portalService.getComponentManager().getLinkService();
190
191                     // create event link
192
CopletLinkEvent event = new CopletLinkEvent(cid, link);
193                     eventLink = linkService.getLinkURI(event);
194                 } else {
195                     fragment = this.endRecording();
196                 }
197
198                 // stream element
199
super.startElement(uri, name, raw, attr);
200                 if (eventLink != null) {
201                     // insert event link
202
super.characters(eventLink.toCharArray(), 0, eventLink.length());
203                 } else if (fragment != null) {
204                     super.sendEvents(fragment);
205                 }
206                 super.endElement(uri, name, raw);
207             } else {
208                 super.endElement(uri, name, raw);
209             }
210         }
211     }
212 }
213
Popular Tags