1 16 package org.apache.cocoon.portal.transformation; 17 18 import java.util.Stack ; 19 20 import org.apache.cocoon.portal.LinkService; 21 import org.apache.cocoon.portal.coplet.CopletInstanceData; 22 import org.apache.cocoon.portal.event.impl.CopletLinkEvent; 23 import org.w3c.dom.DocumentFragment ; 24 import org.xml.sax.Attributes ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.helpers.AttributesImpl ; 27 28 53 public class EventLinkTransformer extends AbstractCopletTransformer { 54 55 58 public static final String NAMESPACE_URI = "http://apache.org/cocoon/portal/eventlink/1.0"; 59 60 63 public static final String EVENT_ELEM = "event"; 64 65 68 public static final String ATTRIBUTE_ATTR = "attribute"; 69 70 73 public static final String ELEMENT_ATTR = "element"; 74 75 78 private boolean insideEvent = false; 79 80 83 private String attributeName; 84 85 88 private String elementName; 89 90 93 private Stack attrStack = new Stack (); 94 95 98 public void recycle() { 99 this.insideEvent = false; 100 this.attributeName = null; 101 this.elementName = null; 102 this.attrStack.clear(); 103 104 super.recycle(); 105 } 106 107 110 public void startElement(String uri, String name, String raw, Attributes attr) 111 throws SAXException { 112 113 if (uri.equals(NAMESPACE_URI) && name.equals(EVENT_ELEM)) { 114 if (this.insideEvent) { 115 throw new SAXException ("Elements "+EVENT_ELEM+" must not be nested."); 116 } 117 this.insideEvent = true; 118 119 this.attributeName = attr.getValue(ATTRIBUTE_ATTR); 121 this.elementName = attr.getValue(ELEMENT_ATTR); 122 123 if (this.attributeName == null && this.elementName == null) { 125 throw new SAXException ("Element "+EVENT_ELEM+" must have one of attributes "+ATTRIBUTE_ATTR+" and "+ELEMENT_ATTR+"."); 126 } 127 } else { 128 if (this.insideEvent) { 129 this.attrStack.push(new AttributesImpl (attr)); 131 132 137 if (this.elementName != null && name.equals(this.elementName)) { 138 this.startTextRecording(); 139 } else { 140 this.startRecording(); 141 } 142 } else { 143 super.startElement(uri, name, raw, attr); 144 } 145 } 146 } 147 148 151 public void endElement(String uri, String name, String raw) 152 throws SAXException { 153 154 if (uri.equals(NAMESPACE_URI) && name.equals(EVENT_ELEM)) { 155 this.attributeName = null; 156 this.elementName = null; 157 this.insideEvent = false; 158 } else { 159 if (this.insideEvent) { 160 AttributesImpl attr = (AttributesImpl )this.attrStack.pop(); 161 162 if (this.attributeName != null) { 164 int index = attr.getIndex(this.attributeName); 165 String link = attr.getValue(index); 166 167 if (link != null) { 169 CopletInstanceData cid = this.getCopletInstanceData(attr.getValue("coplet")); 170 LinkService linkService = this.portalService.getComponentManager().getLinkService(); 171 172 CopletLinkEvent event = new CopletLinkEvent(cid, link); 174 String eventLink = linkService.getLinkURI(event); 175 176 attr.setValue(index, eventLink); 178 } 179 } 180 181 String eventLink = null; 182 DocumentFragment fragment = null; 183 184 if (this.elementName != null && name.equals(this.elementName)) { 186 String link = this.endTextRecording(); 187 188 CopletInstanceData cid = this.getCopletInstanceData(); 189 LinkService linkService = this.portalService.getComponentManager().getLinkService(); 190 191 CopletLinkEvent event = new CopletLinkEvent(cid, link); 193 eventLink = linkService.getLinkURI(event); 194 } else { 195 fragment = this.endRecording(); 196 } 197 198 super.startElement(uri, name, raw, attr); 200 if (eventLink != null) { 201 super.characters(eventLink.toCharArray(), 0, eventLink.length()); 203 } else if (fragment != null) { 204 super.sendEvents(fragment); 205 } 206 super.endElement(uri, name, raw); 207 } else { 208 super.endElement(uri, name, raw); 209 } 210 } 211 } 212 } 213 | Popular Tags |