KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/dom/DOMFactoryXALANImpl.java,v 1.5 2005/02/19 21:26:29 hkollmann Exp $
3  * $Revision: 1.5 $
4  * $Date: 2005/02/19 21:26:29 $
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.apache.xpath.domapi.XPathEvaluatorImpl;
30
31 import org.dbforms.util.Util;
32
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.xpath.XPathEvaluator;
36
37 import org.xml.sax.InputSource JavaDoc;
38
39 import java.io.InputStream JavaDoc;
40 import java.io.OutputStream JavaDoc;
41 import java.io.StringReader JavaDoc;
42 import java.io.StringWriter JavaDoc;
43
44 import javax.xml.parsers.DocumentBuilder JavaDoc;
45 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
46 import javax.xml.transform.Transformer JavaDoc;
47 import javax.xml.transform.TransformerFactory JavaDoc;
48 import javax.xml.transform.dom.DOMSource JavaDoc;
49 import javax.xml.transform.stream.StreamResult JavaDoc;
50
51
52
53 /**
54  * special implementation of the DOMFactory for xerces/xalan implementation,
55  * This is the default class used by DOMFactory!
56  *
57  * @author Henner Kollmann
58  */

59 public class DOMFactoryXALANImpl extends DOMFactory {
60    private DocumentBuilder JavaDoc builder = createDOMBuilder();
61    private Log logCat = LogFactory.getLog(this.getClass().getName());
62    private Transformer JavaDoc transformer = createDOMWriter();
63
64    /**
65     * Creates a string representation of the given DOMDocument
66     *
67     * @param doc The document to transform
68     *
69     * @return string representation
70     */

71    public String JavaDoc DOM2String(Document JavaDoc doc) {
72       StringWriter JavaDoc writer = new StringWriter JavaDoc();
73
74       try {
75          StreamResult JavaDoc result = new StreamResult JavaDoc(writer);
76          DOMSource JavaDoc source = new DOMSource JavaDoc(doc);
77          transformer.transform(source, result);
78       } catch (Exception JavaDoc e) {
79          logCat.error("write", e);
80       }
81
82       String JavaDoc s = writer.toString();
83
84       return s;
85    }
86
87
88    /**
89     * Creates an new DOMDocument from the given string
90     *
91     * @param data the string to parse
92     *
93     * @return The new DOMDocument
94     */

95    public Document JavaDoc String2DOM(String JavaDoc data) {
96       Document JavaDoc doc = null;
97
98       if (!Util.isNull(data)) {
99          // String parsen
100
try {
101             // String parsen
102
InputSource JavaDoc in = new InputSource JavaDoc(new StringReader JavaDoc(data));
103             doc = builder.parse(in);
104          } catch (Exception JavaDoc e) {
105             logCat.error(e.getMessage() + "\n" + data);
106          }
107       }
108
109       return doc;
110    }
111
112
113    /**
114     * Creates a new empty DOMDocument
115     *
116     * @return An empty DOMDocument
117     */

118    public Document JavaDoc newDOMDocument() {
119       return builder.newDocument();
120    }
121
122
123    /**
124     * returns an new created XPathEvaluator
125     *
126     * @return the new XPathEvaluator
127     */

128    public XPathEvaluator newXPathEvaluator() {
129       return new XPathEvaluatorImpl();
130    }
131
132
133    /**
134     * Reads a DOMDocument from given url
135     *
136     * @param in the url to read from
137     *
138     * @return The new parsed DOMDocument
139     */

140    public Document JavaDoc read(InputStream JavaDoc in) {
141       Document JavaDoc doc = null;
142
143       try {
144          InputSource JavaDoc src = new InputSource JavaDoc(in);
145          doc = builder.parse(src);
146       } catch (Exception JavaDoc e) {
147          logCat.error(e);
148       }
149
150       return doc;
151    }
152
153
154    /**
155     * Writes a DOMDocument into an OutputStream
156     *
157     * @param out OutputStream to write into
158     * @param root The Ddcument to write
159     */

160    public void write(OutputStream JavaDoc out,
161                      Element JavaDoc root) {
162       try {
163          StreamResult JavaDoc result = new StreamResult JavaDoc(out);
164          DOMSource JavaDoc source = new DOMSource JavaDoc(root);
165          transformer.transform(source, result);
166       } catch (Exception JavaDoc e) {
167          logCat.error("write", e);
168       }
169    }
170
171
172    private DocumentBuilder JavaDoc createDOMBuilder() {
173       DocumentBuilder JavaDoc res = null;
174
175       try {
176          // Init DOM
177
DocumentBuilderFactory JavaDoc dfactory = DocumentBuilderFactory.newInstance();
178          dfactory.setValidating(false);
179          dfactory.setNamespaceAware(false);
180          res = dfactory.newDocumentBuilder();
181       } catch (Exception JavaDoc e) {
182          logCat.error(e);
183       }
184
185       return res;
186    }
187
188
189    private Transformer JavaDoc createDOMWriter() {
190       Transformer JavaDoc res = null;
191
192       try {
193          TransformerFactory JavaDoc transFactory = TransformerFactory.newInstance();
194          res = transFactory.newTransformer();
195       } catch (Exception JavaDoc e) {
196          logCat.error("createDOMWriter", e);
197       }
198
199       return res;
200    }
201 }
202
Popular Tags