Hi,

Here under are two exercises related to array mapping:
The first one, using a Double[] works as expected.
The second one, using a double[] does not work out of the box, it crashes the bind compiler. I've found a workaround (see version b) as a temporary fix, but I'd really like to see this work as expected.
Details follow.

[May I suggest also to post similar examples of array mapping in JiBX documentation. ]
Bye
/Hervé

Both exercises marshall to and unmarshall from the same data file, whose content is :
<?xml version="1.0" encoding="UTF-8"?>
<waiter>
  <first-name>John</first-name>
  <tips>
     <tip>123.456</tip>
     <tip>0.0</tip>
     <tip>0.48</tip>
  </tips>
</waiter>

Here is the first exercise (works OK):

java code :
package omr.jibx;
public class Waiter {
   private String firstName;
   public Double[] tips;
}

mapping :
<binding>
   <mapping name="waiter" class="omr.jibx.Waiter">
   <value name="first-name" field="firstName" />
   <collection name="tips" field="tips">
       <value name="tip" />
   </collection>
   </mapping>
</binding>

Here is the second exercise, version 'a' (does not work):

java code :
package omr.jibx;
public class Waiter {
   private String firstName;
   public double[] tips;  // notice double instead of Double
}

mapping (unchanged) :
<binding>
   <mapping name="waiter" class="omr.jibx.Waiter">
   <value name="first-name" field="firstName" />
   <collection name="tips" field="tips">
       <value name="tip" />
   </collection>
   </mapping>
</binding>

Here is the output of the binding compiler :

Generating code for mapping omr.jibx.Waiter
Error running binding compiler

*** Error during code generation - please report this error on the JiBX users list so that the condition can be caught during validation ***

java.lang.IllegalStateException: Internal error: Expected object reference on stack , found double

full stack:

 0: double[]

 1: double

at org.jibx.binding.classes.MethodBuilder.verifyStackObject(MethodBuilder.java:509) at org.jibx.binding.classes.MethodBuilder.appendCreateCast(MethodBuilder.java:972) at org.jibx.binding.def.NestedCollection.genContentUnmarshal(NestedCollection.java:145) at org.jibx.binding.def.ObjectBinding.genUnmarshalContentCall(ObjectBinding.java:757) at org.jibx.binding.def.ObjectBinding.genContentUnmarshal(ObjectBinding.java:905) at org.jibx.binding.def.ComponentProperty.genContentUnmarshal(ComponentProperty.java:236) at org.jibx.binding.def.ElementWrapper.genContentUnmarshal(ElementWrapper.java:272) at org.jibx.binding.def.NestedStructure.genContentUnmarshal(NestedStructure.java:156) at org.jibx.binding.def.ObjectBinding.genUnmarshalContentCall(ObjectBinding.java:757) at org.jibx.binding.def.ObjectBinding.genContentUnmarshal(ObjectBinding.java:905) at org.jibx.binding.def.ElementWrapper.genContentUnmarshal(ElementWrapper.java:272) at org.jibx.binding.def.MappingDefinition.generateCode(MappingDefinition.java:565) at org.jibx.binding.def.DefinitionContext.generateCode(DefinitionContext.java:604) at org.jibx.binding.def.BindingDefinition.generateCode(BindingDefinition.java:611)
   at org.jibx.binding.Compile.compile(Compile.java:305)
   at org.jibx.binding.ant.CompileTask.execute(CompileTask.java:248)
   at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
   at org.apache.tools.ant.Task.perform(Task.java:364)
   at org.apache.tools.ant.Target.execute(Target.java:341)
   at org.apache.tools.ant.Target.performTasks(Target.java:369)
   at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
   at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
   at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
   at org.apache.tools.ant.Main.runBuild(Main.java:668)
   at org.apache.tools.ant.Main.startAnt(Main.java:187)
   at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
   at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)

And finally, here is the second exercise, once modified into version 'b' (works OK)
I use temporary Double[] through custom methods :

package omr.jibx;
public class Waiter {
   private String firstName;
   public double[] tips;

   public Double[] getTips()
   {
       Double[] dd = null;
       if (tips != null) {
           dd = new Double[tips.length];
           for (int i = 0; i < tips.length; i++) {
               dd[i] = tips[i];
           }
       }
       return dd;
   }

   public void setTips (Double[] tips)
   {
       this.tips = new double[tips.length];
       for (int i = 0; i < tips.length; i++) {
           this.tips[i] = tips[i];
       }
   }
}

and the mapping modified accordingly :
<binding>
   <mapping name="waiter" class="omr.jibx.Waiter">
   <value name="first-name" field="firstName" />
    <collection name="tips" get-method="getTips" set-method="setTips">
       <value name="tip" />
   </collection>
   </mapping>
</binding>

And this works (functionnally at least).



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_idv37&alloc_id865&op=click
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to