Hi Robert,

After tweaking my code, the xml output is some how broken again. I have spend a few hours on it but cant decipher it. Essentially i have two problems here.

Problem 1, which is the one you been trying to help me earlier ie. if I would to use an Interface class, Betwixt doesn't seems to dump out the Superclass attributes (PersonName is superclass of Person) even though i have declared an interface dot.betwixt file for both PersonIntf and PersonNameIntf

Problem 2, A Custom Creator is the only way to update a member object (Person.address), is this true? If this is not true then, is it possible for me to map the <address> attribute to a method in Person.setAddress(String street1, int postalcode) so as to avoid the use of a CustomCreator class? This is to avoid breaking the encapsulation by re-implementing the private member class (Address) again in the Custom creator class.

I have created a AddressCreator but i can't figure out how to extract the values for the <address> element ie. both postalcode and street1 from the mapping element. I used many combinations of mappingElement.getAttributes() but all return a null value, pls refer to AddressCreator.java

Below are the output and my test case.

Thanks once again.

chung-onn

PS: I am using the nightly built version, downloaded about 2 days ago.

Output from my test
===============
Result:  //without interface
<person>
    <address>
      <postalcode>12345</postalcode>
      <street1>MyStreet</street1>
    </address>
    <age>21</age>
    <first>John</first>
    <last>Smith</last>
  </person>

Result with interface: //with interface, superclass (PersonName) attributes namely <first> & <last> are missing
<person-interface>
<address>
<postalcode>12345</postalcode>
<street1>MyStreet</street1>
</address>
<age>21</age>
</person-interface>


=== PersonNameIntf.betwixt
<?xml version="1.0" ?>
<info primitiveTypes="element">
        <element name="person-name">
            <element name="first" property="first"/>
            <element name="last" property="last"/>
        </element>
</info>

===PersonIntf.betwixt
<?xml version="1.0" ?>
<info primitiveTypes="element">
    <element name="person">
        <addDefaults/>
    </element>
</info>

===PersonNameIntf.java
public interface PersonNameIntf {

    public String getFirst();
    public void setFirst(String first);

    public String getLast();
    public void setLast(String last);

    public void setPersonName(String firstname, String lastname);
}

===PersonIntf
public interface PersonIntf extends PersonNameIntf{

    public int getAge();
    public void setAge(int age);
    public AddressIntf getAddress();
}

===AddressIntf
public interface AddressIntf {
    public String getStreet1();
    public int getPostalcode();
    public void setStreet1(String st);
    public void setPostalcode(int pc);

    //convenience method
    public void setAddress(String st, int pc);

}

===PersonName.java
public class PersonName implements PersonNameIntf {

    private String first;
    private String last;

public PersonName() {}

    public PersonName(String first, String last){
        this.first = first;
        this.last = last;
    }

    public String getFirst() {return this.first;}
    public void setFirst(String first) {this.first = first;}

    public String getLast() {return this.last;}
    public void setLast(String last) {this.last = last;}

    public void setPersonName(String firstname, String lastname) {
        this.first = firstname;
        this.last = lastname;
    }

public String toString() { return last + ", " + first;}

}

===AddressCreator

public class AddressCreator implements org.apache.commons.betwixt.io.read.ChainedBeanCreator {

    /** Creates a new instance of ServiceTicketCreator */
    public AddressCreator() {
    }

public Object create(org.apache.commons.betwixt.io.read.ElementMapping elementMapping, org.apache.commons.betwixt.io.read.ReadContext readContext, org.apache.commons.betwixt.io.read.BeanCreationChain beanCreationChain) {

dumpInfo(elementMapping, readContext, beanCreationChain);

if(AddressIntf.class.equals(elementMapping.getType())){
System.out.println("Creating a new Address");
System.out.println(" address:" + elementMapping.getAttributes().getValue("address"));
System.out.println(" street:" + elementMapping.getAttributes().getValue("street1"));
System.out.println(" postalcode:" + elementMapping.getAttributes().getValue("postalcode"));


            return new Address("new street", 111111);
        }

        return beanCreationChain.create(elementMapping, readContext);
    }

private void dumpInfo(org.apache.commons.betwixt.io.read.ElementMapping elementMapping, org.apache.commons.betwixt.io.read.ReadContext readContext, org.apache.commons.betwixt.io.read.BeanCreationChain beanCreationChain)
{
//elementMapping
System.out.println("===Begin dump===");
System.out.println("Element mapping info...");
System.out.println("first value=" + elementMapping.getAttributes().getValue("first"));
System.out.println("last value=" + elementMapping.getAttributes().getValue("last"));
System.out.println("age value=" + elementMapping.getAttributes().getValue("age"));
System.out.println("street1 value=" + elementMapping.getAttributes().getValue("street1"));
System.out.println("postal value=" + elementMapping.getAttributes().getValue("postalcode"));


System.out.println("Mapping type:"+ elementMapping.getType());
int l = elementMapping.getAttributes().getLength();
System.out.println("number of attr " + l);
for (int i=0; i < l; i++){
System.out.println("name=" + elementMapping.getAttributes().getQName(i) +
", type=" + elementMapping.getAttributes().getType(i) +
", value=" + elementMapping.getAttributes().getValue(i));
}



//readContext
//getBean
System.out.println("\nReadContext Info...");
Object bean = readContext.getBean();
System.out.println("Bean class:" + ((bean == null)?null: bean.getClass()));
java.util.Map vars = readContext.getVariables();
System.out.println("Variables..." + vars);
System.out.println("Classname Attrs..." + readContext.getClassNameAttribute() );
System.out.println("==end dump== \n\n");
}


    private class Address implements AddressIntf {
        private String street1;
        private int postalcode;

        /** Creates a new instance of Name */
        public Address() {}

        public Address(String street1, int postalcode){
            this.street1=street1;
            this.postalcode=postalcode;
        }

        public String getStreet1() {return street1;}
        public void setStreet1(String street1) {this.street1 = street1;}

public int getPostalcode() {return postalcode;}
public void setPostalcode(int postalcode) {this.postalcode = postalcode;}


        public void setAddress(String street1, int postalcode){
            this.street1=street1;
            this.postalcode=postalcode;
        }

public String toString(){return "street1="+street1+", postalcode="+postalcode;}

    }
}


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to