View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/dom/AbstractDOMFactoryImpl.java,v 1.1 2005/12/02 12:35:47 hkollmann Exp $
3    * $Revision: 1.1 $
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.dbforms.interfaces.IDOMFactory;
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 java.io.FileInputStream;
38  import java.io.FileOutputStream;
39  import java.io.IOException;
40  import java.io.InputStream;
41  import java.io.OutputStream;
42  
43  import java.net.URL;
44  import java.net.URLConnection;
45  
46  /***
47   * abstract class to hide the implemtation details of the various dom
48   * implementations.
49   * 
50   * @author Henner Kollmann
51   */
52  public abstract class AbstractDOMFactoryImpl implements IDOMFactory {
53  
54  	private Log logCat = LogFactory.getLog(this.getClass().getName());
55  
56  	/***
57  	 * Creates a string representation of the given DOMDocument
58  	 * 
59  	 * @param doc
60  	 *            The document to transform
61  	 * 
62  	 * @return string representation
63  	 */
64  	public abstract String DOM2String(Document doc);
65  
66  	/***
67  	 * Creates an new DOMDocument from the given string
68  	 * 
69  	 * @param data
70  	 *            the string to parse
71  	 * 
72  	 * @return The new DOMDocument
73  	 */
74  	public abstract Document String2DOM(String data);
75  
76  	/***
77  	 * Creates a new empty DOMDocument
78  	 * 
79  	 * @return An empty DOMDocument
80  	 */
81  	public abstract Document newDOMDocument();
82  
83  	/***
84  	 * Reads a DOMDocument from given InputStream
85  	 * 
86  	 * @param in
87  	 *            The InputStream to read
88  	 * 
89  	 * @return The new parsed DOMDocument
90  	 */
91  	public abstract Document read(InputStream in);
92  
93  	/***
94  	 * Reads a DOMDocument from given url
95  	 * 
96  	 * @param url
97  	 *            the url to read from
98  	 * 
99  	 * @return The new parsed DOMDocument
100 	 */
101 	public Document read(String url) {
102 		Document doc = null;
103 
104 		if (!Util.isNull(url)) {
105 			InputStream in = null;
106 			String path = null;
107 			URL u = null;
108 
109 			try {
110 				u = new URL(url);
111 				path = u.getPath();
112 			} catch (Exception e) {
113 				path = url;
114 			}
115 
116 			if (u != null) {
117 				try {
118 					// Try to parse via URL connection
119 					URLConnection con = u.openConnection();
120 					con.connect();
121 					in = con.getInputStream();
122 				} catch (Exception e) {
123 					logCat.error("read", e);
124 				}
125 			}
126 
127 			if (in == null) {
128 				try {
129 					in = new FileInputStream(path);
130 				} catch (Exception e) {
131 					logCat.error("read", e);
132 				}
133 			}
134 
135 			if (in != null) {
136 				doc = read(in);
137 
138 				try {
139 					in.close();
140 				} catch (Exception e) {
141 					logCat.error("read", e);
142 				}
143 			}
144 		}
145 
146 		return doc;
147 	}
148 
149 	/***
150 	 * Writes a DOMElement into an OutputStream
151 	 * 
152 	 * @param out
153 	 *            OutputStream to write into
154 	 * @param root
155 	 *            root element to start writing
156 	 */
157 	public abstract void write(OutputStream out, Element root)
158 			throws IOException;
159 
160 	/***
161 	 * Writes a DOMDocument into an OutputStream
162 	 * 
163 	 * @param out
164 	 *            OutputStream to write into
165 	 * @param doc
166 	 *            doc to write
167 	 */
168 	public final void write(OutputStream out, Document doc) throws IOException {
169 		write(out, doc.getDocumentElement());
170 	}
171 
172 	/***
173 	 * Writes an DOMDocument into a file
174 	 * 
175 	 * @param url
176 	 *            The url to write to
177 	 * @param doc
178 	 *            The document to write
179 	 */
180 	public final void write(String url, Document doc) throws IOException {
181 		write(url, doc.getDocumentElement());
182 	}
183 
184 	/***
185 	 * Writes an DOMElement into a file
186 	 * 
187 	 * @param url
188 	 *            The url to write to
189 	 * @param root
190 	 *            root element to start writing
191 	 */
192 	public final void write(String url, Element root) throws IOException {
193 		if (!Util.isNull(url) && (root != null)) {
194 			OutputStream out = null;
195 
196 			try {
197 				URL u = new URL(url);
198 				URLConnection con = u.openConnection();
199 				con.connect();
200 				out = con.getOutputStream();
201 			} catch (Exception e) {
202 				logCat.error("write", e);
203 			}
204 
205 			if (out == null) {
206 				try {
207 					out = new FileOutputStream(url);
208 				} catch (Exception e) {
209 					logCat.error("write", e);
210 				}
211 			}
212 
213 			if (out != null) {
214 				write(out, root);
215 				out.close();
216 			} else {
217 				throw new IOException("no target found to wich we can write");
218 			}
219 		}
220 	}
221 
222 	/***
223 	 * returns an new created XPathEvaluator
224 	 * 
225 	 * @return the new XPathEvaluator
226 	 */
227 	public abstract XPathEvaluator newXPathEvaluator();
228 
229 }