haul 2002/12/16 06:56:30 Modified: src/java/org/apache/cocoon/components/modules/input Tag: cocoon_2_0_3_branch AbstractJXPathModule.java AbstractMetaModule.java ChainMetaModule.java JXPathMetaModule.java SimpleMappingMetaModule.java Log: Fix wrong brackets thanks to Jeff Turner <action dev="CH" type="update"> AbstractJXPathModule / JXPathMetaModule default to lenient mode i.e. do not throw an exception on unsupported attributes but return null instead. Made this a configuration option. </action> <action dev="CH" type="add"> SimpleMappingMetaModule: added feature to remove a prefix / suffix. </action> <action dev="CH" type="fix"> "meta" input modules: configuring a different module locally did not override the default one in all circumstances. </action> Revision Changes Path No revision No revision 1.3.2.4 +22 -17 xml-cocoon2/src/java/org/apache/cocoon/components/modules/input/AbstractJXPathModule.java Index: AbstractJXPathModule.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/modules/input/AbstractJXPathModule.java,v retrieving revision 1.3.2.3 retrieving revision 1.3.2.4 diff -u -r1.3.2.3 -r1.3.2.4 --- AbstractJXPathModule.java 6 Dec 2002 09:20:01 -0000 1.3.2.3 +++ AbstractJXPathModule.java 16 Dec 2002 14:56:30 -0000 1.3.2.4 @@ -51,24 +51,14 @@ package org.apache.cocoon.components.modules.input; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.commons.jxpath.*; + import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Iterator; - -import org.apache.avalon.framework.configuration.Configurable; -import org.apache.avalon.framework.configuration.Configuration; -import org.apache.avalon.framework.configuration.ConfigurationException; -import org.apache.avalon.framework.thread.ThreadSafe; - -import org.apache.cocoon.environment.ObjectModelHelper; -import org.apache.cocoon.environment.Request; -import org.apache.cocoon.environment.Session; -import org.apache.cocoon.environment.Context; - -import org.apache.commons.jxpath.*; -import org.apache.commons.jxpath.servlet.*; /** * JXPathModule allows to access properties of any object in generic @@ -80,6 +70,10 @@ * * <p>Configuration example:</p> * <table> + * <tr><td><code><lenient>false</lenient></td> + * <td>When set to true, non-existing attributes return null, when set to false, + * an exception is thrown. Default is true.</td> + *</tr> * <tr><td><code><function name="java.lang.String" prefix="str"/></td> * <td>Imports the class "String" as extension class to the JXPathContext using * the prefix "str". Thus "str:length(xpath)" would apply the method "length" to @@ -106,6 +100,11 @@ */ protected FunctionLibrary library = null; + /** set lenient mode for jxpath (i.e. throw an exception on + * unsupported attributes) ? + */ + protected boolean lenient = true; + /** * Configure component. Preprocess list of packages and functions @@ -119,6 +118,7 @@ // JXPathMetaModule starts copying here // please keep both in sync. + this.lenient = config.getChild("lenient").getValueAsBoolean(this.lenient); this.library = new FunctionLibrary(); getFunctions(this.library, config); getPackages(this.library, config); @@ -223,7 +223,9 @@ try { JXPathContext jxContext = JXPathContext.newContext(contextObj); setupExtensions(jxContext, modeConf); - return jxContext.getValue(name); + if (this.lenient) jxContext.setLenient(true); // return null insted of exception on non existing property + Object obj = jxContext.getValue(name); + return obj; } catch (Exception e) { throw new ConfigurationException( "Module does not support <" + name + ">" + "attribute.", @@ -263,11 +265,14 @@ JXPathContext jxContext = JXPathContext.newContext(contextObj); List values = new LinkedList(); setupExtensions(jxContext, modeConf); + if (this.lenient) jxContext.setLenient(true); // return null insted of exception on non existing property Iterator i = jxContext.iterate(name); while (i.hasNext()) { values.add(i.next()); } - return values.toArray(); + Object[] obj = values.toArray(); + if (obj.length == 0) obj = null; + return obj; } catch (Exception e) { throw new ConfigurationException( "Module does not support <" + name + ">" + "attribute.", 1.3.2.2 +11 -8 xml-cocoon2/src/java/org/apache/cocoon/components/modules/input/AbstractMetaModule.java Index: AbstractMetaModule.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/modules/input/AbstractMetaModule.java,v retrieving revision 1.3.2.1 retrieving revision 1.3.2.2 diff -u -r1.3.2.1 -r1.3.2.2 --- AbstractMetaModule.java 17 Nov 2002 19:12:49 -0000 1.3.2.1 +++ AbstractMetaModule.java 16 Dec 2002 14:56:30 -0000 1.3.2.2 @@ -53,17 +53,15 @@ import org.apache.avalon.framework.activity.Disposable; import org.apache.avalon.framework.activity.Initializable; -import org.apache.avalon.framework.configuration.Configurable; -import org.apache.avalon.framework.configuration.Configuration; -import org.apache.avalon.framework.component.ComponentSelector; import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; +import org.apache.avalon.framework.component.ComponentSelector; import org.apache.avalon.framework.component.Composable; +import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.thread.ThreadSafe; -import org.apache.cocoon.util.HashMap; -import java.util.Map; import java.util.Iterator; +import java.util.Map; /** * AbstractMetaModule gives you the infrastructure for easily @@ -305,10 +303,12 @@ try { + if (getLogger().isDebugEnabled()) + getLogger().debug("parameters "+op+": "+modA+", "+modAName+", "+modAConf+" || "+modB+", "+modBName+", "+modBConf); if (cs == null) cs = (ComponentSelector) this.manager.lookup(INPUT_MODULE_SELECTOR); - if (modB == null) { + if (modB == null && modBName == null) { input = modA; name = modAName; conf = modAConf; @@ -327,7 +327,7 @@ getLogger().warn("No such InputModule: "+name); } } - + switch (op) { case OP_GET: value = input.getAttribute(attr, conf, objectModel); @@ -340,6 +340,9 @@ break; }; + if (getLogger().isDebugEnabled()) + getLogger().debug("using "+name+" as "+input+" for "+op+" ("+attr+") and "+conf+" gives "+value); + } catch (Exception e) { if (getLogger().isWarnEnabled()) getLogger().warn("A problem obtaining a value from "+name+" occurred : "+e.getMessage()); 1.3.2.2 +19 -12 xml-cocoon2/src/java/org/apache/cocoon/components/modules/input/ChainMetaModule.java Index: ChainMetaModule.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/modules/input/ChainMetaModule.java,v retrieving revision 1.3.2.1 retrieving revision 1.3.2.2 diff -u -r1.3.2.1 -r1.3.2.2 --- ChainMetaModule.java 17 Nov 2002 19:12:49 -0000 1.3.2.1 +++ ChainMetaModule.java 16 Dec 2002 14:56:30 -0000 1.3.2.2 @@ -165,19 +165,21 @@ public void dispose() { - if (!this.initialized.booleanValue()) - if (getLogger().isErrorEnabled()) + if (!this.initialized.booleanValue()) { + if (getLogger().isErrorEnabled()) { getLogger().error("Uninitialized Component! dispose() FAILING"); - else - if (this.inputSelector != null) { - - for (int i=0; i<this.inputs.length; i++) { - if (this.inputs[i].input != null) - this.inputSelector.release(this.inputs[i].input); - } - - this.manager.release(this.inputSelector); + } + } else { + if (this.inputSelector != null) { + + for (int i=0; i<this.inputs.length; i++) { + if (this.inputs[i].input != null) + this.inputSelector.release(this.inputs[i].input); } + + this.manager.release(this.inputSelector); + } + } } @@ -199,6 +201,7 @@ inputConfigs = modeConf.getChildren("input-modules"); emptyAsNull = modeConf.getChild("empty-as-null").getValueAsBoolean(emptyAsNull); allValues = modeConf.getChild("all-values").getValueAsBoolean(allValues); + if (inputConfigs.length == 0) inputConfigs = null; } Object[] value = null; @@ -236,6 +239,7 @@ i++; } } + if (debug) getLogger().debug("result chaining for "+attr+" is "+(allValues? values.toArray() : value)); return (allValues? values.toArray() : value); } @@ -264,6 +268,7 @@ inputConfigs = modeConf.getChildren("input-modules"); emptyAsNull = modeConf.getChild("empty-as-null").getValueAsBoolean(emptyAsNull); allNames = modeConf.getChild("all-names").getValueAsBoolean(allNames); + if (inputConfigs.length == 0) inputConfigs = null; } Iterator value = null; @@ -295,6 +300,7 @@ i++; } } + if (debug) getLogger().debug("result chaining names is "+(allNames? values.iterator() : value)); return (allNames? values.iterator() : value); } @@ -303,6 +309,7 @@ throws ConfigurationException { Object[] values = this.getAttributeValues(attr,modeConf,objectModel); + if (getLogger().isDebugEnabled()) getLogger().debug("result chaining single for "+attr+" is "+(values != null? values[0] : "null")); return (values != null? values[0] : null); } 1.1.2.3 +45 -24 xml-cocoon2/src/java/org/apache/cocoon/components/modules/input/JXPathMetaModule.java Index: JXPathMetaModule.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/modules/input/JXPathMetaModule.java,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- JXPathMetaModule.java 6 Dec 2002 09:27:30 -0000 1.1.2.2 +++ JXPathMetaModule.java 16 Dec 2002 14:56:30 -0000 1.1.2.3 @@ -51,24 +51,15 @@ package org.apache.cocoon.components.modules.input; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Iterator; - import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; -import org.apache.avalon.framework.thread.ThreadSafe; - -import org.apache.cocoon.environment.ObjectModelHelper; -import org.apache.cocoon.environment.Request; -import org.apache.cocoon.environment.Session; -import org.apache.cocoon.environment.Context; - import org.apache.commons.jxpath.*; -import org.apache.commons.jxpath.servlet.*; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; /** * JXPathModule allows to access properties of any object in generic @@ -80,6 +71,10 @@ * * <p>Configuration example:</p> * <table> + * <tr><td><code><lenient>false</lenient></td> + * <td>When set to true, non-existing attributes return null, when set to false, + * an exception is thrown. Default is true.</td> + *</tr> * <tr><td><code><input-module name="request-attr" parameter="foo"/></td> * <td>Uses the "request-attr" input module to obtain parameter named "foo" and * applies the given JXPath expression to it.</td> @@ -109,7 +104,13 @@ * */ protected FunctionLibrary library = null; - protected String parameter = null; + + /** set lenient mode for jxpath (i.e. throw an exception on + * unsupported attributes) ? + */ + protected boolean lenient = true; + + protected String parameter = ""; public JXPathMetaModule() { @@ -134,6 +135,7 @@ // start verbatim copy of AbstractJXPathModule // please keep both in sync. + this.lenient = config.getChild("lenient").getValueAsBoolean(this.lenient); this.library = new FunctionLibrary(); getFunctions(this.library, config); getPackages(this.library, config); @@ -234,11 +236,15 @@ Map objectModel) throws ConfigurationException { + Object contextObj = getContextObject(modeConf, objectModel); try { - Object contextObj = getContextObject(modeConf, objectModel); JXPathContext jxContext = JXPathContext.newContext(contextObj); setupExtensions(jxContext, modeConf); - return jxContext.getValue(name); + if (this.lenient) jxContext.setLenient(true); // return null insted of exception on non existing property + Object obj = jxContext.getValue(name); + if (getLogger().isDebugEnabled()) + getLogger().debug("for "+name+" returning an "+(obj == null ? "null" : obj.getClass().getName())+" as "+obj); + return obj; } catch (Exception e) { throw new ConfigurationException( "Module does not support <" + name + ">" + "attribute.", @@ -273,16 +279,21 @@ public Object[] getAttributeValues(String name, Configuration modeConf, Map objectModel) throws ConfigurationException { + Object contextObj = getContextObject(modeConf, objectModel); try { - Object contextObj = getContextObject(modeConf, objectModel); JXPathContext jxContext = JXPathContext.newContext(contextObj); List values = new LinkedList(); setupExtensions(jxContext, modeConf); + if (this.lenient) jxContext.setLenient(true); // return null insted of exception on non existing property Iterator i = jxContext.iterate(name); while (i.hasNext()) { values.add(i.next()); } - return values.toArray(); + Object[] obj = values.toArray(); + if (obj.length == 0) obj = null; + if (getLogger().isDebugEnabled()) + getLogger().debug("for "+name+" returning an "+(obj == null ? "null" : obj.getClass().getName())+" as "+obj); + return obj; } catch (Exception e) { throw new ConfigurationException( "Module does not support <" + name + ">" + "attribute.", @@ -306,12 +317,22 @@ String inputName=null; String parameter = this.parameter; if (modeConf!=null) { - inputName = modeConf.getChild("input-module").getAttribute("name",null); + mConf = modeConf.getChild("input-module"); + inputName = mConf.getAttribute("name",null); parameter = modeConf.getAttribute("parameter",parameter); } - return this.getValue(parameter, objectModel, - this.input, this.defaultInput, this.inputConf, - null, inputName, modeConf); + + if (getLogger().isDebugEnabled()) + getLogger().debug("modeConf is "+modeConf+" this.inputConf is "+this.inputConf+" mConf is "+mConf+" this.input is "+this.input+" this.defaultInput is "+this.defaultInput+" inputName is "+inputName+" parameter is "+parameter); + + Object obj = this.getValue(parameter, objectModel, + this.input, this.defaultInput, this.inputConf, + null, inputName, mConf); + + if (getLogger().isDebugEnabled()) + getLogger().debug("returning an "+(obj == null ? "null" : obj.getClass().getName())+" as "+obj); + + return obj; } } 1.1.2.2 +63 -14 xml-cocoon2/src/java/org/apache/cocoon/components/modules/input/SimpleMappingMetaModule.java Index: SimpleMappingMetaModule.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/modules/input/SimpleMappingMetaModule.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- SimpleMappingMetaModule.java 30 Nov 2002 10:21:39 -0000 1.1.2.1 +++ SimpleMappingMetaModule.java 16 Dec 2002 14:56:30 -0000 1.1.2.2 @@ -52,13 +52,12 @@ import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; -import org.apache.avalon.framework.thread.ThreadSafe; -import java.util.Map; import java.util.HashMap; import java.util.HashSet; -import java.util.Set; import java.util.Iterator; +import java.util.Map; +import java.util.Set; /** Meta module that obtains values from an other module and by * replacing the requested attribute name with another name. This is @@ -76,8 +75,11 @@ * Will map a parameter "foo" to the real one named * "cocoon.bar.attr". If parameters "coocoon.yeeha.attr" and * "shopping.cart" exist, the iterator will return - * "yeeha". "shopping.cart" does not contain the pre-/ suffix and - * thus is dropped.</p> + * "yeeha". "shopping.cart" does not contain the pre-/ suffix and thus + * is dropped.</p> + * + * <p>Similarily, rm-prefix and rm-suffix will be removed from the + * attribute name.</p> * * @author <a href="mailto:[EMAIL PROTECTED]">Christian Haul</a> * @version CVS $Id$ @@ -86,6 +88,8 @@ String prefix = null; String suffix = null; + String rmPrefix = null; + String rmSuffix = null; Mapping mapping = null; protected class Mapping { @@ -141,6 +145,8 @@ this.inputConf = config.getChild("input-module"); this.prefix = config.getChild("prefix").getValue(null); this.suffix = config.getChild("suffix").getValue(null); + this.rmPrefix = config.getChild("rm-prefix").getValue(null); + this.rmSuffix = config.getChild("rm-suffix").getValue(null); this.mapping = new Mapping(config); } @@ -164,6 +170,9 @@ Mapping mapping = this.mapping; String prefix = this.prefix; String suffix = this.suffix; + String rmPrefix = this.rmPrefix; + String rmSuffix = this.rmSuffix; + if (modeConf!=null) { inputName = modeConf.getChild("input-module").getAttribute("name",null); if (inputName != null) { @@ -172,17 +181,33 @@ mapping = new Mapping(modeConf); prefix = modeConf.getChild("prefix").getValue(null); suffix = modeConf.getChild("suffix").getValue(null); + rmPrefix = modeConf.getChild("rm-prefix").getValue(null); + rmSuffix = modeConf.getChild("rm-suffix").getValue(null); } + // remove rm-prefix and rm-suffix + if (rmPrefix != null && name.startsWith(rmPrefix)) { + name = name.substring(rmPrefix.length()); + } + if (rmSuffix != null && name.endsWith(rmSuffix)) { + name = name.substring(0,name.length() - rmSuffix.length()); + } + // map String param = mapping.mapTo(name); + // add prefix and suffix if (prefix != null) param = prefix + param; if (suffix != null) param = param + suffix; if (getLogger().isDebugEnabled()) getLogger().debug("mapping ['"+name+"'] to ['"+param+"']"); - return getValue(param, objectModel, - this.input, this.defaultInput, this.inputConf, - null, inputName, inputConfig); + Object res = getValue(param, objectModel, + this.input, this.defaultInput, this.inputConf, + null, inputName, inputConfig); + + if (getLogger().isDebugEnabled()) + getLogger().debug("getting for real attribute ['"+param+"'] value: "+res); + + return res; } @@ -208,6 +233,9 @@ Mapping mapping = this.mapping; String prefix = this.prefix; String suffix = this.suffix; + String rmPrefix = this.rmPrefix; + String rmSuffix = this.rmSuffix; + if (modeConf!=null) { inputName = modeConf.getChild("input-module").getAttribute("name",null); if (inputName != null) { @@ -216,17 +244,32 @@ mapping = new Mapping(modeConf); prefix = modeConf.getChild("prefix").getValue(null); suffix = modeConf.getChild("suffix").getValue(null); + rmPrefix = modeConf.getChild("rm-prefix").getValue(null); + rmSuffix = modeConf.getChild("rm-suffix").getValue(null); } + // remove rm-prefix and rm-suffix + if (rmPrefix != null && name.startsWith(rmPrefix)) { + name = name.substring(rmPrefix.length()); + } + if (rmSuffix != null && name.endsWith(rmSuffix)) { + name = name.substring(0,name.length() - rmSuffix.length()); + } + // map String param = mapping.mapTo(name); + // add prefix and suffix if (prefix != null) param = prefix + param; if (suffix != null) param = param + suffix; if (getLogger().isDebugEnabled()) getLogger().debug("mapping ['"+name+"'] to ['"+param+"']"); - return getValues(param, objectModel, - this.input, this.defaultInput, this.inputConf, - null, inputName, inputConfig); + Object[] res = getValues(param, objectModel, + this.input, this.defaultInput, this.inputConf, + null, inputName, inputConfig); + if (getLogger().isDebugEnabled()) + getLogger().debug("getting for real attribute ['"+param+"'] value: "+res); + + return res; } @@ -250,6 +293,8 @@ Mapping mapping = this.mapping; String prefix = this.prefix; String suffix = this.suffix; + String rmPrefix = this.rmPrefix; + String rmSuffix = this.rmSuffix; if (modeConf!=null) { inputName = modeConf.getChild("input-module").getAttribute("name",null); if (inputName != null) { @@ -258,6 +303,8 @@ mapping = new Mapping(modeConf); prefix = modeConf.getChild("prefix").getValue(null); suffix = modeConf.getChild("suffix").getValue(null); + rmPrefix = modeConf.getChild("rm-prefix").getValue(null); + rmSuffix = modeConf.getChild("rm-suffix").getValue(null); } Iterator names = getNames(objectModel, @@ -292,6 +339,9 @@ String newName = mapping.mapFrom(param); + if (rmPrefix != null) newName = rmPrefix + newName; + if (rmSuffix != null) newName = newName + rmSuffix; + if (getLogger().isDebugEnabled()) getLogger().debug("reverse mapping results in ['"+newName+"']"); @@ -300,7 +350,6 @@ return set.iterator(); - } - + } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]