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.rm.resources.metadata.properties.ranges;
020:
021: import java.util.StringTokenizer;
022:
023: import org.openharmonise.rm.PopulateException;
024: import org.openharmonise.rm.metadata.GeneralPropertyInstance;
025: import org.openharmonise.rm.publishing.*;
026: import org.w3c.dom.Element;
027:
028: /**
029: *
030: * Class which represents a <code>Property</code> range which is
031: * restricted to <code>String</code> values.
032: *
033: * @author Michael Bell
034: * @version $Revision: 1.2 $
035: *
036: */
037: public class StringRange extends AbstractRange implements Range,
038: Publishable {
039:
040: /**
041: * String range XML tag name
042: */
043: public final static String TAG_STRING_RANGE = "StringRange";
044:
045: /**
046: * Minimum length XML tag name
047: */
048: public final static String TAG_MIN_LENGTH = "MinLength";
049:
050: /**
051: * Maximum length XML tag name
052: */
053: public final static String TAG_MAX_LENGTH = "MaxLength";
054:
055: /**
056: * The minimum length for a <code>String</code> in this range
057: */
058: private int m_minLength = 0;
059:
060: /**
061: * The maximum length for a <code>String</code> in this range
062: */
063: private int m_maxLength = -1;
064:
065: /**
066: * The separator character used to separate range restrictions when compiling
067: * the <code>String</code> representation for the database details field.
068: */
069: static final private String SEPARATOR = ":";
070:
071: /**
072: * Constructs a new <code>StringRange</code>.
073: *
074: */
075: public StringRange() {
076: super (String.class.getName());
077: }
078:
079: /**
080: * Constructs a <code>StringRange</code> with restrictions specified
081: * in a <code>String</code> representation.
082: *
083: * @param sDetails the <code>String</code> representation of the restrictions
084: * on this range
085: */
086: public StringRange(String sDetails) {
087: super (String.class.getName(), sDetails);
088: }
089:
090: /* (non-Javadoc)
091: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#isValid(java.lang.Object)
092: */
093: public boolean isValid(Object obj) {
094: boolean bIsValid = false;
095:
096: if (obj instanceof String) {
097: bIsValid = true;
098: String strObj = (String) obj;
099: int nLength = strObj.length();
100:
101: if (m_minLength > 0) {
102: bIsValid = !(nLength <= m_minLength);
103: }
104:
105: if (m_maxLength > 0 && bIsValid == true) {
106: bIsValid = !(nLength >= m_maxLength);
107: }
108: }
109:
110: return bIsValid;
111: }
112:
113: /**
114: * Sets the minimum length restriction on this range.
115: *
116: * @param nMinLength the minimum length restriction on this range
117: * @throws InvalidRangeRestrictionException
118: */
119: public void setMinLength(int nMinLength)
120: 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
130: + " is not a valid minumum");
131: }
132:
133: if (m_minLength != nMinLength) {
134: isChanged(true);
135: m_minLength = nMinLength;
136: }
137: }
138:
139: }
140:
141: /**
142: * Returns the minimum length restriction on this range.
143: *
144: * @return the minimum length restriction on this range
145: */
146: public int getMinLength() {
147: return m_minLength;
148: }
149:
150: /**
151: * Sets the maximum length restriction on this range.
152: *
153: * @param nMaxLength the maximum length restriction on this range
154: * @throws InvalidRangeRestrictionException
155: */
156: public void setMaxLength(int nMaxLength)
157: throws InvalidRangeRestrictionException {
158: if (nMaxLength > 2000 || nMaxLength < m_minLength) {
159: throw new InvalidRangeRestrictionException(nMaxLength
160: + " is not permitted as a maximum");
161: }
162: if (m_maxLength != nMaxLength) {
163: isChanged(true);
164: }
165: m_maxLength = nMaxLength;
166: }
167:
168: /**
169: * Returns the maximum length restriction on this range.
170: *
171: * @return the maximum length restriction on this range
172: */
173: public int getMaxLength() {
174: return m_maxLength;
175: }
176:
177: /* (non-Javadoc)
178: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getDetails()
179: */
180: public String getDetails() {
181:
182: if (isChanged() == true) {
183: StringBuffer strbuf = new StringBuffer();
184: strbuf.append(String.valueOf(m_minLength))
185: .append(SEPARATOR).append(m_maxLength);
186: super .setDetails(strbuf.toString());
187: }
188:
189: return super .getDetails();
190: }
191:
192: /* (non-Javadoc)
193: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#setDetails(java.lang.String)
194: */
195: public void setDetails(String sDetails) {
196: if (sDetails != null) {
197: StringTokenizer tokenizer = new StringTokenizer(sDetails,
198: SEPARATOR);
199:
200: int i = 0;
201:
202: while (tokenizer.hasMoreTokens()) {
203: String token = tokenizer.nextToken();
204:
205: int nTmp = Integer.parseInt(token);
206:
207: if (i == 0) {
208: m_minLength = nTmp;
209: } else if (i == 1) {
210: m_maxLength = nTmp;
211: }
212: i++;
213: }
214: }
215:
216: super .setDetails(sDetails);
217:
218: }
219:
220: /* (non-Javadoc)
221: * @see java.lang.Object#equals(java.lang.Object)
222: */
223: public boolean equals(Object obj) {
224: boolean bResult = false;
225:
226: if (obj instanceof StringRange) {
227: if (super .equals(obj) == true) {
228: StringRange sRange = (StringRange) obj;
229:
230: if (m_minLength == sRange.getMinLength()
231: && m_maxLength == sRange.getMaxLength()) {
232: bResult = true;
233: }
234: }
235: }
236:
237: return bResult;
238: }
239:
240: /* (non-Javadoc)
241: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getPropertyInstanceClass()
242: */
243: public Class getPropertyInstanceClass()
244: throws ClassNotFoundException {
245: return GeneralPropertyInstance.class;
246: }
247:
248: /* (non-Javadoc)
249: * @see org.openharmonise.rm.publishing.Publishable#getTagName()
250: */
251: public String getTagName() {
252: return TAG_STRING_RANGE;
253: }
254:
255: /**
256: * Returns the <code>int</code> value taken from the first child node
257: * of the specified XML element.
258: *
259: * @param xmlElement the XML element
260: * @return the <code>int</code> value
261: */
262: private int getChildIntValue(Element el) {
263: int nVal = 0;
264:
265: String sVal = el.getFirstChild().getNodeValue();
266:
267: if (sVal != null) {
268: nVal = Integer.parseInt(sVal);
269: }
270:
271: return nVal;
272: }
273:
274: /* (non-Javadoc)
275: * @see org.openharmonise.rm.publishing.Publishable#populate(org.w3c.dom.Element, org.openharmonise.rm.publishing.State)
276: */
277: /* (non-Javadoc)
278: * @see org.openharmonise.rm.publishing.Publishable#populate(org.w3c.dom.Element, org.openharmonise.rm.publishing.State)
279: */
280: public void populate(Element xmlElement, State state)
281: throws PopulateException {
282:
283: String sTagname = xmlElement.getTagName();
284:
285: try {
286: if (sTagname.equals(TAG_MIN_LENGTH)) {
287: setMinLength(getChildIntValue(xmlElement));
288: } else if (sTagname.equals(TAG_MAX_LENGTH)) {
289: setMaxLength(getChildIntValue(xmlElement));
290: } else {
291: super .populate(xmlElement, state);
292: }
293: } catch (InvalidRangeRestrictionException e) {
294: throw new PopulateException(e);
295: } catch (PopulateException e) {
296: throw new PopulateException(e);
297: }
298: }
299:
300: }
|