KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24 import org.apache.avalon.framework.service.ServiceException;
25 import org.apache.cocoon.ProcessingException;
26 import org.apache.cocoon.portal.PortalService;
27 import org.apache.cocoon.portal.coplet.CopletInstanceData;
28 import org.apache.cocoon.portal.coplets.basket.BasketManager.ActionInfo;
29 import org.apache.cocoon.portal.coplets.basket.events.AddItemEvent;
30 import org.apache.cocoon.portal.event.Event;
31 import org.apache.cocoon.xml.AttributesImpl;
32 import org.apache.cocoon.xml.XMLUtils;
33 import org.xml.sax.Attributes JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35
36 /**
37  * This transformer supports the basket and briefcase feature. It can generate links to
38  * add content into a content store.
39  *
40  * @version CVS $Id: BasketTransformer.java 326425 2005-10-19 07:12:34Z cziegeler $
41  */

42 public class BasketTransformer
43     extends AbstractBasketTransformer {
44
45     /** Element to add a link */
46     protected static final String JavaDoc ADD_ITEM_ELEMENT = "add-item";
47
48     /** Element to show all actions */
49     protected static final String JavaDoc SHOW_ACTIONS_ELEMENT = "show-actions";
50
51     /** The default store: briefcase or basket */
52     protected String JavaDoc defaultStoreName = "basket";
53
54     /** The default link element name */
55     protected String JavaDoc defaultLinkElement = "a";
56
57     /** The default namespace for the link element */
58     protected String JavaDoc defaultLinkElementNS = "";
59
60     /* (non-Javadoc)
61      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
62      */

63     public void configure(Configuration configuration)
64     throws ConfigurationException {
65         super.configure(configuration);
66         this.defaultStoreName = configuration.getChild("default-store").getValue(this.defaultStoreName);
67         this.defaultLinkElement = configuration.getChild("default-link-element").getValue(this.defaultLinkElement);
68         this.defaultLinkElementNS = configuration.getChild("default-link-element-ns").getValue(this.defaultLinkElementNS);
69     }
70
71     /* (non-Javadoc)
72      * @see org.apache.cocoon.transformation.AbstractSAXTransformer#endTransformingElement(java.lang.String, java.lang.String, java.lang.String)
73      */

74     public void endTransformingElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw)
75     throws ProcessingException, IOException JavaDoc, SAXException JavaDoc {
76         if ( ADD_ITEM_ELEMENT.equals(name) ) {
77             final String JavaDoc linkElementName = this.parameters.getParameter("link-element", this.defaultLinkElement);
78             final String JavaDoc linkElementNS = this.parameters.getParameter("link-element-ns", this.defaultLinkElementNS);
79             XMLUtils.endElement(this.contentHandler, linkElementNS, linkElementName);
80         } else if ( SHOW_ACTIONS_ELEMENT.equals(name) ) {
81             // nothing to do here
82
}
83     }
84
85     /* (non-Javadoc)
86      * @see org.apache.cocoon.transformation.AbstractSAXTransformer#startTransformingElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
87      */

88     public void startTransformingElement(String JavaDoc uri, String JavaDoc name,
89                                          String JavaDoc raw, Attributes JavaDoc attr)
90     throws ProcessingException, IOException JavaDoc, SAXException JavaDoc {
91         if ( ADD_ITEM_ELEMENT.equals(name) ) {
92             PortalService service = null;
93             try {
94                 service = (PortalService)this.manager.lookup(PortalService.ROLE);
95
96                 // do we want to add content or a link?
97
boolean addContent = false;
98                 final String JavaDoc value = attr.getValue("content");
99             if ( value != null ) {
100                 addContent = new Boolean JavaDoc(value).booleanValue();
101             }
102                 
103                 // do we want to add a url or a coplet?
104
final ContentItem ci;
105                 final String JavaDoc href = attr.getValue("href");
106                 if ( href != null ) {
107                     ci = new ContentItem(href, addContent);
108                 } else {
109                     final String JavaDoc copletId = attr.getValue("coplet");
110                     final CopletInstanceData cid = service.getComponentManager().getProfileManager().getCopletInstanceData(copletId);
111                     ci = new ContentItem(cid, addContent);
112                 }
113
114                 // if a title is present set the title
115
final String JavaDoc title = attr.getValue("title");
116                 if(title!=null) {
117                     ci.setTitle(title);
118                 }
119                 
120                 // do we want to add the content to the basket or to the briefcase
121
final ContentStore store;
122                 final String JavaDoc storeName = (attr.getValue("store") == null ? this.defaultStoreName : attr.getValue("store"));
123                 if ("basket".equalsIgnoreCase(storeName) ) {
124                     store = this.basketManager.getBasket();
125                 } else {
126                     store = this.basketManager.getBriefcase();
127                 }
128
129                 final Event e = new AddItemEvent(store, ci);
130                 final AttributesImpl ai = new AttributesImpl();
131                 String JavaDoc newLink = service.getComponentManager().getLinkService().getLinkURI(e);
132                 // check for bockmark
133
final String JavaDoc bookmark = attr.getValue("bookmark");
134                 if ( bookmark != null && bookmark.length() > 0) {
135                     int pos = newLink.indexOf('?') + 1;
136                     final char separator;
137                     if ( bookmark.indexOf('?') == -1 ) {
138                         separator = '?';
139                     } else {
140                         separator = '&';
141                     }
142                     newLink = bookmark + separator + newLink.substring(pos);
143                 }
144                 ai.addCDATAAttribute("href", newLink);
145
146                 final String JavaDoc linkElementName = this.parameters.getParameter("link-element", this.defaultLinkElement);
147                 final String JavaDoc linkElementNS = this.parameters.getParameter("link-element-ns", this.defaultLinkElementNS);
148                 XMLUtils.startElement(this.contentHandler, linkElementNS, linkElementName, ai);
149             } catch (ServiceException se) {
150                 throw new SAXException JavaDoc("Unable to lookup portal service.", se);
151             } finally {
152                 this.manager.release(service);
153             }
154         } else if ( SHOW_ACTIONS_ELEMENT.equals(name) ) {
155             // basket or briefcase
156
final List JavaDoc actions;
157             final String JavaDoc storeName = (attr.getValue("store") == null ? this.defaultStoreName : attr.getValue("store"));
158             if ("basket".equalsIgnoreCase(storeName) ) {
159                 actions = this.basketManager.getBasketActions();
160             } else {
161                 actions = this.basketManager.getBriefcaseActions();
162                 }
163             final String JavaDoc checkedAction = attr.getValue("checked");
164             final Iterator JavaDoc i = actions.iterator();
165             AttributesImpl a = new AttributesImpl();
166             while ( i.hasNext() ) {
167                 final BasketManager.ActionInfo current = (ActionInfo) i.next();
168                 a.addCDATAAttribute("name", current.name);
169                 if ( current.name.equals(checkedAction) ) {
170                     a.addCDATAAttribute("checked", "true");
171                 }
172                 XMLUtils.createElement(this.xmlConsumer, "action", a);
173                 a.clear();
174             }
175         }
176     }
177
178 }
179
Popular Tags