View Javadoc

1   /*
2    * Copyright (c) 1998-2004 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.experiment.gui.views.helpers;
20  
21  import java.io.File;
22  import java.io.FileNotFoundException;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  
26  import javax.xml.parsers.DocumentBuilder;
27  import javax.xml.parsers.DocumentBuilderFactory;
28  import javax.xml.parsers.ParserConfigurationException;
29  import javax.xml.transform.OutputKeys;
30  import javax.xml.transform.Transformer;
31  import javax.xml.transform.TransformerConfigurationException;
32  import javax.xml.transform.TransformerException;
33  import javax.xml.transform.TransformerFactory;
34  import javax.xml.transform.dom.DOMSource;
35  import javax.xml.transform.stream.StreamResult;
36  
37  import org.w3c.dom.DOMImplementation;
38  import org.w3c.dom.Document;
39  import org.w3c.dom.Element;
40  
41  /**
42   * @author Bjarte Svaeren
43   */
44  public class XMLcreator {
45    
46    ////////////////////////////////////////////////////////////////////////////////////////////
47    // Data fields
48    ////////////////////////////////////////////////////////////////////////////////////////////
49    
50    private Document document;
51    private File     xmlFile;
52    private String   dtdName;
53    private String   encoding;
54  
55  
56    ////////////////////////////////////////////////////////////////////////////////////////////
57    // Constructor
58    ////////////////////////////////////////////////////////////////////////////////////////////
59  
60    /**
61     * 
62     */
63    public XMLcreator(File file, String type, String version, String dtd, String encoding) {
64      xmlFile       = file;
65      dtdName       = dtd;
66      this.encoding = encoding;
67       
68      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
69      try {
70        DocumentBuilder builder = factory.newDocumentBuilder();
71        DOMImplementation impl  = builder.getDOMImplementation();
72  
73        document = impl.createDocument(null, type, null);
74  
75        Element rootElement = document.getDocumentElement();
76        rootElement.setAttribute("version", version);
77      } catch (ParserConfigurationException e) {
78        e.printStackTrace();
79      }     
80    }
81    
82    
83    ////////////////////////////////////////////////////////////////////////////////////////////
84    // Public methods
85    ////////////////////////////////////////////////////////////////////////////////////////////
86  
87    /**
88     * 
89     */
90    public Document getDocument() {
91      return document;
92    }
93    
94    
95    public void append(Document document)
96    {
97    }
98  
99  
100   public void save()
101   {
102     FileOutputStream toFile = null;
103     try {
104       toFile = new FileOutputStream(xmlFile);
105     } catch (FileNotFoundException e) {
106       e.printStackTrace();
107     }
108     
109     DOMSource    source = new DOMSource(document);
110     StreamResult result = new StreamResult(toFile);
111     TransformerFactory transFactory = TransformerFactory.newInstance();
112 
113     try {
114       Transformer transformer = transFactory.newTransformer();
115       transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dtdName);
116       transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
117       transformer.setOutputProperty(OutputKeys.INDENT, "yes");
118       transformer.transform(source, result);
119     } catch (TransformerConfigurationException e1) {
120       e1.printStackTrace();
121     } catch (TransformerException e) {
122       e.printStackTrace();
123     }
124     
125     try {
126       toFile.close();
127     } catch (IOException e2) {
128       e2.printStackTrace();
129     }
130   }
131 }