1 16 package org.apache.cocoon.portal.transformation; 17 18 import org.apache.cocoon.portal.coplet.CopletInstanceData; 19 import org.apache.cocoon.portal.event.impl.CopletLinkEvent; 20 import org.apache.cocoon.xml.AttributesImpl; 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.ContentHandler ; 23 import org.xml.sax.SAXException ; 24 25 57 public class NewEventLinkTransformer extends AbstractCopletTransformer { 58 59 62 public static final String NAMESPACE_URI = 63 "http://apache.org/cocoon/portal/eventlink/1.0"; 64 65 68 public static final String EVENT_ELEM = "eventlink"; 69 70 73 public static final String ATTRIBUTE_ATTR = "attribute"; 74 75 78 public static final String ELEMENT_ATTR = "element"; 79 80 83 public NewEventLinkTransformer() { 84 this.defaultNamespaceURI = NAMESPACE_URI; 85 } 86 87 93 public void startTransformingElement(String uri, 94 String name, 95 String raw, 96 Attributes attributes) 97 throws SAXException { 98 if (!EVENT_ELEM.equals(name)) { 99 throw new SAXException ("Unknown element encountered: " + name); 100 } 101 102 String attributeName = attributes.getValue(ATTRIBUTE_ATTR); 103 String elementName = attributes.getValue(ELEMENT_ATTR); 104 105 if (attributeName == null) { 106 throw new SAXException ( 107 "Element " 108 + EVENT_ELEM 109 + " must have an attribute " 110 + ATTRIBUTE_ATTR 111 + "."); 112 } 113 114 if (elementName == null) { 115 throw new SAXException ( 116 "Element " 117 + EVENT_ELEM 118 + " must have an attribute " 119 + ELEMENT_ATTR 120 + "."); 121 } 122 123 AttributesImpl newAttributes = this.getMutableAttributes(attributes); 125 newAttributes.removeAttribute(ELEMENT_ATTR); 126 newAttributes.removeAttribute(ATTRIBUTE_ATTR); 127 newAttributes.removeAttribute("coplet"); 128 129 int index = newAttributes.getIndex(attributeName); 130 String link = newAttributes.getValue(index); 131 132 boolean formSpecialTreatment = false; 133 if ("form".equals(elementName)) { 134 formSpecialTreatment = true; 136 if ("GET".equalsIgnoreCase(newAttributes.getValue("method")) 137 && link.indexOf('?') > 0) { 138 link = link.substring(0, link.indexOf('?')); 139 } 140 } 141 142 String portalAction = null; 143 String portalEvent = null; 144 145 if (link != null) { 147 CopletInstanceData cid = this.getCopletInstanceData(attributes.getValue("coplet")); 148 CopletLinkEvent event = new CopletLinkEvent(cid, link); 150 String eventLink = this.portalService.getComponentManager().getLinkService().getLinkURI(event); 151 152 if (formSpecialTreatment) { 154 int begin = 155 eventLink.indexOf("cocoon-portal-action=") 156 + "cocoon-portal-action=".length(); 157 int end = eventLink.indexOf('&', begin); 158 if (end == -1) { 159 end = eventLink.length(); 160 } 161 162 portalAction = eventLink.substring(begin, end); 163 164 begin = 165 eventLink.indexOf("cocoon-portal-event=") 166 + "cocoon-portal-event=".length(); 167 end = eventLink.indexOf('&', begin); 168 if (end == -1) { 169 end = eventLink.length(); 170 } 171 portalEvent = eventLink.substring(begin, end); 172 173 eventLink = 174 eventLink.substring(0, eventLink.indexOf('?')); 175 } 176 177 newAttributes.setValue(index, eventLink); 179 } 180 181 this.stack.push(elementName); 182 183 contentHandler.startElement( 184 "", 185 elementName, 186 elementName, 187 newAttributes); 188 189 if (formSpecialTreatment) { 191 sendHiddenFields(contentHandler, portalAction, portalEvent); 192 } 193 } 194 195 205 private void sendHiddenFields(ContentHandler contentHandler, 206 String portalAction, 207 String portalEvent) 208 throws SAXException { 209 AttributesImpl attributes = new AttributesImpl(); 210 attributes.addAttribute("", "type", "type", "CDATA", "hidden"); 211 attributes.addAttribute( 212 "", 213 "name", 214 "name", 215 "CDATA", 216 "cocoon-portal-action"); 217 attributes.addAttribute("", "value", "value", "CDATA", portalAction); 218 contentHandler.startElement("", "input", "input", attributes); 219 contentHandler.endElement("", "input", "input"); 220 221 attributes = new AttributesImpl(); 222 attributes.addAttribute("", "type", "type", "CDATA", "hidden"); 223 attributes.addAttribute( 224 "", 225 "name", 226 "name", 227 "CDATA", 228 "cocoon-portal-event"); 229 attributes.addAttribute("", "value", "value", "CDATA", portalEvent); 230 contentHandler.startElement("", "input", "input", attributes); 231 contentHandler.endElement("", "input", "input"); 232 } 233 234 237 public void endTransformingElement(String uri, String name, String raw) 238 throws SAXException { 239 String elementName = (String ) this.stack.pop(); 240 contentHandler.endElement("", elementName, elementName); 241 } 242 } 243 | Popular Tags |