KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > deferredparsing > XMLCDeferredParsingFactory


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: XMLCDeferredParsingFactory.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.deferredparsing;
25
26 import java.io.File JavaDoc;
27 import java.lang.reflect.Constructor JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.List JavaDoc;
32
33 import org.enhydra.xml.xmlc.XMLCError;
34 import org.enhydra.xml.xmlc.XMLCLogger;
35 import org.enhydra.xml.xmlc.XMLCRuntimeException;
36 import org.enhydra.xml.xmlc.XMLCStdFactory;
37 import org.enhydra.xml.xmlc.XMLObject;
38
39 /**
40  * Factory class to create instances of an XMLC-generated class with
41  * "deferred parsing". Deferred parsing means that the DOM is parsed
42  * from a file on first acess and re-parsed on subsequent acesses
43  * whenever the DOM is out of date with regard to the source file.
44  */

45 public class XMLCDeferredParsingFactory extends XMLCStdFactory {
46     /**
47      * Argument type for object constructor.
48      */

49     private static final Class JavaDoc[] CONSTRUCTOR_ARG_TYPES = {
50         DocumentLoader.class
51     };
52
53     /**
54      * Argument for object constructor.
55      */

56     private final Object JavaDoc[] CONSTRUCTOR_ARGS;
57
58     /**
59      * A list of resoruces path to search for the source files
60      */

61     private List JavaDoc fResourceDirList = new LinkedList JavaDoc();
62     
63     /**
64      * A list of package prefix to removed while searching source
65      * files under the resource path in fResourceDirList
66      */

67     private List JavaDoc fPackagePrefixList = new LinkedList JavaDoc();
68
69     /**
70      * default meta data file. This file is used as the meta file for
71      * the dynamically added new pages
72      */

73     private String JavaDoc defaultMetaDataFile = null;
74
75     /**
76      * The class loader to create and load class for newly added
77      * template to the system without pre-compiled class
78      */

79     private DynamicClassLoader dynamicClassLoader;
80
81     /**
82      * Constructor.
83      *
84      * @param docLoader Document loader to use or <code>null</code>
85      * @param classLoader Classloader used to load classes when a class name
86      * is specified. If null, the system classload is used.
87      * for the {@link StandardDocumentLoader}
88      * @param logger XMLC logger or null for no logging.
89      */

90     public XMLCDeferredParsingFactory(DocumentLoader docLoader,
91                       ClassLoader JavaDoc classLoader,
92                       XMLCLogger logger) {
93     super(classLoader, logger);
94     
95     if (docLoader == null) {
96         docLoader = new StandardDocumentLoader(Collections.synchronizedMap(new HashMap JavaDoc()));
97     }
98     CONSTRUCTOR_ARGS = new Object JavaDoc[] { docLoader };
99
100     dynamicClassLoader = new DynamicClassLoader(classLoader, logger);
101     // Initialize the document loader...
102
docLoader.init(this);
103     }
104
105     /**
106      * Create an object from a XMLC-generate class or subclass.
107      * Don't call buildDocument();
108      */

109     private XMLObject createObject(Class JavaDoc xmlcBasedClass) {
110         try {
111             try {
112                 Constructor JavaDoc constructor = xmlcBasedClass.getConstructor(CONSTRUCTOR_ARG_TYPES);
113                 return (XMLObject)constructor.newInstance(CONSTRUCTOR_ARGS);
114             } catch (NoSuchMethodException JavaDoc e) {
115                 // Class has not been compiled for reloading - try to load it as a standalone class
116
return (XMLObject)xmlcBasedClass.newInstance();
117             }
118         } catch (Exception JavaDoc except) {
119             throw new XMLCRuntimeException(except);
120         }
121     }
122
123     /**
124      * Create a new document instance.
125      */

126     protected XMLObject doCreate(Class JavaDoc xmlcBasedClass)
127             throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
128         if (xmlcBasedClass.isInterface()) {
129             // Convert interface to class name.
130
return doCreate(xmlcBasedClass.getName() + "Impl",
131                             getDefaultClassLoader());
132         } else {
133             if (!XMLObject.class.isAssignableFrom(xmlcBasedClass)) {
134                 throw new XMLCError("class \"" + xmlcBasedClass.getName()
135                                     + "\" does not implement \""
136                                     + XMLObject.class.getName() + "\"");
137             }
138         
139             return createObject(xmlcBasedClass);
140         }
141     }
142
143     /**
144      * Do actualy work of creating a new object given a name.
145      */

146     protected XMLObject doCreate(String JavaDoc xmlcClassName,
147                  ClassLoader JavaDoc classLoader)
148     throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
149
150         if (classLoader != null) {
151         dynamicClassLoader.setDelegate(classLoader);
152         } else {
153         dynamicClassLoader.setDelegate(getClass().getClassLoader());
154     }
155
156     return doCreate(dynamicClassLoader.loadClass(xmlcClassName));
157     }
158
159     /**
160      * Create an XMLCObject from a source file
161      */

162     public XMLObject createFromFile (String JavaDoc filename) {
163     String JavaDoc classname = filename.replace('/', '.');
164     classname = classname.replace('\\', '.');
165     classname = classname.replace(' ', '_');
166     classname = "$$XMLC_GENERATED$$." + classname;
167
168     return create(dynamicClassLoader.createClass(classname, filename));
169     }
170
171     /**
172      * Add a resource directory
173      * @param path path to the directory to be added
174      */

175     public void addResourceDir (String JavaDoc path) {
176     synchronized(fResourceDirList) {
177         File JavaDoc dir = new File JavaDoc(path);
178         if (!fResourceDirList.contains(dir)) {
179         if (dir.exists() && dir.isDirectory()) {
180             fResourceDirList.add(dir);
181         }
182         }
183     }
184     }
185
186     /**
187      * Remove the given directory from the resource directory list
188      * @param path path to be removed
189      */

190     public void removeResourceDir (String JavaDoc path) {
191     synchronized(fResourceDirList) {
192         fResourceDirList.remove(path);
193     }
194     }
195
196     /**
197      * Return the resource directory list
198      */

199     List JavaDoc getResourceDirList () {
200     return fResourceDirList;
201     }
202
203     /**
204      * Add a package prefix to be checked
205      * @param prefix package prefix to be added
206      */

207     public void addPackagePrefix (String JavaDoc prefix) {
208     synchronized (fPackagePrefixList) {
209         prefix = prefix.replace('.', File.pathSeparatorChar);
210         if (!fPackagePrefixList.contains(prefix)) {
211         fPackagePrefixList.add(prefix);
212         }
213     }
214     }
215
216     /**
217      * Remove the package prefix
218      * @param prefix pacakge prefix to be removed
219      */

220     public void remove (String JavaDoc prefix) {
221     synchronized (fPackagePrefixList) {
222         prefix = prefix.replace('.', File.pathSeparatorChar);
223         fPackagePrefixList.remove(prefix);
224     }
225     }
226     
227     /**
228      * Get the package prefix list, the list of package path is
229      * seperated with File.pathSeparator instead of '.'
230      */

231     List JavaDoc getPackagePrefixList () {
232     return fPackagePrefixList;
233     }
234
235     /**
236      * Set the default meta data file to be used by the dynamically
237      * added pages
238      * @param path path to the meta data file
239      */

240     public void setDefaultMetaDataPath(String JavaDoc path) {
241     defaultMetaDataFile = path;
242     }
243
244     /**
245      * Get the default meta data file to be used by the dynamically
246      * added pages
247      */

248     public String JavaDoc getDefaultMetaDataFile() {
249     return defaultMetaDataFile;
250     }
251 }
252
Popular Tags