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 integer type properties.
031: *
032: * @author Matthew Large
033: * @version $Revision: 1.1 $
034: *
035: */
036: public class IntegerRange extends AbstractRange implements Range {
037:
038: /**
039: * Minimum inclusive value.
040: */
041: private Integer m_nMinInclusive = null;
042:
043: /**
044: * Maximum inclusive value.
045: */
046: private Integer m_nMaxInclusive = null;
047:
048: /**
049: * Minimum exclusive value.
050: */
051: private Integer m_nMinExclusive = null;
052:
053: /**
054: * Maximum exclusive value.
055: */
056: private Integer m_nMaxExclusive = null;
057:
058: /**
059: *
060: */
061: public IntegerRange() {
062: super ();
063: }
064:
065: /**
066: * Returns the minimum integer value, will use minimum <i>inclusive</i> value if
067: * there is one, else it returns the minimum <i>exclusive</i> value.
068: *
069: * @return Minimum integer value
070: */
071: public Integer getMinimum() {
072: if (this .m_nMinInclusive != null) {
073: return this .m_nMinInclusive;
074: } else {
075: return this .m_nMinExclusive;
076: }
077: }
078:
079: /**
080: * Returns the maximum integer value, will use maximum <i>inclusive</i> value if
081: * there is one, else it returns the maximum <i>exclusive</i> value.
082: *
083: * @return Maximum integer value
084: */
085: public Integer getMaximum() {
086: if (this .m_nMaxInclusive != null) {
087: return this .m_nMaxInclusive;
088: } else {
089: return this .m_nMaxExclusive;
090: }
091: }
092:
093: /**
094: * Sets the minimum inclusive integer value.
095: *
096: * @param nVal Value
097: */
098: public void setMinimum(Integer nVal) {
099: this .m_nMinInclusive = nVal;
100: }
101:
102: /**
103: * Sets the minimum inclusive integer value.
104: *
105: * @param nVal Value
106: */
107: public void setMinimum(int nVal) {
108: this .m_nMinInclusive = new Integer(nVal);
109: }
110:
111: /**
112: * Sets the maximum inclusive integer value.
113: *
114: * @param nVal Value
115: */
116: public void setMaximum(Integer nVal) {
117: this .m_nMaxInclusive = nVal;
118: }
119:
120: /**
121: * Sets the maximum inclusive integer value.
122: *
123: * @param nVal Value
124: */
125: public void setMaximum(int nVal) {
126: this .m_nMaxInclusive = new Integer(nVal);
127: }
128:
129: /* (non-Javadoc)
130: * @see com.simulacramedia.vfs.metadata.range.AbstractRange#validate(java.lang.String)
131: */
132: public ValidationResult validate(ValueInstance value) {
133: int nVal = ((IntegerValue) value).getValue();
134: boolean bIsValid = true;
135:
136: if (m_nMinInclusive != null) {
137: bIsValid = m_nMinInclusive.intValue() <= nVal;
138: } else if (m_nMinExclusive != null) {
139: bIsValid = m_nMinExclusive.intValue() < nVal;
140: }
141:
142: if (bIsValid == true) {
143: if (m_nMaxInclusive != null) {
144: bIsValid = m_nMaxInclusive.intValue() >= nVal;
145: } else if (m_nMaxExclusive != null) {
146: bIsValid = m_nMaxExclusive.intValue() > nVal;
147: }
148: }
149:
150: return new ValidationResult(bIsValid, "");
151: }
152:
153: /* (non-Javadoc)
154: * @see com.simulacramedia.vfs.metadata.Range#instantiate(org.w3c.dom.Element)
155: */
156: public void instantiate(Element elRange) {
157: Element elRestriction = XMLUtils.getFirstElementChild(elRange);
158: NodeList nl = elRestriction.getChildNodes();
159: for (int i = 0; i < nl.getLength(); i++) {
160: Node node = nl.item(i);
161: if (node.getNodeType() == Node.ELEMENT_NODE) {
162: Element element = (Element) node;
163: if (element.getLocalName().equalsIgnoreCase(
164: "minInclusive")) {
165: Node node2 = element.getFirstChild();
166: if (node2.getNodeType() == Node.TEXT_NODE) {
167: this .m_nMinInclusive = Integer
168: .valueOf(((Text) node2).getNodeValue());
169: }
170: } else if (element.getLocalName().equalsIgnoreCase(
171: "maxInclusive")) {
172: Node node2 = element.getFirstChild();
173: if (node2.getNodeType() == Node.TEXT_NODE) {
174: this .m_nMaxInclusive = Integer
175: .valueOf(((Text) node2).getNodeValue());
176: }
177: } else if (element.getLocalName().equalsIgnoreCase(
178: "minExclusive")) {
179: Node node2 = element.getFirstChild();
180: if (node2.getNodeType() == Node.TEXT_NODE) {
181: this .m_nMinExclusive = Integer
182: .valueOf(((Text) node2).getNodeValue());
183: }
184: } else if (element.getLocalName().equalsIgnoreCase(
185: "maxExclusive")) {
186: Node node2 = element.getFirstChild();
187: if (node2.getNodeType() == Node.TEXT_NODE) {
188: this .m_nMaxExclusive = Integer
189: .valueOf(((Text) node2).getNodeValue());
190: }
191: }
192: }
193: }
194: }
195:
196: public String toString() {
197: StringBuffer sBuff = new StringBuffer();
198:
199: sBuff.append("IntegerRange:\n");
200: if (this .m_nMinInclusive != null) {
201: sBuff.append("minInclusive: ").append(this .m_nMinInclusive)
202: .append("\n");
203: }
204: if (this .m_nMaxInclusive != null) {
205: sBuff.append("maxInclusive: ").append(this .m_nMaxInclusive)
206: .append("\n");
207: }
208: if (this .m_nMinExclusive != null) {
209: sBuff.append("minExclusive: ").append(this .m_nMinExclusive)
210: .append("\n");
211: }
212: if (this .m_nMaxExclusive != null) {
213: sBuff.append("maxExclusive: ").append(this .m_nMaxExclusive)
214: .append("\n");
215: }
216:
217: return sBuff.toString();
218: }
219:
220: }
|