Hi Archie,

Thanks for your reply. Actually, I need mapping for classes B and C to be 
abstract, because in the xml, I don't know what the containing element will be: 
it could be either Identifier1 or Identifier2 or to turn it around, in the xml, 
Identifier1 and Identifier2 could be unmarshalled to either an instance of 
class B or C, depending on the containing xml structure.

To illustrate this, the following snippets of xml should both be 
unmarshallable, the first would unmarshal to an instance of class B, the second 
to an instance of class C.

  <Identifier1>
    <LdapName>1</LdapName>
  </Identifier1>

  <Identifier1>
    <IdentPart1>a</IdentPart1>
    <IdentPart2>b</IdentPart2>
  </Identifier1>

To clarify things some more, class A looks something like this:

/**
 * This class does not know the implementation of it's fields
 */
public class A {
        
        private I one = null;
        
        private I two = null;
        
        public I getOne() {
                return one;
        }
        
        public void setOne(I iImpl) {
                this.one = iImpl;
        }
        
        public I getTwo() {
                return two;
        }
        
        public void setTwo(I iImpl) {
                this.two = iImpl;
        }
        
}


The schemas are fixed and provided, so I cannot change them.

Does anyone have any other suggestions?
Dave


On Feb 1, 2012, at 4:10 PM, Archie Cobbs wrote:

> The terminology is confusing.
> 
> A JiBX "abstract mapping" is just like a macro that declares a bunch of 
> structure fields without declaring the name of the containing XML element. 
> Give it a "type-name" and then dereference it elsewhere with "map-as".
> 
> The JiBX "extends" keywords simply means "member of a substitution group", 
> i.e., anywhere the thing you extend can appear, the extending thing may 
> appear. Extending can only be done with concrete mappings however because the 
> XML element name is used to determine which of the possible choices to apply 
> when unmarshalling.
> 
> So you don't really need abstract mappings unless you want to save keystrokes 
> in your binding file. Try something like this:
> 
>     <mapping abstract="true" class="my.pack.I"/> 
> 
>     <mapping name="Bbb" class="my.pack.B" extends="my.pack.I">
>         <value name="LdapName" field="s1" />
>     </mapping>
> 
>     <mapping name="Ccc" class="my.pack.C" extends="my.pack.I">
>         <value name="IdentPart1" field="s2" />
>         <value name="IdentPart2" field="s3" />
>     </mapping>
> 
>     <mapping name="Aaaa" type="my.pack.A">
>       <structure get-method="getOne" set-method="setOne"/>
>     </mapping>
> 
> -Archie
> 
> On Wed, Feb 1, 2012 at 3:46 AM, Dave Schoorl wrote:
> Hello group,
> 
> I am new to JiBX and I have a set of given schemas and a given domain model. 
> I am writing binding files by hand. I am running into the situation that I 
> have an interface I with two implementations, class B and class C. The 
> bindings for B and C need to be abstract, because they are types. Imagine 
> class A that has multiple fields of type I. I have created the following 
> binding file:
> 
> <binding>
>     <mapping abstract="true" class="my.pack.I" />
> 
>     <mapping abstract="true" class="my.pack.B" extends="my.pack.I">
>         <value name="LdapName" field="s1" />
>     </mapping>
> 
>     <mapping abstract="true" class="my.pack.C" extends="my.pack.I">
>         <value name="IdentPart1" field="s2" />
>         <value name="IdentPart2" field="s3" />
>     </mapping>
> 
>     <mapping name="Aaaa" class="my.pack.A">
>         <structure name="Identifier1" get-method="getOne" set-method="setOne" 
> />
>         <structure name="Identifier2" get-method="getTwo" set-method="setTwo" 
> usage="optional"/>
>     </mapping>
> </binding>
> 
> but compiling the binding into the Java classes first results in the 
> following warning for mapping of class B and C:
> > Warning: Only concrete mappings should be 'leaf' mappings for extensions; 
> > you should either remove 'extends' attribute or add concrete extension 
> > mappings for this abstract mapping;
> 
> followed by the following error for each structure mapping in class A:
> > Error: name attribute not allowed on reference to mapping with extensions; 
> 
> 
> When I change the above binding file and remove the mapping of I and the 
> 'extends'-attribute on the mapping of classes B and C, then the binding gets 
> compiled without errors, but when I want to unmarshall an xml file, I get the 
> following exception:
> 
> org.jibx.runtime.JiBXException: Cannot create instance of interface or 
> abstract class my.pack.I
>       at 
> my.pack.JiBX_binding_AInterfaceMungeAdapter.JiBX_binding_multiple_I_newinstance_1_0()
>       at my.pack.A.JiBX_binding_multiple_I_unmarshal_1_0(A.java)
>       at my.pack.JiBX_binding_multiple_IA_access.unmarshal()
>       at 
> org.jibx.runtime.impl.UnmarshallingContext.unmarshalElement(UnmarshallingContext.java:2757)
>       at 
> org.jibx.runtime.impl.UnmarshallingContext.unmarshalDocument(UnmarshallingContext.java:2900)
>       at 
> my.pack.ADatabindingTest.testUnmarshallAInterface(ADatabindingTest.java:24)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>       at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>       at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>       at java.lang.reflect.Method.invoke(Method.java:597)
>       at 
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
>       at 
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
>       at 
> org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
>       at 
> org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
>       at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
>       at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
>       at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
>       at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
>       at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
>       at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
>       at 
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
>       at 
> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
>       at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
>       at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
>       at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
>       at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
> 
> 
> 
> How can I solve this situation? Can my use case be implemented using standard 
> JiBX mappings, or should I use a custom IUnmarshaller or something else to 
> solve this problem?
> 
> Thanks in advance,
> Dave
> 
> 
> ------------------------------------------------------------------------------
> Keep Your Developer Skills Current with LearnDevNow!
> The most comprehensive online learning library for Microsoft developers
> is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
> Metro Style Apps, more. Free future releases when you subscribe now!
> http://p.sf.net/sfu/learndevnow-d2d
> _______________________________________________
> jibx-users mailing list
> jibx-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jibx-users
> 
> 
> 
> 
> -- 
> Archie L. Cobbs
> 
> ------------------------------------------------------------------------------
> Keep Your Developer Skills Current with LearnDevNow!
> The most comprehensive online learning library for Microsoft developers
> is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
> Metro Style Apps, more. Free future releases when you subscribe now!
> http://p.sf.net/sfu/learndevnow-d2d_______________________________________________
> jibx-users mailing list
> jibx-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jibx-users

------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
jibx-users mailing list
jibx-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to