001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.vfs.metadata.range;
020:
021: import org.openharmonise.commons.xml.*;
022: import org.openharmonise.vfs.metadata.*;
023: import org.openharmonise.vfs.metadata.value.*;
024: import org.w3c.dom.Element;
025: import org.w3c.dom.Node;
026: import org.w3c.dom.NodeList;
027: import org.w3c.dom.Text;
028:
029: /**
030: * This is the range for string type properties.
031: *
032: * @author Matthew Large
033: * @version $Revision: 1.1 $
034: *
035: */
036: public class StringRange extends AbstractRange implements Range {
037:
038: /**
039: * The size at which a string is classes as a long string.
040: */
041: public static int SIZE_BORDER = 100;
042:
043: /**
044: * The minimum length of strings allowed for this range.
045: */
046: private Integer m_nMinLength = null;
047:
048: /**
049: * The maximum length of strings allowed for this range. Defaults to the {@link StringRange#SIZE_BORDER} value.
050: */
051: private Integer m_nMaxLength = new Integer(SIZE_BORDER);
052:
053: /**
054: *
055: */
056: public StringRange() {
057: super ();
058: }
059:
060: /* (non-Javadoc)
061: * @see com.simulacramedia.vfs.metadata.range.AbstractRange#validate(java.lang.String)
062: */
063: public ValidationResult validate(ValueInstance value) {
064: boolean bIsValid = true;
065: int nMax = getMaxLength();
066: int nMin = getMinLength();
067:
068: int nLen = ((StringValue) value).getValue().length();
069:
070: if (nMax > 0 && nLen > nMax) {
071: bIsValid = false;
072: }
073:
074: if (bIsValid == true && nMin > 0 && nLen < nMin) {
075: bIsValid = false;
076: }
077:
078: return new ValidationResult(bIsValid, "");
079: }
080:
081: /**
082: * Returns the minimum string length allowed for this range.
083: *
084: * @return Minimum string length
085: */
086: public int getMinLength() {
087: if (this .m_nMinLength == null) {
088: return 0;
089: } else {
090: return this .m_nMinLength.intValue();
091: }
092: }
093:
094: /**
095: * Returns the maximum string length allowed for this range.
096: *
097: * @return Maximum string length
098: */
099: public int getMaxLength() {
100: if (this .m_nMaxLength == null) {
101: return -1;
102: } else {
103: return this .m_nMaxLength.intValue();
104: }
105: }
106:
107: /**
108: * Sets the minimum string length allowed for this range.
109: *
110: * @param nMin Minimum string length
111: */
112: public void setMinLength(int nMin) {
113: this .m_nMinLength = new Integer(nMin);
114: }
115:
116: /**
117: * Sets the maximum string length allowed for this range.
118: *
119: * @param nMax Maximum string length
120: */
121: public void setMaxLength(int nMax) {
122: this .m_nMaxLength = new Integer(nMax);
123: }
124:
125: /* (non-Javadoc)
126: * @see com.simulacramedia.vfs.metadata.Range#instantiate(org.w3c.dom.Element)
127: */
128: public void instantiate(Element elRange) {
129: Element elRestriction = XMLUtils.getFirstElementChild(elRange);
130: NodeList nl = elRestriction.getChildNodes();
131: for (int i = 0; i < nl.getLength(); i++) {
132: Node node = nl.item(i);
133: if (node.getNodeType() == Node.ELEMENT_NODE) {
134: Element element = (Element) node;
135: if (element.getLocalName()
136: .equalsIgnoreCase("minLength")) {
137: Node node2 = element.getFirstChild();
138: if (node2.getNodeType() == Node.TEXT_NODE) {
139: this .m_nMinLength = Integer
140: .valueOf(((Text) node2).getNodeValue());
141: }
142: } else if (element.getLocalName().equalsIgnoreCase(
143: "maxLength")
144: || element.getLocalName().equalsIgnoreCase(
145: "length")) {
146: Node node2 = element.getFirstChild();
147: if (node2.getNodeType() == Node.TEXT_NODE) {
148: this .m_nMaxLength = Integer
149: .valueOf(((Text) node2).getNodeValue());
150: }
151: }
152: }
153: }
154: }
155:
156: public String toString() {
157: StringBuffer sBuff = new StringBuffer();
158:
159: sBuff.append("StringRange:\n");
160: if (this .m_nMinLength != null) {
161: sBuff.append("minLength: ").append(this .m_nMinLength)
162: .append("\n");
163: }
164: if (this .m_nMaxLength != null) {
165: sBuff.append("maxLength: ").append(this .m_nMaxLength)
166: .append("\n");
167: }
168:
169: return sBuff.toString();
170: }
171:
172: }
|