I have an Marshalling problem using reference IDREFs.

Context:
  I have 2 object "b" and "c" which references the same object "a",
  and the object "d" which contains "b" and "c".
  When i marshal the object "d", the xml data contains "d", "b" and "c"
  but the referenced object "a" is missing, i don't understand why ?

My simple source code:

public class A
{
  private int m_reference;
  private String m_name;

  public void setReference(int _reference) {
    m_reference = _reference;
  }
  public int getReference(){
    return m_reference;
  }
  public void setName(String _name) {
    m_name = _name;
  }
  public String getName() {
    return m_name;
  }
  public A() {
  }
}

public class B
{
  private String m_type;
  private A m_a;

  public void setType(String _type) {
    m_type = _type;
  }
  public String getType()  {
    return m_type;
  }
  public void setA(A _a) {
    m_a = _a;
  }
  public A getA() {
    return m_a;
  }
  public B()  {
  }
}

public class C
{
  private String m_category;
  private A m_a;

  public void setCategory(String _category)  {
    m_category = _category;
  }
  public String getCategory()  {
    return m_category;
  }
  public void setA(A _a)  {
    m_a = _a;
  }
  public A getA()  {
    return m_a;
  }
  public C() {
  }
}

public class D
{
  private int m_number;
  private B m_b;
  private C m_c;

  public void setNumber(int _number)  {
    m_number = _number;
  }
  public int getNumber()  {
    return m_number;
  }
  public void setB(B _b)  {
    m_b = _b;
  }
  public B getB()  {
    return m_b;
  }
  public void setC(C _c)  {
    m_c = _c;
  }
  public C getC() {
    return m_c;
  }
  public D() {
  }
}

package test;

import test.server.*;
import java.io.*;
import org.exolab.castor.xml.*;
import org.exolab.castor.mapping.*;

public class OnServerSide
{
  private Mapping m_mapping = XMappingLoader.Load("Mapping.xml");
  private D m_d;

  public OnServerSide()
  {
    A a = new A();
    a.setReference(15);
    a.setName("TOTO");

    B b = new B();
    b.setType("XX25");
    b.setA(a);

    C c = new C();
    c.setCategory("C3");
    c.setA(a);

    m_d = new D();
    m_d.setNumber(123);
    m_d.setB(b);
    m_d.setC(c);
  }

  public void marshall()
  {
    byte[] xmlData = null;
    try
    {
      // Create a byte array output stream.
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      OutputStreamWriter outputStreamWriter = new OutputStreamWriter(baos);

      // Create a new castor marshaller.
      Marshaller marshaller = new Marshaller(outputStreamWriter);


      // Note the resolver must be set before the mapping, otherwise
      // the mapping will be ignored.
      if (m_mapping != null)
      {
        marshaller.setMapping(m_mapping);
      }

      marshaller.marshal(m_d);
      xmlData = baos.toByteArray();
      baos.flush();
      baos.close();

      // Trace:
      System.out.println("*** Marshalling completed successfully ***");
      System.out.println(new String(xmlData));
    }
    catch (Exception e)
    {
      System.out.println("Marshalling failed !");
    }

  }

  public static void main(String args[])
  {
    OnServerSide tester = new OnServerSide();
    tester.marshall();
  }
}

My simple mapping file:

<?xml version="1.0"?>
<!DOCTYPE databases PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
                           "http://castor.exolab.org/mapping.dtd";>
<mapping>
  <class name="test.server.A" auto-complete="true" identity="reference">
      <map-to xml="A"/>
      <field name="reference" type="int">
         <bind-xml node="attribute" />
      </field>
  </class>

  <class name="test.server.B" auto-complete="true">
      <map-to xml="B"/>
      <field name="a" type="test.server.A">
         <bind-xml name="REF_A" reference="true" node="attribute" />
      </field>
  </class>

  <class name="test.server.C" auto-complete="true">
      <map-to xml="C"/>
      <field name="a" type="test.server.A">
         <bind-xml name="REF_A" reference="true" node="attribute" />
      </field>
  </class>

  <class name="test.server.D" auto-complete="true">
      <map-to xml="D"/>
  </class>
</mapping>

The result of the marshalling where the object a is missing:

<?xml version="1.0" encoding="UTF-8"?>
<D>
    <b REF_A="15">
        <type>XX25</type>
    </b>
    <number>123</number>
    <c REF_A="15">
        <category>C3</category>
    </c>
</D>

Do you have any suggestion about my problem ?

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to