This email is related to my previous email, but I'm phrasing it as a 
separate question.  The code provided here also applies to my previous 
email in case that one needed more clarification.

For the past few hours I have been trying to get IdDefRefMapperBase to 
work in the way I want it to, but with no success.  I've started to come 
to the conclusion that it can't do what it claims to do... and if it 
can, the documentation for it is so poor that there is no way I could 
have been expected to figure it out.  Here is what I have been trying to do:

I have the following classes:
--- AbstractNode.java --
package com.app2you.TestJiBX.classes;

import java.util.Iterator;
import java.util.Vector;

public abstract class AbstractNode {
    private AbstractNode parent;
    private Vector<AbstractNode> children;
    public int id;
   
    public AbstractNode() {
        children = new Vector<AbstractNode>();
    }

    public AbstractNode getParent() {
        return parent;
    }

    public void setParent(AbstractNode parent) {
        if (this.parent != null) {
            parent.children.remove(this);
        }
        this.parent = parent;
        if (this.parent != null) {
            parent.children.add(this);
        }
    }

    public Iterator<AbstractNode> getChildren() {
        return children.iterator();
    }

    public abstract void doSomething();
}

---- PrintNode.java ----
package com.app2you.TestJiBX.classes;

import java.util.Iterator;

public class PrintNode extends AbstractNode {
    private String toPrint;
   
    private PrintNode() {
        super();
    }

    public PrintNode(String whatToPrint) {
        super();
        toPrint = whatToPrint;
    }

    @Override
    public void doSomething() {
        System.out.println(toPrint);
        Iterator<AbstractNode> child = getChildren();
        while (child.hasNext())
        {
            child.next().doSomething();
        }
    }

}

--- AbstractNodeMarshaller.java ---
package com.app2you.TestJiBX.classes;

import org.jibx.extras.IdDefRefMapperBase;

public class AbstractNodeMarshaller extends IdDefRefMapperBase {

    public AbstractNodeMarshaller(String uri, int index, String name) {
        super(uri, index, name);
    }

    @Override
    protected String getIdValue(Object arg0) {
        return Integer.toString(((PrintNode) arg0).id);
    }
}

--- TestJiBX.java --- (just a driver for testing purposes)
package com.app2you.TestJiBX;

import java.io.*;
import org.jibx.runtime.*;
import com.app2you.TestJiBX.classes.PrintNode;

public class TestJiBX {
    private static int counter = 0;

    public static void main(String[] args) {
        PrintNode top = new PrintNode("top");
        top.id = counter++;
        populate(top, 3, "-");

        top.doSomething();
       
        IBindingFactory bfact;
       
        System.out.println("Writing the tree out to xml");
        try {
            bfact = BindingDirectory.getFactory(PrintNode.class);

            IMarshallingContext mctx = bfact.createMarshallingContext();
            mctx.marshalDocument(top, "UTF-8", null, new FileOutputStream(
                    "top.xml"));
        } catch (JiBXException e) {
            e.printStackTrace();
            return;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        }

        System.out.println("Reading in the tree from xml");
       
        try {
            bfact = BindingDirectory.getFactory(PrintNode.class);
           
            IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
            PrintNode newTop = (PrintNode)uctx.unmarshalDocument
                (new FileInputStream("top.xml"), null);
            newTop.doSomething();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JiBXException e) {
            e.printStackTrace();
        }
    }

    private static void populate(PrintNode node, int level, String prefix) {
        for (int c = 0; c < level; c++) {
            PrintNode child = new PrintNode(prefix + 
Integer.toString(level));
            child.id = counter++;
            child.setParent(node);
            populate(child, level - 1, prefix+"-");
        }
    }
}

What I am trying to do is get it so that whenever an AbstractNode is 
used, it uses the AbstractNodeMarshaller.  I have run into several 
roadblocks:
1) A mapping cannot extend a mapping that uses a custom 
marshaller/unmarshaller
2) A mapping which specifies a custom marshaller/unmarshaller cannot 
contain any child XML nodes, such as a <structure>, <value>, or 
<collection> specification
3) If at any point an AbtractNode is rendered without using the 
AbstractNodeMarshaller, then the marshaller does not know that it has 
already been fleshed out, so it writes it out again - this means that it 
MUST be used for EVERY instance of this object

Those are the major ones... there were also several more smaller ones.

The results I always get are one of the following:
1) A stack overflow when attempting to marshal the top of the tree - 
this means that the parent nodes are getting fleshed out instead of 
referencing the one already written out, causing infinite loops
2) A marshalled output that does not read back in properly.  Either the 
unmarshaller is expecting a different tag at some point, or it cannot 
find what something is referencing.
3) A marshalled output that does not contain any of the values in 
AbstractNode, just the values in PrintNode, and so it 
marshalls/unmarshalls without error, but what I get back is only one 
node with no children.

Is there any binding.xml that would actually work? 

Sorry for my second ridiculously long post, but I like to be thorough :)
-David Clamage

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
jibx-users mailing list
jibx-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to