Christopher Oliver
2003-02-21 19:04:24 UTC
It appears that XMLForm's use of JXPath is hardcoded to setting indexed
values only on Java arrays and Collections (not DOM nodes or other types
of JXPath nodes). I was attempting to use a JavaScript object as a
JXPath node, but ran into the below problem in Form.java
(http://cvs.apache.org/viewcvs.cgi/*checkout*/xml-cocoon2/src/java/org/apache/cocoon/components/xmlform/Form.java?rev=HEAD&content-type=text/plain)
Dmitri, what is the proper way to set collection values in JXPath?
Should we be using createPathAndSetValue() here?
Regards,
Chris
public void setValue(String xpath, Object[] values) {
// // Dmitri Plotnikov's patch
//
// // if there are multiple values to set
// // (like in the selectMany case),
// // iterate over the array and set individual values
// if ( values.length > 1 )
// {
// Iterator iter =
jxcontext_.iteratePointers(xpath);
// for (int i = 0; i < values.length; i++ )
// {
// Pointer ptr = (Pointer)iter.next();
// ptr.setValue(values[i]);
// }
// }
// else
// {
// // This is supposed to do the right thing
// jxcontext_.setValue(xpath, values);
// }
//
Pointer pointer = jxcontext_.getPointer(xpath);
Object property = pointer.getValue();
// if there are multiple values to set
// (like in the selectMany case),
// iterate over the array and set individual values
// when the instance property is array
if (property != null && property.getClass().isArray()) {
Class componentType =
property.getClass().getComponentType();
property =
java.lang.reflect.Array.newInstance(
componentType,
values.length);
java.lang.System.arraycopy(values, 0, property,
0, values.length);
pointer.setValue(property);
} else if (property instanceof Collection) {
Collection cl = (Collection) property;
cl.clear();
cl.addAll(java.util.Arrays.asList(values));
}
// otherwise set the value of the first element
// (and the only) from the values array
else {
pointer.setValue(values[0]);
}
}
values only on Java arrays and Collections (not DOM nodes or other types
of JXPath nodes). I was attempting to use a JavaScript object as a
JXPath node, but ran into the below problem in Form.java
(http://cvs.apache.org/viewcvs.cgi/*checkout*/xml-cocoon2/src/java/org/apache/cocoon/components/xmlform/Form.java?rev=HEAD&content-type=text/plain)
Dmitri, what is the proper way to set collection values in JXPath?
Should we be using createPathAndSetValue() here?
Regards,
Chris
public void setValue(String xpath, Object[] values) {
// // Dmitri Plotnikov's patch
//
// // if there are multiple values to set
// // (like in the selectMany case),
// // iterate over the array and set individual values
// if ( values.length > 1 )
// {
// Iterator iter =
jxcontext_.iteratePointers(xpath);
// for (int i = 0; i < values.length; i++ )
// {
// Pointer ptr = (Pointer)iter.next();
// ptr.setValue(values[i]);
// }
// }
// else
// {
// // This is supposed to do the right thing
// jxcontext_.setValue(xpath, values);
// }
//
Pointer pointer = jxcontext_.getPointer(xpath);
Object property = pointer.getValue();
// if there are multiple values to set
// (like in the selectMany case),
// iterate over the array and set individual values
// when the instance property is array
if (property != null && property.getClass().isArray()) {
Class componentType =
property.getClass().getComponentType();
property =
java.lang.reflect.Array.newInstance(
componentType,
values.length);
java.lang.System.arraycopy(values, 0, property,
0, values.length);
pointer.setValue(property);
} else if (property instanceof Collection) {
Collection cl = (Collection) property;
cl.clear();
cl.addAll(java.util.Arrays.asList(values));
}
// otherwise set the value of the first element
// (and the only) from the values array
else {
pointer.setValue(values[0]);
}
}