001: /*
002: * Project: Gulden Utilies
003: * Class: de.gulden.util.xml.serializer.smart.SmartReflectionXMLSerializerFactory
004: * Version: snapshot-beautyj-1.1
005: *
006: * Date: 2004-09-29
007: *
008: * This is a snapshot version of the Gulden Utilities,
009: * it is not released as a seperate version.
010: *
011: * Note: Contains auto-generated Javadoc comments created by BeautyJ.
012: *
013: * This is licensed under the GNU Lesser General Public License (LGPL)
014: * and comes with NO WARRANTY.
015: *
016: * Author: Jens Gulden
017: * Email: amoda@jensgulden.de
018: */
019:
020: package de.gulden.util.xml.serializer.smart;
021:
022: import de.gulden.util.xml.XMLException;
023: import de.gulden.util.xml.serializer.XMLSerializer;
024: import de.gulden.util.xml.serializer.XMLSerializerFactory;
025: import de.gulden.util.xml.serializer.smart.SmartReflectionXMLSerializer;
026: import java.util.*;
027: import java.util.Hashtable;
028:
029: /**
030: * Class SmartReflectionXMLSerializerFactory.
031: *
032: * @author Jens Gulden
033: * @version snapshot-beautyj-1.1
034: */
035: public class SmartReflectionXMLSerializerFactory implements Cloneable,
036: XMLSerializerFactory {
037:
038: // ------------------------------------------------------------------------
039: // --- fields ---
040: // ------------------------------------------------------------------------
041:
042: /**
043: * The tagnames to classes.
044: */
045: protected Hashtable tagnamesToClasses;
046:
047: /**
048: * The classes to tagnames.
049: */
050: protected Hashtable classesToTagnames;
051:
052: /**
053: * The tagnames to classes.
054: */
055: protected TreeSet tagnamesAskClassname;
056:
057: // ------------------------------------------------------------------------
058: // --- constructor ---
059: // ------------------------------------------------------------------------
060:
061: /**
062: * Creates a new instance of SmartReflectionXMLSerializerFactory.
063: */
064: public SmartReflectionXMLSerializerFactory() {
065: tagnamesToClasses = new Hashtable();
066: classesToTagnames = new Hashtable();
067: tagnamesAskClassname = new TreeSet();
068: }
069:
070: // ------------------------------------------------------------------------
071: // --- methods ---
072: // ------------------------------------------------------------------------
073:
074: /**
075: * Creates the x m l serializer.
076: */
077: public XMLSerializer createXMLSerializer() {
078: try {
079: return new SmartReflectionXMLSerializer(
080: (SmartReflectionXMLSerializerFactory) this .clone());
081: } catch (CloneNotSupportedException e) {
082: throw new Error("internal cloning error");
083: }
084: }
085:
086: public void registerTagname(String tagname, Class serializableClass) {
087: tagnamesToClasses.put(tagname, serializableClass);
088: classesToTagnames.put(serializableClass, tagname);
089: }
090:
091: public void registerTagname(String tagname,
092: Class serializableClass, boolean askClassname) {
093: registerTagname(tagname, serializableClass);
094: if (askClassname) {
095: tagnamesAskClassname.add(tagname);
096: }
097: }
098:
099: Class mapToClass(String tagname, String classAttribute)
100: throws XMLException {
101: Class cl = (Class) tagnamesToClasses.get(tagname);
102: if (cl == null) {
103: throw new XMLException("tagname '" + tagname
104: + "' is not known");
105: }
106: if (tagnamesAskClassname.contains(tagname)) {
107: if (classAttribute == null || classAttribute.equals("")) {
108: throw new XMLException("tagname '" + tagname
109: + "' requires a 'class' attribute");
110: }
111: Class cl2;
112: try {
113: cl2 = Class.forName(classAttribute);
114: } catch (ClassNotFoundException cnfe) {
115: throw new XMLException("tagname '" + tagname
116: + "' cannot load class '" + classAttribute
117: + "' - not found");
118: }
119: if (!cl.isAssignableFrom(cl2)) {
120: throw new XMLException("tagname '" + tagname
121: + "' cannot use class '" + classAttribute
122: + "' - not compatible with '" + cl.getName()
123: + "'");
124: }
125: return cl2;
126: } else {
127: return cl;
128: }
129: }
130:
131: String mapToTagname(Class serializableClass) {
132: return (String) classesToTagnames.get(serializableClass);
133: }
134:
135: } // end SmartReflectionXMLSerializerFactory
|