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