You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Matt Goodwin <mg...@metalexis.com> on 2004/11/18 23:34:19 UTC

betwixt and namespace

I have an object and I am trying to output as xml.  That part is working 
fine, but I need to put a namespace on the elements.  How is the 
namespace set?  I am using the following code.  In my startElement 
method in my ContentHandler, the namespaceURI is "".  What am I doing wrong?

HardErrorContentHandler beanHandler = 
HardErrorContentHandlerFactory.getInstance(obj);
beanHandler.setPrettyPrint(prettyPrint);
SAXBeanWriter beanWriter = new SAXBeanWriter(beanHandler);
  if(capitalize) {
      
beanWriter.getXMLIntrospector().getConfiguration().setElementNameMapper(new 
CapitalizeNameMapper());
  }
// set namespace
NamespacePrefixMapper namespaceMapper = new NamespacePrefixMapper();
namespaceMapper.setPrefix("xmlns","http://tempuri.org/segHedge.xsd");
beanWriter.getXMLIntrospector().getConfiguration().setPrefixMapper(namespaceMapper);
       
Thanks,

Matt

-- 
Matt Goodwin
mgoodwin@metalexis.com
(515)708-0114
Metalexis
"Transcending the Ordinary"


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


Re: Problems reading DynaBeans from xml using Betwixt

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

good to see someone's interested in picking up dyna bean support :)

i'm not going to be able to go through your email and add detailed  
comments tonight but i should be able to find time tomorrow.

- robert

On 27 Nov 2004, at 13:21, Kristian Mandrup wrote:

> Hi all,
>
> I have been trying to tweak betwixt to allow writing and reading of
> DynaBeans. Betwixt currently only supports xml writing through a  
> specialized
> case in:
>
> XMLIntrospector.XMLBeanInfo introspect(Object bean)
>
> if ( bean instanceof DynaBean )
> ...
>             // this is DynaBean use the DynaClass for introspection
>             return introspect( ((DynaBean) bean).getDynaClass() );
>
>         } else {
>             // normal bean so normal introspection
>
> Looking at DynaClass interface:
>
> public DynaClass getDynaClass();
>
> DynaClass implementations only implements this getter, not any setters  
> since
> done in constructor (would be too dirty I guess!)
>
> What is clearly needed is a setter to allow setting the dynaClass of  
> the
> bean when reading the bean from xml. Currently no dynaClass reference  
> is
> saved when writing the dynabean to xml, making it impossible to later
> reconstruct the bean.
>
> Obvious solution is to use the Adapter pattern. So I created my own
> DynaBeanAdapter, DynaClassAdapter and DynaPropertyAdapter, including  
> public
> setter methods to allow reading the "dyna-info" from xml using  
> betwixt...
>
> Also created XMLDynaBeanIntrospector, dismissing special DynaBean  
> behaviour.
>
> In the end I get the following result:
>
> String dynaBeanName = "dynabean";
> String xmlStr = write(dynaBeanName, dynaBean);
> DynaBeanAdapter dynaBeanM = (DynaBeanAdapter) read(xmlStr,  
> dynaBeanName,
> DynaBeanAdapter.class);
> System.out.println("DynaBean:" + dynaBeanM);
>
> String xmlStr2M = write(dynaBeanName, dynaClassM);
> System.out.println("DynaClassM:" + xmlStr2M);
>
>
> <?xml version='1.0' ?>  <dynaclass class="betwixt.DynaClassAdapter"
> className="betwixt.DynaClassAdapter"
> dynaBeanClass="org.apache.commons.beanutils.BasicDynaBean"  
> name="Person"
> id="1">
>     <dynaProperties>
>       <DynaPropertyAdapter class="betwixt.DynaPropertyAdapter"
> className="betwixt.DynaPropertyAdapter" name="name"  
> type="java.lang.String"
> id="2"/>
>       <DynaPropertyAdapter class="betwixt.DynaPropertyAdapter"
> className="betwixt.DynaPropertyAdapter" name="age"  
> type="java.lang.Integer"
> id="3"/>
>       <DynaPropertyAdapter class="betwixt.DynaPropertyAdapter"
> className="betwixt.DynaPropertyAdapter" name="married"
> type="java.lang.Boolean" id="4"/>
>     </dynaProperties>
>   </dynaclass>
>
> betwixt.DynaClassAdapter@134e4fb
> DynaClass:betwixt.DynaClassAdapter@134e4fb
> <?xml version='1.0' ?>  <dynabean class="betwixt.DynaClassAdapter"
> className="betwixt.DynaClassAdapter"
> dynaBeanClass="org.apache.commons.beanutils.BasicDynaBean"
> name="betwixt.DynaClassAdapter" id="1">
>     <dynaProperties/>
>   </dynabean>
>
> As the result indicate, my reconstructed DynaClass object is EMPTY!!!
>
>
> Problem seems to be, that the BasicDynaBean which I extend in my  
> adapter
> does not internally conform to the bean specifications:
>
> protected DynaProperty properties[];
> protected HashMap propertiesMap;
>
> public DynaProperty getDynaProperty(String name);
> public DynaProperty[] getDynaProperties();
>
> How should I resolve this?
>
> Tried add all these "adapter methods" in my DynaClassAdapter with no  
> luck...
> Not totally sure about how maps and arrays are handled in betwixt, the  
> user
> guide has very little info on these issues. How exactly are plurals  
> resolved
> in the above case. Should be handled by PluralStemmer, so I assume
> properties in singular form becomes property!?
>
> Class DynaClassAdapter {
> ...
>
>     public DynaProperty getPropertiesMap(String name) {
>
>         if (name == null) {
>             throw new IllegalArgumentException
>                     ("No property name specified");
>         }
>         return ((DynaProperty) propertiesMap.get(name));
>
>     }
>
>     public void addDynaPropertiesMap(String name, DynaProperty  
> property) {
>         propertiesMap.put(name, property);
>     }
>
>     public void addDynaPropertyMap(String name, DynaProperty property)  
> {
>         propertiesMap.put(name, property);
>     }
>
>     public void setDynaProperties() {
>         setProperties(properties);
>     }
>
>     public void setProperties(DynaProperty properties[]) {
>         super.setProperties(properties);
>     }
>
> ===
> /*
>  * Created on 2004-11-27 by @author Kristian
>  *
>  */
> package betwixt;
>
> import org.apache.commons.beanutils.BasicDynaClass;
> import org.apache.commons.beanutils.DynaProperty;
>
> /**
>  * Created on 2004-11-27 by @author Kristian
>  *
>  * TODO Describe type
>  *
>  */
> public class DynaClassAdapter extends BasicDynaClass {
>
>     /**
>      * Comment for <code>serialVersionUID</code>
>      */
>     private static final long serialVersionUID = -6300500058450550700L;
>
>     private String className = "betwixt.DynaClassAdapter";
>
>     /**
>      * @return Returns the className.
>      */
>     public String getClassName() {
>         return className;
>     }
>     /**
>      * @param className The className to set.
>      */
>     public void setClassName(String className) {
>         this.className = className;
>     }
>     public DynaClassAdapter() {
>         super();
>     }
>
>     public DynaClassAdapter(String name, Class dynaBeanClass,
>             DynaProperty properties[]) {
>         super(name, dynaBeanClass, properties);
>     }
>
>     public void setDynaBeanClass(Class dynaBeanClass) {
>         super.setDynaBeanClass(dynaBeanClass);
>     }
>
>     public DynaProperty getPropertiesMap(String name) {
>
>         if (name == null) {
>             throw new IllegalArgumentException
>                     ("No property name specified");
>         }
>         return ((DynaProperty) propertiesMap.get(name));
>
>     }
>
>     public void addDynaPropertiesMap(String name, DynaProperty  
> property) {
>         propertiesMap.put(name, property);
>     }
>
>     public void addDynaPropertyMap(String name, DynaProperty property)  
> {
>         propertiesMap.put(name, property);
>     }
>
>
>     public void setDynaProperties() {
>         setProperties(properties);
>     }
>
>     public void setProperties(DynaProperty properties[]) {
>         super.setProperties(properties);
>     }
>
> }
> ===
> /*
>  * Created on 2004-11-27 by @author Kristian
>  *
>  */
> package betwixt;
>
> import org.apache.commons.beanutils.DynaProperty;
>
> /**
>  * Created on 2004-11-27 by @author Kristian
>  *
>  * TODO Describe type
>  *
>  */
> public class DynaPropertyAdapter extends DynaProperty {
>     /**
>      * Comment for <code>serialVersionUID</code>
>      */
>     private static final long serialVersionUID = 9105672480819027837L;
>
>     private String className = "betwixt.DynaPropertyAdapter";
>
>     public DynaPropertyAdapter() {
>         super("noname");
>     }
>
>     public DynaPropertyAdapter(String name, Class type) {
>         super(name, type);
>     }
>
>     public void setName(String name) {
>         this.name = name;
>     }
>     public void setType(Class type) {
>         this.type = type;
>     }
>
>     /**
>      * @return Returns the className.
>      */
>     public String getClassName() {
>         return className;
>     }
>     /**
>      * @param className The className to set.
>      */
>     public void setClassName(String className) {
>         this.className = className;
>     }
> }
> ===
> /*
>  * Created on 2004-11-27 by @author Kristian
>  *
>  */
> package betwixt;
>
> import java.util.HashMap;
>
> import org.apache.commons.beanutils.BasicDynaBean;
> import org.apache.commons.beanutils.DynaClass;
>
> /**
>  * Created on 2004-11-27 by @author Kristian
>  *
>  * TODO Describe type
>  *
>  */
> public class DynaBeanAdapter extends BasicDynaBean {
>
>     /**
>      * Comment for <code>serialVersionUID</code>
>      */
>     private static final long serialVersionUID = 1436974242229864237L;
>
>     private DynaClass dynaClassRef;
>
>     public DynaBeanAdapter() {
>         super(null);
>     }
>
>     public DynaBeanAdapter(DynaClass dynaClass) {
>         super(dynaClass);
>         setDynaClassRef(dynaClass);
>     }
>
>     /**
>      * @return Returns the dynaClassRef.
>      */
>     public DynaClass getDynaClassRef() {
>         return dynaClassRef;
>     }
>     /**
>      * @param dynaClassRef The dynaClassRef to set.
>      */
>     public void setDynaClassRef(DynaClass dynaClassRef) {
>         this.dynaClassRef = dynaClassRef;
>         this.dynaClass = dynaClassRef;
>     }
>     /**
>      * @return Returns the values.
>      */
>     public HashMap getValues() {
>         return values;
>     }
>     /**
>      * @param values The values to set.
>      */
>     public void setValues(HashMap values) {
>         this.values = values;
>     }
> }
> ===
> /*
>  * Created on 2004-11-27 by @author Kristian
>  *
>  */
> package betwixt;
>
> import java.io.StringReader;
> import java.io.StringWriter;
>
> import org.apache.commons.beanutils.DynaProperty;
> import org.apache.commons.betwixt.io.BeanReader;
> import org.apache.commons.betwixt.io.BeanWriter;
> import org.apache.commons.betwixt.strategy.PropertySuppressionStrategy;
>
> /**
>  * Created on 2004-11-27 by
>  *
>  * @author Kristian
>  *
>  * TODO Describe type
>  *  @see commons-betwixt test DynaWithDotBetwixt, TestDynaBeanSupport  
> (write
> bean)
>  *  Does not yet support reading DynaBeans...
>  *
>  *  But must be possible to write DynaClass along with DynaBean and  
> assemble
> this way?
>  *
>  *
>  */
> public class DynaBeanApp {
>
>     public static void main(String[] args) throws Exception {
>         DynaPropertyAdapter name = new DynaPropertyAdapter("name",
> String.class);
>         DynaPropertyAdapter age = new DynaPropertyAdapter("age",
> Integer.class);
>         DynaPropertyAdapter married = new  
> DynaPropertyAdapter("married",
> Boolean.class);
>         DynaProperty[] properties = new DynaProperty[] {name, age,  
> married};
>         DynaClassAdapter dynaClass = new DynaClassAdapter("Person",  
> null,
> properties);
>         DynaBeanAdapter dynaBean = new DynaBeanAdapter(dynaClass);
>         dynaBean.set("name", "Kristian Mandrup");
>         dynaBean.set("age", new Integer(30));
>         dynaBean.set("married", new Boolean(false));
>         String dynaBeanName = "dynabean";
>         String dynaClassName = "dynaclass";
> //        String xmlStr = write(dynaBeanName, dynaBean);
>         String xmlStr2 = write(dynaClassName, dynaClass);
>         DynaClassAdapter dynaClassM = (DynaClassAdapter) read(xmlStr2,
> dynaClassName, DynaClassAdapter.class);
>         System.out.println("DynaClass:" + dynaClassM);
>         String xmlStr2M = write(dynaBeanName, dynaClassM);
>         System.out.println("DynaClassM:" + xmlStr2M);
>
>         /*
>         DynaBeanAdapter dynaBeanM = (DynaBeanAdapter) read(xmlStr,
> dynaBeanName, DynaBeanAdapter.class);
>         System.out.println("DynaBean:" + dynaBeanM);
>
>         String xmlStrM = write(dynaBeanName, dynaBeanM);
>         System.out.println("DynaBeanM:" + xmlStrM);
> */
>     }
>
>     public static Object read(String xmlStr, String name,Class aclass)
> throws Exception {
>         // First construct the xml which will be read in
>         // For this example, read in from a hard coded string
>         StringReader xmlReader = new StringReader(xmlStr);
>
>         // Now convert this to a bean using betwixt
>         // Create BeanReader
>         BeanReader beanReader  = new BeanReader();
>
>         // Configure the reader
>         // If you're round-tripping, make sure that the configurations  
> are
> compatible!
>
> beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrim 
> itive
> s(true);
>         beanReader.getBindingConfiguration().setMapIDs(true);
>
>         // Register beans so that betwixt knows what the xml is to be
> converted to
>         // Since the element mapped to a PersonBean isn't called the  
> same,
>         // need to register the path as well
>         beanReader.registerBeanClass(name, aclass);
>
>         // Now we parse the xml
>         Object obj = beanReader.parse(xmlReader);
>
>         // send bean to system out
>         System.out.println(obj);
>         return obj;
>     }
>
>     public static String write(String name, Object obj) throws  
> Exception {
>         // Start by preparing the writer
>         // We'll write to a string
>         StringWriter outputWriter = new StringWriter();
>
>         // Betwixt just writes out the bean as a fragment
>         // So if we want well-formed xml, we need to add the prolog
>         outputWriter.write("<?xml version='1.0' ?>");
>
>         // Create a BeanWriter which writes to our prepared stream
>         BeanWriter beanWriter = new BeanWriter(outputWriter);
>         beanWriter.setXMLIntrospector(new XMLDynaBeanIntrospector());
>         // Configure betwixt
>         // For more details see java docs or later in the main  
> documentation
>
> beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrim 
> itive
> s(true);
>         beanWriter.getBindingConfiguration().setMapIDs(true);
>         beanWriter.enablePrettyPrint();
>         // do NOT suppress any properties
>
> beanWriter.getXMLIntrospector().getConfiguration().setPropertySuppressi 
> onStr
> ategy(
>                 new PropertySuppressionStrategy() {
>                      public boolean suppressProperty(Class clazz, Class
> type, String name) {
>                          if ("indexed".equals(name) ||
> "mapped".equals(name))
>                              return true;
>                          else return false;
>                      }
>                 });
>
>
>
>         beanWriter.write(name, obj);
>         // Write to System.out
>         // (We could have used the empty constructor for BeanWriter
>         // but this way is more instructive)
>         String xmlStr = outputWriter.toString();
>         System.out.println(xmlStr);
>         return xmlStr;
>     }
> }
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.779 / Virus Database: 526 - Release Date: 19-10-2004
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>


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


Problems reading DynaBeans from xml using Betwixt

Posted by Kristian Mandrup <kr...@mandrup.dk>.
Hi all,

I have been trying to tweak betwixt to allow writing and reading of
DynaBeans. Betwixt currently only supports xml writing through a specialized
case in: 

XMLIntrospector.XMLBeanInfo introspect(Object bean)

if ( bean instanceof DynaBean ) 
...
            // this is DynaBean use the DynaClass for introspection
            return introspect( ((DynaBean) bean).getDynaClass() );
            
        } else {
            // normal bean so normal introspection

Looking at DynaClass interface:

public DynaClass getDynaClass();

DynaClass implementations only implements this getter, not any setters since
done in constructor (would be too dirty I guess!)

What is clearly needed is a setter to allow setting the dynaClass of the
bean when reading the bean from xml. Currently no dynaClass reference is
saved when writing the dynabean to xml, making it impossible to later
reconstruct the bean.

Obvious solution is to use the Adapter pattern. So I created my own
DynaBeanAdapter, DynaClassAdapter and DynaPropertyAdapter, including public
setter methods to allow reading the "dyna-info" from xml using betwixt...

Also created XMLDynaBeanIntrospector, dismissing special DynaBean behaviour.

In the end I get the following result:

String dynaBeanName = "dynabean";
String xmlStr = write(dynaBeanName, dynaBean);
DynaBeanAdapter dynaBeanM = (DynaBeanAdapter) read(xmlStr, dynaBeanName,
DynaBeanAdapter.class);
System.out.println("DynaBean:" + dynaBeanM);

String xmlStr2M = write(dynaBeanName, dynaClassM);
System.out.println("DynaClassM:" + xmlStr2M);


<?xml version='1.0' ?>  <dynaclass class="betwixt.DynaClassAdapter"
className="betwixt.DynaClassAdapter"
dynaBeanClass="org.apache.commons.beanutils.BasicDynaBean" name="Person"
id="1">
    <dynaProperties>
      <DynaPropertyAdapter class="betwixt.DynaPropertyAdapter"
className="betwixt.DynaPropertyAdapter" name="name" type="java.lang.String"
id="2"/>
      <DynaPropertyAdapter class="betwixt.DynaPropertyAdapter"
className="betwixt.DynaPropertyAdapter" name="age" type="java.lang.Integer"
id="3"/>
      <DynaPropertyAdapter class="betwixt.DynaPropertyAdapter"
className="betwixt.DynaPropertyAdapter" name="married"
type="java.lang.Boolean" id="4"/>
    </dynaProperties>
  </dynaclass>

betwixt.DynaClassAdapter@134e4fb
DynaClass:betwixt.DynaClassAdapter@134e4fb
<?xml version='1.0' ?>  <dynabean class="betwixt.DynaClassAdapter"
className="betwixt.DynaClassAdapter"
dynaBeanClass="org.apache.commons.beanutils.BasicDynaBean"
name="betwixt.DynaClassAdapter" id="1">
    <dynaProperties/>
  </dynabean>

As the result indicate, my reconstructed DynaClass object is EMPTY!!!


Problem seems to be, that the BasicDynaBean which I extend in my adapter
does not internally conform to the bean specifications:

protected DynaProperty properties[];
protected HashMap propertiesMap;

public DynaProperty getDynaProperty(String name);
public DynaProperty[] getDynaProperties();

How should I resolve this?

Tried add all these "adapter methods" in my DynaClassAdapter with no luck...
Not totally sure about how maps and arrays are handled in betwixt, the user
guide has very little info on these issues. How exactly are plurals resolved
in the above case. Should be handled by PluralStemmer, so I assume
properties in singular form becomes property!?

Class DynaClassAdapter {
...

    public DynaProperty getPropertiesMap(String name) {

        if (name == null) {
            throw new IllegalArgumentException
                    ("No property name specified");
        }
        return ((DynaProperty) propertiesMap.get(name));

    }    
    
    public void addDynaPropertiesMap(String name, DynaProperty property) {
        propertiesMap.put(name, property);
    }

    public void addDynaPropertyMap(String name, DynaProperty property) {
        propertiesMap.put(name, property);
    }
  
    public void setDynaProperties() {
        setProperties(properties);
    }
        
    public void setProperties(DynaProperty properties[]) {
        super.setProperties(properties);
    }

===
/*
 * Created on 2004-11-27 by @author Kristian
 * 
 */
package betwixt;

import org.apache.commons.beanutils.BasicDynaClass;
import org.apache.commons.beanutils.DynaProperty;

/**
 * Created on 2004-11-27 by @author Kristian
 * 
 * TODO Describe type
 * 
 */
public class DynaClassAdapter extends BasicDynaClass {

    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = -6300500058450550700L;

    private String className = "betwixt.DynaClassAdapter";
    
    /**
     * @return Returns the className.
     */
    public String getClassName() {
        return className;
    }
    /**
     * @param className The className to set.
     */
    public void setClassName(String className) {
        this.className = className;
    }
    public DynaClassAdapter() {
        super();
    }

    public DynaClassAdapter(String name, Class dynaBeanClass,
            DynaProperty properties[]) {
        super(name, dynaBeanClass, properties);
    }
    
    public void setDynaBeanClass(Class dynaBeanClass) {
        super.setDynaBeanClass(dynaBeanClass);
    }

    public DynaProperty getPropertiesMap(String name) {

        if (name == null) {
            throw new IllegalArgumentException
                    ("No property name specified");
        }
        return ((DynaProperty) propertiesMap.get(name));

    }    
    
    public void addDynaPropertiesMap(String name, DynaProperty property) {
        propertiesMap.put(name, property);
    }
    
    public void addDynaPropertyMap(String name, DynaProperty property) {
        propertiesMap.put(name, property);
    }
    
  
    public void setDynaProperties() {
        setProperties(properties);
    }
        
    public void setProperties(DynaProperty properties[]) {
        super.setProperties(properties);
    }
    
}
===
/*
 * Created on 2004-11-27 by @author Kristian
 * 
 */
package betwixt;

import org.apache.commons.beanutils.DynaProperty;

/**
 * Created on 2004-11-27 by @author Kristian
 * 
 * TODO Describe type
 * 
 */
public class DynaPropertyAdapter extends DynaProperty {
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 9105672480819027837L;

    private String className = "betwixt.DynaPropertyAdapter";
    
    public DynaPropertyAdapter() {
        super("noname");
    }

    public DynaPropertyAdapter(String name, Class type) {
        super(name, type);
    }    
    
    public void setName(String name) {
        this.name = name;  
    }
    public void setType(Class type) {
        this.type = type;
    }
  
    /**
     * @return Returns the className.
     */
    public String getClassName() {
        return className;
    }
    /**
     * @param className The className to set.
     */
    public void setClassName(String className) {
        this.className = className;
    }
}
===
/*
 * Created on 2004-11-27 by @author Kristian
 * 
 */
package betwixt;

import java.util.HashMap;

import org.apache.commons.beanutils.BasicDynaBean;
import org.apache.commons.beanutils.DynaClass;

/**
 * Created on 2004-11-27 by @author Kristian
 * 
 * TODO Describe type
 * 
 */
public class DynaBeanAdapter extends BasicDynaBean {

    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 1436974242229864237L;
    
    private DynaClass dynaClassRef;
    
    public DynaBeanAdapter() {
        super(null);
    }

    public DynaBeanAdapter(DynaClass dynaClass) {
        super(dynaClass);
        setDynaClassRef(dynaClass);
    }    
    
    /**
     * @return Returns the dynaClassRef.
     */
    public DynaClass getDynaClassRef() {
        return dynaClassRef;
    }
    /**
     * @param dynaClassRef The dynaClassRef to set.
     */
    public void setDynaClassRef(DynaClass dynaClassRef) {
        this.dynaClassRef = dynaClassRef;
        this.dynaClass = dynaClassRef;
    }
    /**
     * @return Returns the values.
     */
    public HashMap getValues() {
        return values;
    }
    /**
     * @param values The values to set.
     */
    public void setValues(HashMap values) {
        this.values = values;
    }    
}
===
/*
 * Created on 2004-11-27 by @author Kristian
 * 
 */
package betwixt;

import java.io.StringReader;
import java.io.StringWriter;

import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.betwixt.io.BeanReader;
import org.apache.commons.betwixt.io.BeanWriter;
import org.apache.commons.betwixt.strategy.PropertySuppressionStrategy;

/**
 * Created on 2004-11-27 by
 * 
 * @author Kristian
 * 
 * TODO Describe type
 *  @see commons-betwixt test DynaWithDotBetwixt, TestDynaBeanSupport (write
bean)
 *  Does not yet support reading DynaBeans...
 *  
 *  But must be possible to write DynaClass along with DynaBean and assemble
this way?
 *  
 *  
 */
public class DynaBeanApp {

    public static void main(String[] args) throws Exception {
        DynaPropertyAdapter name = new DynaPropertyAdapter("name",
String.class);
        DynaPropertyAdapter age = new DynaPropertyAdapter("age",
Integer.class);
        DynaPropertyAdapter married = new DynaPropertyAdapter("married",
Boolean.class);
        DynaProperty[] properties = new DynaProperty[] {name, age, married};
        DynaClassAdapter dynaClass = new DynaClassAdapter("Person", null,
properties);
        DynaBeanAdapter dynaBean = new DynaBeanAdapter(dynaClass);
        dynaBean.set("name", "Kristian Mandrup");
        dynaBean.set("age", new Integer(30));
        dynaBean.set("married", new Boolean(false));
        String dynaBeanName = "dynabean";
        String dynaClassName = "dynaclass";
//        String xmlStr = write(dynaBeanName, dynaBean);
        String xmlStr2 = write(dynaClassName, dynaClass);
        DynaClassAdapter dynaClassM = (DynaClassAdapter) read(xmlStr2,
dynaClassName, DynaClassAdapter.class);
        System.out.println("DynaClass:" + dynaClassM);
        String xmlStr2M = write(dynaBeanName, dynaClassM);
        System.out.println("DynaClassM:" + xmlStr2M);
        
        /*
        DynaBeanAdapter dynaBeanM = (DynaBeanAdapter) read(xmlStr,
dynaBeanName, DynaBeanAdapter.class);
        System.out.println("DynaBean:" + dynaBeanM);

        String xmlStrM = write(dynaBeanName, dynaBeanM);
        System.out.println("DynaBeanM:" + xmlStrM);
*/        
    }

    public static Object read(String xmlStr, String name,Class aclass)
throws Exception {
        // First construct the xml which will be read in
        // For this example, read in from a hard coded string
        StringReader xmlReader = new StringReader(xmlStr);
        
        // Now convert this to a bean using betwixt
        // Create BeanReader
        BeanReader beanReader  = new BeanReader();
        
        // Configure the reader
        // If you're round-tripping, make sure that the configurations are
compatible!
 
beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitive
s(true);
        beanReader.getBindingConfiguration().setMapIDs(true);
        
        // Register beans so that betwixt knows what the xml is to be
converted to
        // Since the element mapped to a PersonBean isn't called the same, 
        // need to register the path as well
        beanReader.registerBeanClass(name, aclass);
        
        // Now we parse the xml
        Object obj = beanReader.parse(xmlReader);
        
        // send bean to system out
        System.out.println(obj);
        return obj;
    }
    
    public static String write(String name, Object obj) throws Exception {
        // Start by preparing the writer
        // We'll write to a string 
        StringWriter outputWriter = new StringWriter(); 
        
        // Betwixt just writes out the bean as a fragment
        // So if we want well-formed xml, we need to add the prolog
        outputWriter.write("<?xml version='1.0' ?>");
        
        // Create a BeanWriter which writes to our prepared stream
        BeanWriter beanWriter = new BeanWriter(outputWriter);
        beanWriter.setXMLIntrospector(new XMLDynaBeanIntrospector());
        // Configure betwixt
        // For more details see java docs or later in the main documentation
 
beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitive
s(true);
        beanWriter.getBindingConfiguration().setMapIDs(true);
        beanWriter.enablePrettyPrint();
        // do NOT suppress any properties
 
beanWriter.getXMLIntrospector().getConfiguration().setPropertySuppressionStr
ategy(
                new PropertySuppressionStrategy() {
                     public boolean suppressProperty(Class clazz, Class
type, String name) {
                         if ("indexed".equals(name) ||
"mapped".equals(name))
                             return true;
                         else return false;
                     }
                });

        
        
        beanWriter.write(name, obj);
        // Write to System.out
        // (We could have used the empty constructor for BeanWriter 
        // but this way is more instructive)
        String xmlStr = outputWriter.toString();
        System.out.println(xmlStr);
        return xmlStr;
    }
}

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.779 / Virus Database: 526 - Release Date: 19-10-2004
 



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


Re: [betwixt] DynaBean read problem

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

i took a look at the dynabeans introspection code and found that it was 
incomplete (not updaters). i've committed this functionality to CVS. 
please update and retry.

it is (as yet) incomplete, lacking support for keyed dynabean entries. 
i'm not sure when i'll get round to add this so if anyone fancies 
taking this on, just shout...

BTW your code looks quite interesting. if you'd like to contribute it 
to apache (together with unit tests and documentation, if possible), 
i'd be glad to review it.

- robert

On 27 Nov 2004, at 14:46, Kristian Mandrup wrote:

> Back again,
>
> Did some "deep" debugging in Eclipse...
> Traced problem to BeanBindAction, where it seems the ReadContext 
> context is
> populated correctly, its objectStack being filled up with the 
> appropriate
> objects read from XML...
>
> But for some reason the DynaBean instance is not populated with the 
> context
> elements, resulting in an empty DynaBean in the end.
>
> Where should I look to find a possible answer to this problem?
>
> Kristian
>
>
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.779 / Virus Database: 526 - Release Date: 19-10-2004
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>


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


[betwixt] DynaBean read problem

Posted by Kristian Mandrup <kr...@mandrup.dk>.
Back again,

Did some "deep" debugging in Eclipse...
Traced problem to BeanBindAction, where it seems the ReadContext context is
populated correctly, its objectStack being filled up with the appropriate
objects read from XML...

But for some reason the DynaBean instance is not populated with the context
elements, resulting in an empty DynaBean in the end.

Where should I look to find a possible answer to this problem?

Kristian


 

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.779 / Virus Database: 526 - Release Date: 19-10-2004
 



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


[betwixt] DynaBean read from xml problem

Posted by Kristian Mandrup <kr...@mandrup.dk>.
I can now successfully read a DynaClass complete with DynaProperties using
my previously posted adapters.
Had a bug in DynaClassAdapter - fix here: 

    public void setDynaProperties(DynaProperty[] properties) {
        setProperties(properties);
    }
        
    public void setProperties(DynaProperty[] properties) {
        super.setProperties(properties);
    } 

I now only have a problem reading a DynaBean:

<?xml version='1.0' ?>  <dynabean class="betwixt.DynaBeanAdapter"
className="betwixt.DynaBeanAdapter" id="1">
    <dynaClass class="betwixt.DynaClassAdapter"
className="betwixt.DynaClassAdapter"
dynaBeanClass="org.apache.commons.beanutils.BasicDynaBean" name="Person"
id="2">
      <dynaProperties>
        <DynaPropertyAdapter class="betwixt.DynaPropertyAdapter"
className="betwixt.DynaPropertyAdapter" name="name" type="java.lang.String"
id="3"/>
        <DynaPropertyAdapter class="betwixt.DynaPropertyAdapter"
className="betwixt.DynaPropertyAdapter" name="age" type="java.lang.Integer"
id="4"/>
        <DynaPropertyAdapter class="betwixt.DynaPropertyAdapter"
className="betwixt.DynaPropertyAdapter" name="married"
type="java.lang.Boolean" id="5"/>
      </dynaProperties>
    </dynaClass>
    <dynaClassRef idref="2"/>
    <values>
      <entry class="java.util.HashMap$Entry" id="6">
        <key>married</key>
        <value>false</value>
      </entry>
      <entry class="java.util.HashMap$Entry" id="7">
        <key>age</key>
        <value>30</value>
      </entry>
      <entry class="java.util.HashMap$Entry" id="8">
        <key>name</key>
        <value>Kristian Mandrup</value>
      </entry>
    </values>
  </dynabean>

betwixt.DynaBeanAdapter@1be0f0a
DynaBean:betwixt.DynaBeanAdapter@1be0f0a

<?xml version='1.0' ?>  <dynabean class="betwixt.DynaBeanAdapter"
className="betwixt.DynaBeanAdapter" id="1">
    <values/>
  </dynabean>

As the following DynaBeanAdapter class demonstrates, I have plenty of
setters but none of them (except the primitive types such as the string
className) seem to be called on correctly on construction, again resulting
in an empty DynaBean!
Que-pasa!!! :)
Almost there.... help or tips appreciated ;-)

===
import java.util.HashMap;

import org.apache.commons.beanutils.BasicDynaBean;
import org.apache.commons.beanutils.DynaClass;

/**
 * Created on 2004-11-27 by @author Kristian
 * 
 * TODO Describe type
 * 
 */
public class DynaBeanAdapter extends BasicDynaBean {

    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 1436974242229864237L;
    
    private DynaClass dynaClassRef;
    private String className = "betwixt.DynaBeanAdapter";
    
    public DynaBeanAdapter() {
        this(null);
    }

    public DynaBeanAdapter(DynaClass dynaClass) {
        super(dynaClass);
        setDynaClassRef(dynaClass);
    }    
    
    /**
     * @return Returns the dynaClassRef.
     */
    public DynaClass getDynaClassRef() {
        return dynaClassRef;
    }
    /**
     * @param dynaClassRef The dynaClassRef to set.
     */
    public void setDynaClassRef(DynaClass dynaClassRef) {
        this.dynaClassRef = dynaClassRef;
        this.dynaClass = dynaClassRef;
    }
    
    /**
     * @return Returns the values.
     */
    public Object getValue(String key) {
        return getValues().get(key);
    }    
    
    /**
     * @return Returns the values.
     */
    public HashMap getValues() {
        return values;
    }
    /**
     * @param values The values to set.
     */
    public void setValues(HashMap values) {
        this.values = values;
    }    
    
    public void addValue(String key, Object value) {
        set(key, value);
    }      

    public void addValues(String key, Object value) {
        set(key, value);
    }     
    
    /**
     * @return Returns the className.
     */
    public String getClassName() {
        return className;
    }
    /**
     * @param className The className to set.
     */
    public void setClassName(String className) {
        this.className = className;
    }
}

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.779 / Virus Database: 526 - Release Date: 19-10-2004
 



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


Re: betwixt and namespace

Posted by Matt Goodwin <mg...@metalexis.com>.
Thanks for the quick response. I looked at your PersonWithNamespaces 
example (wish I had done that before I posted).  I wasn't using betwixt 
files and was just using bean introspection, but I am going to use a 
betwixt file now.  Thanks!!!!  Good night.

Matt


robert burrell donkin wrote:

> hi matt
>
> i've very sorry to say that i can't remember right now and i can't 
> find  an example in the document!
>
> i'm probably not going to find time (before i head off to bed) to 
> find  out how (in detail) to make namespaces work and write up an 
> example for  the documentation (or fix up what's left to do if support 
> isn't  finished yet). however, i do recommend taking a look at the 
> source for  org.apache.commons.betwixt.schema - this package contains 
> an  (experimental) w3c schema generator which produces namespace 
> aware  output. setting uri attributes for element tags in the betwixt 
> files  should do it. there should be a way to coursely set namespaces 
> (but i'm  not sure if this is in as yet).
>
> if you're using a DTD then you'll need to set an appropriate  
> NamespacePrefixMapper so that your uri can be converted to a suitable  
> prefix.
>
> hope this helps
>
> - robert
>
> On 18 Nov 2004, at 22:34, Matt Goodwin wrote:
>
>> I have an object and I am trying to output as xml.  That part is  
>> working fine, but I need to put a namespace on the elements.  How is  
>> the namespace set?  I am using the following code.  In my 
>> startElement  method in my ContentHandler, the namespaceURI is "".  
>> What am I doing  wrong?
>>
>> HardErrorContentHandler beanHandler =  
>> HardErrorContentHandlerFactory.getInstance(obj);
>> beanHandler.setPrettyPrint(prettyPrint);
>> SAXBeanWriter beanWriter = new SAXBeanWriter(beanHandler);
>>  if(capitalize) {
>>       
>> beanWriter.getXMLIntrospector().getConfiguration().setElementNameMapper 
>> (new CapitalizeNameMapper());
>>  }
>> // set namespace
>> NamespacePrefixMapper namespaceMapper = new NamespacePrefixMapper();
>> namespaceMapper.setPrefix("xmlns","http://tempuri.org/segHedge.xsd");
>> beanWriter.getXMLIntrospector().getConfiguration().setPrefixMapper(name 
>> spaceMapper);
>>       Thanks,
>>
>> Matt
>>
>> -- 
>> Matt Goodwin
>> mgoodwin@metalexis.com
>> (515)708-0114
>> Metalexis
>> "Transcending the Ordinary"
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>

-- 
Matt Goodwin
mgoodwin@metalexis.com
(515)708-0114
Metalexis
"Transcending the Ordinary"


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


Re: betwixt and namespace

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

i've very sorry to say that i can't remember right now and i can't find  
an example in the document!

i'm probably not going to find time (before i head off to bed) to find  
out how (in detail) to make namespaces work and write up an example for  
the documentation (or fix up what's left to do if support isn't  
finished yet). however, i do recommend taking a look at the source for  
org.apache.commons.betwixt.schema - this package contains an  
(experimental) w3c schema generator which produces namespace aware  
output. setting uri attributes for element tags in the betwixt files  
should do it. there should be a way to coursely set namespaces (but i'm  
not sure if this is in as yet).

if you're using a DTD then you'll need to set an appropriate  
NamespacePrefixMapper so that your uri can be converted to a suitable  
prefix.

hope this helps

- robert

On 18 Nov 2004, at 22:34, Matt Goodwin wrote:

> I have an object and I am trying to output as xml.  That part is  
> working fine, but I need to put a namespace on the elements.  How is  
> the namespace set?  I am using the following code.  In my startElement  
> method in my ContentHandler, the namespaceURI is "".  What am I doing  
> wrong?
>
> HardErrorContentHandler beanHandler =  
> HardErrorContentHandlerFactory.getInstance(obj);
> beanHandler.setPrettyPrint(prettyPrint);
> SAXBeanWriter beanWriter = new SAXBeanWriter(beanHandler);
>  if(capitalize) {
>       
> beanWriter.getXMLIntrospector().getConfiguration().setElementNameMapper 
> (new CapitalizeNameMapper());
>  }
> // set namespace
> NamespacePrefixMapper namespaceMapper = new NamespacePrefixMapper();
> namespaceMapper.setPrefix("xmlns","http://tempuri.org/segHedge.xsd");
> beanWriter.getXMLIntrospector().getConfiguration().setPrefixMapper(name 
> spaceMapper);
>       Thanks,
>
> Matt
>
> -- 
> Matt Goodwin
> mgoodwin@metalexis.com
> (515)708-0114
> Metalexis
> "Transcending the Ordinary"
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>


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