com.ls.util.xml
Class JavaXMLConverter

java.lang.Object
  |
  +--com.ls.util.xml.JavaXMLConverter

public class JavaXMLConverter
extends Object

This class is for the creating of XML String from Java Object or Java Object from XML String.

 Java objects can contains the follwoing data types 
 		primitve Attributes
 		structured Attributes 
 		the only collection class is Vector (to store the other objects class)

 e.g
	 
      public class Instrument { 
         public	String	name;
         public	Date	created; 
         public	int		nomAmt; 
         public  Vector	instrStructure;
      } 
				
      public class InstrStructure {    
         public int componentAmt;    
         public Instrument inst;    
      }  
 

 Creating the Objects

		Instrument instrument		= new Instrument();
 		instrument.name				= "Tom";
 		instrument.created			= new Date();
 		instrument.nomAmt			= 10;
 		instrument.instrStructure	= new Vector();
 		InstrStructure instrStruct	= new InstrStructure();
 		instrStruct.componentAmt	= 50;
 		instrStruct.inst			= instrument;
		instrument.instrStructure.add(instrStruct);

      Hashtable objectHash = new Hashtable();
      objectHash.put("instrument",instrument);

 call the function javaToXML like
   JavaXMLConverter javaXMLConverter = new JavaXMLConverter();
   javaXMLConverter.javaToXML(objectHash);
   
 Output of the object
		<FIDM> 
			<instrument id="2" class="Instrument">
				<name>Tom</name>
				<created>02.02.2001 13:44:43</created>
				<nomAmt>10</nomAmt>
				<instrStructure id="3" class="InstrStructure">
					<componentAmt>50</componentAmt>
					<inst ref="2"/>
				</instrStructure>
			</instrument>
		</FIDM>

It is also possible to convert multiple objects at the same time
for that need to insert in the objectHash 
like
		objectHash.put("instrument",instrument);
		objectHash.put("student",student);


 To convert the xml String to java object
 first need to set up the class package 
 for details	 setClassPackageMapping(java.lang.String[][])
 and then call the javaXMLConverter.xmlToJava(xmlString)
 

Version:
$Revision: 1.12 $
Author:
Last modified by $Author: PMandal $

Constructor Summary
JavaXMLConverter()
           
 
Method Summary
 void addStaticDataObject(Object object)
          Allows to specify the object which contain the static data.
 String javaToXML(Object object)
          Conversion of XML string form Object
 String javaToXML(Object object, String outermostTag)
          Conversion of XML string form Object
 void setClassPackageMapping(String[][] classPackageMapping)
          Defines the mapping from class names to package names.
 void setStaticDataObjects(Iterator staicObjectIterator)
          Allows to specify the objects which contain the static data.
 void setTab(boolean tab)
          Setting the tab for the XML output
 void setTransientSemantic(boolean writeTransient, String transientIDName)
          Allow to set transient semantic.
 Object xmlToJava(String xmlString)
          Converts an XML String to java objects.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JavaXMLConverter

public JavaXMLConverter()
Method Detail

setTab

public void setTab(boolean tab)
Setting the tab for the XML output
Parameters:
tab - The tab is true to set the tab in the XML string else false

setTransientSemantic

public void setTransientSemantic(boolean writeTransient,
                                 String transientIDName)
                          throws XMLConversionException
Allow to set transient semantic. If writeTransient is true then the second parameter is used as attribute name to refer static object in the xmlString. This attribute name need to match with one attribute name of the class.
Parameters:
writeTransient - true to write the transinet attribute in the XML string else false
transientIDName - name of the attribute which is used as attribute name to refer transient object, if writeTransient is true.
Throws:
XMLConversionException - If writeTransient is true and transientIDName is null

setClassPackageMapping

public void setClassPackageMapping(String[][] classPackageMapping)
Defines the mapping from class names to package names. All classes that may occure in an xml String and that should be instantiated as java objects need to be contained in the array passed. This method must be called BEFORE calling xmlToJava().
 Example:
 String classPackage[][] = {
                             {"Liability",        "com.ls.three60t.fidm.instrument_Package"},
                             {"InstrStructure",   "com.ls.three60t.fidm.instrument_Package"},
                             {"InstrInGroup",     "com.ls.three60t.fidm.instrument_Package"},
                             {"InstrIdent",       "com.ls.three60t.fidm.instrument_Package"},
                             {"InstrGroup",       "com.ls.three60t.fidm.instrument_Package"},
                           };
Parameters:
classPackageMapping - a two dimensional array containing the mapping from classes to package names

addStaticDataObject

public void addStaticDataObject(Object object)
                         throws XMLConversionException
Allows to specify the object which contain the static data. After specifying objects which contain the static data using setStaticDataObjects(), specify any new object as static data this method is used.
Parameters:
object - the static data object
Throws:
XMLConversionException - If any problem occurs while storingt the transient objects

setStaticDataObjects

public void setStaticDataObjects(Iterator staicObjectIterator)
                          throws XMLConversionException
Allows to specify the objects which contain the static data. All the Objects that may occure as static need to be contained in the Iterator passed. This method must be called before calling xmlToJava() method, if the setTransientSemantic(true, ..) method is called before.
Parameters:
staicObjectIterator - an Iterator of static objects
Throws:
XMLConversionException - If any problem occurs while storingt the transient objects

xmlToJava

public Object xmlToJava(String xmlString)
                 throws XMLToJavaConversionException
Converts an XML String to java objects. Before calling this method, the setClassPackageMapping() method must be called to define the classname - packagename mapping. Need to call the method setStaticDataObjects() before this method if the method setTransientSemantic(true,..) was called, to store the transinet objects. The classnames used in the xml file must be unique, i.e. even in different packages no duplicate class names may exist.
Parameters:
xmlString - The String which contains the XML formatted tags and need to convert as Java Object
Returns:
hashtable containing all objects converted from xml. Key is the classname (!we have to define something better if two objects of the same class are contained) value is the object itself.
Throws:
XMLToJavaConversionException - If any problem occurs while converting the xml string to java objects

javaToXML

public String javaToXML(Object object)
                 throws JavaToXMLConversionException
Conversion of XML string form Object
Parameters:
object - either a single object to be converted or a hashtable containing the names of the objects as keys and the objects to be converted as values.
Returns:
XML conversion string of the object
Throws:
JavaToXMLConversionException - If any problem occurs while converting

javaToXML

public String javaToXML(Object object,
                        String outermostTag)
                 throws JavaToXMLConversionException
Conversion of XML string form Object
Parameters:
object - either a single object to be converted or a hashtable containing the names of the objects as keys and the objects to be converted as values.
outermostTag - tag which is used as outter most tag
Returns:
XML conversion string of the object
Throws:
JavaToXMLConversionException - If any problem occurs while converting