1 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 ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.xpath.XPathEvaluator; 35 36 import java.io.FileInputStream ; 37 import java.io.FileOutputStream ; 38 import java.io.IOException ; 39 import java.io.InputStream ; 40 import java.io.OutputStream ; 41 42 import java.net.URL ; 43 import java.net.URLConnection ; 44 45 46 47 53 public abstract class DOMFactory { 54 private static final ThreadLocal singlePerThread = new ThreadLocal (); 55 private static Log logCat = LogFactory.getLog(DOMFactory.class 56 .getName()); 57 private static String factoryClass = "org.dbforms.dom.DOMFactoryXALANImpl"; 58 59 62 protected DOMFactory() { 63 } 64 65 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 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 98 public abstract String DOM2String(Document doc); 99 100 101 108 public abstract Document String2DOM(String data); 109 110 111 116 public abstract Document newDOMDocument(); 117 118 119 126 public abstract Document read(InputStream in); 127 128 129 136 public Document read(String url) { 137 Document doc = null; 138 139 if (!Util.isNull(url)) { 140 InputStream in = null; 141 String path = null; 142 URL u = null; 143 144 try { 145 u = new URL (url); 146 path = u.getPath(); 147 } catch (Exception e) { 148 path = url; 149 } 150 151 if (u != null) { 152 try { 153 URLConnection con = u.openConnection(); 155 con.connect(); 156 in = con.getInputStream(); 157 } catch (Exception e) { 158 logCat.error("read", e); 159 } 160 } 161 162 if (in == null) { 163 try { 164 in = new FileInputStream (path); 165 } catch (Exception 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 e) { 176 logCat.error("read", e); 177 } 178 } 179 } 180 181 return doc; 182 } 183 184 185 191 public abstract void write(OutputStream out, 192 Element root) throws IOException ; 193 194 195 201 public void write(OutputStream out, 202 Document doc) throws IOException { 203 write(out, doc.getDocumentElement()); 204 } 205 206 207 213 public final void write(String url, 214 Document doc) throws IOException { 215 write(url, doc.getDocumentElement()); 216 } 217 218 219 225 public final void write(String url, 226 Element root) throws IOException { 227 if (!Util.isNull(url) && (root != null)) { 228 OutputStream out = null; 229 230 try { 231 URL u = new URL (url); 232 URLConnection con = u.openConnection(); 233 con.connect(); 234 out = con.getOutputStream(); 235 } catch (Exception e) { 236 logCat.error("write", e); 237 } 238 239 if (out == null) { 240 try { 241 out = new FileOutputStream (url); 242 } catch (Exception 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 ("no target found to wich we can write"); 252 } 253 } 254 } 255 256 257 262 public abstract XPathEvaluator newXPathEvaluator(); 263 264 265 270 public static void setFactoryClass(String string) { 271 factoryClass = string; 272 } 273 } 274 | Popular Tags |