KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.environment.wrapper.RequestParameters;
25 import org.apache.cocoon.portal.LinkService;
26 import org.apache.cocoon.portal.coplet.CopletInstanceData;
27 import org.apache.cocoon.portal.event.impl.ChangeCopletInstanceAspectDataEvent;
28 import org.apache.cocoon.portal.event.impl.CopletJXPathEvent;
29 import org.apache.cocoon.portal.event.impl.JXPathEvent;
30 import org.apache.cocoon.xml.AttributesImpl;
31 import org.apache.cocoon.xml.XMLUtils;
32 import org.apache.commons.jxpath.JXPathContext;
33 import org.apache.excalibur.xml.sax.XMLizable;
34 import org.xml.sax.Attributes JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36
37 /**
38  * This transformer offers various functions for developing pipeline based coplets.
39  *
40  * Includes coplet instance data by using JXPath expressions.
41  * The transformer searches for tags <coplet:coplet xmlns:coplet="http://apache.org/cocoon/portal/coplet/1.0">.
42  * They must have an attribute "select" that contains a valid JXPath expression applying to the coplet instance data.<br><br>
43  *
44  * Example:<br><br>
45  *
46  * <pre>&lt;maxpageable xmlns:coplet="http://apache.org/cocoon/portal/coplet/1.0"&gt;
47  * &lt;coplet:coplet select="copletData.maxpageable"/&gt;
48  * &lt;/maxpageable&gt;<br></pre>
49  *
50  * The transformer will insert the boolean value specifying whether the coplet is
51  * maxpageable or not.<br>
52  * Please see also the documentation of superclass AbstractCopletTransformer for how
53  * the coplet instance data are acquired.
54  *
55  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
56  * @author <a HREF="mailto:bluetkemeier@s-und-n.de">Bj&ouml;rn L&uuml;tkemeier</a>
57  * @version CVS $Id: CopletTransformer.java 344185 2005-11-14 19:40:03Z cziegeler $
58  */

59 public class CopletTransformer
60 extends AbstractCopletTransformer {
61
62     /**
63      * The namespace URI to listen for.
64      */

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

70     public static final String JavaDoc COPLET_ELEM = "coplet";
71
72     /**
73      * The attribute containing the JXPath expression.
74      */

75     public static final String JavaDoc SELECT_ATTR = "select";
76
77
78     /**
79      * The XML element name to listen for.
80      */

81     public static final String JavaDoc LINK_ELEM = "link";
82
83     /** Create a link containing several events */
84     public static final String JavaDoc LINKS_ELEM = "links";
85
86     /** Create a link containing several events */
87     public static final String JavaDoc PARAMETER_ELEM = "parameter";
88
89     /** The content for the links element */
90     public static final String JavaDoc CONTENT_ELEM = "content";
91
92     /** Are we inside a links element? */
93     protected boolean insideLinks;
94
95     /** The collected list of events */
96     protected List JavaDoc collectedEvents = new ArrayList JavaDoc();
97
98     /** The content of the links */
99     protected XMLizable content;
100
101     /**
102      * Creates new CopletTransformer.
103      */

104     public CopletTransformer() {
105         this.defaultNamespaceURI = NAMESPACE_URI;
106     }
107
108     /* (non-Javadoc)
109      * @see org.apache.cocoon.transformation.AbstractSAXTransformer#setupTransforming()
110      */

111     public void setupTransforming()
112     throws IOException JavaDoc, ProcessingException, SAXException JavaDoc {
113         super.setupTransforming();
114         this.insideLinks = false;
115         this.content = null;
116         this.collectedEvents.clear();
117     }
118
119     /**
120      * Overridden from superclass.
121      */

122     public void startTransformingElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attr)
123     throws ProcessingException, IOException JavaDoc, SAXException JavaDoc {
124         if (name.equals(COPLET_ELEM)) {
125             String JavaDoc expression = attr.getValue(SELECT_ATTR);
126             if (expression == null) {
127                 throw new ProcessingException("Attribute "+SELECT_ATTR+" must be spcified.");
128             }
129
130             final CopletInstanceData cid = this.getCopletInstanceData();
131
132             final JXPathContext jxpathContext = JXPathContext.newContext( cid );
133             final Object JavaDoc object = jxpathContext.getValue(expression);
134
135             if (object != null) {
136                 XMLUtils.valueOf(contentHandler, object);
137             }
138
139         } else if (name.equals(LINK_ELEM)) {
140
141             final LinkService linkService = this.portalService.getComponentManager().getLinkService();
142             final String JavaDoc format = attr.getValue("format");
143             AttributesImpl newAttrs = new AttributesImpl();
144             newAttrs.setAttributes(attr);
145             newAttrs.removeAttribute("format");
146
147             if ( attr.getValue("href") != null ) {
148                 final CopletInstanceData cid = this.getCopletInstanceData();
149                 ChangeCopletInstanceAspectDataEvent event = new ChangeCopletInstanceAspectDataEvent(cid, null, null);
150
151                 String JavaDoc value = linkService.getLinkURI(event);
152                 if (value.indexOf('?') == -1) {
153                     value = value + '?' + attr.getValue("href");
154                 } else {
155                     value = value + '&' + attr.getValue("href");
156                 }
157                 newAttrs.removeAttribute("href");
158                 this.output(value, format, newAttrs );
159             } else {
160                 final String JavaDoc path = attr.getValue("path");
161                 final String JavaDoc value = attr.getValue("value");
162
163                 newAttrs.removeAttribute("path");
164                 newAttrs.removeAttribute("value");
165
166                 JXPathEvent event = null;
167                 if ( attr.getValue("layout") != null ) {
168                     newAttrs.removeAttribute("layout");
169                     final String JavaDoc layoutId = attr.getValue("layout");
170                     Object JavaDoc layout = this.portalService.getComponentManager().getProfileManager().getPortalLayout(null, layoutId);
171                     if ( layout != null ) {
172                         event = new JXPathEvent(layout, path, value);
173                     }
174                 } else {
175                     String JavaDoc copletId = attr.getValue("coplet");
176                     newAttrs.removeAttribute("coplet");
177                     final CopletInstanceData cid = this.getCopletInstanceData(copletId);
178                     if ( cid != null ) {
179                         event = new CopletJXPathEvent(cid, path, value);
180                     }
181                 }
182                 if ( this.insideLinks ) {
183                     if ( event != null ) {
184                         this.collectedEvents.add(event);
185                     }
186                 } else {
187                     final String JavaDoc href = linkService.getLinkURI(event);
188                     this.output(href, format, newAttrs );
189                 }
190             }
191         } else if (name.equals(PARAMETER_ELEM)) {
192             if (this.insideLinks) {
193                 String JavaDoc href = attr.getValue("href");
194                 if ( href != null ) {
195                     final int pos = href.indexOf('?');
196                     if ( pos != -1 ) {
197                         href = href.substring(pos+1);
198                     }
199                     this.collectedEvents.add(new LinkService.ParameterDescription(href));
200                 }
201             }
202         } else if (name.equals(LINKS_ELEM) ) {
203             this.insideLinks = true;
204             final AttributesImpl newAttrs = new AttributesImpl();
205             newAttrs.setAttributes(attr);
206             newAttrs.removeAttribute("format");
207             this.stack.push(newAttrs);
208
209             String JavaDoc format = attr.getValue("format");
210             if ( format == null ) {
211                 format = "html-link";
212             }
213             this.stack.push(format);
214         } else if ( name.equals(CONTENT_ELEM) && this.insideLinks ) {
215             this.startSAXRecording();
216         } else {
217             super.startTransformingElement(uri, name, raw, attr);
218         }
219     }
220
221     /**
222      * Overridden from superclass.
223      */

224     public void endTransformingElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw)
225     throws ProcessingException, IOException JavaDoc, SAXException JavaDoc {
226         if ( name.equals(LINK_ELEM) ) {
227             if ( !this.insideLinks ) {
228                 String JavaDoc elem = (String JavaDoc)this.stack.pop();
229                 if ( elem.length() > 0 ) {
230                     this.sendEndElementEvent(elem);
231                 }
232             }
233         } else if ( name.equals(LINKS_ELEM) ) {
234             this.insideLinks = false;
235             final String JavaDoc format = (String JavaDoc)this.stack.pop();
236             final LinkService linkService = this.portalService.getComponentManager().getLinkService();
237             String JavaDoc href = linkService.getLinkURI(this.collectedEvents);
238
239             AttributesImpl newAttrs = (AttributesImpl)this.stack.pop();
240             // test for alternate base url
241
final String JavaDoc baseURL = newAttrs.getValue("base-url");
242             if ( baseURL != null ) {
243                 newAttrs.removeAttribute("base-url");
244                 int pos = href.indexOf('?') + 1;
245                 final char separator;
246                 if ( baseURL.indexOf('?') == -1 ) {
247                     separator = '?';
248                 } else {
249                     separator = '&';
250                 }
251                 href = baseURL + separator + href.substring(pos);
252
253             }
254             this.output(href, format, newAttrs );
255
256             this.collectedEvents.clear();
257             if ( this.content != null ) {
258                 this.content.toSAX(this.contentHandler);
259                 this.content = null;
260             }
261             String JavaDoc elem = (String JavaDoc)this.stack.pop();
262             if ( elem.length() > 0 ) {
263                 this.sendEndElementEvent(elem);
264             }
265         } else if ( name.equals(CONTENT_ELEM) && this.insideLinks ) {
266             this.content = this.endSAXRecording();
267         } else if (!name.equals(COPLET_ELEM) && !name.equals(PARAMETER_ELEM)) {
268             super.endTransformingElement(uri, name, raw);
269         }
270     }
271
272     /**
273      * Output the link
274      */

275     protected void output(String JavaDoc uri, String JavaDoc format, AttributesImpl newAttrs)
276     throws SAXException JavaDoc {
277         if ( format == null ) {
278             // default
279
format = "html-link";
280         }
281
282         if ( "html-link".equals(format) ) {
283             newAttrs.addCDATAAttribute("href", uri);
284             this.sendStartElementEvent("a", newAttrs);
285             this.stack.push("a");
286
287         } else if ( "html-form".equals(format) ) {
288             boolean addParametersAsHiddenFields = false;
289             String JavaDoc parameters = null;
290             final String JavaDoc enctype = newAttrs.getValue("enctype");
291             if ( enctype== null
292                 || "application/x-www-form-urlencoded".equalsIgnoreCase(enctype)
293                 || "multipart/form-data".equalsIgnoreCase(enctype) ) {
294                 final int pos = uri.indexOf('?');
295                 if ( pos != -1 ) {
296                     parameters = uri.substring(pos+1);
297                     uri = uri.substring(0, pos);
298                     addParametersAsHiddenFields = true;
299                 }
300             }
301             newAttrs.addCDATAAttribute("action", uri);
302             this.sendStartElementEvent("form", newAttrs);
303             this.stack.push("form");
304             if ( addParametersAsHiddenFields ) {
305                 // create hidden input fields
306
RequestParameters pars = new RequestParameters(parameters);
307                 Enumeration JavaDoc enumeration = pars.getParameterNames();
308                 while ( enumeration.hasMoreElements() ) {
309                     final String JavaDoc pName = (String JavaDoc)enumeration.nextElement();
310                     final String JavaDoc[] pValues = pars.getParameterValues(pName);
311                     for(int k=0; k<pValues.length; k++) {
312                         final String JavaDoc pValue = pValues[k];
313                         AttributesImpl hiddenAttrs = new AttributesImpl();
314                         hiddenAttrs.addCDATAAttribute("type", "hidden");
315                         hiddenAttrs.addCDATAAttribute("name", pName);
316                         hiddenAttrs.addCDATAAttribute("value", pValue);
317                         this.startElement("", "input", "input", hiddenAttrs);
318                         this.endElement("", "input", "input");
319                     }
320                 }
321
322             }
323         } else if ( "text".equals(format) ) {
324             this.sendTextEvent(uri);
325             this.stack.push("");
326         } else if ( "parameters".equals(format) ) {
327             final String JavaDoc value = uri.substring(uri.indexOf('?')+1);
328             this.sendTextEvent(value);
329             this.stack.push("");
330         } else {
331             // own format
332
newAttrs.addCDATAAttribute("href", uri);
333             this.sendStartElementEvent("link", newAttrs);
334             this.stack.push("link");
335         }
336     }
337 }
338
Popular Tags