1 16 package org.apache.cocoon.portal.transformation; 17 18 import java.io.IOException ; 19 import java.util.Map ; 20 21 import org.apache.avalon.framework.parameters.Parameters; 22 import org.apache.cocoon.ProcessingException; 23 import org.apache.cocoon.environment.SourceResolver; 24 import org.apache.cocoon.portal.coplet.CopletInstanceData; 25 import org.apache.cocoon.xml.AttributesImpl; 26 import org.apache.excalibur.source.SourceUtil; 27 import org.xml.sax.Attributes ; 28 import org.xml.sax.SAXException ; 29 30 42 public class HTMLEventLinkTransformer 43 extends AbstractCopletTransformer { 44 45 46 protected String attributeName; 47 48 49 protected String jxPath; 50 51 54 public void setup(SourceResolver resolver, 55 Map objectModel, 56 String src, 57 Parameters par) 58 throws ProcessingException, SAXException , IOException { 59 super.setup(resolver, objectModel, src, par); 60 this.attributeName = par.getParameter("attribute-name", "application-uri"); 61 this.jxPath = "temporaryAttributes/" + this.attributeName; 62 } 63 64 67 public void startElement(String uri, String name, String raw, Attributes attr) 68 throws SAXException { 69 boolean processed = false; 70 if ("a".equals(name) ) { 71 boolean convert = false; 72 final boolean isRemoteAnchor = this.isRemoteAnchor(attr); 73 if ( isRemoteAnchor ) { 74 convert = !this.isExternalLink(attr); 75 } 76 this.stack.push(convert ? Boolean.TRUE: Boolean.FALSE); 77 if ( convert ) { 78 this.createAnchorEvent(attr); 79 processed = true; 80 } 81 } else if ("form".equals(name) ) { 82 this.createFormEvent(attr); 83 processed = true; 84 } 85 if ( !processed ) { 86 super.startElement(uri, name, raw, attr); 87 } 88 } 89 90 93 public void endElement(String uri, String name, String raw) 94 throws SAXException { 95 boolean processed = false; 96 if ( "a".equals(name) ) { 97 final Boolean converted = (Boolean )this.stack.pop(); 98 if ( converted.booleanValue() ) { 99 this.xmlConsumer.endElement(CopletTransformer.NAMESPACE_URI, 100 CopletTransformer.LINK_ELEM, 101 "coplet:" + CopletTransformer.LINK_ELEM); 102 this.xmlConsumer.endPrefixMapping("coplet"); 103 processed = true; 104 } 105 } else if ( "form".equals(name) ) { 106 this.xmlConsumer.endElement(CopletTransformer.NAMESPACE_URI, 107 CopletTransformer.LINK_ELEM, 108 "coplet:" + CopletTransformer.LINK_ELEM); 109 this.xmlConsumer.endPrefixMapping("coplet"); 110 processed = true; 111 } 112 if ( !processed ) { 113 super.endElement(uri, name, raw); 114 } 115 } 116 117 protected void createAnchorEvent(Attributes attributes) 118 throws SAXException { 119 AttributesImpl newAttributes = new AttributesImpl(attributes); 120 newAttributes.removeAttribute("href"); 121 newAttributes.removeAttribute("external"); 122 String link = attributes.getValue("href"); 123 124 CopletInstanceData cid = this.getCopletInstanceData(); 125 link = this.getLink((String )cid.getTemporaryAttribute(this.attributeName), link); 126 127 newAttributes.addCDATAAttribute("path", this.jxPath); 128 newAttributes.addCDATAAttribute("value", link); 129 newAttributes.addCDATAAttribute("coplet", cid.getId()); 130 newAttributes.addCDATAAttribute("format", "html-link"); 131 this.xmlConsumer.startPrefixMapping("coplet", CopletTransformer.NAMESPACE_URI); 132 this.xmlConsumer.startElement(CopletTransformer.NAMESPACE_URI, 133 CopletTransformer.LINK_ELEM, 134 "coplet:" + CopletTransformer.LINK_ELEM, 135 newAttributes); 136 } 137 138 protected void createFormEvent(Attributes attributes) 139 throws SAXException { 140 AttributesImpl newAttributes = new AttributesImpl(attributes); 141 newAttributes.removeAttribute("action"); 142 String link = attributes.getValue("action"); 143 144 CopletInstanceData cid = this.getCopletInstanceData(); 145 link = this.getLink((String )cid.getTemporaryAttribute(this.attributeName), link); 146 147 newAttributes.addCDATAAttribute("path", this.jxPath); 148 newAttributes.addCDATAAttribute("value", link); 149 newAttributes.addCDATAAttribute("coplet", cid.getId()); 150 newAttributes.addCDATAAttribute("format", "html-form"); 151 if ( newAttributes.getIndex("method") == -1 ) { 152 newAttributes.addCDATAAttribute("method", "POST"); 153 } 154 155 this.xmlConsumer.startPrefixMapping("coplet", CopletTransformer.NAMESPACE_URI); 156 this.xmlConsumer.startElement(CopletTransformer.NAMESPACE_URI, 157 CopletTransformer.LINK_ELEM, 158 "coplet:" + CopletTransformer.LINK_ELEM, 159 newAttributes); 160 161 } 162 163 protected String getLink(String base, String link) { 164 final String v = SourceUtil.absolutize(base, link); 165 return v; 166 } 167 168 175 protected boolean isRemoteAnchor(Attributes attributes) { 176 String link = attributes.getValue("href"); 177 178 if (link != null && link.trim().length() > 0) { 180 if (!link.trim().startsWith("#")) { 182 return true; 183 } 184 } 185 186 return false; 187 } 188 189 197 private boolean isExternalLink (Attributes attributes) { 198 final String external = attributes.getValue("external"); 199 if (external != null && external.trim().length() > 0 201 && external.trim().toLowerCase().equals ("true") ) { 202 return true; 203 } 204 final String link = attributes.getValue("href"); 205 if ( link != null 206 && (link.startsWith("mailto:") || link.startsWith("javascript:") ) ) { 207 return true; 208 } 209 return false; 210 } 211 212 } 213 | Popular Tags |