package org.lenya.cms.cocoon.acting;

import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.Session;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.acting.ConfigurableComposerAction;

import org.apache.excalibur.source.Source;
import org.apache.cocoon.xml.AbstractXMLConsumer;

import org.xml.sax.Attributes;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Category;

public class URIParametrizerAction extends ConfigurableComposerAction  {
    static Category log = Category.getInstance(URIParametrizerAction.class);

    public class URIParametrizerConsumer extends AbstractXMLConsumer {

	boolean inParamElement = false;
	String parameterValue = null;

	public void startElement(String uri, String loc, String raw, Attributes a) {
	    if (loc.equals("parameter")) {
		log.error("start Element " + uri + ":"+ loc + ":" + raw);
		inParamElement = true;
	    }
	}

	public void endElement(String uri, String loc, String raw, Attributes a) {
	    if (loc.equals("parameter")) {
		log.error("stop Element " + uri + ":"+ loc + ":" + raw);
		inParamElement = false;
	    }
            log.error("processing Element " + uri + ":"+ loc + ":" + raw);
	}

	public void characters(char[] ch, int start, int len) {
	    if (inParamElement) {
		parameterValue = new String(ch, start, len);
		log.error("grab Element " + parameterValue);
	    }
	}
	public String getParameter() {
	    return parameterValue;
	}
    }


    public Map act (Redirector redirector, SourceResolver resolver,
		    Map objectModel, String src, Parameters par) throws Exception {
	Source inputSource = resolver.resolveURI(src);
	URIParametrizerConsumer xmlConsumer = new URIParametrizerConsumer();

	Map map = new HashMap();
	
	if (this.getLogger().isDebugEnabled()) {
	    this.getLogger().debug("processing file " + src);
	    this.getLogger().debug("file resolved to " + inputSource.getURI());
	}
 	resolver.toSAX(inputSource, xmlConsumer);
	
	map.put("parameter", xmlConsumer.getParameter());
	
	return map;
    }
}




@@ -143,16 +143,154 @@
 adSafe;%0A
+import org.apache.avalon.framework.configuration.Configuration;%0Aimport org.apache.avalon.framework.configuration.ConfigurationException;%0A%0A
 import o
@@ -765,16 +765,43 @@
 til.Map;
+%0Aimport java.util.Iterator;
 %0A%0Aimport
@@ -980,16 +980,45 @@
 lass);%0A%0A
+    private Map parameters;%0A%0A
     publ
@@ -1897,16 +1897,624 @@
 %0A    %7D%0A%0A
+    /**%0A     * Describe %3Ccode%3Econfigure%3C/code%3E method here.%0A     *%0A     * @param conf a %3Ccode%3EConfiguration%3C/code%3E value%0A     *%0A     * @exception ConfigurationException if an error occurs%0A     */%0A    public void configure(Configuration conf) throws ConfigurationException %7B%0A        super.configure(conf);%0A%09%0A%09Configuration%5B%5D parameterConfigs = null;%0A%09this.parameters = new HashMap();%0A%0A%09parameterConfigs = conf.getChildren(%22parameter%22);%0A%09for (int i = 0; i %3C parameterConfigs.length; i++) %7B%0A%09    parameters.put(parameterConfigs%5Bi%5D.getAttribute(%22name%22),%0A%09%09%09   parameterConfigs%5Bi%5D.getAttribute(%22src%22));%0A%09%7D%0A    %7D%0A%0A
 %0A    pub
@@ -2670,32 +2670,12 @@
 e = 
-resolver.resolveURI(src)
+null
 ;%0A%09U
@@ -2948,10 +2948,306 @@
 %0A%09%7D%0A
+%0A%09Iterator parameterMappings = this.parameters.keySet().iterator();%0A%09while (parameterMappings.hasNext()) %7B%0A%09    String parameterName = (String)parameterMappings.next();%0A%09    String parameterSrc = (String)parameters.get(parameterName);%0A%09%09%0A%09    inputSource = resolver.resolveURI(parameterSrc);%0A%0A%09   
  
-%09
 reso
@@ -3291,16 +3291,20 @@
 %0A%09%0A%09
+    
 map.put(
 %22par
@@ -3299,17 +3299,16 @@
 map.put(
-%22
 paramete
@@ -3308,17 +3308,20 @@
 arameter
-%22
+Name
 , xmlCon
@@ -3344,16 +3344,17 @@
 er());%0A%09
+%7D
 %0A%09return

Make the action configurable.