commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Diego Amicabile" <1822-...@onlinehome.de>
Subject [Betwixt] Example continued, I have a problem
Date Sat, 01 May 2004 23:34:35 GMT
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


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message