KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > util > dom > XMLCDeferredParsingDOMFactory


1 /*
2  * Copyright (C) 2001 Jacob Kjome [hoju@visi.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: XMLCDeferredParsingDOMFactory.java,v 1.5 2004/01/29 18:02:35 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.util.dom;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25 import javax.servlet.ServletContext JavaDoc;
26 import org.w3c.dom.Document JavaDoc;
27 import org.apache.log4j.Logger;
28 import org.enhydra.xml.xmlc.XMLObject;
29 import org.enhydra.xml.xmlc.deferredparsing.XMLCDeferredParsingFactory;
30
31
32 /**
33  * XMLC deferred parsing implementation of a DOMFactory. This class will load a
34  * DOM using XMLCDeferredParsingFactory.
35  * <p>Optionally, one may provide the XMLCDeferredParsingFactory that this DOMFactory
36  * backs with context init parameters in the web.xml. In order to do that, the
37  * current ServletContext must be set using {@link #setServletContext(ServletContext)}
38  * before adding a DOMFactory object to a {@link org.enhydra.barracuda.core.util.dom.DOMLoader DOMLoader}
39  * object. This can be done either programatically or set via the assembly descriptor with
40  * <pre>
41  * &lt;dom-loader factory=&quot;org.enhydra.xml.xmlc.deferredparsing.XMLCDeferredParsingFactory&quot;&gt;
42  * &lt;set-property name=&quot;servletContext&quot; delegateRuntimeValue=&quot;true&quot;/&gt;
43  * &lt;/dom-loader&gt;
44  * </pre></p>
45  * <p>Once above requirement is met, one can set the following &lt;context-param&gt;'s in
46  * the web.xml...
47  * <ul>
48  * <li><tt>xmlcReparseResourceDirs</tt> - A list of additional directories to
49  * load the html and meta data files, separated by File.separator</li>
50  * <li><tt>xmlcReparsePackagePrefixes</tt> - A list of package prefixes to be
51  * removed while searching for html and meta data files, relative to the
52  * classpath and resource dirs, separated by File.separator</li>
53  * <li><tt>xmlcReparseDefaultMetaDataPath</tt> - A path to the default meta data
54  * file, relative to the classpath and resource dirs</li>
55  * </ul></p>
56  * <p>These context init parameters match up exactly with those used by org.enhydra.xml.xmlc.servlet.XMLCContext.
57  * Therefore, the configuration used for this class is entirely compatible with XMLCContext.</p>
58  */

59 public class XMLCDeferredParsingDOMFactory implements DOMFactory {
60
61     /**
62      * used for logging
63      */

64     protected static final Logger logger = Logger.getLogger(XMLCDeferredParsingDOMFactory.class.getName());
65
66     /**
67      * Definition of xmlcReparseResourceDirs parameter.
68      */

69     protected final String JavaDoc PARAM_XMLC_REPARSE_RESOURCE_DIRS = "xmlcReparseResourceDirs";
70
71     /**
72      * Definition of xmlcReparsePackagePrefixes parameter
73      */

74     protected final String JavaDoc PARAM_XMLC_REPARSE_PACKAGE_PREFIXES = "xmlcReparsePackagePrefixes";
75
76     /**
77      * Definition of xmlcReparseDefaultMetaDataPath parameter
78      */

79     protected final String JavaDoc PARAM_XMLC_REPARSE_DEFAULT_METADATA_PATH = "xmlcReparseDefaultMetaDataPath";
80
81     /**
82      * optional, used to access context init parameters if set, ignored if null
83      *
84      * @see #setServletContext(ServletContext)
85      */

86     protected ServletContext JavaDoc servletContext;
87
88     /**
89      * optional, used to access programatically set xmlc reparse resource dirs, ignored if null
90      *
91      * @see #setXMLCReparseResourceDirs(String)
92      */

93     protected String JavaDoc xmlcReparseResourceDirs;
94
95     /**
96      * optional, used to access programatically set xmlc reparse package prefixes, ignored if null
97      *
98      * @see #setXMLCReparsePackagePrefixes(String)
99      */

100     protected String JavaDoc xmlcReparsePackagePrefixes;
101
102     /**
103      * optional, used to access programatically set xmlc reparse default meta data path, ignored if null
104      *
105      * @see #setXMLCReparseDefaultMetaDataPath(String)
106      */

107     protected String JavaDoc xmlcReparseDefaultMetaDataPath;
108
109     /**
110      * arbitrary object used to synchronize upon
111      */

112     protected final Object JavaDoc sync = new Object JavaDoc();
113
114     /**
115      * used for flagging whether the xmlcFactory has been initialized and
116      * avoiding unnecessary synchronization.
117      */

118     protected boolean initialized = false;
119
120     /**
121      * XMLCFactory instance, stored so that it isn't re-created on
122      * every request.
123      */

124     protected XMLCDeferredParsingFactory xmlcFactory;
125
126     /**
127      * Get a new instance of the DOM that is associated with the
128      * given class
129      *
130      * @param clazz the class to be loaded as a DOM object. In this case, this class
131      * should implement XMLObject or the underlying XMLCFactory will
132      * not be able to instantiate it.
133      * @return the document that most closely corresponds with the requested
134      * class/locale combination
135      */

136     public Document JavaDoc getInstance(Class JavaDoc clazz) throws IOException JavaDoc {
137         if (!((XMLObject.class).isAssignableFrom(clazz))) throw new IOException JavaDoc ("Class "+clazz.getName()+" can not be loaded by this DOMFactory because it does not implement XMLOBject");
138         initFactory();
139         return xmlcFactory.create(clazz);
140     }
141
142     /**
143      * Load a document directly from file. This method allows for skipping the compilation
144      * of XMLC classes. In order to be used, the DOMLoader being utilized must support
145      * calling this method. Make sure to provide &quot;xmlcReparseResourceDirs&quot;,
146      * and &quot;xmlcReparseDefaultMetaDataPath&quot; &lt;context-param&gt;'s because
147      * the path provide is relative to the provided resource dirs and the xmlc options file
148      * must be available in order to load the document file.
149      *
150      * @param docPath path to file relative to provided resource directories
151      * @return the document that most closely corresponds with the requested
152      * doc path/locale combination
153      */

154     public Document JavaDoc getInstance(String JavaDoc docPath) throws IOException JavaDoc {
155         initFactory();
156         return xmlcFactory.createFromFile(docPath);
157     }
158
159     /**
160      * optional method to set the current servlet context. Must be set
161      * before getInstance() is first called in order to be used.
162      *
163      * @param iservletContext the current servlet context
164      */

165     public void setServletContext(ServletContext JavaDoc iservletContext) {
166         this.servletContext = iservletContext;
167     }
168
169     /**
170      * optional method to directly set the xmlc resource dirs path. Note
171      * that, if set, this supercedes the xmlcReparseResourceDirs context
172      * param value (described above) obtained from a ServletContext.
173      * This must be set before getInstance() is first called in order to be used.
174      *
175      * @param ireparseResourceDirs a list of additional directories to load the html and meta
176      * data files separated by File.separator
177      */

178     public void setXMLCReparseResourceDirs(String JavaDoc ireparseResourceDirs) {
179         this.xmlcReparseResourceDirs = ireparseResourceDirs;
180     }
181
182     /**
183      * optional method to directly set the xmlc reparse package prefixes. Note
184      * that, if set, this supercedes the xmlcReparsePackagePrefixes context
185      * param value (described above) obtained from a ServletContext.
186      * This must be set before getInstance() is first called in order to be used.
187      *
188      * @param ireparsePackagePrefixes a list of package prefixes to be
189      * removed while searching for html and meta data files, relative to the
190      * classpath and resource dirs, separated by File.separator
191      */

192     public void setXMLCReparsePackagePrefixes(String JavaDoc ireparsePackagePrefixes) {
193         this.xmlcReparsePackagePrefixes = ireparsePackagePrefixes;
194     }
195
196     /**
197      * optional method to directly set the xmlc reparse default meta data path. Note
198      * that, if set, this supercedes the xmlcReparseDefaultMetaDataPath context
199      * param value (described above) obtained from a ServletContext.
200      * This must be set before getInstance() is first called in order to be used.
201      *
202      * @param ireparseDefaultMetaDataPath a path to the default meta data
203      * file, relative to the classpath and resource dirs
204      */

205     public void setXMLCReparseDefaultMetaDataPath(String JavaDoc ireparseDefaultMetaDataPath) {
206         this.xmlcReparseDefaultMetaDataPath = ireparseDefaultMetaDataPath;
207     }
208
209     private void initFactory() {
210         if (initialized == false) {
211             synchronized (sync) {
212                 logger.info("initializing an XMLC deferred parsing factory for returning XMLC-generated documents");
213                 xmlcFactory = new XMLCDeferredParsingFactory(null, Thread.currentThread().getContextClassLoader(), null);
214
215                 String JavaDoc resDirs = null;
216                 if (xmlcReparseResourceDirs != null) {
217                     resDirs = xmlcReparseResourceDirs;
218                 } else {
219                     if (servletContext != null) {
220                         resDirs = servletContext.getInitParameter(PARAM_XMLC_REPARSE_RESOURCE_DIRS);
221                     }
222                 }
223                 if (resDirs != null) {
224                     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (resDirs, File.pathSeparator);
225                     while (st.hasMoreTokens()) {
226                         xmlcFactory.addResourceDir(st.nextToken());
227                     }
228                 }
229
230                 String JavaDoc pkgPrefixes = null;
231                 if (xmlcReparsePackagePrefixes != null) {
232                     pkgPrefixes = xmlcReparsePackagePrefixes;
233                 } else {
234                     if (servletContext != null) {
235                         pkgPrefixes = servletContext.getInitParameter(PARAM_XMLC_REPARSE_PACKAGE_PREFIXES);
236                     }
237                 }
238                 if (pkgPrefixes != null) {
239                     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (pkgPrefixes, File.pathSeparator);
240                     while (st.hasMoreTokens()) {
241                         xmlcFactory.addPackagePrefix(st.nextToken());
242                     }
243                 }
244
245                 String JavaDoc defaultMetaDataPath = null;
246                 if (xmlcReparseDefaultMetaDataPath != null) {
247                     defaultMetaDataPath = xmlcReparseDefaultMetaDataPath;
248                 } else {
249                     if (servletContext != null) {
250                         defaultMetaDataPath = servletContext.getInitParameter(PARAM_XMLC_REPARSE_DEFAULT_METADATA_PATH);
251                     }
252                 }
253                 if (defaultMetaDataPath != null) {
254                     xmlcFactory.setDefaultMetaDataPath(defaultMetaDataPath);
255                 }
256                 initialized = true;
257             }
258         }
259     }
260
261 }
262
Popular Tags