View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2009 Ordina and committers to Mod4j
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *     Ordina - initial implementation
10   *******************************************************************************/
11  package org.mod4j.dslcommon.xml;
12  
13  import java.io.File;
14  import java.io.FileOutputStream;
15  import java.io.IOException;
16  import java.io.Reader;
17  
18  import org.jdom.Document;
19  import org.jdom.JDOMException;
20  import org.jdom.input.SAXBuilder;
21  
22  // import org.jdom.output.Format;
23  import org.jdom.output.Format;
24  import org.jdom.output.XMLOutputter;
25  
26  /**
27   * XmlUtil :
28   * 
29   * @author Jos Warmer
30   * @version $Id: XmlUtil.java,v 1.1 2006/03/01 18:16:11 jwarmer Exp $
31   */
32  public class XmlUtil {
33  
34      /**
35       * Read an Xml Document from <code>file</code>.
36       * 
37       * @param file
38       * @param ignoreWhitespace
39       *            If true, whitespace will be ignored
40       * @return
41       */
42      public static Document readXmlDocument(File file, boolean ignoreWhitespace) {
43          try {
44              SAXBuilder builder = null;
45              builder = new SAXBuilder();
46              builder.setIgnoringElementContentWhitespace(ignoreWhitespace);
47  
48              Document doc = builder.build(file);
49              return doc;
50          } catch (Exception e) {
51              e.printStackTrace();
52          }
53          return null;
54      }
55  
56      /**
57       * Read an Xml Document from <code>reader</code>.
58       * 
59       * @param reader
60       * @param ignoreWhitespace
61       *            If true, whitespace will be ignored
62       * @return
63       * @throws JDOMException
64       */
65      public static Document readXmlDocument(Reader reader, boolean ignoreWhitespace) throws JDOMException {
66          try {
67              SAXBuilder builder = null;
68              builder = new SAXBuilder();
69              builder.setIgnoringElementContentWhitespace(ignoreWhitespace);
70  
71              Document doc = builder.build(reader);
72              return doc;
73          } catch (IOException e) {
74              e.printStackTrace();
75          }
76          return null;
77      }
78  
79      public static void writeDocument(Document doc, FileOutputStream stream, boolean newLines, String indent) {
80          try {
81              XMLOutputter outputter = new XMLOutputter();
82              Format format = Format.getPrettyFormat();
83              if (indent != null) {
84                  format.setIndent(indent);
85              }
86              outputter.setFormat(format);
87              outputter.output(doc, stream);
88          } catch (IOException exc) {
89              exc.printStackTrace();
90          }
91      }
92  
93      public static void writeDocument(Document doc, String outfilename, boolean newLines, String indent) {
94          try {
95              FileOutputStream stream = new FileOutputStream(new File(outfilename));
96              writeDocument(doc, stream, newLines, indent);
97              stream.close();
98          } catch (IOException exc) {
99              exc.printStackTrace();
100         }
101     }
102 
103 }