Loading...

jibx-users@lists.sourceforge.net

[Prev] Thread [Next]  |  [Prev] Date [Next]

Re: [jibx-users] How to: abstract mappings with interface implementations? Archie Cobbs Fri Feb 03 18:03:27 2012

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 <[EMAIL PROTECTED]> 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
> [EMAIL PROTECTED]
> 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
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jibx-users