1 package com.explosion.utilities.preferences.impl.xml;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }