KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.openharmonise.vfs.*;
26 import org.openharmonise.vfs.event.*;
27 import org.openharmonise.vfs.metadata.range.*;
28 import org.openharmonise.vfs.metadata.value.*;
29 import org.openharmonise.vfs.servers.ServerList;
30
31
32 /**
33  * A property instance is an item of metadata which is attached to a
34  * virtual file.
35  *
36  * @author Matthew Large
37  * @version $Revision: 1.2 $
38  *
39  */

40 public class PropertyInstance {
41
42     /**
43      * Namespace of this property instance.
44      */

45     private String JavaDoc m_sNamespaceURI = null;
46     
47     /**
48      * Name of this property instance.
49      */

50     private String JavaDoc m_sName = null;
51     
52     /**
53      * Path to the virtual file for the property defining this property instance.
54      */

55     private String JavaDoc m_sPropDefnPath = null;
56     
57     /**
58      * Version identifier of the property defining this property instance.
59      */

60     private String JavaDoc m_sPropDefnVersion = null;
61     
62     /**
63      * List of {@link ValueInstance} objects which are the values for this property instance.
64      */

65     private ArrayList JavaDoc m_values = new ArrayList JavaDoc();
66     
67     /**
68      * Virtual file that owns this property instance
69      */

70     private VirtualFile m_vfFile = null;
71
72     public PropertyInstance() {
73         super();
74     }
75
76     /**
77      * Constructs a new property instance.
78      *
79      * @param sNamespaceURI Namespace
80      * @param sName Name
81      */

82     public PropertyInstance(String JavaDoc sNamespaceURI, String JavaDoc sName) {
83         super();
84         this.m_sNamespaceURI = sNamespaceURI;
85         this.m_sName = sName;
86     }
87
88     /**
89      * Constructs a new property instance.
90      *
91      * @param sNamespaceURI Namespace
92      * @param sName Name
93      * @param aValues List of {@link ValueInstance} objects
94      */

95     public PropertyInstance(String JavaDoc sNamespaceURI, String JavaDoc sName, List JavaDoc aValues) {
96         this(sNamespaceURI, sName);
97         this.m_values.addAll(aValues);
98     }
99     
100     /**
101      * Clears all the values for this property instance.
102      *
103      */

104     public void clearAllValues() {
105         this.m_values.clear();
106         if(this.m_vfFile!=null) {
107             this.m_vfFile.fireVirtualFileEvent(VirtualFileEvent.FILE_METADATA_CHANGED);
108         } else {
109         }
110     }
111     
112     /**
113      * Returns the namespace for this property instance.
114      *
115      * @return Namespace
116      */

117     public String JavaDoc getNamespaceURI() {
118         return this.m_sNamespaceURI;
119     }
120     
121     /**
122      * Sets the namespace for this property instance.
123      *
124      * @param sNamespaceURI Namespace
125      */

126     public void setNamespaceURI(String JavaDoc sNamespaceURI) {
127         this.m_sNamespaceURI = sNamespaceURI;
128     }
129     
130     /**
131      * Returns the name for this property instance.
132      *
133      * @return Name
134      */

135     public String JavaDoc getName() {
136         return this.m_sName;
137     }
138     
139     /**
140      * Sets the name for this property instance.
141      *
142      * @param sName Name
143      */

144     public void setName(String JavaDoc sName) {
145         this.m_sName = sName;
146     }
147     
148     /**
149      * Returns a list of the values for this property instance.
150      *
151      * @return List of {@link ValueInstance} objects
152      */

153     public List JavaDoc getValues() {
154         return (List JavaDoc)this.m_values.clone();
155     }
156     
157     /**
158      * Sets the value for this property instance.
159      *
160      * @param value Value
161      */

162     public void setValue(ValueInstance value) {
163         if(this.m_values.size()>0) {
164             this.m_values.set(0, value);
165         } else {
166             this.m_values.add(value);
167         }
168         if(this.m_vfFile!=null) {
169             this.m_vfFile.fireVirtualFileEvent(VirtualFileEvent.FILE_METADATA_CHANGED);
170         } else {
171         }
172     }
173     
174     /**
175      * Sets the values for this property instance.
176      *
177      * @param aValues List of {@link ValueInstance} objects
178      */

179     public void setValues(List JavaDoc aValues) {
180         this.m_values=(ArrayList JavaDoc) aValues;
181         if(this.m_vfFile!=null) {
182             this.m_vfFile.fireVirtualFileEvent(VirtualFileEvent.FILE_METADATA_CHANGED);
183         } else {
184         }
185     }
186     
187     /**
188      * Adds a value to this property instance WITHOUT FIRING A VIRTUAL
189      * FILE EVENT. This is a bit of an evil cludge......use with caution,
190      * however it does do what it says on the tin!! ;D
191      *
192      * @param value Value
193      */

194     public void addValueWithoutFiringVirtualFileEvent(ValueInstance value) {
195         boolean bAddValue = true;
196         if(this.getDefinition().getRange() instanceof ValueRange) {
197             if( !((ValueRange)this.getDefinition().getRange()).validate(value).isValid() ) {
198                 bAddValue=false;
199             }
200         }
201         
202         if(bAddValue && !this.m_values.contains(value)) {
203             this.m_values.add(value);
204         }
205     }
206     
207     /**
208      * Adds a value to this property instance.
209      *
210      * @param value Value
211      */

212     public void addValue(ValueInstance value) {
213         boolean bAddValue = true;
214         if(this.getDefinition().getRange() instanceof ValueRange) {
215             if( !((ValueRange)this.getDefinition().getRange()).validate(value).isValid() ) {
216                 bAddValue=false;
217             }
218         }
219         
220         if(bAddValue && !this.m_values.contains(value)) {
221             this.m_values.add(value);
222         }
223         if(bAddValue && this.m_vfFile!=null) {
224             this.m_vfFile.fireVirtualFileEvent(VirtualFileEvent.FILE_METADATA_CHANGED);
225         }
226     }
227     
228     /**
229      * Removes a value from this property instance.
230      *
231      * @param value Value
232      */

233     public void removeValue(ValueInstance value) {
234         this.m_values.remove(value);
235         if(this.m_vfFile!=null) {
236             this.m_vfFile.fireVirtualFileEvent(VirtualFileEvent.FILE_METADATA_CHANGED);
237         }
238     }
239     /**
240      * Removes a value from this property instance.
241      *
242      * @param value Value
243      */

244     public void removeValueWithoutFiringVirtualFileEvent(ValueInstance value) {
245         this.m_values.remove(value);
246     }
247     
248     /**
249      * Sets the version identifier and full path to the virtual file
250      * for the property which defines this property instance.
251      *
252      * @param sPath Full path
253      * @param sVersion Version identifier
254      */

255     public void setDefinitionPath(String JavaDoc sPath, String JavaDoc sVersion) {
256         this.m_sPropDefnPath = sPath;
257         this.m_sPropDefnVersion = sVersion;
258     }
259     
260     /**
261      * Returns the full path to the virtual file for the property
262      * which defines this property instance.
263      *
264      * @return Full path
265      */

266     public String JavaDoc getDefinitionPath() {
267         return this.m_sPropDefnPath;
268     }
269     
270     /**
271      * Returns the version identifier for the property which
272      * defines this property instance.
273      *
274      * @return Version identifier
275      */

276     public String JavaDoc getDefinitionVersion() {
277         return this.m_sPropDefnVersion;
278     }
279     
280     /**
281      * Returns the property which defines this property instance.
282      *
283      * @return Property
284      */

285     public Property getDefinition() {
286         if(this.m_sPropDefnPath!=null) {
287             Property prop = PropertyCache.getInstance().getPropertyByPath(m_sPropDefnPath, m_sPropDefnVersion);
288             if(prop!=null) {
289                 prop.setNamespace(m_sNamespaceURI);
290                 return prop;
291             } else {
292                 return PropertyCache.getInstance().getPropertyByName(this.m_sNamespaceURI, this.m_sName);
293             }
294         } else {
295             return PropertyCache.getInstance().getPropertyByName(this.m_sNamespaceURI, this.m_sName);
296         }
297     }
298     
299     public String JavaDoc toString() {
300         return this.m_sNamespaceURI + ":" + this.m_sName + ":" + this.m_values;
301     }
302     
303     /**
304      * Sets the virtual file which owns this property instance.
305      *
306      * @param vfFile Virtual file
307      */

308     public void setVirtualFile(VirtualFile vfFile) {
309         this.m_vfFile = vfFile;
310         if(this.getDefinition()!=null && this.getDefinition().getRange() instanceof PropertyRange) {
311             Iterator JavaDoc itor = this.m_values.iterator();
312             while(itor.hasNext()) {
313                 PropertyValue val = (PropertyValue)itor.next();
314                 Iterator JavaDoc itor2 = val.getValue().iterator();
315                 while (itor2.hasNext()) {
316                     PropertyInstance propInst = (PropertyInstance) itor2.next();
317                     propInst.setVirtualFile(vfFile);
318                 }
319             }
320         }
321     }
322     
323     /**
324      * Returns the virtual file which owns this property instance.
325      *
326      * @return Virtual file
327      */

328     public VirtualFile getVirtualFile() {
329         return this.m_vfFile;
330     }
331     
332     /**
333      * Returns a new value of the correct concrete type for this
334      * property instance.
335      *
336      * @return Value
337      */

338     public ValueInstance getNewValueInstance() {
339         if(this.m_vfFile.getVFS()!=null) {
340             return this.m_vfFile.getVFS().getNewValueInstance(this);
341         } else {
342             return ServerList.getInstance().getHarmoniseServer().getVFS().getNewValueInstance(this);
343         }
344     }
345     
346     /* (non-Javadoc)
347      * @see java.lang.Object#equals(java.lang.Object)
348      */

349     public boolean equals(Object JavaDoc arg0) {
350         if(super.equals(arg0)) {
351             return true;
352         } else if(arg0 instanceof PropertyInstance) {
353             PropertyInstance propInst = (PropertyInstance)arg0;
354             if(propInst.m_sName.equals(this.m_sName)
355                 && propInst.m_sNamespaceURI.equals(this.m_sNamespaceURI)
356                 && propInst.m_values.containsAll(this.m_values)) {
357                 return true;
358             } else {
359                 return false;
360             }
361         } else {
362             return false;
363         }
364     }
365
366
367 }
368
Popular Tags