KickJava   Java API By Example, From Geeks To Geeks.

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


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.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28 import org.w3c.dom.Text JavaDoc;
29
30 /**
31  * Domains describe the applicability of properties by stating the paths
32  * that they are allowed to be used in and the min and max occurrence values
33  * for that path and any resource/content type restrictions. Domains are
34  * stored as metadata on the {@link org.openharmonise.vfs.VirtualFile}s for {@link Property} objects.
35  *
36  * @author Matthew Large
37  * @version $Revision: 1.1 $
38  *
39  */

40 public class Domain {
41
42     /**
43      * Allowed resource type.
44      */

45     private String JavaDoc m_sResourceType = null;
46     
47     /**
48      * Allowed content types.
49      */

50     private ArrayList JavaDoc m_aContentTypes = new ArrayList JavaDoc(3);
51     
52     /**
53      * Allowed paths.
54      */

55     private ArrayList JavaDoc m_aPaths = new ArrayList JavaDoc(3);
56     
57     /**
58      * Minimum occurrence.
59      */

60     private int m_nMinOccurs = 0;
61     
62     /**
63      * Maximum occurrence. A value of -1 represents UNBOUNDED.
64      */

65     private int m_nMaxOccurs = -1;
66
67     /**
68      * Constructs a new Domain.
69      */

70     public Domain() {
71         super();
72     }
73     
74     
75
76     /**
77      * Returns the maximum occurrence.
78      *
79      * @return Maximum Occurrence
80      */

81     public int getMaxOccurs() {
82         return m_nMaxOccurs;
83     }
84
85     /**
86      * Returns the minimum occurrence.
87      *
88      * @return Minimum occurrence
89      */

90     public int getMinOccurs() {
91         return m_nMinOccurs;
92     }
93
94     /**
95      * Returns a list of the allowed content types.
96      *
97      * @return Content types
98      */

99     public List JavaDoc getContentTypes() {
100         return (List JavaDoc)m_aContentTypes.clone();
101     }
102
103     /**
104      * Allowed resource type.
105      *
106      * @return Resource type
107      */

108     public String JavaDoc getResourceType() {
109         return m_sResourceType;
110     }
111
112     /**
113      * Sets the maximum occurrence.
114      *
115      * @param i Maximum occurrence
116      */

117     public void setMaxOccurs(int i) {
118         m_nMaxOccurs = i;
119     }
120
121     /**
122      * Sets the minimum occurrence.
123      *
124      * @param i Minimum occurrence
125      */

126     public void setMinOccurs(int i) {
127         m_nMinOccurs = i;
128     }
129
130     /**
131      * Adds an allowed content type.
132      *
133      * @param string Content type
134      */

135     public void addContentType(String JavaDoc string) {
136         m_aContentTypes.add( string);
137     }
138
139     /**
140      * Sets the allowed resource type.
141      *
142      * @param string Resource type
143      */

144     public void setResourceType(String JavaDoc string) {
145         m_sResourceType = string;
146     }
147
148     /**
149      * Adds an allowed path.
150      *
151      * @param sPath Full path
152      */

153     public void addPath(String JavaDoc sPath) {
154         this.m_aPaths.add(sPath);
155     }
156     
157     /**
158      * Returns a list of all the allowed paths.
159      *
160      * @return Full paths
161      */

162     public List JavaDoc getPaths() {
163         return (List JavaDoc) this.m_aPaths.clone();
164     }
165     
166     /**
167      * Validates a list of values against the restrictions in this domain.
168      *
169      * @param aValues List of values to be validated
170      * @return Results of the validation
171      */

172     public ValidationResult validate(List JavaDoc aValues) {
173         ValidationResult result = new ValidationResult();
174         
175         if( aValues.size()< this.m_nMinOccurs ) {
176             result.setValid(false);
177             result.setMessage(this.m_nMinOccurs + " is the minimum number of values required for this property.");
178         }
179         
180         if( this.m_nMaxOccurs>-1 && aValues.size()>this.m_nMaxOccurs) {
181             result.setValid(false);
182             result.setMessage(this.m_nMaxOccurs + " is the maximum number of values allowed for this property.");
183         }
184         
185         return result;
186     }
187     
188     /**
189      * Populates the domain from a XML element.
190      *
191      * @param elDomain Root element of domain XML
192      */

193     public void instantiate(Element JavaDoc elDomain) {
194         NodeList JavaDoc nl = elDomain.getElementsByTagNameNS("DAV:", "href");
195         for(int i=0; i<nl.getLength(); i++) {
196             Element JavaDoc elPath = (Element JavaDoc)nl.item(i);
197             if(elPath.getChildNodes().getLength()==1) {
198                 Node JavaDoc node = elPath.getFirstChild();
199                 if(node.getNodeType()==Node.TEXT_NODE) {
200                     this.m_aPaths.add( ((Text JavaDoc)node).getNodeValue() );
201                 }
202             }
203         }
204
205         nl = elDomain.getElementsByTagNameNS("DAV:", "resourcetype");
206         for(int i=0; i<nl.getLength(); i++) {
207             Element JavaDoc elResourceType = (Element JavaDoc)nl.item(i);
208             if(elResourceType.getChildNodes().getLength()>0) {
209                 NodeList JavaDoc nl2 = elResourceType.getChildNodes();
210                 for(int j=0; j<nl2.getLength(); j++) {
211                     //this.m_sResourceType = nl2.item(j).getNodeName();
212
if(nl2.item(j).getNodeType()==Node.ELEMENT_NODE) {
213                         this.m_sResourceType = ((Element JavaDoc)nl2.item(j)).getTagName();
214                     }
215                 }
216             }
217         }
218
219         nl = elDomain.getElementsByTagNameNS("DAV:", "contenttype");
220         for(int i=0; i<nl.getLength(); i++) {
221             Element JavaDoc elContentType = (Element JavaDoc)nl.item(i);
222             if(elContentType.getChildNodes().getLength()==1) {
223                 Node JavaDoc node = elContentType.getFirstChild();
224                 if(node.getNodeType()==Node.TEXT_NODE) {
225                     this.m_aContentTypes.add( ((Text JavaDoc)node).getNodeValue() );
226                 }
227             }
228         }
229
230         nl = elDomain.getElementsByTagNameNS("DAV:", "minOccurs");
231         for(int i=0; i<nl.getLength(); i++) {
232             Element JavaDoc elMinOccurs = (Element JavaDoc)nl.item(i);
233             if(elMinOccurs.getChildNodes().getLength()==1) {
234                 Node JavaDoc node = elMinOccurs.getFirstChild();
235                 if(node.getNodeType()==Node.TEXT_NODE) {
236                     this.m_nMinOccurs = Integer.parseInt( ((Text JavaDoc)node).getNodeValue() );
237                 }
238             }
239         }
240
241         nl = elDomain.getElementsByTagNameNS("DAV:", "maxOccurs");
242         for(int i=0; i<nl.getLength(); i++) {
243             Element JavaDoc elMaxOccurs = (Element JavaDoc)nl.item(i);
244             if(elMaxOccurs.getChildNodes().getLength()==1) {
245                 Node JavaDoc node = elMaxOccurs.getFirstChild();
246                 if(node.getNodeType()==Node.TEXT_NODE) {
247                     this.m_nMaxOccurs = Integer.parseInt( ((Text JavaDoc)node).getNodeValue() );
248                 }
249             }
250         }
251     }
252
253     public String JavaDoc toString() {
254         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
255         
256         sBuff.append("Domain:\n")
257              .append("Resourcetype: ").append(this.m_sResourceType).append("\n")
258              .append("MinOccurs: ").append(this.m_nMinOccurs).append("\n")
259              .append("MaxOccurs: ").append(this.m_nMaxOccurs).append("\n");
260              
261         Iterator JavaDoc itor = this.m_aContentTypes.iterator();
262         while(itor.hasNext()) {
263             sBuff.append("Contenttype: ").append(((String JavaDoc)itor.next())).append("\n");
264         }
265         
266         itor = this.m_aPaths.iterator();
267         while(itor.hasNext()) {
268             sBuff.append("Path: ").append(((String JavaDoc)itor.next())).append("\n");
269         }
270         
271         return sBuff.toString();
272     }
273
274 }
275
Popular Tags