00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 package ikayaki.util;
00024
00025 import org.w3c.dom.Document;
00026 import org.xml.sax.SAXException;
00027
00028 import javax.xml.parsers.DocumentBuilder;
00029 import javax.xml.parsers.DocumentBuilderFactory;
00030 import javax.xml.parsers.ParserConfigurationException;
00031 import javax.xml.transform.OutputKeys;
00032 import javax.xml.transform.Transformer;
00033 import javax.xml.transform.TransformerException;
00034 import javax.xml.transform.TransformerFactory;
00035 import javax.xml.transform.dom.DOMSource;
00036 import javax.xml.transform.stream.StreamResult;
00037 import java.io.*;
00038
00044 public class DocumentUtilities {
00045
00054 public static boolean storeToXML(File file, Document document) {
00055 if (file == null || document == null) {
00056 throw new NullPointerException();
00057 }
00058 try {
00059 return storeToXML(new FileOutputStream(file), document);
00060 } catch (FileNotFoundException e) {
00061 e.printStackTrace();
00062 }
00063 return false;
00064 }
00065
00074 public static boolean storeToXML(OutputStream out, Document document) {
00075 if (out == null || document == null) {
00076 throw new NullPointerException();
00077 }
00078 try {
00079 TransformerFactory tf = TransformerFactory.newInstance();
00080 tf.setAttribute("indent-number", new Integer(2));
00081
00082 Transformer t = tf.newTransformer();
00083 t.setOutputProperty(OutputKeys.INDENT, "yes");
00084
00085 DOMSource source = new DOMSource(document);
00086 StreamResult result = new StreamResult(new OutputStreamWriter(out, "utf-8"));
00087 t.transform(source, result);
00088 return true;
00089
00090 } catch (UnsupportedEncodingException e) {
00091 e.printStackTrace();
00092 } catch (TransformerException e) {
00093 e.printStackTrace();
00094 } finally {
00095 try {
00096 out.close();
00097 } catch (IOException e) {
00098 e.printStackTrace();
00099 }
00100 }
00101 return false;
00102 }
00103
00111 public static Document loadFromXML(File file) {
00112 if (file == null) {
00113 throw new NullPointerException();
00114 }
00115 try {
00116 return loadFromXML(new FileInputStream(file));
00117 } catch (FileNotFoundException e) {
00118 e.printStackTrace();
00119 }
00120 return null;
00121 }
00122
00130 public static Document loadFromXML(InputStream in) {
00131 if (in == null) {
00132 throw new NullPointerException();
00133 }
00134 try {
00135 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
00136 Document document = builder.parse(in);
00137 return document;
00138
00139 } catch (ParserConfigurationException e) {
00140 e.printStackTrace();
00141 } catch (IOException e) {
00142 e.printStackTrace();
00143 } catch (SAXException e) {
00144 e.printStackTrace();
00145 } finally {
00146 try {
00147 in.close();
00148 } catch (IOException e) {
00149 e.printStackTrace();
00150 }
00151 }
00152 return null;
00153 }
00154 }