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.vfs.metadata;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.xml.parsers.DocumentBuilderFactory;
027: import javax.xml.parsers.FactoryConfigurationError;
028: import javax.xml.parsers.ParserConfigurationException;
029:
030: import org.openharmonise.vfs.*;
031: import org.openharmonise.vfs.metadata.range.*;
032: import org.openharmonise.vfs.servers.ServerList;
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Element;
035: import org.w3c.dom.Node;
036: import org.w3c.dom.NodeList;
037: import org.xml.sax.SAXException;
038:
039: /**
040: * The value cache was initially intended to cache values and value groups
041: * so that they are not fetched constantly from the Harmonise server.
042: * However this is impractical as version information is not supplied
043: * with value instances. Currently, the value cache simply
044: * manages the conversion from virtual file resources to value or value
045: * group objects.
046: *
047: * @author Matthew Large
048: * @version $Revision: 1.1 $
049: *
050: */
051: public class ValueCache {
052:
053: /**
054: * Instance of the value cache, following the Singleton pattern.
055: */
056: private static ValueCache m_instance = null;
057:
058: /**
059: * Value lookup.
060: */
061: private ResourceLookup m_values = new ResourceLookup(11);
062:
063: /**
064: * Value group lookup
065: */
066: private ResourceLookup m_valueGroups = new ResourceLookup(11);
067:
068: /**
069: *
070: */
071: public ValueCache() {
072: super ();
073: }
074:
075: /**
076: * Returns the instance of the value cache. Follows the Singleton
077: * pattern.
078: *
079: * @return The value cache
080: */
081: public static ValueCache getInstance() {
082: if (m_instance == null) {
083: m_instance = new ValueCache();
084: }
085: return m_instance;
086: }
087:
088: /**
089: * Loads stored cache information from an XML file.
090: *
091: * @param sFilePath Path to XML file
092: */
093: public void load(String sFilePath) {
094: File file = new File(sFilePath);
095: if (file.exists()) {
096:
097: Document xmlDoc = null;
098:
099: try {
100: DocumentBuilderFactory factory = DocumentBuilderFactory
101: .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()
121: .equalsIgnoreCase("value")) {
122: Element elProperty = ((Element) node);
123: String sName = elProperty
124: .getAttributeNS(
125: "http://www.simulacramedia.com/harmoniseclient/propdefs",
126: "name");
127: String sQName = sName;
128: if (this .m_values.getQNameKeySet().contains(sQName)) {
129: Value val = (Value) this .m_values
130: .getByQName(sQName);
131: val.instantiate(elProperty);
132: } else {
133: Value val = new Value();
134: val.setName(sName);
135: val.instantiate(elProperty);
136: this .m_values.put(sQName, val.getHREF(), val);
137: }
138: } else if (node.getNodeType() == Node.ELEMENT_NODE
139: && ((Element) node).getLocalName()
140: .equalsIgnoreCase("valuegroup")) {
141: Element elProperty = ((Element) node);
142: String sName = elProperty
143: .getAttributeNS(
144: "http://www.simulacramedia.com/harmoniseclient/propdefs",
145: "name");
146: String sQName = sName;
147: if (this .m_valueGroups.getQNameKeySet().contains(
148: sQName)) {
149: ValueGroup valGroup = (ValueGroup) this .m_valueGroups
150: .getByQName(sQName);
151: valGroup.instantiate(elProperty);
152: } else {
153: ValueGroup valGroup = new ValueGroup();
154: valGroup.setName(sName);
155: valGroup.instantiate(elProperty);
156: this .m_valueGroups.put(sQName, valGroup
157: .getHREF(), valGroup);
158: }
159: }
160: }
161:
162: }
163: }
164:
165: /**
166: * Saves cache information to an XML file.
167: *
168: * @param sFilePath Full path to XML file
169: */
170: public void save(String sFilePath) {
171:
172: }
173:
174: /**
175: * Returns a value for a given full path to the virtual file for that
176: * value.
177: *
178: * @param sHREF Full path
179: * @return Value or null if not found.
180: */
181: public Value getValue(String sHREF) {
182: Value value = null;
183:
184: if (value == null) {
185: VirtualFile vfFile = ServerList.getInstance()
186: .getHarmoniseServer().getVFS()
187: .getVirtualFile(sHREF).getResource();
188: value = new Value(vfFile.getFileName(), vfFile
189: .getFullPath());
190: value.setDisplayName(vfFile.getVFS()
191: .getVirtualFileSystemView().getDisplayName(vfFile));
192: this .m_values.put(value.getName(), value.getHREF(), value);
193: }
194:
195: return value;
196: }
197:
198: /**
199: * Returns a value group for a given full path to the virtual file
200: * for that value group.
201: *
202: * @param sHREF Full path
203: * @return Value group or null if not found
204: */
205: public ValueGroup getValueGroup(String sHREF) {
206: ValueGroup valueGroup = null;
207:
208: if (valueGroup == null) {
209: VirtualFile vfFile = ServerList.getInstance()
210: .getHarmoniseServer().getVFS()
211: .getVirtualFile(sHREF).getResource();
212: if (vfFile != null) {
213: valueGroup = new ValueGroup(vfFile.getFileName(),
214: vfFile.getFullPath());
215: valueGroup.setDisplayName(vfFile.getVFS()
216: .getVirtualFileSystemView().getDisplayName(
217: vfFile));
218:
219: List children = vfFile.getChildren();
220: Iterator itor = children.iterator();
221: while (itor.hasNext()) {
222: String childHREF = (String) itor.next();
223: VirtualFile vfChild = ServerList.getInstance()
224: .getHarmoniseServer().getVFS()
225: .getVirtualFile(childHREF).getResource();
226: if (vfChild.isDirectory()) {
227: valueGroup.addSubGroup(childHREF);
228: } else if (vfChild.getState() == VirtualFile.STATE_LIVE) {
229: valueGroup.addChild(childHREF);
230: }
231: }
232:
233: this .m_valueGroups.put(valueGroup.getName(), valueGroup
234: .getHREF(), valueGroup);
235: }
236: }
237:
238: return valueGroup;
239: }
240:
241: public String toString() {
242: StringBuffer sBuff = new StringBuffer();
243:
244: Iterator itor = this .m_values.getValues().iterator();
245: while (itor.hasNext()) {
246: sBuff.append(((Value) itor.next()).toString()).append("\n");
247: }
248:
249: return sBuff.toString();
250: }
251:
252: }
|