Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

DocumentUtilities.java

Go to the documentation of this file.
00001 /*
00002  * DocumentUtilities.java
00003  *
00004  * Copyright (C) 2005 Project SQUID, http://www.cs.helsinki.fi/group/squid/
00005  *
00006  * This file is part of Ikayaki.
00007  *
00008  * Ikayaki is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Ikayaki is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Ikayaki; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 }

Generated on Fri May 6 16:00:33 2005 for Squid by  doxygen 1.4.1