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.Iterator;
021: import java.util.List;
022:
023: import org.apache.avalon.framework.configuration.Configuration;
024: import org.apache.avalon.framework.configuration.ConfigurationException;
025: import org.apache.avalon.framework.service.ServiceException;
026: import org.apache.cocoon.ProcessingException;
027: import org.apache.cocoon.portal.PortalService;
028: import org.apache.cocoon.portal.coplet.CopletInstanceData;
029: import org.apache.cocoon.portal.coplets.basket.BasketManager.ActionInfo;
030: import org.apache.cocoon.portal.coplets.basket.events.AddItemEvent;
031: import org.apache.cocoon.portal.event.Event;
032: import org.apache.cocoon.xml.AttributesImpl;
033: import org.apache.cocoon.xml.XMLUtils;
034: import org.xml.sax.Attributes;
035: import org.xml.sax.SAXException;
036:
037: /**
038: * This transformer supports the basket and briefcase feature. It can generate links to
039: * add content into a content store.
040: *
041: * @version CVS $Id: BasketTransformer.java 433543 2006-08-22 06:22:54Z crossley $
042: */
043: public class BasketTransformer extends AbstractBasketTransformer {
044:
045: /** Element to add a link */
046: protected static final String ADD_ITEM_ELEMENT = "add-item";
047:
048: /** Element to show all actions */
049: protected static final String SHOW_ACTIONS_ELEMENT = "show-actions";
050:
051: /** The default store: briefcase or basket */
052: protected String defaultStoreName = "basket";
053:
054: /** The default link element name */
055: protected String defaultLinkElement = "a";
056:
057: /** The default namespace for the link element */
058: protected String defaultLinkElementNS = "";
059:
060: /* (non-Javadoc)
061: * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
062: */
063: public void configure(Configuration configuration)
064: throws ConfigurationException {
065: super .configure(configuration);
066: this .defaultStoreName = configuration.getChild("default-store")
067: .getValue(this .defaultStoreName);
068: this .defaultLinkElement = configuration.getChild(
069: "default-link-element").getValue(
070: this .defaultLinkElement);
071: this .defaultLinkElementNS = configuration.getChild(
072: "default-link-element-ns").getValue(
073: this .defaultLinkElementNS);
074: }
075:
076: /* (non-Javadoc)
077: * @see org.apache.cocoon.transformation.AbstractSAXTransformer#endTransformingElement(java.lang.String, java.lang.String, java.lang.String)
078: */
079: public void endTransformingElement(String uri, String name,
080: String raw) throws ProcessingException, IOException,
081: SAXException {
082: if (ADD_ITEM_ELEMENT.equals(name)) {
083: final String linkElementName = this .parameters
084: .getParameter("link-element",
085: this .defaultLinkElement);
086: final String linkElementNS = this .parameters.getParameter(
087: "link-element-ns", this .defaultLinkElementNS);
088: XMLUtils.endElement(this .contentHandler, linkElementNS,
089: linkElementName);
090: } else if (SHOW_ACTIONS_ELEMENT.equals(name)) {
091: // nothing to do here
092: }
093: }
094:
095: /* (non-Javadoc)
096: * @see org.apache.cocoon.transformation.AbstractSAXTransformer#startTransformingElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
097: */
098: public void startTransformingElement(String uri, String name,
099: String raw, Attributes attr) throws ProcessingException,
100: IOException, SAXException {
101: if (ADD_ITEM_ELEMENT.equals(name)) {
102: PortalService service = null;
103: try {
104: service = (PortalService) this .manager
105: .lookup(PortalService.ROLE);
106:
107: // do we want to add content or a link?
108: boolean addContent = false;
109: final String value = attr.getValue("content");
110: if (value != null) {
111: addContent = new Boolean(value).booleanValue();
112: }
113:
114: // do we want to add a url or a coplet?
115: final ContentItem ci;
116: final String href = attr.getValue("href");
117: if (href != null) {
118: ci = new ContentItem(href, addContent);
119: } else {
120: final String copletId = attr.getValue("coplet");
121: final CopletInstanceData cid = service
122: .getComponentManager().getProfileManager()
123: .getCopletInstanceData(copletId);
124: ci = new ContentItem(cid, addContent);
125: }
126:
127: // if a title is present set the title
128: final String title = attr.getValue("title");
129: if (title != null) {
130: ci.setTitle(title);
131: }
132:
133: // do we want to add the content to the basket or to the briefcase
134: final ContentStore store;
135: final String storeName = (attr.getValue("store") == null ? this .defaultStoreName
136: : attr.getValue("store"));
137: if ("basket".equalsIgnoreCase(storeName)) {
138: store = this .basketManager.getBasket();
139: } else {
140: store = this .basketManager.getBriefcase();
141: }
142:
143: final Event e = new AddItemEvent(store, ci);
144: final AttributesImpl ai = new AttributesImpl();
145: String newLink = service.getComponentManager()
146: .getLinkService().getLinkURI(e);
147: // check for bockmark
148: final String bookmark = attr.getValue("bookmark");
149: if (bookmark != null && bookmark.length() > 0) {
150: int pos = newLink.indexOf('?') + 1;
151: final char separator;
152: if (bookmark.indexOf('?') == -1) {
153: separator = '?';
154: } else {
155: separator = '&';
156: }
157: newLink = bookmark + separator
158: + newLink.substring(pos);
159: }
160: ai.addCDATAAttribute("href", newLink);
161:
162: final String linkElementName = this .parameters
163: .getParameter("link-element",
164: this .defaultLinkElement);
165: final String linkElementNS = this .parameters
166: .getParameter("link-element-ns",
167: this .defaultLinkElementNS);
168: XMLUtils.startElement(this .contentHandler,
169: linkElementNS, linkElementName, ai);
170: } catch (ServiceException se) {
171: throw new SAXException(
172: "Unable to lookup portal service.", se);
173: } finally {
174: this .manager.release(service);
175: }
176: } else if (SHOW_ACTIONS_ELEMENT.equals(name)) {
177: // basket or briefcase
178: final List actions;
179: final String storeName = (attr.getValue("store") == null ? this .defaultStoreName
180: : attr.getValue("store"));
181: if ("basket".equalsIgnoreCase(storeName)) {
182: actions = this .basketManager.getBasketActions();
183: } else {
184: actions = this .basketManager.getBriefcaseActions();
185: }
186: final String checkedAction = attr.getValue("checked");
187: final Iterator i = actions.iterator();
188: AttributesImpl a = new AttributesImpl();
189: while (i.hasNext()) {
190: final BasketManager.ActionInfo current = (ActionInfo) i
191: .next();
192: a.addCDATAAttribute("name", current.name);
193: if (current.name.equals(checkedAction)) {
194: a.addCDATAAttribute("checked", "true");
195: }
196: XMLUtils.createElement(this .xmlConsumer, "action", a);
197: a.clear();
198: }
199: }
200: }
201:
202: }
|