commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Igor Marakov" <igor...@ivmsystems.com>
Subject [betwixt] unable to read XML into collection. Please help
Date Tue, 07 Feb 2006 20:04:08 GMT
Hi,

I have a simple (or so it seems) problem. 
There is a piece of XML that represents a list of items. 
I need to convert this XML to the List of beans.
I must be doing something very wrong but for couple days now I am not able
to come up with combination of code and configuration that would work. I can
see beans being created but assignment into List never happens. At the same
time running single beans does work.
Obviously I'm missing something big time here; 
Any help will be greatly appreciated.
Relevant code pieces and configuration is below.

Thanks,
Igor

****  SETUP  ( done once in translator life cycle )

// insert my bean factory, verified - works, beans created ok.
beanCreationChain = BeanCreationList.createStandardChain();
beanCreationChain.insertBeanCreator(1, new BeanCreator() ); 

// load betwixt-config, save XMLIntrospector for future use
BeanWriter writer = new BeanWriter();
introspector = writer.getXMLIntrospector();
InputSource is = new
InputSource(TranslatorImpl.class.getResourceAsStream(translatorConfig));

introspector.register(is);
introspector.getConfiguration().setWrapCollectionsInElement(true);
writer.getBindingConfiguration().setMapIDs(false);

// save Configuration for future use
configuration = writer.getBindingConfiguration();   

 

**** BETWIXT CONFIG  ( loaded by above code )

<?xml version="1.0" encoding="UTF-8"?>
<betwixt-config>
   <class name='com.my.CollectionWrapper' >     
    <element name='elements'>
      <element property='element' />
    </element>  
   </class>   
 
   <class name='com.my.Application'>
     <element name='application'>
       <attribute name='id' property='id'/>
       <attribute name='name' property='name' />
       <element name='description' property='description' />       
       <addDefaults add-properties='false'/>
     </element>
   </class>
</betwixt-config>
 

**** XML IN  ( to be loaded into collection )

<?xml version='1.0' ?>
  <elements>
    <application id="2" name="bean2">
      <description>Application Bean 2</description>
    </application>
    <application id="3" name="bean3">
      <description>Application Bean 3</description>
    </application>
    <application id="1" name="bean1">
      <description> Application Bean 1</description>
    </application>
  </elements>
 

**** READING  ( actual translation step )

 Object result = null;
 StringReader xmlReader = new StringReader(buffer);  // XML IN

 BeanReader reader  = new BeanReader();

// using configuration saved earlier 
 reader.setXMLIntrospector(introspector);              
 reader.setBindingConfiguration(configuration);
 reader.getReadConfiguration().setBeanCreationChain(beanCreationChain);

 reader.registerBeanClass("elements",
Class.forName("com.my.CollectionWrapper")); 

 reader.registerBeanClass("application",
Class.forName("com.my.Application"));    

 result = reader.parse(xmlReader);
	 
//  RESULT Contains empty collection. 
//  Adding to collection in CollectionWrapper never happens
 

**** COLLECTION WRAPPER

public class CollectionWrapper
{
  private static Logger log = Logger.getLogger(CollectionWrapper.class);
  private Collection elements = null;
  public CollectionWrapper()
  {
    log.debug("*********  CONSTRUCTOR  ******************");
    elements = new java.util.ArrayList();      
  }

  public void addElement(Object element) 
  { 
    log.debug("**********  ADD  *************** " + element.toString());
    elements.add(element); 
  }
  
  public Collection getElementCollection() { return elements; }
 
  public Iterator getElements() { return elements.iterator(); }
 
  public void setElements(Collection elements) { this.elements = elements; }

 

// this did not work either 

//  public Collection getApplications(Application element) {
addElement(element); }

//  public void addApplication(Application element) { addElement(element); }


//  public void setApplication(Application element) { addElement(element); }


 

}





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


Mime
View raw message