KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > vfs > metadata > range > 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.vfs.metadata.range;
20
21 import org.openharmonise.commons.xml.*;
22 import org.openharmonise.vfs.metadata.*;
23 import org.openharmonise.vfs.metadata.value.*;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27 import org.w3c.dom.Text JavaDoc;
28
29
30 /**
31  * This is the range for string type properties.
32  *
33  * @author Matthew Large
34  * @version $Revision: 1.1 $
35  *
36  */

37 public class StringRange extends AbstractRange implements Range {
38
39     /**
40      * The size at which a string is classes as a long string.
41      */

42     public static int SIZE_BORDER = 100;
43
44     /**
45      * The minimum length of strings allowed for this range.
46      */

47     private Integer JavaDoc m_nMinLength = null;
48     
49     /**
50      * The maximum length of strings allowed for this range. Defaults to the {@link StringRange#SIZE_BORDER} value.
51      */

52     private Integer JavaDoc m_nMaxLength = new Integer JavaDoc(SIZE_BORDER);
53
54     /**
55      *
56      */

57     public StringRange() {
58         super();
59     }
60
61     /* (non-Javadoc)
62      * @see com.simulacramedia.vfs.metadata.range.AbstractRange#validate(java.lang.String)
63      */

64     public ValidationResult validate(ValueInstance value) {
65         boolean bIsValid = true;
66         int nMax = getMaxLength();
67         int nMin = getMinLength();
68         
69         int nLen = ((StringValue)value).getValue().length();
70         
71         if(nMax > 0 && nLen > nMax) {
72             bIsValid = false;
73         }
74         
75         if(bIsValid == true && nMin > 0 && nLen < nMin) {
76             bIsValid = false;
77         }
78         
79         return new ValidationResult(bIsValid,"");
80     }
81     
82     /**
83      * Returns the minimum string length allowed for this range.
84      *
85      * @return Minimum string length
86      */

87     public int getMinLength() {
88         if(this.m_nMinLength==null) {
89             return 0;
90         } else {
91             return this.m_nMinLength.intValue();
92         }
93     }
94     
95     /**
96      * Returns the maximum string length allowed for this range.
97      *
98      * @return Maximum string length
99      */

100     public int getMaxLength() {
101         if(this.m_nMaxLength==null) {
102             return -1;
103         } else {
104             return this.m_nMaxLength.intValue();
105         }
106     }
107     
108     /**
109      * Sets the minimum string length allowed for this range.
110      *
111      * @param nMin Minimum string length
112      */

113     public void setMinLength(int nMin) {
114         this.m_nMinLength = new Integer JavaDoc(nMin);
115     }
116     
117     /**
118      * Sets the maximum string length allowed for this range.
119      *
120      * @param nMax Maximum string length
121      */

122     public void setMaxLength(int nMax) {
123         this.m_nMaxLength = new Integer JavaDoc(nMax);
124     }
125
126     /* (non-Javadoc)
127      * @see com.simulacramedia.vfs.metadata.Range#instantiate(org.w3c.dom.Element)
128      */

129     public void instantiate(Element JavaDoc elRange) {
130         Element JavaDoc elRestriction = XMLUtils.getFirstElementChild(elRange);
131         NodeList JavaDoc nl = elRestriction.getChildNodes();
132         for(int i=0; i<nl.getLength();i++) {
133             Node JavaDoc node = nl.item(i);
134             if(node.getNodeType()==Node.ELEMENT_NODE) {
135                 Element JavaDoc element = (Element JavaDoc)node;
136                 if(element.getLocalName().equalsIgnoreCase("minLength")) {
137                     Node JavaDoc node2 = element.getFirstChild();
138                     if( node2.getNodeType()==Node.TEXT_NODE) {
139                         this.m_nMinLength = Integer.valueOf( ((Text JavaDoc)node2).getNodeValue() );
140                     }
141                 } else if(element.getLocalName().equalsIgnoreCase("maxLength") || element.getLocalName().equalsIgnoreCase("length")) {
142                     Node JavaDoc node2 = element.getFirstChild();
143                     if( node2.getNodeType()==Node.TEXT_NODE) {
144                         this.m_nMaxLength = Integer.valueOf( ((Text JavaDoc)node2).getNodeValue() );
145                     }
146                 }
147             }
148         }
149     }
150
151     public String JavaDoc toString() {
152         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
153         
154         sBuff.append("StringRange:\n");
155         if(this.m_nMinLength!=null) {
156             sBuff.append("minLength: ").append(this.m_nMinLength).append("\n");
157         }
158         if(this.m_nMaxLength!=null) {
159             sBuff.append("maxLength: ").append(this.m_nMaxLength).append("\n");
160         }
161         
162         return sBuff.toString();
163     }
164
165 }
166
Popular Tags