cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andreas Hartmann <andr...@apache.org>
Subject Interest in link rewriting transformer?
Date Wed, 26 Aug 2009 12:17:19 GMT
Hi Cocoon devs,

Lenya contains an AbstractLinkTransformer for link rewriting purposes. 
I'm using it for other Cocoon projects too.

To be able to rewrite links in arbitrary XML formats, the transformer is 
configured with a set of attribute configurations which can contain 
links. Here's an example how you would configure your custom transformer 
to rewrite links in XHTML streams:


<map:transformer name="links-xhtml"
   src="org.myproject.MyLinkTransformer">

   <transform namespace="http://www.w3.org/1999/xhtml" element="a" 
attribute="href"/>
   <transform namespace="http://www.w3.org/1999/xhtml" element="object" 
attribute="data"/>
   <transform namespace="http://www.w3.org/1999/xhtml" element="img" 
attribute="src"/>
   <transform namespace="http://www.w3.org/1999/xhtml" element="link" 
attribute="href"/>
   <transform namespace="http://www.w3.org/1999/xhtml" element="form" 
attribute="action"/>
   <transform namespace="http://www.w3.org/1999/xhtml" element="script" 
attribute="src"/>
   <transform namespace="http://www.w3.org/1999/xhtml" element="input" 
attribute="src"/>
   <transform namespace="http://www.w3.org/1999/xhtml" element="input" 
attribute="value"/>

</map:transformer>


The AbstractLinkTransformer subclass must provide a link rewriter object 
implementing the LinkRewriter interface. Usually the LinkRewriter 
matches a particular protocol, extension or other parts of the URI.


public interface LinkRewriter {

     /**
      * @param url The URL.
      * @return If this rewriter feels responsible for the URL.
      */
     boolean matches(String url);

     /**
      * @param url The original URL.
      * @return The rewritten URL or <code>null</code> if the URL could 
not be rewritten (i.e., the link is broken).
      */
     String rewrite(String url);

}


IMO the link rewriting transformer is quite useful and generic enough to 
be used for various purposes, so it would probably make sense to move it 
from Lenya to Cocoon. In my experience it makes life much easier to use 
specific link protocols (e.g., internal:/) and have a single link 
rewriting step at the end of the page processing pipeline(s). The module 
also contains a ChainLinkRewriter to apply multiple rewriting steps in a 
single SAX transformation.

If anyone is interested, I could commit a Cocoon 2.2 block to the 
whiteboard. The JAR will be compatible with Cocoon 2.1.x too.

-- Andreas



-- 
Andreas Hartmann, CTO
BeCompany GmbH
http://www.becompany.ch
Tel.: +41 (0) 43 818 57 01


Mime
View raw message