KickJava   Java API By Example, From Geeks To Geeks.

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


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: JivanDOMFactory.java,v 1.4 2004/02/01 07:52:23 jacobk Exp $
19  */

20 package org.enhydra.barracuda.core.util.dom;
21
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import org.w3c.dom.Document JavaDoc;
26 import org.apache.log4j.Logger;
27 import org.jivan.html.document.DocumentFactory;
28
29
30 /**
31  * Jivan implementation of a DOMFactory. This class will load a DOM using
32  * the Jivan DocumentFactory.
33  */

34 public class JivanDOMFactory implements DOMFactory {
35
36     /**
37      * used for logging
38      */

39     protected static final Logger logger = Logger.getLogger(JivanDOMFactory.class.getName());
40
41     /**
42      * optional, used to load resources from oa servlet context
43      *
44      * @see #setServletContext(ServletContext)
45      */

46     protected ServletContext JavaDoc servletContext;
47
48     /**
49      * arbitrary object used to synchronize upon
50      */

51     protected final Object JavaDoc sync = new Object JavaDoc();
52
53     /**
54      * used for flagging whether the jivanFactory has been initialized and
55      * avoiding unnecessary synchronization.
56      */

57     protected boolean initialized = false;
58
59     /**
60      * Jivan DocumentFactory instance, stored so that it isn't re-created on
61      * every request.
62      */

63     protected DocumentFactory jivanFactory;
64
65     /**
66      * This method is not supported by this dom factory and will
67      * immediately throw an IOException if called!
68      *
69      * <p>Jivan does not use a wrapper class for documents like XMLC.
70      * If you already have a Jivan DocumentManager, just use that directly.</p>
71      */

72     public Document JavaDoc getInstance(Class JavaDoc clazz) throws IOException JavaDoc {
73         throw new IOException JavaDoc("Error: Unimplemented - Jivan DocumentFactory does not support creation of documents from a class. If you already have a DocumentManager instance, use it directly");
74     }
75
76     /**
77      * Load a document directly from file using Jivan.
78      *
79      * <p>The docPath can be provided in one of two forms: a path relative to
80      * a servlet context such as "/WEB-INF/templates/foo.html", in which case
81      * one would have to call {@link #setServletContext} before the first call
82      * to this method, or a fully qualified URL (or URI) such as that returned
83      * by {@link java.net.URL#toExternalForm}.</p>
84      *
85      * @see DOMFactory#getInstance(String)
86      */

87     public Document JavaDoc getInstance(String JavaDoc docPath) throws IOException JavaDoc {
88         initFactory();
89         if (servletContext != null && docPath.startsWith("/")) {
90             InputStream JavaDoc in = servletContext.getResourceAsStream(docPath);
91             if (in == null) throw new IOException JavaDoc("non-existent resource path: " + docPath);
92             return jivanFactory.docManFor(docPath, in).getDocument();
93         }
94         return jivanFactory.docManFor(docPath).getDocument();
95     }
96
97     /**
98      * Optional method to set the current servlet context. Allows for
99      * a docPath such as "/WEB-INF/templates/foo.html". If the servlet context
100      * is not set, a docPath of this type will fail resulting in getInstance()
101      * returning an IOException. Must be set before getInstance() is first
102      * called in order to be used.
103      *
104      * @param iservletContext the current servlet context
105      */

106     public void setServletContext(ServletContext JavaDoc iservletContext) {
107         this.servletContext = iservletContext;
108     }
109
110     private void initFactory() {
111         if (initialized == false) {
112             synchronized (sync) {
113                 logger.info("initializing a Jivan factory for returning HTML documents");
114                 jivanFactory = DocumentFactory.getInstance();
115                 initialized = true;
116             }
117         }
118     }
119
120 }
121
Popular Tags