package org.bifrost.xmlio.config;
import java.lang.reflect.Method;
import java.util.logging.Logger;
import org.bifrost.xmlio.XmlException;
/**
* <p>This configuration and delegate class specifies the class and methods
* that convert a single datatype and also calls the print and parse methods
* of that class.</p>
* <p>
* Created: Feb 26, 2004<br/>
* Copyright: Copyright (c) 2004<br/>
* Assumptions: none<br/>
* Requires: nothing<br/>
* Required by: nothing<br/>
* Revision History:<br/>
* <br/>
* </p>
* <p>Example:</p>
* <pre>
* </pre>
* <p>Conventions:<br/>
* <ul>
* </ul>
* </p>
* @author Donald Kittle <dkittle@mintinc.ca> (last commit: $Author: donald $)
* @version 1.0 (revision $Revision: 1.3 $)
* @stereotype Config
*/
public class Converter
{
/**
* CVS version information.
*/
private final static String _VERSION =
"$Id: Converter.java,v 1.3 2004/05/20 17:53:14 donald Exp $";
private Logger logger = Logger.getLogger(this.getClass().getName());
private String type;
private String classname;
private String parseMethod;
private String printMethod;
protected Object instance;
/**
* Zero argument constructor
*/
public Converter()
{
} // end Constructor()
/**
* Constructor which creates a fully defined converter.
* @param type the (java) data type to convert
* @param className the fully qualified classname of the converter which
* implements the conversion
* @param parseMethod the name of the method to parse a string into the
* data type
* @param printMethod the name of the method to print a string given a value
* of this data type
*/
public Converter(String type, String className, String parseMethod,
String printMethod)
{
setType(type);
setClassname(className);
setParseMethod(parseMethod);
setPrintMethod(printMethod);
} // end Constructor()
/**
* @return Returns the classname.
*/
public String getClassname()
{
return classname;
}
/**
* @param classname The classname to set.
*/
public void setClassname(String classname)
{
this.classname = classname;
}
/**
* @return Returns the parseMethod.
*/
public String getParseMethod()
{
return parseMethod;
}
/**
* @param parseMethod The parseMethod to set.
*/
public void setParseMethod(String parseMethod)
{
this.parseMethod = parseMethod;
}
/**
* @return Returns the printMethod.
*/
public String getPrintMethod()
{
return printMethod;
}
/**
* @param printMethod The printMethod to set.
*/
public void setPrintMethod(String printMethod)
{
this.printMethod = printMethod;
}
/**
* @return Returns the type.
*/
public String getType()
{
return type;
}
/**
* @param type The type to set.
*/
public void setType(String type)
{
this.type = type;
}
public String print(Object value) throws XmlException
{
Object result = new String("");
if (instance == null)
{
try
{
instance = Class.forName(classname).newInstance();
} catch (Exception e)
{
logger.fine("Cannot create converter: " + classname);
return "";
}
}
String methodName = printMethod;
Class[] param = { value.getClass() };
Object[] values = { value };
try
{
// Find the setter method
Method method = instance.getClass().getMethod(methodName, param);
if (method != null)
result = method.invoke(instance, values);
}
catch (Exception e)
{
logger.fine("Cannot print value: " + e);
throw new XmlException("Cannot print value for type " +
value.getClass().getName() + ", " + e);
}
if (result == null)
return "";
return result.toString();
}
} // end Converter Class