You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Christopher Oliver <re...@verizon.net> on 2003/02/21 20:04:24 UTC

Setting values in XMLForm (via JXPath)

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]);
                }
        }




Re: Setting values in XMLForm (via JXPath)

Posted by Christopher Oliver <re...@verizon.net>.
Not really. I was hoping to avoid creating a dependency on JavaScript 
objects 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 


since I implemented the JXPath Pointer interface for JavaScript objects:

http://cvs.apache.org/viewcvs.cgi/*checkout*/xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/ScriptablePropertyPointer.java?rev=HEAD&content-type=text/plain
http://cvs.apache.org/viewcvs.cgi/*checkout*/xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/ScriptablePointer.java?rev=HEAD&content-type=text/plain

However, it currently isn't possible to provide this behavior within 
Pointer itself.

Regards,

Chris

ivelin wrote:
> Apparently I missed this letter before.
> Yes Chris, this is the reason why the collection is being replaced.
> Any ideas for improvement?
> 
> -=Ivelin=-
> ----- Original Message -----
> From: "Christopher Oliver" <re...@verizon.net>
> To: <dm...@apache.org>
> Cc: <iv...@apache.org>; <co...@xml.apache.org>
> Sent: Friday, February 21, 2003 4:10 PM
> Subject: Re: Setting values in XMLForm (via JXPath)
> 
> 
> 
>>In this case it looks like XMLForm is trying to replace all the elements
>>of a collection with a new set of elements. It looks to me like there is
>>no reasonable way to do this in general with JXPath without the caller
>>being aware of the representation of the collection. Or am I wrong?
>>
>>Regards,
>>
>>Chris
>>
>>
>>Dmitri Plotnikov wrote:
>>
>>
>>>Christopher,
>>>
>>>The answer to your question really depends on whether you want to
>>>replace an existing collection element or create a new one.  To create
>>>and element, use createPathAndSetValue.  To replace the textual
>>>contents of an existing element, simply use setValue()
>>>
>>>- Dmitri
>>>
>>>--- Christopher Oliver <re...@verizon.net> wrote:
>>>
>>>
>>>
>>>>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/apac
> 
> he/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]);
>>>>               }
>>>>       }
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>>__________________________________________________
>>>Do you Yahoo!?
>>>Yahoo! Tax Center - forms, calculators, tips, more
>>>http://taxes.yahoo.com/
>>>
>>>
>>>
>>
> 
> 



Re: Setting values in XMLForm (via JXPath)

Posted by ivelin <iv...@apache.org>.
Apparently I missed this letter before.
Yes Chris, this is the reason why the collection is being replaced.
Any ideas for improvement?

-=Ivelin=-
----- Original Message -----
From: "Christopher Oliver" <re...@verizon.net>
To: <dm...@apache.org>
Cc: <iv...@apache.org>; <co...@xml.apache.org>
Sent: Friday, February 21, 2003 4:10 PM
Subject: Re: Setting values in XMLForm (via JXPath)


> In this case it looks like XMLForm is trying to replace all the elements
> of a collection with a new set of elements. It looks to me like there is
> no reasonable way to do this in general with JXPath without the caller
> being aware of the representation of the collection. Or am I wrong?
>
> Regards,
>
> Chris
>
>
> Dmitri Plotnikov wrote:
>
> >Christopher,
> >
> >The answer to your question really depends on whether you want to
> >replace an existing collection element or create a new one.  To create
> >and element, use createPathAndSetValue.  To replace the textual
> >contents of an existing element, simply use setValue()
> >
> >- Dmitri
> >
> >--- Christopher Oliver <re...@verizon.net> wrote:
> >
> >
> >>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/apac
he/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]);
> >>                }
> >>        }
> >>
> >>
> >>
> >>
> >>
> >
> >
> >__________________________________________________
> >Do you Yahoo!?
> >Yahoo! Tax Center - forms, calculators, tips, more
> >http://taxes.yahoo.com/
> >
> >
> >
>


Re: Setting values in XMLForm (via JXPath)

Posted by Christopher Oliver <re...@verizon.net>.
In this case it looks like XMLForm is trying to replace all the elements 
of a collection with a new set of elements. It looks to me like there is 
no reasonable way to do this in general with JXPath without the caller 
being aware of the representation of the collection. Or am I wrong?

Regards,

Chris


Dmitri Plotnikov wrote:

>Christopher,
>
>The answer to your question really depends on whether you want to
>replace an existing collection element or create a new one.  To create
>and element, use createPathAndSetValue.  To replace the textual
>contents of an existing element, simply use setValue()
>
>- Dmitri
>
>--- Christopher Oliver <re...@verizon.net> wrote:
>  
>
>>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]);
>>                }
>>        }
>>
>>
>>
>>    
>>
>
>
>__________________________________________________
>Do you Yahoo!?
>Yahoo! Tax Center - forms, calculators, tips, more
>http://taxes.yahoo.com/
>
>  
>



Re: Setting values in XMLForm (via JXPath)

Posted by Dmitri Plotnikov <dp...@yahoo.com>.
Christopher,

The answer to your question really depends on whether you want to
replace an existing collection element or create a new one.  To create
and element, use createPathAndSetValue.  To replace the textual
contents of an existing element, simply use setValue()

- Dmitri

--- Christopher Oliver <re...@verizon.net> wrote:
> 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]);
>                 }
>         }
> 
> 
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/