001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.coplets.basket;
018:
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.Enumeration;
022: import java.util.List;
023:
024: import org.apache.avalon.framework.service.ServiceException;
025: import org.apache.cocoon.ProcessingException;
026: import org.apache.cocoon.environment.wrapper.RequestParameters;
027: import org.apache.cocoon.portal.PortalService;
028: import org.apache.cocoon.portal.coplets.basket.events.UploadItemEvent;
029: import org.apache.cocoon.portal.event.Event;
030: import org.apache.cocoon.xml.AttributesImpl;
031: import org.xml.sax.Attributes;
032: import org.xml.sax.SAXException;
033:
034: /**
035: * This transformer supports the basket feature. It can generate links to
036: * add content and to upload files into the basket.
037: *
038: * @version $Id: BasketTransformer.java 47047 2004-09-22 12:27:27Z vgritsenko $
039: */
040: public class FolderTransformer extends AbstractBasketTransformer {
041:
042: /** Element to upload an item */
043: protected static final String UPLOAD_ITEM_ELEMENT = "upload-item";
044:
045: /** Element for the upload form */
046: protected static final String UPLOAD_FORM_ELEMENT = "upload-form";
047:
048: /** Upload element list */
049: protected List uploadElements = new ArrayList();
050:
051: /* (non-Javadoc)
052: * @see org.apache.avalon.excalibur.pool.Recyclable#recycle()
053: */
054: public void recycle() {
055: this .uploadElements.clear();
056: super .recycle();
057: }
058:
059: /* (non-Javadoc)
060: * @see org.apache.cocoon.transformation.AbstractSAXTransformer#endTransformingElement(java.lang.String, java.lang.String, java.lang.String)
061: */
062: public void endTransformingElement(String uri, String name,
063: String raw) throws ProcessingException, IOException,
064: SAXException {
065: if (UPLOAD_ITEM_ELEMENT.equals(name)) {
066: this .endElement("", "input", "input");
067: } else if (UPLOAD_FORM_ELEMENT.equals(name)) {
068: this .endElement("", "form", "form");
069: this .uploadElements = new ArrayList();
070: }
071: }
072:
073: /* (non-Javadoc)
074: * @see org.apache.cocoon.transformation.AbstractSAXTransformer#startTransformingElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
075: */
076: public void startTransformingElement(String uri, String name,
077: String raw, Attributes attr) throws ProcessingException,
078: IOException, SAXException {
079: if (UPLOAD_ITEM_ELEMENT.equals(name)) {
080: this .uploadElements.add(attr.getValue("name"));
081: this .startElement("", "input", "input", attr);
082: } else if (UPLOAD_FORM_ELEMENT.equals(name)) {
083: AttributesImpl ai = new AttributesImpl(attr);
084: PortalService service = null;
085: String parameters;
086: try {
087: service = (PortalService) this .manager
088: .lookup(PortalService.ROLE);
089: Event e = new UploadItemEvent(this .basketManager
090: .getFolder(), this .uploadElements);
091: parameters = service.getComponentManager()
092: .getLinkService().getLinkURI(e);
093: int pos = parameters.indexOf('?');
094: ai.addCDATAAttribute("action", parameters.substring(0,
095: pos));
096: parameters = parameters.substring(pos + 1);
097: } catch (ServiceException se) {
098: throw new SAXException(
099: "Unable to lookup portal service.", se);
100: } finally {
101: this .manager.release(service);
102: }
103: this .startElement("", "form", "form", ai);
104: if (parameters != null && parameters.length() > 0) {
105: // create hidden input fields
106: RequestParameters pars = new RequestParameters(
107: parameters);
108: Enumeration enumPars = pars.getParameterNames();
109: while (enumPars.hasMoreElements()) {
110: String pName = (String) enumPars.nextElement();
111: String pValue = pars.getParameter(pName);
112: AttributesImpl hiddenAttrs = new AttributesImpl();
113: hiddenAttrs.addCDATAAttribute("type", "hidden");
114: hiddenAttrs.addCDATAAttribute("name", pName);
115: hiddenAttrs.addCDATAAttribute("value", pValue);
116: this
117: .startElement("", "input", "input",
118: hiddenAttrs);
119: this .endElement("", "input", "input");
120: }
121: }
122: }
123: }
124:
125: }
|