KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > dom > DOMFactory


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/dom/DOMFactory.java,v 1.7 2004/08/18 12:25:58 hkollmann Exp $
3  * $Revision: 1.7 $
4  * $Date: 2004/08/18 12:25:58 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.dom;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.dbforms.util.ReflectionUtil;
30 import org.dbforms.util.Util;
31
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.xpath.XPathEvaluator;
35
36 import java.io.FileInputStream JavaDoc;
37 import java.io.FileOutputStream JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.io.OutputStream JavaDoc;
41
42 import java.net.URL JavaDoc;
43 import java.net.URLConnection JavaDoc;
44
45
46
47 /**
48  * abstract class to hide the implemtation details of the various dom
49  * implementations.
50  *
51  * @author Henner Kollmann
52  */

53 public abstract class DOMFactory {
54    private static final ThreadLocal JavaDoc singlePerThread = new ThreadLocal JavaDoc();
55    private static Log logCat = LogFactory.getLog(DOMFactory.class
56                                                                .getName());
57    private static String JavaDoc factoryClass = "org.dbforms.dom.DOMFactoryXALANImpl";
58
59    /**
60     * Creates a new DOMFactory object.
61     */

62    protected DOMFactory() {
63    }
64
65    /**
66     * Get the thread singelton instance
67     *
68     * @return a DOMFactory instance per thread
69     */

70    public static DOMFactory instance() {
71       DOMFactory fact = (DOMFactory) singlePerThread.get();
72
73       if (fact == null) {
74          try {
75             fact = (DOMFactory) ReflectionUtil.newInstance(factoryClass);
76          } catch (Exception JavaDoc e) {
77             logCat.error("instance", e);
78          }
79
80          if (fact == null) {
81             fact = new DOMFactoryXALANImpl();
82          }
83
84          singlePerThread.set(fact);
85       }
86
87       return fact;
88    }
89
90
91    /**
92     * Creates a string representation of the given DOMDocument
93     *
94     * @param doc The document to transform
95     *
96     * @return string representation
97     */

98    public abstract String JavaDoc DOM2String(Document JavaDoc doc);
99
100
101    /**
102     * Creates an new DOMDocument from the given string
103     *
104     * @param data the string to parse
105     *
106     * @return The new DOMDocument
107     */

108    public abstract Document JavaDoc String2DOM(String JavaDoc data);
109
110
111    /**
112     * Creates a new empty DOMDocument
113     *
114     * @return An empty DOMDocument
115     */

116    public abstract Document JavaDoc newDOMDocument();
117
118
119    /**
120     * Reads a DOMDocument from given InputStream
121     *
122     * @param in The InputStream to read
123     *
124     * @return The new parsed DOMDocument
125     */

126    public abstract Document JavaDoc read(InputStream JavaDoc in);
127
128
129    /**
130     * Reads a DOMDocument from given url
131     *
132     * @param url the url to read from
133     *
134     * @return The new parsed DOMDocument
135     */

136    public Document JavaDoc read(String JavaDoc url) {
137       Document JavaDoc doc = null;
138
139       if (!Util.isNull(url)) {
140          InputStream JavaDoc in = null;
141          String JavaDoc path = null;
142          URL JavaDoc u = null;
143
144          try {
145             u = new URL JavaDoc(url);
146             path = u.getPath();
147          } catch (Exception JavaDoc e) {
148             path = url;
149          }
150
151          if (u != null) {
152             try {
153                // Try to parse via URL connection
154
URLConnection JavaDoc con = u.openConnection();
155                con.connect();
156                in = con.getInputStream();
157             } catch (Exception JavaDoc e) {
158                logCat.error("read", e);
159             }
160          }
161
162          if (in == null) {
163             try {
164                in = new FileInputStream JavaDoc(path);
165             } catch (Exception JavaDoc e) {
166                logCat.error("read", e);
167             }
168          }
169
170          if (in != null) {
171             doc = read(in);
172
173             try {
174                in.close();
175             } catch (Exception JavaDoc e) {
176                logCat.error("read", e);
177             }
178          }
179       }
180
181       return doc;
182    }
183
184
185    /**
186     * Writes a DOMElement into an OutputStream
187     *
188     * @param out OutputStream to write into
189     * @param root root element to start writing
190     */

191    public abstract void write(OutputStream JavaDoc out,
192                               Element JavaDoc root) throws IOException JavaDoc;
193
194
195    /**
196     * Writes a DOMDocument into an OutputStream
197     *
198     * @param out OutputStream to write into
199     * @param doc doc to write
200     */

201    public void write(OutputStream JavaDoc out,
202                      Document JavaDoc doc) throws IOException JavaDoc {
203       write(out, doc.getDocumentElement());
204    }
205
206
207    /**
208     * Writes an DOMDocument into a file
209     *
210     * @param url The url to write to
211     * @param doc The document to write
212     */

213    public final void write(String JavaDoc url,
214                            Document JavaDoc doc) throws IOException JavaDoc {
215       write(url, doc.getDocumentElement());
216    }
217
218
219    /**
220     * Writes an DOMElement into a file
221     *
222     * @param url The url to write to
223     * @param root root element to start writing
224     */

225    public final void write(String JavaDoc url,
226                            Element JavaDoc root) throws IOException JavaDoc {
227       if (!Util.isNull(url) && (root != null)) {
228          OutputStream JavaDoc out = null;
229
230          try {
231             URL JavaDoc u = new URL JavaDoc(url);
232             URLConnection JavaDoc con = u.openConnection();
233             con.connect();
234             out = con.getOutputStream();
235          } catch (Exception JavaDoc e) {
236             logCat.error("write", e);
237          }
238
239          if (out == null) {
240             try {
241                out = new FileOutputStream JavaDoc(url);
242             } catch (Exception JavaDoc e) {
243                logCat.error("write", e);
244             }
245          }
246
247          if (out != null) {
248             write(out, root);
249             out.close();
250          } else {
251             throw new IOException JavaDoc("no target found to wich we can write");
252          }
253       }
254    }
255
256
257    /**
258     * returns an new created XPathEvaluator
259     *
260     * @return the new XPathEvaluator
261     */

262    public abstract XPathEvaluator newXPathEvaluator();
263
264
265    /**
266     * DOCUMENT ME!
267     *
268     * @param string
269     */

270    public static void setFactoryClass(String JavaDoc string) {
271       factoryClass = string;
272    }
273 }
274
Popular Tags