1 19 package org.openharmonise.him.configuration; 20 21 import java.io.StringReader ; 22 import java.net.URI ; 23 import java.net.URISyntaxException ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 31 import org.openharmonise.commons.xml.*; 32 import org.openharmonise.localfilesystem.*; 33 import org.openharmonise.vfs.*; 34 import org.openharmonise.vfs.context.*; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 import org.w3c.dom.Text ; 40 41 42 49 public class ConfigStore { 50 51 54 private static ConfigStore m_instance = null; 55 56 59 private AbstractVirtualFileSystem m_vfs = null; 60 61 64 private static String URI_WINDOWS = "file:/C:/ContentManager/"; 65 66 69 private static String URI_MAC = "???"; 70 71 74 private static String URI_LINUX = "???"; 75 76 77 80 private static String URI = "???"; 81 82 85 private String m_sConfigFileURI = "/ContentManager/config.xml"; 86 87 90 private Document m_xml = null; 91 92 95 private HashMap m_properties = new HashMap (11); 96 97 100 private ConfigStore() { 101 super(); 102 this.setup(); 103 } 104 105 111 public static ConfigStore getInstance() { 112 if (m_instance == null) { 113 m_instance = new ConfigStore(); 114 } 115 116 return m_instance; 117 } 118 119 123 private void setup() { 124 try { 125 URI = ConfigStore.URI_WINDOWS; 126 127 m_vfs = new LocalFileSystem(new URI (URI)); 128 129 this.populate(); 130 } catch (URISyntaxException e) { 131 e.printStackTrace(); 132 } 133 } 134 135 141 public AbstractVirtualFileSystem getApplicationFileSystem() { 142 return this.m_vfs; 143 } 144 145 150 public List getPropertyNames() { 151 ArrayList aRetn = new ArrayList (m_properties.keySet()); 152 return aRetn; 153 } 154 155 159 private void populate() { 160 this.getXML(); 161 Element root = m_xml.getDocumentElement(); 162 163 } 164 165 171 public void setProperty(String sPropName, ArrayList vVals) { 172 if (m_properties.containsKey(sPropName)) { 173 m_properties.remove(sPropName); 174 } 175 m_properties.put(sPropName, vVals); 176 ContextEvent ce = new ContextEvent(ContextType.CONTEXT_SYSTEM_PROP_CHANGED, sPropName); 177 ContextHandler.getInstance().fireContextEvent(ce); 178 } 179 180 186 public void setProperty(String sPropName, String sValue) { 187 ArrayList aVals = new ArrayList (1); 188 aVals.add(sValue); 189 this.setProperty(sPropName, aVals); 190 ContextEvent ce = new ContextEvent(ContextType.CONTEXT_SYSTEM_PROP_CHANGED, sPropName); 191 ContextHandler.getInstance().fireContextEvent(ce); 192 } 193 194 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 216 public String getPropertyValue(String sPropName) { 217 if (m_properties.containsKey(sPropName)) { 218 return (String ) ((ArrayList ) m_properties.get(sPropName)).get(0); 219 } else { 220 return null; 221 } 222 } 223 224 228 public void save() { 229 try { 230 m_xml = 231 DocumentBuilderFactory 232 .newInstance() 233 .newDocumentBuilder() 234 .newDocument(); 235 236 Element rootEl = m_xml.createElement("Project"); 237 m_xml.appendChild(rootEl); 238 239 Iterator itor = m_properties.keySet().iterator(); 240 while (itor.hasNext()) { 241 String sPropName = (String ) itor.next(); 242 ArrayList vVals = (ArrayList ) m_properties.get(sPropName); 243 244 if (vVals.size() > 0) { 245 Element propEl = m_xml.createElement("Property"); 246 propEl.setAttribute("name", sPropName); 247 rootEl.appendChild(propEl); 248 249 ArrayList vVals2 = (ArrayList ) vVals.clone(); 250 for (int i = 0; i < vVals2.size(); i++) { 251 String sValue = (String ) vVals2.get(i); 252 if (sValue != null) { 253 Element valEl = m_xml.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(this.m_sConfigFileURI).getResource(); 266 String sXML = printer.printNode(m_xml.getDocumentElement()); 267 projFile.setContent( sXML.getBytes()); 268 269 this.m_vfs.synchroniseFile(projFile); 270 271 } catch (Exception e) { 272 e.printStackTrace(System.out); 273 } 274 } 275 276 280 private void getXML() { 281 282 VirtualFile projFile = null; 283 projFile = this.m_vfs.getVirtualFile(this.m_sConfigFileURI).getResource(); 284 285 if (projFile.exists()) { 286 287 try { 288 m_xml = 289 DocumentBuilderFactory 290 .newInstance() 291 .newDocumentBuilder() 292 .parse( 293 new org.xml.sax.InputSource (new StringReader ( new String (projFile.getContent()) ))); 294 295 Element rootEl = m_xml.getDocumentElement(); 296 297 NodeList nlProps = rootEl.getChildNodes(); 298 299 for (int i = 0; i < nlProps.getLength(); i++) { 300 Node node = nlProps.item(i); 301 if (node.getNodeType() != Node.ELEMENT_NODE) { 302 continue; 303 } else { 304 Element propEl = (Element ) node; 305 String sPropName = propEl.getAttribute("name"); 306 ArrayList vVals = new ArrayList (); 307 308 NodeList nlVals = propEl.getChildNodes(); 309 for (int j = 0; j < nlVals.getLength(); j++) { 310 Node node2 = nlVals.item(j); 311 if (node2.getNodeType() != Node.ELEMENT_NODE) { 312 continue; 313 } else { 314 Element valEl = (Element ) node2; 315 vVals.add(valEl.getFirstChild().getNodeValue()); 316 } 317 } 318 319 m_properties.put(sPropName, vVals); 320 321 } 322 } 323 324 } catch (Exception e) { 325 e.printStackTrace(System.out); 326 } 327 } else { 328 329 try { 330 m_xml = 331 DocumentBuilderFactory 332 .newInstance() 333 .newDocumentBuilder() 334 .newDocument(); 335 336 Element rootEl = m_xml.createElement("Project"); 337 m_xml.appendChild(rootEl); 338 } catch (Exception e) { 339 e.printStackTrace(System.out); 340 } 341 } 342 } 343 344 } 345 | Popular Tags |