KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > dom > XMLCDomFactoryCache


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: XMLCDomFactoryCache.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.dom;
25
26 import java.util.HashMap JavaDoc;
27
28 import org.enhydra.xml.xmlc.XMLCError;
29 import org.w3c.dom.DocumentType JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31
32 /**
33  * Class that provides a cache of XMLCDomFactory objects. Since these classes
34  * are simple, they are normally reentrant. This cache provides a mechanism
35  * to maintain only one instance of each class. Currently, instances are
36  * never freed from this cache. Also creates factories for use at
37  * compile-time.
38  */

39 public class XMLCDomFactoryCache {
40     /**
41      * Class name of default XML DOM factory.
42      */

43     private static final String JavaDoc DEFAULT_XML_DOM_FACTORY_CLASS
44         = "org.enhydra.xml.xmlc.dom.lazydom.LazyDomFactory";
45     
46     /**
47      * Class name of default HTML DOM factory.
48      */

49     private static final String JavaDoc DEFAULT_HTML_DOM_FACTORY_CLASS
50         = "org.enhydra.xml.xmlc.dom.lazydom.LazyHTMLDomFactory";
51     
52     /**
53      * Cache of factories, keyed by class object.
54      */

55     private static HashMap JavaDoc cache = new HashMap JavaDoc();
56
57     /**
58      * Disallow instanciation.
59      */

60     private XMLCDomFactoryCache() {
61     }
62
63     /**
64      * Generate an error message when we can't create an instance of an
65      * XMLCDomFactory class.
66      */

67     private static void handleCreateError(Class JavaDoc factoryClass,
68                                           Exception JavaDoc except) {
69         throw new XMLCError("Can't create an instance of \""
70                             + factoryClass.getName() + "\": "
71                             + except.getClass().getName(),
72                             except);
73     }
74
75     /**
76      * Create an instance of an XMLCDomFactory. Don't synchronize, as its ok
77      * to create more than one.
78      */

79     private static XMLCDomFactory createFactory(Class JavaDoc factoryClass) {
80         try {
81             return (XMLCDomFactory)factoryClass.newInstance();
82         } catch (IllegalAccessException JavaDoc except) {
83             handleCreateError(factoryClass, except);
84         } catch (InstantiationException JavaDoc except) {
85             handleCreateError(factoryClass, except);
86         } catch (ClassCastException JavaDoc except) {
87             handleCreateError(factoryClass, except);
88         }
89         return null; // Never makes it here.
90
}
91
92     /**
93      * Create an instance of an XMLCDomFactory, however don't add it
94      * to the cache. This is used at compile time to get a factory.
95      *
96      * @param factoryClassName Name of factory to create, if null, the
97      * default is user.
98      * @param isHtmlDocument Use only to determine the default.
99      */

100     public static XMLCDomFactory createFactory(String JavaDoc factoryClassName,
101                                                 boolean isHtmlDocument) {
102         if (factoryClassName == null) {
103             if (isHtmlDocument) {
104                 factoryClassName = DEFAULT_HTML_DOM_FACTORY_CLASS;
105             } else {
106                 factoryClassName = DEFAULT_XML_DOM_FACTORY_CLASS;
107             }
108         }
109         try {
110             return createFactory(Class.forName(factoryClassName));
111         } catch (Exception JavaDoc except) {
112             throw new XMLCError("Can't load class \""
113                                 + factoryClassName + "\": "
114                                 + except.getClass().getName(),
115                                 except);
116         }
117     }
118
119     /**
120      * Get an instance of an XMLCDomFactory given it's class.
121      */

122     public static XMLCDomFactory getFactory(Class JavaDoc factoryClass) {
123         XMLCDomFactory factory = (XMLCDomFactory)cache.get(factoryClass);
124         if (factory == null) {
125             factory = createFactory(factoryClass);
126             cache.put(factoryClass, factory);
127         }
128         return factory;
129     }
130
131     /**
132      * Check for an outdate DOM factory method,
133      */

134     private static void checkForOutdateMethod(Class JavaDoc factoryClass,
135                                               String JavaDoc methodName,
136                                               Class JavaDoc[] methodArgTypes,
137                                               String JavaDoc methodDesc) {
138         try {
139             factoryClass.getMethod(methodName, methodArgTypes);
140             throw new XMLCError("XMLCDomFactory implementation "
141                                 + factoryClass.getName()
142                                 + " contains outdated method "
143                                 + methodDesc
144                                 + "; please update your code");
145         } catch (NoSuchMethodException JavaDoc except) {
146             // Good, don't have it...
147
}
148     }
149
150     /**
151      * Check a DOM factory for outdated methods. Several methods were
152      * changed in the XMLCDomFactory. Since few users implement these
153      * classes, its was decided to just obsolute the methods. However,
154      * since most XMLCDomFactories extend the base one, one just got
155      * weird behavior (generic documents instead of specific ones).
156      * To work around this, this method checks for outdated factory
157      * methods and generates errors if found.
158      */

159     public static void checkForOutdatedClass(XMLCDomFactory domFactory) {
160         //FIXME: Added in XMLC 2.0, delete at some future date
161
Class JavaDoc factoryClass = domFactory.getClass();
162
163         // createDocument(String qualifiedName, DocumentType doctype);
164
checkForOutdateMethod(factoryClass, "createDocument",
165                               new Class JavaDoc[] {String JavaDoc.class, DocumentType JavaDoc.class},
166                               "createDocument(String,DocumentType)");
167
168         // getElementURLAttrs(Element element);
169
checkForOutdateMethod(factoryClass, "getElementURLAttrs",
170                               new Class JavaDoc[] {Element JavaDoc.class},
171                               "getElementURLAttrs(Element)");
172
173         // getBaseInterfaceName()
174
checkForOutdateMethod(factoryClass, "getBaseInterfaceName",
175                               null, "getBaseInterfaceName()");
176
177         // needsCreateTextSetMethod(Element)
178
checkForOutdateMethod(factoryClass, "needsCreateTextSetMethod",
179                               new Class JavaDoc[] {Element JavaDoc.class},
180                               "needsCreateTextSetMethod(Element)");
181     }
182 }
183
Popular Tags