View Javadoc

1   package com.explosion.utilities.preferences.impl.xml;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.IOException;
26  
27  import javax.xml.parsers.DocumentBuilder;
28  import javax.xml.parsers.DocumentBuilderFactory;
29  import javax.xml.parsers.ParserConfigurationException;
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.TransformerFactoryConfigurationError;
35  import javax.xml.transform.dom.DOMSource;
36  import javax.xml.transform.stream.StreamResult;
37  
38  import org.w3c.dom.Document;
39  import org.w3c.dom.Node;
40  import org.w3c.dom.NodeList;
41  import org.xml.sax.SAXException;
42  
43  import com.explosion.utilities.FileSystemUtils;
44  
45  /***
46   * @author Stephen Cowx Date created:@22-Apr-2003
47   */
48  public class XMLPreferenceBackingStore
49  {
50  
51      private Document document = null;
52  
53      private File file;
54  
55      public XMLPreferenceBackingStore(String filename) throws Exception
56      {
57          this.file = FileSystemUtils.checkGivenPathValid(filename);
58          load();
59      }
60  
61      public XMLPreferenceBackingStore() throws Exception
62      {
63          load();
64      }
65  
66      private void load() throws IOException, ParserConfigurationException, SAXException
67      {
68          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
69          factory.setValidating(true);
70          factory.setNamespaceAware(true);
71  
72          DocumentBuilder builder = factory.newDocumentBuilder();
73  
74          if (file != null && file.exists())
75              document = builder.parse(new FileInputStream(file));
76          else
77              document = builder.newDocument();
78      }
79  
80      public void save()
81      {
82          try
83          {
84              DOMSource source = new DOMSource(document);
85              StreamResult result = new StreamResult(file);
86  
87              TransformerFactory factory = TransformerFactory.newInstance();
88              Transformer domTransformer = factory.newTransformer();
89              domTransformer.transform(source, result);
90          } catch (TransformerConfigurationException e)
91          {
92              com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
93          } catch (TransformerFactoryConfigurationError e)
94          {
95              com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
96          } catch (TransformerException e)
97          {
98              com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
99          }
100     }
101 
102     /***
103      * Returns the document.
104      * 
105      * @return Document
106      */
107     public Document getDocument()
108     {
109         return document;
110     }
111 
112     /***
113      * Sets the document.
114      * 
115      * @param document The document to set
116      */
117     public void setDocument(Document document)
118     {
119         this.document = document;
120     }
121 
122     /***
123      * Returns the file.
124      * 
125      * @return File
126      */
127     public File getFile()
128     {
129         return file;
130     }
131 
132     /***
133      * Sets the file.
134      * 
135      * @param file The file to set
136      */
137     public void setFile(File file)
138     {
139         this.file = file;
140     }
141 
142     public static Node getChildNamed(Node node, String childName)
143     {
144         NodeList list = node.getChildNodes();
145         for (int i = 0; i < list.getLength(); i++)
146         {
147             Node child = list.item(i);
148             if (child.getNodeName().equalsIgnoreCase(childName)) return child;
149         }
150 
151         return null;
152     }
153 
154 }