View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/dom/DOMFactoryXALANImpl.java,v 1.7 2005/12/02 12:35:47 hkollmann Exp $
3    * $Revision: 1.7 $
4    * $Date: 2005/12/02 12:35:47 $
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;
34  import org.w3c.dom.Element;
35  import org.w3c.dom.xpath.XPathEvaluator;
36  
37  import org.xml.sax.InputSource;
38  
39  import java.io.InputStream;
40  import java.io.OutputStream;
41  import java.io.StringReader;
42  import java.io.StringWriter;
43  
44  import javax.xml.parsers.DocumentBuilder;
45  import javax.xml.parsers.DocumentBuilderFactory;
46  import javax.xml.transform.Transformer;
47  import javax.xml.transform.TransformerFactory;
48  import javax.xml.transform.dom.DOMSource;
49  import javax.xml.transform.stream.StreamResult;
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 AbstractDOMFactoryImpl
60            {
61     private DocumentBuilder builder     = createDOMBuilder();
62     private Log             logCat      = LogFactory.getLog(this.getClass().getName());
63     private Transformer     transformer = createDOMWriter();
64  
65     /***
66      * Creates a string representation of the given DOMDocument
67      *
68      * @param doc The document to transform
69      *
70      * @return string representation
71      */
72     public String DOM2String(Document doc) {
73        StringWriter writer = new StringWriter();
74  
75        try {
76           StreamResult result = new StreamResult(writer);
77           DOMSource    source = new DOMSource(doc);
78           transformer.transform(source, result);
79        } catch (Exception e) {
80           logCat.error("write", e);
81        }
82  
83        String s = writer.toString();
84  
85        return s;
86     }
87  
88  
89     /***
90      * Creates an new DOMDocument from the given string
91      *
92      * @param data the string to parse
93      *
94      * @return The new DOMDocument
95      */
96     public Document String2DOM(String data) {
97        Document doc = null;
98  
99        if (!Util.isNull(data)) {
100          // String parsen
101          try {
102             // String parsen
103             InputSource in = new InputSource(new StringReader(data));
104             doc = builder.parse(in);
105          } catch (Exception e) {
106             logCat.error(e.getMessage() + "\n" + data);
107          }
108       }
109 
110       return doc;
111    }
112 
113 
114    /***
115     * Creates a new empty DOMDocument
116     *
117     * @return An empty DOMDocument
118     */
119    public Document newDOMDocument() {
120       return builder.newDocument();
121    }
122 
123 
124    /***
125     * returns an new created XPathEvaluator
126     *
127     * @return the new XPathEvaluator
128     */
129    public XPathEvaluator newXPathEvaluator() {
130       return new XPathEvaluatorImpl();
131    }
132 
133 
134    /***
135     * Reads a DOMDocument from given url
136     *
137     * @param in the url to read from
138     *
139     * @return The new parsed DOMDocument
140     */
141    public Document read(InputStream in) {
142       Document doc = null;
143 
144       try {
145          InputSource src = new InputSource(in);
146          doc = builder.parse(src);
147       } catch (Exception e) {
148          logCat.error(e);
149       }
150 
151       return doc;
152    }
153 
154 
155    /***
156     * Writes a DOMDocument into an OutputStream
157     *
158     * @param out OutputStream to write into
159     * @param root The Ddcument to write
160     */
161    public void write(OutputStream out,
162                      Element      root) {
163       try {
164          StreamResult result = new StreamResult(out);
165          DOMSource    source = new DOMSource(root);
166          transformer.transform(source, result);
167       } catch (Exception e) {
168          logCat.error("write", e);
169       }
170    }
171 
172 
173    private DocumentBuilder createDOMBuilder() {
174       DocumentBuilder res = null;
175 
176       try {
177          // Init DOM
178          DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
179          dfactory.setValidating(false);
180          dfactory.setNamespaceAware(false);
181          res = dfactory.newDocumentBuilder();
182       } catch (Exception e) {
183          logCat.error(e);
184       }
185 
186       return res;
187    }
188 
189 
190    private Transformer createDOMWriter() {
191       Transformer res = null;
192 
193       try {
194          TransformerFactory transFactory = TransformerFactory.newInstance();
195          res = transFactory.newTransformer();
196       } catch (Exception e) {
197          logCat.error("createDOMWriter", e);
198       }
199 
200       return res;
201    }
202 }