You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Diego Amicabile <18...@onlinehome.de> on 2004/05/02 01:34:35 UTC

[Betwixt] Example continued, I have a problem

but if I add a class CustomerList

import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.betwixt.io.BeanCreateRule;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CustomerList {
 Collection customers = new ArrayList();
 
 private static Log log = LogFactory.getLog( BeanCreateRule.class );
    
    public static void setLog(Log aLog) {
        log = aLog;
    }

 
 
 
 public Collection getCustomers() {
  if (customers == null) {
   customers = new ArrayList();
  }
  return customers;
   
 }
 public void addCustomer(CustomerBean newCustomerBean) {
  log.info("adding "+newCustomerBean); 
  getCustomers().add(newCustomerBean);
 }
 
 
 
 public String toString() {
  return ToStringBuilder.reflectionToString(this);
 }
 
}

I leave CustomerBean.betwixt intact
I use this class to write CustomerBean3.xml


import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.commons.betwixt.io.BeanWriter;
import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;

public class WriteCustomerListApp {

    public static final void main(String [] args) throws Exception {
        
       StringWriter outputWriter = new StringWriter(); 
       outputWriter.write("<?xml version='1.0' ?>");

        BeanWriter beanWriter = new BeanWriter(outputWriter);
   
        beanWriter.getXMLIntrospector().setAttributesForPrimitives(false);
        beanWriter.setWriteIDs(false);
        beanWriter.enablePrettyPrint();
        beanWriter.getXMLIntrospector().setAttributeNameMapper(new HyphenatedNameMapper());
   
        beanWriter.getXMLIntrospector().setElementNameMapper(new DecapitalizeNameMapper());
  CustomerList customerList = new CustomerList();
  
        CustomerBean customerBean = new CustomerBean();
        customerBean.setName("Diego");

        customerBean.addEmailAddre("A");
        customerBean.addEmailAddre("B");
        customerBean.addEmailAddre("C");
 
  customerBean.addOrder(new Order(1));
  customerBean.addOrder(new Order(2));


        CustomerBean customerBean2 = new CustomerBean();
        customerBean2.setName("Paolo");

        customerBean2.addEmailAddre("E");
        customerBean2.addEmailAddre("F");
 
  customerBean2.addOrder(new Order(4));
  customerBean2.addOrder(new Order(5));
  customerBean2.addOrder(new Order(6));

  customerList.addCustomer(customerBean);
  customerList.addCustomer(customerBean2);

        beanWriter.write("customerList", customerList);

  
        System.out.println(outputWriter.toString());
        FileOutputStream fos = new FileOutputStream( new File( "CustomerBean3.xml" ));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(fos)); 
        out.println(outputWriter.toString());
        out.println();
        
        out.flush();
        out.close();
        
    }        


}

Result is following

<?xml version='1.0' ?>
<customerList>
  <customers>
    <customer>
      <emailAddre>A</emailAddre>
      <emailAddre>B</emailAddre>
      <emailAddre>C</emailAddre>
      <name>Diego</name>
      <order id="1"/>
      <order id="2"/>
    </customer>
    <customer>
      <emailAddre>E</emailAddre>
      <emailAddre>F</emailAddre>
      <name>Paolo</name>/
      <order id="4"/>
      <order id="5"/>
      <order id="6"/>
    </customer>
  </customers>
</customerList>

I try to read this file like that


import java.io.FileReader;
import java.util.Collection;

import org.apache.commons.betwixt.io.BeanReader;
import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;

public class ReadCustomerListApp {
    
    public static final void main(String args[]) throws Exception{

     FileReader xmlReader = new FileReader("CustomerBean3.xml");

        BeanReader beanReader  = new BeanReader();
        
        beanReader.getXMLIntrospector().setAttributesForPrimitives(false);
        beanReader.setMatchIDs(false);
        beanReader.getXMLIntrospector().setAttributeNameMapper(new HyphenatedNameMapper());
        beanReader.getXMLIntrospector().setElementNameMapper(new DecapitalizeNameMapper());
         beanReader.registerBeanClass("customerList", CustomerList.class);
    
         beanReader.registerBeanClass("customer", CustomerBean.class);
     
     
         beanReader.registerBeanClass("order", Order.class);
 

        CustomerList customerList = (CustomerList ) beanReader.parse(xmlReader);
  
        System.out.println(customerList );
    }
    
}

and I get the output 
CustomerList@329f3d[customers=[CustomerBean@186c6b2[name=Diego,orders=[],emailAddres=[]], CustomerBean@1749757[name=Paolo,orders=[],emailAddres=[]]]]

that, is the methods addOrder and addEmailAddre are never called


Re: [Betwixt] Example continued, I have a problem

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
hi diego

i'm afraid that again it's the lack of a plural stemmer for your  
language. (see reply to your last email.

- robert

On 2 May 2004, at 00:34, Diego Amicabile wrote:

> but if I add a class CustomerList
>
> import java.util.ArrayList;
> import java.util.Collection;
>
> import org.apache.commons.betwixt.io.BeanCreateRule;
> import org.apache.commons.lang.builder.ToStringBuilder;
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
>
> public class CustomerList {
>  Collection customers = new ArrayList();
>
>  private static Log log = LogFactory.getLog( BeanCreateRule.class );
>
>     public static void setLog(Log aLog) {
>         log = aLog;
>     }
>
>
>
>
>  public Collection getCustomers() {
>   if (customers == null) {
>    customers = new ArrayList();
>   }
>   return customers;
>
>  }
>  public void addCustomer(CustomerBean newCustomerBean) {
>   log.info("adding "+newCustomerBean);
>   getCustomers().add(newCustomerBean);
>  }
>
>
>
>  public String toString() {
>   return ToStringBuilder.reflectionToString(this);
>  }
>
> }
>
> I leave CustomerBean.betwixt intact
> I use this class to write CustomerBean3.xml
>
>
> import java.io.File;
> import java.io.FileOutputStream;
> import java.io.OutputStreamWriter;
> import java.io.PrintWriter;
> import java.io.StringWriter;
>
> import org.apache.commons.betwixt.io.BeanWriter;
> import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
> import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
>
> public class WriteCustomerListApp {
>
>     public static final void main(String [] args) throws Exception {
>
>        StringWriter outputWriter = new StringWriter();
>        outputWriter.write("<?xml version='1.0' ?>");
>
>         BeanWriter beanWriter = new BeanWriter(outputWriter);
>
>          
> beanWriter.getXMLIntrospector().setAttributesForPrimitives(false);
>         beanWriter.setWriteIDs(false);
>         beanWriter.enablePrettyPrint();
>         beanWriter.getXMLIntrospector().setAttributeNameMapper(new  
> HyphenatedNameMapper());
>
>         beanWriter.getXMLIntrospector().setElementNameMapper(new  
> DecapitalizeNameMapper());
>   CustomerList customerList = new CustomerList();
>
>         CustomerBean customerBean = new CustomerBean();
>         customerBean.setName("Diego");
>
>         customerBean.addEmailAddre("A");
>         customerBean.addEmailAddre("B");
>         customerBean.addEmailAddre("C");
>
>   customerBean.addOrder(new Order(1));
>   customerBean.addOrder(new Order(2));
>
>
>         CustomerBean customerBean2 = new CustomerBean();
>         customerBean2.setName("Paolo");
>
>         customerBean2.addEmailAddre("E");
>         customerBean2.addEmailAddre("F");
>
>   customerBean2.addOrder(new Order(4));
>   customerBean2.addOrder(new Order(5));
>   customerBean2.addOrder(new Order(6));
>
>   customerList.addCustomer(customerBean);
>   customerList.addCustomer(customerBean2);
>
>         beanWriter.write("customerList", customerList);
>
>
>         System.out.println(outputWriter.toString());
>         FileOutputStream fos = new FileOutputStream( new File(  
> "CustomerBean3.xml" ));
>         PrintWriter out = new PrintWriter(new OutputStreamWriter(fos));
>         out.println(outputWriter.toString());
>         out.println();
>
>         out.flush();
>         out.close();
>
>     }
>
>
> }
>
> Result is following
>
> <?xml version='1.0' ?>
> <customerList>
>   <customers>
>     <customer>
>       <emailAddre>A</emailAddre>
>       <emailAddre>B</emailAddre>
>       <emailAddre>C</emailAddre>
>       <name>Diego</name>
>       <order id="1"/>
>       <order id="2"/>
>     </customer>
>     <customer>
>       <emailAddre>E</emailAddre>
>       <emailAddre>F</emailAddre>
>       <name>Paolo</name>/
>       <order id="4"/>
>       <order id="5"/>
>       <order id="6"/>
>     </customer>
>   </customers>
> </customerList>
>
> I try to read this file like that
>
>
> import java.io.FileReader;
> import java.util.Collection;
>
> import org.apache.commons.betwixt.io.BeanReader;
> import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
> import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
>
> public class ReadCustomerListApp {
>
>     public static final void main(String args[]) throws Exception{
>
>      FileReader xmlReader = new FileReader("CustomerBean3.xml");
>
>         BeanReader beanReader  = new BeanReader();
>
>          
> beanReader.getXMLIntrospector().setAttributesForPrimitives(false);
>         beanReader.setMatchIDs(false);
>         beanReader.getXMLIntrospector().setAttributeNameMapper(new  
> HyphenatedNameMapper());
>         beanReader.getXMLIntrospector().setElementNameMapper(new  
> DecapitalizeNameMapper());
>          beanReader.registerBeanClass("customerList",  
> CustomerList.class);
>
>          beanReader.registerBeanClass("customer", CustomerBean.class);
>
>
>          beanReader.registerBeanClass("order", Order.class);
>
>
>         CustomerList customerList = (CustomerList )  
> beanReader.parse(xmlReader);
>
>         System.out.println(customerList );
>     }
>
> }
>
> and I get the output
> CustomerList@329f3d[customers=[CustomerBean@186c6b2[name=Diego,orders=[ 
> ],emailAddres=[]],  
> CustomerBean@1749757[name=Paolo,orders=[],emailAddres=[]]]]
>
> that, is the methods addOrder and addEmailAddre are never called
>


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org