1 16 package org.apache.cocoon.transformation; 17 18 import org.apache.avalon.framework.parameters.Parameters; 19 import org.apache.cocoon.ProcessingException; 20 import org.apache.cocoon.environment.ObjectModelHelper; 21 import org.apache.cocoon.environment.Request; 22 import org.apache.cocoon.environment.SourceResolver; 23 import org.xml.sax.Attributes ; 24 import org.xml.sax.SAXException ; 25 import org.xml.sax.helpers.AttributesImpl ; 26 27 import java.io.IOException ; 28 import java.util.Map ; 29 30 43 public class AugmentTransformer 44 extends AbstractTransformer { 45 46 protected Map objectModel; 47 protected Request request; 48 protected String baseURI; 49 50 public void setup(SourceResolver resolver, 51 Map objectModel, 52 String source, 53 Parameters parameters) 54 throws ProcessingException, SAXException , IOException { 55 this.objectModel = objectModel; 56 this.request = ObjectModelHelper.getRequest( this.objectModel ); 57 58 String mountPoint = parameters.getParameter("mount", null); 59 60 StringBuffer uribuf = new StringBuffer (); 61 boolean isSecure = this.request.isSecure(); 62 int port = this.request.getServerPort(); 63 64 if (isSecure) { 65 uribuf.append("https://"); 66 } else { 67 uribuf.append("http://"); 68 } 69 uribuf.append(request.getServerName()); 70 71 if (isSecure) { 72 if (port != 443) { 73 uribuf.append(":").append(port); 74 } 75 } else { 76 if (port != 80) { 77 uribuf.append(":").append(port); 78 } 79 } 80 if (mountPoint == null) { 81 String requestedURI = this.request.getRequestURI(); 82 requestedURI = requestedURI.substring(0, requestedURI.lastIndexOf("/")); 83 uribuf.append(requestedURI); 84 uribuf.append("/"); 85 } else { 86 uribuf.append(request.getContextPath()); 87 uribuf.append("/"); 88 uribuf.append(mountPoint); 89 } 90 this.baseURI = uribuf.toString(); 91 } 92 93 public void startElement(String uri, 94 String name, 95 String qname, 96 Attributes attrs) 97 throws SAXException { 98 AttributesImpl newAttrs = null; 99 100 for (int i = 0, size = attrs.getLength(); i < size; i++) { 101 String attrName = attrs.getLocalName(i); 102 if (attrName.equals("href")) { 103 String value = attrs.getValue(i); 104 105 if (value.startsWith("http:") || value.startsWith("https:")) { 107 continue; 108 } 109 110 if (newAttrs == null) { 111 newAttrs = new AttributesImpl (attrs); 112 } 113 114 String newValue = baseURI + value; 115 newAttrs.setValue(i, newValue); 116 } 117 } 118 119 if (newAttrs == null) { 120 super.startElement(uri, name, qname, attrs); 121 } else { 122 super.startElement(uri, name, qname, newAttrs); 123 } 124 } 125 126 129 public void recycle() { 130 this.objectModel = null; 131 this.request = null; 132 this.baseURI = null; 133 super.recycle(); 134 } 135 } 136 | Popular Tags |