KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > util > xml > serializer > smart > SmartReflectionXMLSerializerFactory


1 /*
2  * Project: Gulden Utilies
3  * Class: de.gulden.util.xml.serializer.smart.SmartReflectionXMLSerializerFactory
4  * Version: snapshot-beautyj-1.1
5  *
6  * Date: 2004-09-29
7  *
8  * This is a snapshot version of the Gulden Utilities,
9  * it is not released as a seperate version.
10  *
11  * Note: Contains auto-generated Javadoc comments created by BeautyJ.
12  *
13  * This is licensed under the GNU Lesser General Public License (LGPL)
14  * and comes with NO WARRANTY.
15  *
16  * Author: Jens Gulden
17  * Email: amoda@jensgulden.de
18  */

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 JavaDoc;
28
29 /**
30  * Class SmartReflectionXMLSerializerFactory.
31  *
32  * @author Jens Gulden
33  * @version snapshot-beautyj-1.1
34  */

35 public class SmartReflectionXMLSerializerFactory implements Cloneable JavaDoc, XMLSerializerFactory {
36
37     // ------------------------------------------------------------------------
38
// --- fields ---
39
// ------------------------------------------------------------------------
40

41     /**
42      * The tagnames to classes.
43      */

44     protected Hashtable JavaDoc tagnamesToClasses;
45
46     /**
47      * The classes to tagnames.
48      */

49     protected Hashtable JavaDoc classesToTagnames;
50
51     /**
52      * The tagnames to classes.
53      */

54     protected TreeSet tagnamesAskClassname;
55
56
57     // ------------------------------------------------------------------------
58
// --- constructor ---
59
// ------------------------------------------------------------------------
60

61     /**
62      * Creates a new instance of SmartReflectionXMLSerializerFactory.
63      */

64     public SmartReflectionXMLSerializerFactory() {
65         tagnamesToClasses=new Hashtable JavaDoc();
66         classesToTagnames=new Hashtable JavaDoc();
67         tagnamesAskClassname=new TreeSet();
68     }
69
70
71     // ------------------------------------------------------------------------
72
// --- methods ---
73
// ------------------------------------------------------------------------
74

75     /**
76      * Creates the x m l serializer.
77      */

78     public XMLSerializer createXMLSerializer() {
79         try {
80             return new SmartReflectionXMLSerializer((SmartReflectionXMLSerializerFactory)this.clone());
81         } catch (CloneNotSupportedException JavaDoc e) {
82             throw new Error JavaDoc("internal cloning error");
83         }
84     }
85
86     public void registerTagname(String JavaDoc tagname, Class JavaDoc serializableClass) {
87         tagnamesToClasses.put(tagname,serializableClass);
88         classesToTagnames.put(serializableClass,tagname);
89     }
90
91     public void registerTagname(String JavaDoc tagname, Class JavaDoc serializableClass, boolean askClassname) {
92         registerTagname(tagname, serializableClass);
93         if (askClassname) {
94             tagnamesAskClassname.add(tagname);
95         }
96     }
97
98     Class JavaDoc mapToClass(String JavaDoc tagname, String JavaDoc classAttribute) throws XMLException {
99         Class JavaDoc cl = (Class JavaDoc)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 JavaDoc cl2;
108             try {
109                 cl2 = Class.forName(classAttribute);
110             } catch (ClassNotFoundException JavaDoc 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 JavaDoc mapToTagname(Class JavaDoc serializableClass) {
123         return (String JavaDoc)classesToTagnames.get(serializableClass);
124     }
125
126 } // end SmartReflectionXMLSerializerFactory
127
Popular Tags