haul 2002/12/16 02:37:33 Modified: src/java/org/apache/cocoon/components/modules/input AbstractJXPathModule.java JXPathMetaModule.java Log: <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> Revision Changes Path 1.7 +18 -3 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.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- AbstractJXPathModule.java 6 Dec 2002 09:20:20 -0000 1.6 +++ AbstractJXPathModule.java 16 Dec 2002 10:37:33 -0000 1.7 @@ -70,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 @@ -96,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 @@ -109,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); @@ -213,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.", @@ -253,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.4 +40 -10 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.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- JXPathMetaModule.java 6 Dec 2002 09:28:55 -0000 1.3 +++ JXPathMetaModule.java 16 Dec 2002 10:37:33 -0000 1.4 @@ -71,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> @@ -100,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() { @@ -125,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); @@ -225,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.", @@ -264,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.", @@ -297,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; } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]