Just found out that the mailing list doesn't like attachements (sorry!), so I'm
resending this.
As a test case for Castor, I'm trying to marshal and unmarshal again a simple bean.
Works fine so far, however, an int value in the bean is not unmarshalled (the setter
method isn't being called). Adding a set<Name> (Integer value) didn't help.
Just tested with CVS version, same behavior.
I attached the source files for further info.
The XMLDriver class is just a generic wrapper for calling the marshaller/unmarshaller,
the bean can be found in the test/data package.
Would appreciate any help...
Heiko Erhardt
skynamics AG
www.skynamics.com
/*
* File: TestXMLDriver.java
* Product: Prisma
*
* Date Author Changes
* Sep 01 00 Heiko Erhardt Created
*
* (c) 2000 skynamics AG All rights reserved.
*/
package com.skynamics.prisma.core.util.xml.test;
import java.io.IOException;
import com.skynamics.prisma.core.util.xml.XMLDriver;
import com.skynamics.prisma.core.util.xml.test.data.TestCompany;
/**
* Test class for the XML driver.
*/
public class TestXMLDriver
{
//////////////////////////////////////////////////
// @@ Construction
//////////////////////////////////////////////////
/**
* Private constructor.
*/
public TestXMLDriver ()
{
}
//////////////////////////////////////////////////
// @@ Main class
//////////////////////////////////////////////////
/** XML file name */
public static final String FILENAME = "c:/Temp/TestCompany.xml";
/**
* Main class for test.
* Serializes test objects to the file "c:/Temp/TestCompany.xml" and reads
them back again.
*/
public static void main (String [] args)
{
TestCompany tc = new TestCompany ();
tc.setName ("skynamics AG");
tc.setCity ("Munich");
tc.setPhone (null);
tc.setNrEmployees (8);
tc.setRevenue (1000000.0);
System.out.println ("***** Serializing *****");
tc.dump ();
XMLDriver driver = XMLDriver.getInstance ();
String encoding = null;
// String encoding = "ASCII";
// String encoding = "UTF8";
// String encoding = "UTF-16";
// String encoding = "ISO8859_1";
driver.setEncoding (encoding);
// Serialize
try
{
driver.serialize (tc, FILENAME);
}
catch (IOException ioe)
{
System.out.println ("Error serializing test class: ");
System.out.println (ioe);
System.exit (1);
}
// Deserialize
try
{
TestCompany cNew = (TestCompany) driver.deserialize
(TestCompany.class, FILENAME);
System.out.println ("***** Deserializing *****");
cNew.dump ();
}
catch (IOException ioe)
{
System.out.println ("Error deserializing test class: ");
System.out.println (ioe);
System.exit (1);
}
}
}
/*
* File: TestXMLDriver.java
* Product: Prisma
*
* Date Author Changes
* Sep 01 00 Heiko Erhardt Created
*
* (c) 2000 skynamics AG All rights reserved.
*/
package com.skynamics.prisma.core.util.xml.test.data;
import com.skynamics.prisma.core.util.xml.XMLDriver;
/**
* Test class for the XML driver.
*/
public class TestCompany
{
//////////////////////////////////////////////////
// @@ Private data
//////////////////////////////////////////////////
/** Name */
protected String name;
/** City */
protected String city;
/** Phone */
protected String phone;
/** NrEmployees */
protected int nrEmployees;
/** Revenue */
protected double revenue;
//////////////////////////////////////////////////
// @@ Construction
//////////////////////////////////////////////////
/**
* Private constructor.
*/
public TestCompany ()
{
}
//////////////////////////////////////////////////
// @@ Attributes
//////////////////////////////////////////////////
/**
* Gets the name.
* @nowarn
*/
public String getName ()
{
return name;
}
/**
* Sets the name.
* @nowarn
*/
public void setName (String name)
{
this.name = name;
}
/**
* Gets the city.
* @nowarn
*/
public String getCity ()
{
return city;
}
/**
* Sets the city.
* @nowarn
*/
public void setCity (String city)
{
this.city = city;
}
/**
* Gets the phone.
* @nowarn
*/
public String getPhone ()
{
return phone;
}
/**
* Sets the phone.
* @nowarn
*/
public void setPhone (String phone)
{
this.phone = phone;
}
/**
* Gets the nrEmployees.
* @nowarn
*/
public int getNrEmployees ()
{
return nrEmployees;
}
/**
* Sets the nrEmployees.
* @nowarn
*/
public void setNrEmployees (int nrEmployees)
{
this.nrEmployees = nrEmployees;
}
/**
* Sets the nrEmployees.
* @nowarn
*/
public void setNrEmployees (Integer nrEmployees)
{
this.nrEmployees = nrEmployees.intValue ();
}
/**
* Gets the revenue.
* @nowarn
*/
public double getRevenue ()
{
return revenue;
}
/**
* Sets the revenue.
* @nowarn
*/
public void setRevenue (double revenue)
{
this.revenue = revenue;
}
//////////////////////////////////////////////////
// @@ Dump
//////////////////////////////////////////////////
/**
* Dumps the object to standard output.
*/
public void dump ()
{
dump (null);
}
/**
* Dumps the object to standard output.
* @param indent Indent to prepend to the output or null
*/
public void dump (String indent)
{
if (indent == null)
indent = "";
System.out.println (indent + "Class: '" + getClass ().getName ());
System.out.println (indent + " Name: '" + name + "'");
System.out.println (indent + " City: '" + city + "'");
System.out.println (indent + " Phone: '" + phone + "'");
System.out.println (indent + " NrEmployees: '" + nrEmployees + "'");
System.out.println (indent + " Revenue: '" + revenue + "'");
}
}
*********** File TestCompany.xml
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN"
"http://castor.exolab.org/mapping.dtd">
<mapping>
<class name="com.skynamics.prisma.core.util.xml.test.data.TestCompany">
<map-to xml="company"/>
<field name="Name"
type="string">
<bind-xml name="name" node="attribute"/>
</field>
<field name="City"
type="string">
<bind-xml name="city"/>
</field>
<field name="Phone"
type="string">
<bind-xml name="phone"/>
</field>
<field name="Revenue"
type="double">
<bind-xml name="revenue"/>
</field>
<field name="NrEmployees"
type="integer">
<bind-xml name="nr-of-employees"/>
</field>
</class>
</mapping>
/*
* File: XMLDriver.java
* Product: Prisma
*
* Date Author Changes
* Sep 01 00 Heiko Erhardt Created
*
* (c) 2000 skynamics AG All rights reserved.
*/
package com.skynamics.prisma.core.util.xml;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.HashMap;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
/**
* The XML driver class implements a generic driver for XML
serialzation/deserialization.
* It builds upon Castor XML/bean mapping support.
*
* @index XML
*/
public class XMLDriver
{
//////////////////////////////////////////////////
// @@ Private data
//////////////////////////////////////////////////
/**
* Mapping indicator table.
* Maps classes (Class objects) to an empty string to indicate that the class
is
* contained in the mapping table.
*/
protected HashMap mappedClasses = new HashMap ();
/** Castor mapping table */
protected Mapping mapping;
/** Encoding for XML I/O */
protected String encoding;
/** Debug mode flag. Performs object model validation. */
protected boolean debugMode = true;
/** Singleton instance */
private static XMLDriver singletonInstance;
//////////////////////////////////////////////////
// @@ Construction
//////////////////////////////////////////////////
/**
* Gets the singleton instance of this class.
* @nowarn
*/
public static XMLDriver getInstance ()
{
if (singletonInstance == null)
{
singletonInstance = new XMLDriver ();
}
return singletonInstance;
}
/**
* Private constructor.
*/
private XMLDriver ()
{
mapping = new Mapping (getClass ().getClassLoader ());
}
//////////////////////////////////////////////////
// @@ Serialization
//////////////////////////////////////////////////
/**
* Serializes an object to an output file.
* @param o Object to serialize
* @param fileName Output file name
* @throws IOException On i/o or xml/marshalling error
*/
public void serialize (Object o, String fileName)
throws IOException
{
FileOutputStream out = null;
try
{
out = new FileOutputStream (fileName);
}
catch (FileNotFoundException e)
{
throw new IOException ("Cannot create file '" + fileName + "':
" + e.getMessage ());
}
try
{
serialize (o, out);
}
finally
{
out.close ();
}
}
/**
* Serializes an object to an output stream.
* @param o Object to serialize
* @param out Output stream
* @throws IOException On i/o or xml/marshalling error
*/
public void serialize (Object o, OutputStream out)
throws IOException
{
Class cls = o.getClass ();
// Make sure the mapping is loaded
loadMapping (cls);
// Marshal the object
try
{
OutputStreamWriter osw;
if (encoding != null)
osw = new OutputStreamWriter (out, encoding);
else
osw = new OutputStreamWriter (out);
Marshaller marshaller = new Marshaller (osw);
marshaller.setMapping (mapping);
marshaller.setValidation (debugMode);
marshaller.marshal (o);
}
catch (MappingException mpx)
{
throw new IOException ("Error setting XML mapping for class '"
+ cls.getName () + "': " +
mpx.getMessage ());
}
catch (MarshalException mx)
{
throw new IOException ("Error serializing an object of class
'" + cls.getName () + "' to XML: " +
mx.getMessage ());
}
catch (ValidationException vx)
{
throw new IOException ("Error validating XML serialization for
an object of class '" +
cls.getName () + "': " + vx.getMessage ());
}
}
//////////////////////////////////////////////////
// @@ Deserialization
//////////////////////////////////////////////////
/**
* Deserializes an object from an output file.
* @param cls Class of the object to deserialize
* @param fileName Input file name
* @return The deserialized object
* @throws IOException On i/o or xml/marshalling error
*/
public Object deserialize (Class cls, String fileName)
throws IOException
{
FileInputStream in = null;
try
{
in = new FileInputStream (fileName);
}
catch (FileNotFoundException e)
{
throw new IOException ("Cannot open file '" + fileName + "': "
+ e.getMessage ());
}
try
{
return deserialize (cls, in);
}
finally
{
in.close ();
}
}
/**
* Deserializes an object from an output stream.
* @param cls Class of the object to deserialize
* @param in Input stream
* @return The deserialized object
* @throws IOException On i/o or xml/marshalling error
*/
public Object deserialize (Class cls, InputStream in)
throws IOException
{
// Make sure the mapping is loaded
loadMapping (cls);
// Marshal the object
try
{
InputStreamReader isr;
if (encoding != null)
isr = new InputStreamReader (in, encoding);
else
isr = new InputStreamReader (in);
Unmarshaller unmarshaller = new Unmarshaller (mapping);
unmarshaller.setValidation (debugMode);
return unmarshaller.unmarshal (cls, isr);
}
catch (MappingException mpx)
{
throw new IOException ("Error setting XML mapping for class '"
+ cls.getName () + "': " +
mpx.getMessage ());
}
catch (MarshalException mx)
{
throw new IOException ("Error serializing an object of class
'" + cls.getName () + "' to XML: " +
mx.getMessage ());
}
catch (ValidationException vx)
{
throw new IOException ("Error validating XML serialization for
an object of class '" +
cls.getName () + "': " + vx.getMessage ());
}
}
//////////////////////////////////////////////////
// @@ Properties
//////////////////////////////////////////////////
/**
* Gets the encoding for XML.
* @return enc The current encoding. Possible value are for example:\n
* "ASCII" (regular ASCII characters)\n
* "UTF8" (8 bit Unicode)\n
* "UTF-16" (16 bit Unicode)\n
* "ISO8859_1" (regular Windows character set)\n
* or null for default encoding.\n
* For further details, see the JDK specs.
*/
public String getEncoding ()
{
return encoding;
}
/**
* Sets the encoding for XML.
* @param encoding The encoding. Possible value are for example:\n
* "ASCII" (regular ASCII characters)\n
* "UTF8" (8 bit Unicode)\n
* "UTF-16" (16 bit Unicode)\n
* "ISO8859_1" (regular Windows character set)\n
* or null for default encoding.\n
* For further details, see the JDK specs.
*/
public void setEncoding (String encoding)
{
this.encoding = encoding;
}
//////////////////////////////////////////////////
// @@ Helpers
//////////////////////////////////////////////////
/**
* Loads the XML mapping for the specified class.
* Checks the mapping table first. If no mapping is present yet, the method
tries to read the
mapping
* from the Castor mapping file with the same name as the class in the same
directory as the class.
*
* @param cls Class to get the mapping for
* @throws IOException If no mapping file exists for this class
*/
protected void loadMapping (Class cls)
throws IOException
{
if (mappedClasses.get (cls) == null)
{
String className = cls.getName ();
int index = className.lastIndexOf ('.');
String mappingFileName = className.substring (index + 1);
mappingFileName += ".xml";
URL url = cls.getResource (mappingFileName);
if (url == null)
{
throw new IOException ("Mapping resource not found for
class '" + className + "'");
}
// Load the mapping from the resource
try
{
mapping.loadMapping (url);
}
catch (MappingException mex)
{
throw new IOException ("Error loading mapping resource
for class '" + className + "': " +
mex.getMessage ());
}
// Set indicator that mapping was loaded
mappedClasses.put (cls, "");
}
}
}
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev