1 16 package org.apache.cocoon.portal.coplets.basket; 17 18 import java.io.IOException ; 19 import java.util.Iterator ; 20 import java.util.List ; 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 ; 34 import org.xml.sax.SAXException ; 35 36 42 public class BasketTransformer 43 extends AbstractBasketTransformer { 44 45 46 protected static final String ADD_ITEM_ELEMENT = "add-item"; 47 48 49 protected static final String SHOW_ACTIONS_ELEMENT = "show-actions"; 50 51 52 protected String defaultStoreName = "basket"; 53 54 55 protected String defaultLinkElement = "a"; 56 57 58 protected String defaultLinkElementNS = ""; 59 60 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 74 public void endTransformingElement(String uri, String name, String raw) 75 throws ProcessingException, IOException , SAXException { 76 if ( ADD_ITEM_ELEMENT.equals(name) ) { 77 final String linkElementName = this.parameters.getParameter("link-element", this.defaultLinkElement); 78 final String 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 } 83 } 84 85 88 public void startTransformingElement(String uri, String name, 89 String raw, Attributes attr) 90 throws ProcessingException, IOException , SAXException { 91 if ( ADD_ITEM_ELEMENT.equals(name) ) { 92 PortalService service = null; 93 try { 94 service = (PortalService)this.manager.lookup(PortalService.ROLE); 95 96 boolean addContent = false; 98 final String value = attr.getValue("content"); 99 if ( value != null ) { 100 addContent = new Boolean (value).booleanValue(); 101 } 102 103 final ContentItem ci; 105 final String href = attr.getValue("href"); 106 if ( href != null ) { 107 ci = new ContentItem(href, addContent); 108 } else { 109 final String copletId = attr.getValue("coplet"); 110 final CopletInstanceData cid = service.getComponentManager().getProfileManager().getCopletInstanceData(copletId); 111 ci = new ContentItem(cid, addContent); 112 } 113 114 final String title = attr.getValue("title"); 116 if(title!=null) { 117 ci.setTitle(title); 118 } 119 120 final ContentStore store; 122 final String 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 newLink = service.getComponentManager().getLinkService().getLinkURI(e); 132 final String 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 linkElementName = this.parameters.getParameter("link-element", this.defaultLinkElement); 147 final String 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 ("Unable to lookup portal service.", se); 151 } finally { 152 this.manager.release(service); 153 } 154 } else if ( SHOW_ACTIONS_ELEMENT.equals(name) ) { 155 final List actions; 157 final String 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 checkedAction = attr.getValue("checked"); 164 final Iterator 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 |