KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > vfs > metadata > ValueCache


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.vfs.metadata;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
28 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38
39
40 /**
41  * The value cache was initially intended to cache values and value groups
42  * so that they are not fetched constantly from the Harmonise server.
43  * However this is impractical as version information is not supplied
44  * with value instances. Currently, the value cache simply
45  * manages the conversion from virtual file resources to value or value
46  * group objects.
47  *
48  * @author Matthew Large
49  * @version $Revision: 1.1 $
50  *
51  */

52 public class ValueCache {
53     
54     /**
55      * Instance of the value cache, following the Singleton pattern.
56      */

57     private static ValueCache m_instance = null;
58
59     /**
60      * Value lookup.
61      */

62     private ResourceLookup m_values = new ResourceLookup(11);
63     
64     /**
65      * Value group lookup
66      */

67     private ResourceLookup m_valueGroups = new ResourceLookup(11);
68
69     /**
70      *
71      */

72     public ValueCache() {
73         super();
74     }
75     
76     /**
77      * Returns the instance of the value cache. Follows the Singleton
78      * pattern.
79      *
80      * @return The value cache
81      */

82     public static ValueCache getInstance() {
83         if(m_instance==null) {
84             m_instance = new ValueCache();
85         }
86         return m_instance;
87     }
88
89     /**
90      * Loads stored cache information from an XML file.
91      *
92      * @param sFilePath Path to XML file
93      */

94     public void load(String JavaDoc sFilePath) {
95         File JavaDoc file = new File JavaDoc(sFilePath);
96         if(file.exists()) {
97             
98             Document JavaDoc xmlDoc = null;
99             
100             try {
101                 DocumentBuilderFactory JavaDoc factory =DocumentBuilderFactory.newInstance();
102                 factory.setNamespaceAware(true);
103                  
104                 xmlDoc = factory.newDocumentBuilder().parse(file);
105             } catch (SAXException JavaDoc e) {
106                 e.printStackTrace();
107             } catch (IOException JavaDoc e) {
108                 e.printStackTrace();
109             } catch (ParserConfigurationException JavaDoc e) {
110                 e.printStackTrace();
111             } catch (FactoryConfigurationError JavaDoc e) {
112                 e.printStackTrace();
113             }
114             
115             Element JavaDoc elRoot = xmlDoc.getDocumentElement();
116             NodeList JavaDoc nl = elRoot.getChildNodes();
117             for(int i=0; i<nl.getLength();i++) {
118                 Node JavaDoc node = nl.item(i);
119                 if(node.getNodeType()==Node.ELEMENT_NODE
120                         && ((Element JavaDoc)node).getLocalName().equalsIgnoreCase("value")) {
121                     Element JavaDoc elProperty = ((Element JavaDoc)node);
122                     String JavaDoc sName = elProperty.getAttributeNS("http://www.simulacramedia.com/harmoniseclient/propdefs", "name");
123                     String JavaDoc 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 JavaDoc)node).getLocalName().equalsIgnoreCase("valuegroup")) {
135                     Element JavaDoc elProperty = ((Element JavaDoc)node);
136                     String JavaDoc sName = elProperty.getAttributeNS("http://www.simulacramedia.com/harmoniseclient/propdefs", "name");
137                     String JavaDoc 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     /**
154      * Saves cache information to an XML file.
155      *
156      * @param sFilePath Full path to XML file
157      */

158     public void save(String JavaDoc sFilePath) {
159         
160     }
161     
162     /**
163      * Returns a value for a given full path to the virtual file for that
164      * value.
165      *
166      * @param sHREF Full path
167      * @return Value or null if not found.
168      */

169     public Value getValue(String JavaDoc 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     /**
183      * Returns a value group for a given full path to the virtual file
184      * for that value group.
185      *
186      * @param sHREF Full path
187      * @return Value group or null if not found
188      */

189     public ValueGroup getValueGroup(String JavaDoc 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 JavaDoc children = vfFile.getChildren();
199                 Iterator JavaDoc itor = children.iterator();
200                 while (itor.hasNext()) {
201                     String JavaDoc childHREF = (String JavaDoc) 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 JavaDoc toString() {
218         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
219         
220         Iterator JavaDoc 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