cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jorg Heymans ...@domek.be>
Subject Re: trouble passing parameter to custom transformer
Date Sat, 13 Mar 2004 23:07:03 GMT
you can just override setup() in your transformer and take parameters 
from there. Have a look at some existing transformers and see how they 
do it.

Francesco Rossi (Milano, Italy) wrote:
> ... I have trouble passing a parameter to my custom transformer
> 
> here is the code to the transformer (which is pretty straightforward: it 
> just changes the attribute containing an URL, in elements of some given 
> kinds):
> 
> package mypackage.transformation;
> 
> import org.apache.cocoon.transformation.AbstractTransformer;
> import org.apache.cocoon.environment.SourceResolver;
> import org.apache.cocoon.ProcessingException;
> import org.apache.avalon.framework.parameters.*;
> import org.apache.avalon.excalibur.pool.Poolable;
> import org.xml.sax.SAXException;
> import org.xml.sax.helpers.AttributesImpl;
> import java.io.IOException;
> import java.util.Map;
> import java.lang.String;
> 
> public class MyLinkRewriter
> extends AbstractTransformer implements Parameterizable, Poolable {
>  
>  public static final String CSSLINK_ELEMENT = "link";
>  public static final String IMG_ELEMENT = "img";
>  public static final String ANCHOR_ELEMENT = "a";
>  
>  protected String absPath;
>  
>  protected String myUrl;
>  
>  public void parameterize(Parameters parameters) throws ParameterException {
>   this.myUrl = parameters.getParameter("myurl");
>  }
>  
>  public void setup(SourceResolver resolver,Map objectModel,String 
> src,Parameters par)
>  throws ProcessingException,SAXException,IOException {
>   // salva il cammino assoluto fino alla pagina richiesta (compresa 
> ultima '/')
>   this.absPath = this.myUrl.substring(0,this.myUrl.lastIndexOf('/'));
>  }
>  
>  public void startElement(String uri,String name,String 
> raw,AttributesImpl attr)
>  throws SAXException {
>   
>   int i;
>   String u,l,q,t,newValue;
>   
>   if (name.equals(CSSLINK_ELEMENT) && attr.getLength()>0) {
>    i = attr.getIndex("href");
>    
>    if (i==-1 || attr.getValue(i).startsWith("http://")==true)
>     super.startElement(uri, name, raw, attr);
>    else {
>     u = attr.getURI(i);
>     l = attr.getLocalName(i);
>     q = attr.getQName(i);
>     t = attr.getType(i);
>     newValue = "http://" + absPath + attr.getValue(i);
>     
>     attr.setAttribute(i, u, l, q, t, newValue);
>     super.startElement(uri, name, raw, attr);   
>    }
>   } else if (name.equals(IMG_ELEMENT) && attr.getLength()>0) {
>    i = attr.getIndex("src");
>    
>    if (i==-1 || attr.getValue(i).startsWith("http://")==true)
>     super.startElement(uri, name, raw, attr);
>    else {
>     u = attr.getURI(i);
>     l = attr.getLocalName(i);
>     q = attr.getQName(i);
>     t = attr.getType(i);
>     newValue = "http://" + absPath + attr.getValue(i);
>     
>     attr.setAttribute(i, u, l, q, t, newValue);
>     super.startElement(uri, name, raw, attr);
>    }
>   } else if (name.equals(ANCHOR_ELEMENT) && attr.getLength()>0) {
>    i = attr.getIndex("href");
>    
>    if (i==-1 || attr.getValue(i).startsWith("http://")==true)
>     super.startElement(uri, name, raw, attr);
>    else {
>     u = attr.getURI(i);
>     l = attr.getLocalName(i);
>     q = attr.getQName(i);
>     t = attr.getType(i);
>     newValue = "protected-url?myurl=" + absPath + attr.getValue(i);
>     
>     attr.setAttribute(i, u, l, q, t, newValue);
>     super.startElement(uri, name, raw, attr);
>    }
>   } else // passo avanti
>    super.startElement(uri, name, raw, attr);
>  }
>  
>  public void characters(char[] buffer,int start,int length)
>  throws SAXException {
>   super.characters(buffer, start, length);
>  }
>  
>  public void endElement(String uri,String name,String raw)
>  throws SAXException {
>   super.endElement(uri, name, raw);
>  }
> }
> 
> I successfully compiled it, made a .jar file containing the package, 
> moved it to WEB-INF/lib.
> Here is the excerpt from my sub-sitemap where I configure it:
> 
>  <map:transformers default="xslt">
>     <map:transformer name="myLinkRewriter" 
> src="mypackage.transformation.MyLinkRewriter"/>
>  </map:transformers>
> 
> Finally, here is the excerpt from my sub-sitemap where I make use of it:
> 
>  <map:match pattern="protected-url">
>     <map:generate src="http://{request-param:myurl}" type="html"/>
>     <map:transform type="myLinkRewriter">
>        <map:parameter name="myurl" value="{request-param:myurl}"/>
>     </map:transform>
> 
> 
> Now, the error I get reads:
> 
>  Lookup of transformer for role 'myLinkRewriter' failed.
> 
> org.apache.cocoon.ProcessingException: Lookup of transformer for role 
> 'myLinkRewriter' failed.: 
> org.apache.avalon.framework.component.ComponentException: 
> transformers: ComponentSelector could not access the Component for hint 
> [myLinkRewriter] (key [myLinkRewriter])
> 
> cause: org.apache.avalon.framework.parameters.ParameterException: The 
> parameter 'myurl' does not contain a value
> 
> ... can anybody help me?
> thank you in advance
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message