KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > resources > metadata > properties > ranges > StringRange


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.rm.resources.metadata.properties.ranges;
20
21 import java.util.StringTokenizer JavaDoc;
22
23 import org.openharmonise.rm.PopulateException;
24 import org.openharmonise.rm.metadata.GeneralPropertyInstance;
25 import org.openharmonise.rm.publishing.*;
26 import org.w3c.dom.Element JavaDoc;
27
28
29 /**
30  *
31  * Class which represents a <code>Property</code> range which is
32  * restricted to <code>String</code> values.
33  *
34  * @author Michael Bell
35  * @version $Revision: 1.2 $
36  *
37  */

38 public class StringRange extends AbstractRange implements Range, Publishable {
39
40     /**
41      * String range XML tag name
42      */

43     public final static String JavaDoc TAG_STRING_RANGE = "StringRange";
44     
45     /**
46      * Minimum length XML tag name
47      */

48     public final static String JavaDoc TAG_MIN_LENGTH = "MinLength";
49     
50     /**
51      * Maximum length XML tag name
52      */

53     public final static String JavaDoc TAG_MAX_LENGTH = "MaxLength";
54     
55
56     /**
57      * The minimum length for a <code>String</code> in this range
58      */

59     private int m_minLength = 0;
60     
61     /**
62      * The maximum length for a <code>String</code> in this range
63      */

64     private int m_maxLength = -1;
65     
66     /**
67      * The separator character used to separate range restrictions when compiling
68      * the <code>String</code> representation for the database details field.
69      */

70     static final private String JavaDoc SEPARATOR = ":";
71
72     /**
73      * Constructs a new <code>StringRange</code>.
74      *
75      */

76     public StringRange() {
77         super(String JavaDoc.class.getName());
78     }
79
80     /**
81      * Constructs a <code>StringRange</code> with restrictions specified
82      * in a <code>String</code> representation.
83      *
84      * @param sDetails the <code>String</code> representation of the restrictions
85      * on this range
86      */

87     public StringRange(String JavaDoc sDetails) {
88         super(String JavaDoc.class.getName(), sDetails);
89     }
90
91     /* (non-Javadoc)
92      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#isValid(java.lang.Object)
93      */

94     public boolean isValid(Object JavaDoc obj) {
95         boolean bIsValid = false;
96
97         if (obj instanceof String JavaDoc) {
98             bIsValid = true;
99             String JavaDoc strObj = (String JavaDoc) obj;
100             int nLength = strObj.length();
101
102             if (m_minLength > 0) {
103                 bIsValid = !(nLength <= m_minLength);
104             }
105
106             if (m_maxLength > 0 && bIsValid == true) {
107                 bIsValid = !(nLength >= m_maxLength);
108             }
109         }
110
111         return bIsValid;
112     }
113
114     /**
115      * Sets the minimum length restriction on this range.
116      *
117      * @param nMinLength the minimum length restriction on this range
118      * @throws InvalidRangeRestrictionException
119      */

120     public void setMinLength(int nMinLength) throws InvalidRangeRestrictionException {
121         //special case for negative number to let it be the same as stating a min length of 0
122
if(nMinLength < 0) {
123             if(m_minLength > 0) {
124                 m_minLength = 0;
125                 isChanged(true);
126             }
127         } else {
128             if(m_maxLength > 0 && nMinLength > m_maxLength) {
129                 throw new InvalidRangeRestrictionException(nMinLength + " is not a valid minumum");
130             }
131             
132             if (m_minLength != nMinLength) {
133                 isChanged(true);
134                 m_minLength = nMinLength;
135             }
136         }
137         
138     }
139
140     /**
141      * Returns the minimum length restriction on this range.
142      *
143      * @return the minimum length restriction on this range
144      */

145     public int getMinLength() {
146         return m_minLength;
147     }
148
149     /**
150      * Sets the maximum length restriction on this range.
151      *
152      * @param nMaxLength the maximum length restriction on this range
153      * @throws InvalidRangeRestrictionException
154      */

155     public void setMaxLength(int nMaxLength) throws InvalidRangeRestrictionException {
156         if(nMaxLength > 2000 || nMaxLength < m_minLength) {
157             throw new InvalidRangeRestrictionException(nMaxLength + " is not permitted as a maximum");
158         }
159         if (m_maxLength != nMaxLength) {
160             isChanged(true);
161         }
162         m_maxLength = nMaxLength;
163     }
164
165     /**
166      * Returns the maximum length restriction on this range.
167      *
168      * @return the maximum length restriction on this range
169      */

170     public int getMaxLength() {
171         return m_maxLength;
172     }
173
174     /* (non-Javadoc)
175      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getDetails()
176      */

177     public String JavaDoc getDetails() {
178
179         if (isChanged() == true) {
180             StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
181             strbuf.append(String.valueOf(m_minLength)).append(
182                 SEPARATOR).append(
183                 m_maxLength);
184             super.setDetails(strbuf.toString());
185         }
186
187         return super.getDetails();
188     }
189
190     /* (non-Javadoc)
191      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#setDetails(java.lang.String)
192      */

193     public void setDetails(String JavaDoc sDetails) {
194         if (sDetails != null) {
195             StringTokenizer JavaDoc tokenizer =
196                 new StringTokenizer JavaDoc(sDetails, SEPARATOR);
197
198             int i = 0;
199
200             while (tokenizer.hasMoreTokens()) {
201                 String JavaDoc token = tokenizer.nextToken();
202
203                 int nTmp = Integer.parseInt(token);
204
205                 if (i == 0) {
206                     m_minLength = nTmp;
207                 } else if (i == 1) {
208                     m_maxLength = nTmp;
209                 }
210                 i++;
211             }
212         }
213
214         super.setDetails(sDetails);
215
216     }
217
218     /* (non-Javadoc)
219      * @see java.lang.Object#equals(java.lang.Object)
220      */

221     public boolean equals(Object JavaDoc obj) {
222         boolean bResult = false;
223
224         if (obj instanceof StringRange) {
225             if (super.equals(obj) == true) {
226                 StringRange sRange = (StringRange) obj;
227
228                 if (m_minLength == sRange.getMinLength()
229                     && m_maxLength == sRange.getMaxLength()) {
230                     bResult = true;
231                 }
232             }
233         }
234
235         return bResult;
236     }
237
238     /* (non-Javadoc)
239      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getPropertyInstanceClass()
240      */

241     public Class JavaDoc getPropertyInstanceClass() throws ClassNotFoundException JavaDoc {
242         return GeneralPropertyInstance.class;
243     }
244     
245     /* (non-Javadoc)
246      * @see org.openharmonise.rm.publishing.Publishable#getTagName()
247      */

248     public String JavaDoc getTagName() {
249         return TAG_STRING_RANGE;
250     }
251     
252     
253     
254     /**
255      * Returns the <code>int</code> value taken from the first child node
256      * of the specified XML element.
257      *
258      * @param xmlElement the XML element
259      * @return the <code>int</code> value
260      */

261     private int getChildIntValue(Element JavaDoc el) {
262         int nVal = 0;
263         
264         String JavaDoc sVal = el.getFirstChild().getNodeValue();
265         
266         if(sVal != null) {
267             nVal = Integer.parseInt(sVal);
268         }
269         
270         return nVal;
271     }
272     
273     /* (non-Javadoc)
274      * @see org.openharmonise.rm.publishing.Publishable#populate(org.w3c.dom.Element, org.openharmonise.rm.publishing.State)
275      */

276     /* (non-Javadoc)
277      * @see org.openharmonise.rm.publishing.Publishable#populate(org.w3c.dom.Element, org.openharmonise.rm.publishing.State)
278      */

279     public void populate(Element JavaDoc xmlElement, State state)
280         throws PopulateException {
281         
282         String JavaDoc sTagname = xmlElement.getTagName();
283         
284         try {
285             if(sTagname.equals(TAG_MIN_LENGTH)) {
286                 setMinLength(getChildIntValue(xmlElement));
287             } else if(sTagname.equals(TAG_MAX_LENGTH)) {
288                 setMaxLength(getChildIntValue(xmlElement));
289             } else {
290                 super.populate(xmlElement, state);
291             }
292         } catch (InvalidRangeRestrictionException e) {
293             throw new PopulateException(e);
294         } catch (PopulateException e) {
295             throw new PopulateException(e);
296         }
297     }
298
299 }
300
Popular Tags