KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > config > XmlConfig


1 package org.columba.core.config;
2
3 import java.io.BufferedInputStream JavaDoc;
4 import java.io.BufferedOutputStream JavaDoc;
5 import java.io.File JavaDoc;
6 import java.io.FileInputStream JavaDoc;
7 import java.io.FileNotFoundException JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.OutputStream JavaDoc;
12
13 import org.columba.api.exception.StoreException;
14 import org.columba.api.shutdown.IShutdownManager;
15 import org.columba.core.io.DiskIO;
16 import org.columba.core.shutdown.ShutdownManager;
17 import org.jdom.Document;
18 import org.jdom.JDOMException;
19 import org.jdom.input.SAXBuilder;
20 import org.jdom.output.XMLOutputter;
21
22 public class XmlConfig {
23
24     private static final java.util.logging.Logger JavaDoc LOG = java.util.logging.Logger
25             .getLogger("org.columba.core.confignew");
26
27     private String JavaDoc defaultConfig;
28
29     private File JavaDoc xmlFile;
30
31     private Document document;
32     
33     /**
34      * @param xmlFile
35      * location of xml configuration file
36      * @param defaultConfig
37      * location of default configuration file in classpath (for
38      * example: "org/columba/core/config/tags"). Can be
39      * <code>null</code>.
40      */

41     public XmlConfig(final File JavaDoc xmlFile, final String JavaDoc defaultConfig) {
42         if (xmlFile == null)
43             throw new IllegalArgumentException JavaDoc("xmlFile == null");
44
45         this.defaultConfig = defaultConfig;
46         this.xmlFile = xmlFile;
47
48         // register at shutdown manager
49
// -> this will save all configuration data, when closing Columba
50
final IShutdownManager shutdownManager = ShutdownManager.getInstance();
51
52         shutdownManager.register(new Runnable JavaDoc() {
53             public void run() {
54                 try {
55                     // callback
56
transformModelToDocument(document);
57                     
58                     File JavaDoc tmpFile = File.createTempFile("tag", ".backup");
59                     
60                     BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(
61                             new FileOutputStream JavaDoc(tmpFile));
62                      
63                     saveDocument(document, out);
64                     
65                     // if everything worked out all right, we copy the tmp file to the destination file
66
DiskIO.copyFile(tmpFile, xmlFile);
67                 } catch (final Exception JavaDoc e) {
68                     LOG.severe(e.getMessage());
69                     throw new StoreException(
70                             "could not store configuration file at: "
71                                     + xmlFile.getAbsolutePath(), e);
72                 }
73             }
74         });
75     }
76
77     public Document load() throws StoreException {
78         if (!xmlFile.exists()) {
79             if (defaultConfig != null) {
80                 // create initial version from default configuration file
81
try {
82                     DiskIO.copyResource(defaultConfig + ".xml", xmlFile);
83                 } catch (IOException JavaDoc e) {
84                     LOG.severe(e.getMessage());
85                     throw new StoreException(
86                             "could not create initial configuration file at: "
87                                     + xmlFile.getAbsolutePath(), e);
88                 }
89             } else {
90                 // return plain document
91
document = new Document();
92                 return document;
93             }
94
95         }
96
97         // load xml configuration from file
98
BufferedInputStream JavaDoc buf;
99         try {
100             buf = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(xmlFile));
101         } catch (FileNotFoundException JavaDoc e) {
102             LOG.severe(e.getMessage());
103             throw new StoreException("could not load configuration file at: "
104                     + xmlFile.getAbsolutePath(), e);
105         }
106
107         document = retrieveDocument(buf);
108
109         return document;
110     }
111
112     /**
113      * retrieve JDom Document from inputstream
114      */

115     private Document retrieveDocument(InputStream JavaDoc is) throws StoreException {
116         SAXBuilder builder = new SAXBuilder();
117         builder.setIgnoringElementContentWhitespace(true);
118         Document doc = null;
119         try {
120             doc = builder.build(is);
121         } catch (JDOMException e) {
122             LOG.severe(e.getMessage());
123             throw new StoreException("could not parse configuration file at: "
124                     + xmlFile.getAbsolutePath(), e);
125         } catch (IOException JavaDoc e) {
126             LOG.severe(e.getMessage());
127             throw new StoreException("could not parse configuration file at: "
128                     + xmlFile.getAbsolutePath(), e);
129         }
130         return doc;
131     }
132
133     /**
134      * store JDom document in outputstream
135      */

136     public void saveDocument(Document doc, OutputStream JavaDoc os)
137             throws StoreException {
138         try {
139             XMLOutputter outp = new XMLOutputter(" ", true);
140
141             outp.output(doc, os);
142         } catch (IOException JavaDoc e) {
143             LOG.severe(e.getMessage());
144             throw new StoreException("could not store configuration file at: "
145                     + xmlFile.getAbsolutePath(), e);
146         }
147     }
148     
149     
150     /**
151      * Overwrite method in case you transformed the xml presentation to your internal
152      * domain model. Before saving the xml document this method is used to transform
153      * the domain model back to the xml presentation.
154      *
155      * @param document jdom xml document
156      */

157     protected void transformModelToDocument(Document document) {}
158 }
159
Popular Tags