You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lenya.apache.org by Andreas Hartmann <an...@apache.org> on 2009/08/30 11:06:26 UTC

[FWD] Interest in link rewriting transformer?

Hi everyone,

FYI: I posted this mail to dev@cocoon. If you want to discuss it, please 
use the Cocoon list to avoid forking the thread. Thanks!

-- Andreas

----


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




---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lenya.apache.org
For additional commands, e-mail: dev-help@lenya.apache.org