1 16 package org.apache.cocoon.portal.transformation; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.Enumeration ; 21 import java.util.List ; 22 23 import org.apache.cocoon.ProcessingException; 24 import org.apache.cocoon.environment.wrapper.RequestParameters; 25 import org.apache.cocoon.portal.LinkService; 26 import org.apache.cocoon.portal.coplet.CopletInstanceData; 27 import org.apache.cocoon.portal.event.impl.ChangeCopletInstanceAspectDataEvent; 28 import org.apache.cocoon.portal.event.impl.CopletJXPathEvent; 29 import org.apache.cocoon.portal.event.impl.JXPathEvent; 30 import org.apache.cocoon.xml.AttributesImpl; 31 import org.apache.cocoon.xml.XMLUtils; 32 import org.apache.commons.jxpath.JXPathContext; 33 import org.apache.excalibur.xml.sax.XMLizable; 34 import org.xml.sax.Attributes ; 35 import org.xml.sax.SAXException ; 36 37 59 public class CopletTransformer 60 extends AbstractCopletTransformer { 61 62 65 public static final String NAMESPACE_URI = "http://apache.org/cocoon/portal/coplet/1.0"; 66 67 70 public static final String COPLET_ELEM = "coplet"; 71 72 75 public static final String SELECT_ATTR = "select"; 76 77 78 81 public static final String LINK_ELEM = "link"; 82 83 84 public static final String LINKS_ELEM = "links"; 85 86 87 public static final String PARAMETER_ELEM = "parameter"; 88 89 90 public static final String CONTENT_ELEM = "content"; 91 92 93 protected boolean insideLinks; 94 95 96 protected List collectedEvents = new ArrayList (); 97 98 99 protected XMLizable content; 100 101 104 public CopletTransformer() { 105 this.defaultNamespaceURI = NAMESPACE_URI; 106 } 107 108 111 public void setupTransforming() 112 throws IOException , ProcessingException, SAXException { 113 super.setupTransforming(); 114 this.insideLinks = false; 115 this.content = null; 116 this.collectedEvents.clear(); 117 } 118 119 122 public void startTransformingElement(String uri, String name, String raw, Attributes attr) 123 throws ProcessingException, IOException , SAXException { 124 if (name.equals(COPLET_ELEM)) { 125 String expression = attr.getValue(SELECT_ATTR); 126 if (expression == null) { 127 throw new ProcessingException("Attribute "+SELECT_ATTR+" must be spcified."); 128 } 129 130 final CopletInstanceData cid = this.getCopletInstanceData(); 131 132 final JXPathContext jxpathContext = JXPathContext.newContext( cid ); 133 final Object object = jxpathContext.getValue(expression); 134 135 if (object != null) { 136 XMLUtils.valueOf(contentHandler, object); 137 } 138 139 } else if (name.equals(LINK_ELEM)) { 140 141 final LinkService linkService = this.portalService.getComponentManager().getLinkService(); 142 final String format = attr.getValue("format"); 143 AttributesImpl newAttrs = new AttributesImpl(); 144 newAttrs.setAttributes(attr); 145 newAttrs.removeAttribute("format"); 146 147 if ( attr.getValue("href") != null ) { 148 final CopletInstanceData cid = this.getCopletInstanceData(); 149 ChangeCopletInstanceAspectDataEvent event = new ChangeCopletInstanceAspectDataEvent(cid, null, null); 150 151 String value = linkService.getLinkURI(event); 152 if (value.indexOf('?') == -1) { 153 value = value + '?' + attr.getValue("href"); 154 } else { 155 value = value + '&' + attr.getValue("href"); 156 } 157 newAttrs.removeAttribute("href"); 158 this.output(value, format, newAttrs ); 159 } else { 160 final String path = attr.getValue("path"); 161 final String value = attr.getValue("value"); 162 163 newAttrs.removeAttribute("path"); 164 newAttrs.removeAttribute("value"); 165 166 JXPathEvent event = null; 167 if ( attr.getValue("layout") != null ) { 168 newAttrs.removeAttribute("layout"); 169 final String layoutId = attr.getValue("layout"); 170 Object layout = this.portalService.getComponentManager().getProfileManager().getPortalLayout(null, layoutId); 171 if ( layout != null ) { 172 event = new JXPathEvent(layout, path, value); 173 } 174 } else { 175 String copletId = attr.getValue("coplet"); 176 newAttrs.removeAttribute("coplet"); 177 final CopletInstanceData cid = this.getCopletInstanceData(copletId); 178 if ( cid != null ) { 179 event = new CopletJXPathEvent(cid, path, value); 180 } 181 } 182 if ( this.insideLinks ) { 183 if ( event != null ) { 184 this.collectedEvents.add(event); 185 } 186 } else { 187 final String href = linkService.getLinkURI(event); 188 this.output(href, format, newAttrs ); 189 } 190 } 191 } else if (name.equals(PARAMETER_ELEM)) { 192 if (this.insideLinks) { 193 String href = attr.getValue("href"); 194 if ( href != null ) { 195 final int pos = href.indexOf('?'); 196 if ( pos != -1 ) { 197 href = href.substring(pos+1); 198 } 199 this.collectedEvents.add(new LinkService.ParameterDescription(href)); 200 } 201 } 202 } else if (name.equals(LINKS_ELEM) ) { 203 this.insideLinks = true; 204 final AttributesImpl newAttrs = new AttributesImpl(); 205 newAttrs.setAttributes(attr); 206 newAttrs.removeAttribute("format"); 207 this.stack.push(newAttrs); 208 209 String format = attr.getValue("format"); 210 if ( format == null ) { 211 format = "html-link"; 212 } 213 this.stack.push(format); 214 } else if ( name.equals(CONTENT_ELEM) && this.insideLinks ) { 215 this.startSAXRecording(); 216 } else { 217 super.startTransformingElement(uri, name, raw, attr); 218 } 219 } 220 221 224 public void endTransformingElement(String uri, String name, String raw) 225 throws ProcessingException, IOException , SAXException { 226 if ( name.equals(LINK_ELEM) ) { 227 if ( !this.insideLinks ) { 228 String elem = (String )this.stack.pop(); 229 if ( elem.length() > 0 ) { 230 this.sendEndElementEvent(elem); 231 } 232 } 233 } else if ( name.equals(LINKS_ELEM) ) { 234 this.insideLinks = false; 235 final String format = (String )this.stack.pop(); 236 final LinkService linkService = this.portalService.getComponentManager().getLinkService(); 237 String href = linkService.getLinkURI(this.collectedEvents); 238 239 AttributesImpl newAttrs = (AttributesImpl)this.stack.pop(); 240 final String baseURL = newAttrs.getValue("base-url"); 242 if ( baseURL != null ) { 243 newAttrs.removeAttribute("base-url"); 244 int pos = href.indexOf('?') + 1; 245 final char separator; 246 if ( baseURL.indexOf('?') == -1 ) { 247 separator = '?'; 248 } else { 249 separator = '&'; 250 } 251 href = baseURL + separator + href.substring(pos); 252 253 } 254 this.output(href, format, newAttrs ); 255 256 this.collectedEvents.clear(); 257 if ( this.content != null ) { 258 this.content.toSAX(this.contentHandler); 259 this.content = null; 260 } 261 String elem = (String )this.stack.pop(); 262 if ( elem.length() > 0 ) { 263 this.sendEndElementEvent(elem); 264 } 265 } else if ( name.equals(CONTENT_ELEM) && this.insideLinks ) { 266 this.content = this.endSAXRecording(); 267 } else if (!name.equals(COPLET_ELEM) && !name.equals(PARAMETER_ELEM)) { 268 super.endTransformingElement(uri, name, raw); 269 } 270 } 271 272 275 protected void output(String uri, String format, AttributesImpl newAttrs) 276 throws SAXException { 277 if ( format == null ) { 278 format = "html-link"; 280 } 281 282 if ( "html-link".equals(format) ) { 283 newAttrs.addCDATAAttribute("href", uri); 284 this.sendStartElementEvent("a", newAttrs); 285 this.stack.push("a"); 286 287 } else if ( "html-form".equals(format) ) { 288 boolean addParametersAsHiddenFields = false; 289 String parameters = null; 290 final String enctype = newAttrs.getValue("enctype"); 291 if ( enctype== null 292 || "application/x-www-form-urlencoded".equalsIgnoreCase(enctype) 293 || "multipart/form-data".equalsIgnoreCase(enctype) ) { 294 final int pos = uri.indexOf('?'); 295 if ( pos != -1 ) { 296 parameters = uri.substring(pos+1); 297 uri = uri.substring(0, pos); 298 addParametersAsHiddenFields = true; 299 } 300 } 301 newAttrs.addCDATAAttribute("action", uri); 302 this.sendStartElementEvent("form", newAttrs); 303 this.stack.push("form"); 304 if ( addParametersAsHiddenFields ) { 305 RequestParameters pars = new RequestParameters(parameters); 307 Enumeration enumeration = pars.getParameterNames(); 308 while ( enumeration.hasMoreElements() ) { 309 final String pName = (String )enumeration.nextElement(); 310 final String [] pValues = pars.getParameterValues(pName); 311 for(int k=0; k<pValues.length; k++) { 312 final String pValue = pValues[k]; 313 AttributesImpl hiddenAttrs = new AttributesImpl(); 314 hiddenAttrs.addCDATAAttribute("type", "hidden"); 315 hiddenAttrs.addCDATAAttribute("name", pName); 316 hiddenAttrs.addCDATAAttribute("value", pValue); 317 this.startElement("", "input", "input", hiddenAttrs); 318 this.endElement("", "input", "input"); 319 } 320 } 321 322 } 323 } else if ( "text".equals(format) ) { 324 this.sendTextEvent(uri); 325 this.stack.push(""); 326 } else if ( "parameters".equals(format) ) { 327 final String value = uri.substring(uri.indexOf('?')+1); 328 this.sendTextEvent(value); 329 this.stack.push(""); 330 } else { 331 newAttrs.addCDATAAttribute("href", uri); 333 this.sendStartElementEvent("link", newAttrs); 334 this.stack.push("link"); 335 } 336 } 337 } 338 | Popular Tags |