KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > coplets > basket > FolderTransformer


1 /*
2  * Copyright 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.coplets.basket;
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.avalon.framework.service.ServiceException;
24 import org.apache.cocoon.ProcessingException;
25 import org.apache.cocoon.environment.wrapper.RequestParameters;
26 import org.apache.cocoon.portal.PortalService;
27 import org.apache.cocoon.portal.coplets.basket.events.UploadItemEvent;
28 import org.apache.cocoon.portal.event.Event;
29 import org.apache.cocoon.xml.AttributesImpl;
30 import org.xml.sax.Attributes JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32
33 /**
34  * This transformer supports the basket feature. It can generate links to
35  * add content and to upload files into the basket.
36  *
37  * @version $Id: BasketTransformer.java 47047 2004-09-22 12:27:27Z vgritsenko $
38  */

39 public class FolderTransformer extends AbstractBasketTransformer {
40
41     /** Element to upload an item */
42     protected static final String JavaDoc UPLOAD_ITEM_ELEMENT = "upload-item";
43
44     /** Element for the upload form */
45     protected static final String JavaDoc UPLOAD_FORM_ELEMENT = "upload-form";
46
47     /** Upload element list */
48     protected List JavaDoc uploadElements = new ArrayList JavaDoc();
49
50     /* (non-Javadoc)
51      * @see org.apache.avalon.excalibur.pool.Recyclable#recycle()
52      */

53     public void recycle() {
54         this.uploadElements.clear();
55         super.recycle();
56     }
57
58     /* (non-Javadoc)
59      * @see org.apache.cocoon.transformation.AbstractSAXTransformer#endTransformingElement(java.lang.String, java.lang.String, java.lang.String)
60      */

61     public void endTransformingElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw)
62     throws ProcessingException, IOException JavaDoc, SAXException JavaDoc {
63         if ( UPLOAD_ITEM_ELEMENT.equals(name) ) {
64             this.endElement("", "input", "input");
65         } else if ( UPLOAD_FORM_ELEMENT.equals(name) ) {
66             this.endElement("", "form", "form");
67             this.uploadElements = new ArrayList JavaDoc();
68         }
69     }
70
71     /* (non-Javadoc)
72      * @see org.apache.cocoon.transformation.AbstractSAXTransformer#startTransformingElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
73      */

74     public void startTransformingElement(String JavaDoc uri, String JavaDoc name,
75                                          String JavaDoc raw, Attributes JavaDoc attr)
76     throws ProcessingException, IOException JavaDoc, SAXException JavaDoc {
77         if ( UPLOAD_ITEM_ELEMENT.equals(name) ) {
78             this.uploadElements.add(attr.getValue("name"));
79             this.startElement("", "input", "input", attr);
80         } else if ( UPLOAD_FORM_ELEMENT.equals(name) ) {
81             AttributesImpl ai = new AttributesImpl(attr);
82             PortalService service = null;
83             String JavaDoc parameters;
84             try {
85                 service = (PortalService)this.manager.lookup(PortalService.ROLE);
86                 Event e = new UploadItemEvent(this.basketManager.getFolder(), this.uploadElements);
87                 parameters = service.getComponentManager().getLinkService().getLinkURI(e);
88                 int pos = parameters.indexOf('?');
89                 ai.addCDATAAttribute("action", parameters.substring(0, pos));
90                 parameters = parameters.substring(pos+1);
91             } catch (ServiceException se) {
92                 throw new SAXException JavaDoc("Unable to lookup portal service.", se);
93             } finally {
94                 this.manager.release(service);
95             }
96             this.startElement("", "form", "form", ai);
97             if ( parameters != null && parameters.length() > 0 ) {
98                 // create hidden input fields
99
RequestParameters pars = new RequestParameters(parameters);
100                 Enumeration JavaDoc enumPars = pars.getParameterNames();
101                 while ( enumPars.hasMoreElements() ) {
102                     String JavaDoc pName = (String JavaDoc)enumPars.nextElement();
103                     String JavaDoc pValue = pars.getParameter(pName);
104                     AttributesImpl hiddenAttrs = new AttributesImpl();
105                     hiddenAttrs.addCDATAAttribute("type", "hidden");
106                     hiddenAttrs.addCDATAAttribute("name", pName);
107                     hiddenAttrs.addCDATAAttribute("value", pValue);
108                     this.startElement("", "input", "input", hiddenAttrs);
109                     this.endElement("", "input", "input");
110                 }
111             }
112         }
113     }
114
115 }
116
Popular Tags