ovidiu      02/04/15 17:22:50

  Added:       src/scratchpad/schecoon/src/org/apache/cocoon/transformation
                        AugmentTransformer.java
  Log:
  Added. Prepends to the "href" content of elements the full path of the
  request.
  
  Revision  Changes    Path
  1.1                  
xml-cocoon2/src/scratchpad/schecoon/src/org/apache/cocoon/transformation/AugmentTransformer.java
  
  Index: AugmentTransformer.java
  ===================================================================
  package org.apache.cocoon.transformation;
  
  import org.apache.avalon.excalibur.pool.Poolable;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.cocoon.ProcessingException;
  import org.apache.cocoon.environment.Request;
  import org.apache.cocoon.transformation.AbstractTransformer;
  import org.apache.cocoon.environment.SourceResolver;
  import org.apache.cocoon.components.language.markup.xsp.XSPRequestHelper;
  import org.apache.cocoon.environment.Request;
  import org.xml.sax.Attributes;
  import org.xml.sax.SAXException;
  import org.xml.sax.helpers.AttributesImpl;
  
  import java.io.IOException;
  import java.util.Map;
  
  /**
   * Augments any <href> attributes with the full path to the request.
   *
   * Usage in sitemap
   *    &lt;map:transform type="augment"/&gt;
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Ovidiu Predescu</a>
   * @since October 10, 2001
   */
  public class AugmentTransformer
    extends AbstractTransformer
    implements Poolable
  {
    Map objectModel;
    Request request;
    String baseURI;
    
    public void setup(SourceResolver resolver,
                      Map objectModel,
                      String source,
                      Parameters parameters)
      throws ProcessingException, SAXException, IOException
    {
      this.objectModel = objectModel;
      request = (Request)objectModel.get("request");
  
      String mountPoint = (String)parameters.getParameter("mount", null);
      
      StringBuffer uribuf = new StringBuffer();
      boolean isSecure = request.isSecure();
      int port = request.getServerPort();
  
      if (isSecure)
        uribuf.append("https://";);
      else
        uribuf.append("http://";);
  
      uribuf.append(request.getServerName());
  
      if (isSecure) {
        if (port != 443) {
          uribuf.append(":").append(port);
        }
      }
      else {
        if (port != 80) {
          uribuf.append(":").append(port);
        }
      }
      if (mountPoint == null) {
        String requestedURI = request.getRequestURI();
        requestedURI = requestedURI.substring(0, requestedURI.lastIndexOf("/"));
        uribuf.append(requestedURI);
        uribuf.append("/");
      }
      else {
        uribuf.append(request.getContextPath());
        uribuf.append("/");
        uribuf.append(mountPoint);
      }
      baseURI = uribuf.toString();
    }
  
    public void startElement(String uri,
                             String name,
                             String qname,
                             Attributes attrs)
      throws SAXException
    {
      AttributesImpl newAttrs = null;
      
      for (int i = 0, size = attrs.getLength(); i < size; i++) {
        String attrName = attrs.getLocalName(i);
        if (attrName.equals("href")) {
          String value = attrs.getValue(i);
  
          // Don't touch the attribute if it's an absolute URL
          if (value.startsWith("http:") || value.startsWith("https:"))
            continue;
  
          if (newAttrs == null)
            newAttrs = new AttributesImpl(attrs);
  
          String newValue = baseURI + value;
          newAttrs.setValue(i, newValue);
        }
      }
  
      if (newAttrs == null)
        super.startElement(uri, name, qname, attrs);
      else
        super.startElement(uri, name, qname, newAttrs);
    }
  
    public void recycle()
    {
      this.objectModel = null;
    }
  }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to