1 19 package org.openharmonise.vfs.metadata; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 import javax.xml.parsers.FactoryConfigurationError ; 28 import javax.xml.parsers.ParserConfigurationException ; 29 30 import org.openharmonise.vfs.*; 31 import org.openharmonise.vfs.metadata.range.*; 32 import org.openharmonise.vfs.servers.ServerList; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.NodeList ; 37 import org.xml.sax.SAXException ; 38 39 40 52 public class ValueCache { 53 54 57 private static ValueCache m_instance = null; 58 59 62 private ResourceLookup m_values = new ResourceLookup(11); 63 64 67 private ResourceLookup m_valueGroups = new ResourceLookup(11); 68 69 72 public ValueCache() { 73 super(); 74 } 75 76 82 public static ValueCache getInstance() { 83 if(m_instance==null) { 84 m_instance = new ValueCache(); 85 } 86 return m_instance; 87 } 88 89 94 public void load(String sFilePath) { 95 File file = new File (sFilePath); 96 if(file.exists()) { 97 98 Document xmlDoc = null; 99 100 try { 101 DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance(); 102 factory.setNamespaceAware(true); 103 104 xmlDoc = factory.newDocumentBuilder().parse(file); 105 } catch (SAXException e) { 106 e.printStackTrace(); 107 } catch (IOException e) { 108 e.printStackTrace(); 109 } catch (ParserConfigurationException e) { 110 e.printStackTrace(); 111 } catch (FactoryConfigurationError e) { 112 e.printStackTrace(); 113 } 114 115 Element elRoot = xmlDoc.getDocumentElement(); 116 NodeList nl = elRoot.getChildNodes(); 117 for(int i=0; i<nl.getLength();i++) { 118 Node node = nl.item(i); 119 if(node.getNodeType()==Node.ELEMENT_NODE 120 && ((Element )node).getLocalName().equalsIgnoreCase("value")) { 121 Element elProperty = ((Element )node); 122 String sName = elProperty.getAttributeNS("http://www.simulacramedia.com/harmoniseclient/propdefs", "name"); 123 String sQName = sName; 124 if( this.m_values.getQNameKeySet().contains(sQName)) { 125 Value val = (Value)this.m_values.getByQName(sQName); 126 val.instantiate(elProperty); 127 } else { 128 Value val = new Value(); 129 val.setName(sName); 130 val.instantiate(elProperty); 131 this.m_values.put(sQName, val.getHREF(), val); 132 } 133 } else if(node.getNodeType()==Node.ELEMENT_NODE 134 && ((Element )node).getLocalName().equalsIgnoreCase("valuegroup")) { 135 Element elProperty = ((Element )node); 136 String sName = elProperty.getAttributeNS("http://www.simulacramedia.com/harmoniseclient/propdefs", "name"); 137 String sQName = sName; 138 if( this.m_valueGroups.getQNameKeySet().contains(sQName)) { 139 ValueGroup valGroup = (ValueGroup)this.m_valueGroups.getByQName(sQName); 140 valGroup.instantiate(elProperty); 141 } else { 142 ValueGroup valGroup = new ValueGroup(); 143 valGroup.setName(sName); 144 valGroup.instantiate(elProperty); 145 this.m_valueGroups.put(sQName, valGroup.getHREF(), valGroup); 146 } 147 } 148 } 149 150 } 151 } 152 153 158 public void save(String sFilePath) { 159 160 } 161 162 169 public Value getValue(String sHREF) { 170 Value value = null; 171 172 if(value==null) { 173 VirtualFile vfFile = ServerList.getInstance().getHarmoniseServer().getVFS().getVirtualFile(sHREF).getResource(); 174 value = new Value(vfFile.getFileName(), vfFile.getFullPath()); 175 value.setDisplayName(vfFile.getVFS().getVirtualFileSystemView().getDisplayName(vfFile)); 176 this.m_values.put(value.getName(), value.getHREF(), value); 177 } 178 179 return value; 180 } 181 182 189 public ValueGroup getValueGroup(String sHREF) { 190 ValueGroup valueGroup = null; 191 192 if(valueGroup==null) { 193 VirtualFile vfFile = ServerList.getInstance().getHarmoniseServer().getVFS().getVirtualFile(sHREF).getResource(); 194 if(vfFile!=null) { 195 valueGroup = new ValueGroup(vfFile.getFileName(), vfFile.getFullPath()); 196 valueGroup.setDisplayName( vfFile.getVFS().getVirtualFileSystemView().getDisplayName(vfFile) ); 197 198 List children = vfFile.getChildren(); 199 Iterator itor = children.iterator(); 200 while (itor.hasNext()) { 201 String childHREF = (String ) itor.next(); 202 VirtualFile vfChild = ServerList.getInstance().getHarmoniseServer().getVFS().getVirtualFile(childHREF).getResource(); 203 if(vfChild.isDirectory()) { 204 valueGroup.addSubGroup(childHREF); 205 } else if(vfChild.getState()==VirtualFile.STATE_LIVE) { 206 valueGroup.addChild(childHREF); 207 } 208 } 209 210 this.m_valueGroups.put(valueGroup.getName(), valueGroup.getHREF(), valueGroup); 211 } 212 } 213 214 return valueGroup; 215 } 216 217 public String toString() { 218 StringBuffer sBuff = new StringBuffer (); 219 220 Iterator itor = this.m_values.getValues().iterator(); 221 while(itor.hasNext()) { 222 sBuff.append( ((Value)itor.next()).toString() ).append("\n"); 223 } 224 225 return sBuff.toString(); 226 } 227 228 } 229 | Popular Tags |