KickJava   Java API By Example, From Geeks To Geeks.

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


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: XMLCStdDOMFactory.java,v 1.4 2004/01/29 18:02:35 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.util.dom;
21
22 import java.io.IOException JavaDoc;
23 import org.w3c.dom.Document JavaDoc;
24 import org.apache.log4j.Logger;
25 import org.enhydra.xml.xmlc.XMLObject;
26 import org.enhydra.xml.xmlc.XMLCStdFactory;
27
28
29 /**
30  * XMLC standard implementation of a DOMFactory. This class will load a DOM using
31  * XMLCStdFactory.
32  */

33 public class XMLCStdDOMFactory implements DOMFactory {
34
35     /**
36      * used for logging
37      */

38     protected static final Logger logger = Logger.getLogger(XMLCStdDOMFactory.class.getName());
39
40     /**
41      * arbitrary object used to synchronize upon
42      */

43     protected final Object JavaDoc sync = new Object JavaDoc();
44
45     /**
46      * used for flagging whether the xmlcFactory has been initialized and
47      * avoiding unnecessary synchronization.
48      */

49     protected boolean initialized = false;
50
51     /**
52      * XMLCFactory instance, stored so that it isn't re-created on
53      * every request.
54      */

55     protected XMLCStdFactory xmlcFactory;
56
57     /**
58      * Get a new instance of the DOM that is associated with the
59      * given class.
60      *
61      * @param clazz the class to be loaded as a DOM object. In this case, this class
62      * should implement XMLObject or the underlying XMLCFactory will
63      * not be able to instantiate it.
64      * @return the document that most closely corresponds with the requested
65      * class/locale combination
66      */

67     public Document JavaDoc getInstance(Class JavaDoc clazz) throws IOException JavaDoc {
68         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");
69         initFactory();
70         return xmlcFactory.create(clazz);
71     }
72
73     /**
74      * This method is not supported by this dom factory and will
75      * immediately throw an IOException if called!
76      */

77     public Document JavaDoc getInstance(String JavaDoc docPath) throws IOException JavaDoc {
78         throw new IOException JavaDoc("Error: Unimplemented - XMLCStdFactory does not support creation of documents directly from file");
79     }
80
81     private void initFactory() {
82         if (initialized == false) {
83             synchronized (sync) {
84                 logger.info("initializing an XMLC Std factory for returning XMLC-generated documents");
85                 xmlcFactory = new XMLCStdFactory(Thread.currentThread().getContextClassLoader(), null);
86                 initialized = true;
87             }
88         }
89     }
90
91 }
92
Popular Tags