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 java.text.*;
022: import java.text.ParseException;
023: import java.util.Date;
024:
025: import org.openharmonise.commons.xml.*;
026: import org.openharmonise.vfs.metadata.*;
027: import org.openharmonise.vfs.metadata.value.*;
028: import org.w3c.dom.DOMException;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Node;
031: import org.w3c.dom.NodeList;
032: import org.w3c.dom.Text;
033:
034: /**
035: * This is the range for date type properties.
036: *
037: * @author Matthew Large
038: * @version $Revision: 1.1 $
039: *
040: */
041: public class DateRange extends AbstractRange implements Range {
042:
043: /**
044: * Format for date values.
045: */
046: public static final String XSD_DATE_FORMAT = "yyyy-MM-dd G";
047:
048: /**
049: * Date formatter.
050: */
051: private static SimpleDateFormat df = new java.text.SimpleDateFormat(
052: XSD_DATE_FORMAT);
053:
054: /**
055: * Minimum inclusive date.
056: */
057: private Date m_dMinInclusive = null;
058:
059: /**
060: * Maximum inclusive date.
061: */
062: private Date m_dMaxInclusive = null;
063:
064: /**
065: * Minimum exclusive date.
066: */
067: private Date m_dMinExclusive = null;
068:
069: /**
070: * Maximum exclusive date.
071: */
072: private Date m_dMaxExclusive = null;
073:
074: /**
075: *
076: */
077: public DateRange() {
078: super ();
079: }
080:
081: /* (non-Javadoc)
082: * @see com.simulacramedia.vfs.metadata.range.AbstractRange#validate(java.lang.String)
083: */
084: public ValidationResult validate(ValueInstance value) {
085: boolean bIsValid = true;
086: Date maxDt = getMaximum();
087: Date minDt = getMinimum();
088: Date curDate = getDate(((DateValue) value).getValue());
089:
090: if (maxDt != null && curDate.after(maxDt)) {
091: bIsValid = false;
092: }
093:
094: if (bIsValid == true && minDt != null && curDate.before(minDt)) {
095: bIsValid = false;
096: }
097:
098: return new ValidationResult(bIsValid, "");
099: }
100:
101: /**
102: * Returns a {@link Date} object from formatted text.
103: *
104: * @param sValue Date formatted text
105: * @return Date
106: */
107: private Date getDate(String sValue) {
108: Date date = null;
109:
110: try {
111: if (sValue != null && sValue.length() > 0) {
112: date = df.parse(sValue);
113: }
114:
115: } catch (ParseException e) {
116: e.printStackTrace();
117: }
118:
119: return date;
120: }
121:
122: /**
123: * Returns the minimum date value, will use minimum <i>inclusive</i> value if
124: * there is one, else it returns the minimum <i>exclusive</i> value.
125: *
126: * @return Minimum date value
127: */
128: public Date getMinimum() {
129: if (this .m_dMinInclusive != null) {
130: return this .m_dMinInclusive;
131: } else {
132: return this .m_dMinExclusive;
133: }
134: }
135:
136: /**
137: * Returns the maximum date value, will use maximum <i>inclusive</i> value if
138: * there is one, else it returns the maximum <i>exclusive</i> value.
139: *
140: * @return Maximum date value
141: */
142: public Date getMaximum() {
143: if (this .m_dMinInclusive != null) {
144: return this .m_dMaxInclusive;
145: } else {
146: return this .m_dMaxExclusive;
147: }
148: }
149:
150: /**
151: * Sets the minimum <i>inclusive</i> date value.
152: *
153: * @param dt Date value
154: */
155: public void setMinimum(Date dt) {
156: this .m_dMinInclusive = dt;
157: }
158:
159: /**
160: * Sets the miaximum <i>inclusive</i> date value.
161: *
162: * @param dt Date value
163: */
164: public void setMaximum(Date dt) {
165: this .m_dMaxInclusive = dt;
166: }
167:
168: /* (non-Javadoc)
169: * @see com.simulacramedia.vfs.metadata.Range#instantiate(org.w3c.dom.Element)
170: */
171: public void instantiate(Element elRange) {
172: Element elRestriction = XMLUtils.getFirstElementChild(elRange);
173: NodeList nl = elRestriction.getChildNodes();
174: for (int i = 0; i < nl.getLength(); i++) {
175: Node node = nl.item(i);
176: if (node.getNodeType() == Node.ELEMENT_NODE) {
177: Element element = (Element) node;
178: if (element.getLocalName().equalsIgnoreCase(
179: "minInclusive")) {
180: Node node2 = element.getFirstChild();
181: if (node2.getNodeType() == Node.TEXT_NODE) {
182: java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
183: XSD_DATE_FORMAT);
184: try {
185: this .m_dMinInclusive = df
186: .parse(((Text) node2)
187: .getNodeValue());
188: } catch (DOMException e) {
189: e.printStackTrace();
190: } catch (ParseException e) {
191: e.printStackTrace();
192: }
193: }
194: } else if (element.getLocalName().equalsIgnoreCase(
195: "maxInclusive")) {
196: Node node2 = element.getFirstChild();
197: if (node2.getNodeType() == Node.TEXT_NODE) {
198: java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
199: XSD_DATE_FORMAT);
200: try {
201: this .m_dMaxInclusive = df
202: .parse(((Text) node2)
203: .getNodeValue());
204: } catch (DOMException e) {
205: e.printStackTrace();
206: } catch (ParseException e) {
207: e.printStackTrace();
208: }
209: }
210: } else if (element.getLocalName().equalsIgnoreCase(
211: "minExclusive")) {
212: Node node2 = element.getFirstChild();
213: if (node2.getNodeType() == Node.TEXT_NODE) {
214: java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
215: XSD_DATE_FORMAT);
216: try {
217: this .m_dMinExclusive = df
218: .parse(((Text) node2)
219: .getNodeValue());
220: } catch (DOMException e) {
221: e.printStackTrace();
222: } catch (ParseException e) {
223: e.printStackTrace();
224: }
225: }
226: } else if (element.getLocalName().equalsIgnoreCase(
227: "maxExclusive")) {
228: Node node2 = element.getFirstChild();
229: if (node2.getNodeType() == Node.TEXT_NODE) {
230: java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
231: XSD_DATE_FORMAT);
232: try {
233: this .m_dMaxExclusive = df
234: .parse(((Text) node2)
235: .getNodeValue());
236: } catch (DOMException e) {
237: e.printStackTrace();
238: } catch (ParseException e) {
239: e.printStackTrace();
240: }
241: }
242: }
243: }
244: }
245: }
246:
247: public String toString() {
248: StringBuffer sBuff = new StringBuffer();
249:
250: java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
251: XSD_DATE_FORMAT);
252:
253: sBuff.append("DateRange:\n");
254: if (this .m_dMinInclusive != null) {
255: sBuff.append("minInclusive: ").append(
256: df.format(this .m_dMinInclusive)).append("\n");
257: }
258: if (this .m_dMaxInclusive != null) {
259: sBuff.append("maxInclusive: ").append(
260: df.format(this .m_dMaxInclusive)).append("\n");
261: }
262: if (this .m_dMinExclusive != null) {
263: sBuff.append("minExclusive: ").append(
264: df.format(this .m_dMinExclusive)).append("\n");
265: }
266: if (this .m_dMaxExclusive != null) {
267: sBuff.append("maxExclusive: ").append(
268: df.format(this .m_dMaxExclusive)).append("\n");
269: }
270:
271: return sBuff.toString();
272: }
273:
274: }
|