1 19 20 package de.gulden.util.xml.serializer.smart; 21 22 import de.gulden.util.xml.XMLException; 23 import de.gulden.util.xml.serializer.XMLSerializer; 24 import de.gulden.util.xml.serializer.XMLSerializerFactory; 25 import de.gulden.util.xml.serializer.smart.SmartReflectionXMLSerializer; 26 import java.util.*; 27 import java.util.Hashtable ; 28 29 35 public class SmartReflectionXMLSerializerFactory implements Cloneable , XMLSerializerFactory { 36 37 41 44 protected Hashtable tagnamesToClasses; 45 46 49 protected Hashtable classesToTagnames; 50 51 54 protected TreeSet tagnamesAskClassname; 55 56 57 61 64 public SmartReflectionXMLSerializerFactory() { 65 tagnamesToClasses=new Hashtable (); 66 classesToTagnames=new Hashtable (); 67 tagnamesAskClassname=new TreeSet(); 68 } 69 70 71 75 78 public XMLSerializer createXMLSerializer() { 79 try { 80 return new SmartReflectionXMLSerializer((SmartReflectionXMLSerializerFactory)this.clone()); 81 } catch (CloneNotSupportedException e) { 82 throw new Error ("internal cloning error"); 83 } 84 } 85 86 public void registerTagname(String tagname, Class serializableClass) { 87 tagnamesToClasses.put(tagname,serializableClass); 88 classesToTagnames.put(serializableClass,tagname); 89 } 90 91 public void registerTagname(String tagname, Class serializableClass, boolean askClassname) { 92 registerTagname(tagname, serializableClass); 93 if (askClassname) { 94 tagnamesAskClassname.add(tagname); 95 } 96 } 97 98 Class mapToClass(String tagname, String classAttribute) throws XMLException { 99 Class cl = (Class )tagnamesToClasses.get(tagname); 100 if (cl==null) { 101 throw new XMLException("tagname '"+tagname+"' is not known"); 102 } 103 if (tagnamesAskClassname.contains(tagname)) { 104 if (classAttribute==null||classAttribute.equals("")) { 105 throw new XMLException("tagname '"+tagname+"' requires a 'class' attribute"); 106 } 107 Class cl2; 108 try { 109 cl2 = Class.forName(classAttribute); 110 } catch (ClassNotFoundException cnfe) { 111 throw new XMLException("tagname '"+tagname+"' cannot load class '"+classAttribute+"' - not found"); 112 } 113 if (!cl.isAssignableFrom(cl2)) { 114 throw new XMLException("tagname '"+tagname+"' cannot use class '"+classAttribute+"' - not compatible with '"+cl.getName()+"'"); 115 } 116 return cl2; 117 } else { 118 return cl; 119 } 120 } 121 122 String mapToTagname(Class serializableClass) { 123 return (String )classesToTagnames.get(serializableClass); 124 } 125 126 } | Popular Tags |