001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.configuration;
020:
021: import java.io.StringReader;
022: import java.net.URI;
023: import java.net.URISyntaxException;
024: import java.util.ArrayList;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: import javax.xml.parsers.DocumentBuilderFactory;
030:
031: import org.openharmonise.commons.xml.*;
032: import org.openharmonise.localfilesystem.*;
033: import org.openharmonise.vfs.*;
034: import org.openharmonise.vfs.context.*;
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Element;
037: import org.w3c.dom.Node;
038: import org.w3c.dom.NodeList;
039: import org.w3c.dom.Text;
040:
041: /**
042: * Store for all configuration options. Persists to XML virtual file.
043: *
044: * @author Matthew Large
045: * @version $Revision: 1.1 $
046: *
047: */
048: public class ConfigStore {
049:
050: /**
051: * Instance of store, following singleton pattern.
052: */
053: private static ConfigStore m_instance = null;
054:
055: /**
056: * Virtual file system to persist to.
057: */
058: private AbstractVirtualFileSystem m_vfs = null;
059:
060: /**
061: * Virtual file system uri for Windows.
062: */
063: private static String URI_WINDOWS = "file:/C:/ContentManager/";
064:
065: /**
066: * Virtual file system uri for MacOS X.
067: */
068: private static String URI_MAC = "???";
069:
070: /**
071: * Virtual file system uri for Linux.
072: */
073: private static String URI_LINUX = "???";
074:
075: /**
076: * Virtual file system uri.
077: */
078: private static String URI = "???";
079:
080: /**
081: * Full path to virtual file for persisted data.
082: */
083: private String m_sConfigFileURI = "/ContentManager/config.xml";
084:
085: /**
086: * Configuration XML.
087: */
088: private Document m_xml = null;
089:
090: /**
091: * Map of property name to value
092: */
093: private HashMap m_properties = new HashMap(11);
094:
095: /**
096: *
097: */
098: private ConfigStore() {
099: super ();
100: this .setup();
101: }
102:
103: /**
104: * Returns the instance of the configuration store, following the
105: * singleton pattern.
106: *
107: * @return Instance of configuration store
108: */
109: public static ConfigStore getInstance() {
110: if (m_instance == null) {
111: m_instance = new ConfigStore();
112: }
113:
114: return m_instance;
115: }
116:
117: /**
118: * Configures the config store.
119: *
120: */
121: private void setup() {
122: try {
123: URI = ConfigStore.URI_WINDOWS;
124:
125: m_vfs = new LocalFileSystem(new URI(URI));
126:
127: this .populate();
128: } catch (URISyntaxException e) {
129: e.printStackTrace();
130: }
131: }
132:
133: /**
134: * Returns the virtual file system that Content Manager
135: * is to use for local persistence.
136: *
137: * @return Virtual file system
138: */
139: public AbstractVirtualFileSystem getApplicationFileSystem() {
140: return this .m_vfs;
141: }
142:
143: /**
144: * Returns a list of all the property names.
145: *
146: * @return List of property names
147: */
148: public List getPropertyNames() {
149: ArrayList aRetn = new ArrayList(m_properties.keySet());
150: return aRetn;
151: }
152:
153: /**
154: * Populates the config store from persisted data.
155: *
156: */
157: private void populate() {
158: this .getXML();
159: Element root = m_xml.getDocumentElement();
160:
161: }
162:
163: /**
164: * Sets list of values for a given property.
165: *
166: * @param sPropName Property name
167: * @param vVals List of values
168: */
169: public void setProperty(String sPropName, ArrayList vVals) {
170: if (m_properties.containsKey(sPropName)) {
171: m_properties.remove(sPropName);
172: }
173: m_properties.put(sPropName, vVals);
174: ContextEvent ce = new ContextEvent(
175: ContextType.CONTEXT_SYSTEM_PROP_CHANGED, sPropName);
176: ContextHandler.getInstance().fireContextEvent(ce);
177: }
178:
179: /**
180: * Sets a value for a given property.
181: *
182: * @param sPropName Property name
183: * @param sValue Value
184: */
185: public void setProperty(String sPropName, String sValue) {
186: ArrayList aVals = new ArrayList(1);
187: aVals.add(sValue);
188: this .setProperty(sPropName, aVals);
189: ContextEvent ce = new ContextEvent(
190: ContextType.CONTEXT_SYSTEM_PROP_CHANGED, sPropName);
191: ContextHandler.getInstance().fireContextEvent(ce);
192: }
193:
194: /**
195: * Returns a list of values for a given property.
196: *
197: * @param sPropName Property name
198: * @return List of values
199: */
200: public ArrayList getPropertyVals(String sPropName) {
201: if (m_properties.containsKey(sPropName)) {
202: return (ArrayList) ((ArrayList) m_properties.get(sPropName))
203: .clone();
204: } else {
205: return null;
206: }
207: }
208:
209: /**
210: * Returns a value for a given property. If the property has more than
211: * one value only the first is returned.
212: *
213: * @param sPropName Property name
214: * @return Value
215: */
216: public String getPropertyValue(String sPropName) {
217: if (m_properties.containsKey(sPropName)) {
218: return (String) ((ArrayList) m_properties.get(sPropName))
219: .get(0);
220: } else {
221: return null;
222: }
223: }
224:
225: /**
226: * Persists all data to the application virtual file system.
227: *
228: */
229: public void save() {
230: try {
231: m_xml = DocumentBuilderFactory.newInstance()
232: .newDocumentBuilder().newDocument();
233:
234: Element rootEl = m_xml.createElement("Project");
235: m_xml.appendChild(rootEl);
236:
237: Iterator itor = m_properties.keySet().iterator();
238: while (itor.hasNext()) {
239: String sPropName = (String) itor.next();
240: ArrayList vVals = (ArrayList) m_properties
241: .get(sPropName);
242:
243: if (vVals.size() > 0) {
244: Element propEl = m_xml.createElement("Property");
245: propEl.setAttribute("name", sPropName);
246: rootEl.appendChild(propEl);
247:
248: ArrayList vVals2 = (ArrayList) vVals.clone();
249: for (int i = 0; i < vVals2.size(); i++) {
250: String sValue = (String) vVals2.get(i);
251: if (sValue != null) {
252: Element valEl = m_xml
253: .createElement("Value");
254: propEl.appendChild(valEl);
255: Text txt = m_xml.createTextNode(sValue);
256: valEl.appendChild(txt);
257: }
258: }
259: }
260: }
261:
262: XMLPrettyPrint printer = new XMLPrettyPrint();
263: printer.setNamespaceAware(true);
264:
265: VirtualFile projFile = this .m_vfs.getVirtualFile(
266: this .m_sConfigFileURI).getResource();
267: String sXML = printer.printNode(m_xml.getDocumentElement());
268: projFile.setContent(sXML.getBytes());
269:
270: this .m_vfs.synchroniseFile(projFile);
271:
272: } catch (Exception e) {
273: e.printStackTrace(System.out);
274: }
275: }
276:
277: /**
278: * Retrieves persisted data and populates the configuration store.
279: *
280: */
281: private void getXML() {
282:
283: VirtualFile projFile = null;
284: projFile = this .m_vfs.getVirtualFile(this .m_sConfigFileURI)
285: .getResource();
286:
287: if (projFile.exists()) {
288:
289: try {
290: m_xml = DocumentBuilderFactory
291: .newInstance()
292: .newDocumentBuilder()
293: .parse(
294: new org.xml.sax.InputSource(
295: new StringReader(new String(
296: projFile.getContent()))));
297:
298: Element rootEl = m_xml.getDocumentElement();
299:
300: NodeList nlProps = rootEl.getChildNodes();
301:
302: for (int i = 0; i < nlProps.getLength(); i++) {
303: Node node = nlProps.item(i);
304: if (node.getNodeType() != Node.ELEMENT_NODE) {
305: continue;
306: } else {
307: Element propEl = (Element) node;
308: String sPropName = propEl.getAttribute("name");
309: ArrayList vVals = new ArrayList();
310:
311: NodeList nlVals = propEl.getChildNodes();
312: for (int j = 0; j < nlVals.getLength(); j++) {
313: Node node2 = nlVals.item(j);
314: if (node2.getNodeType() != Node.ELEMENT_NODE) {
315: continue;
316: } else {
317: Element valEl = (Element) node2;
318: vVals.add(valEl.getFirstChild()
319: .getNodeValue());
320: }
321: }
322:
323: m_properties.put(sPropName, vVals);
324:
325: }
326: }
327:
328: } catch (Exception e) {
329: e.printStackTrace(System.out);
330: }
331: } else {
332:
333: try {
334: m_xml = DocumentBuilderFactory.newInstance()
335: .newDocumentBuilder().newDocument();
336:
337: Element rootEl = m_xml.createElement("Project");
338: m_xml.appendChild(rootEl);
339: } catch (Exception e) {
340: e.printStackTrace(System.out);
341: }
342: }
343: }
344:
345: }
|