KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > vfs > metadata > range > DateTimeRange


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.vfs.metadata.range;
20
21 import java.text.*;
22 import java.text.ParseException JavaDoc;
23 import java.util.Date JavaDoc;
24
25 import org.openharmonise.commons.xml.*;
26 import org.openharmonise.vfs.metadata.*;
27 import org.openharmonise.vfs.metadata.value.*;
28 import org.w3c.dom.DOMException JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32 import org.w3c.dom.Text JavaDoc;
33
34
35 /**
36  * This is the range for date and time type properties.
37  *
38  * @author Matthew Large
39  * @version $Revision: 1.1 $
40  *
41  */

42 public class DateTimeRange extends AbstractRange implements Range {
43     
44     /**
45      * Format for date values.
46      */

47     public static final String JavaDoc XSD_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss G";
48
49     /**
50      * Date formatter.
51      */

52     private static SimpleDateFormat df =
53                 new java.text.SimpleDateFormat JavaDoc(XSD_DATE_FORMAT);
54
55     /**
56      * Minimum inclusive date.
57      */

58     private Date JavaDoc m_dMinInclusive = null;
59     
60     /**
61      * Maximum inclusive date.
62      */

63     private Date JavaDoc m_dMaxInclusive = null;
64     
65     /**
66      * Minimum exclusive date.
67      */

68     private Date JavaDoc m_dMinExclusive = null;
69     
70     /**
71      * Maximum exclusive date.
72      */

73     private Date JavaDoc m_dMaxExclusive = null;
74
75     /**
76      *
77      */

78     public DateTimeRange() {
79         super();
80     }
81
82     /**
83      * Returns the minimum date value, will use minimum <i>inclusive</i> value if
84      * there is one, else it returns the minimum <i>exclusive</i> value.
85      *
86      * @return Minimum date value
87      */

88     public Date JavaDoc getMinimum() {
89         if(this.m_dMinInclusive!=null) {
90             return this.m_dMinInclusive;
91         } else {
92             return this.m_dMinExclusive;
93         }
94     }
95
96     /**
97      * Returns the maximum date value, will use maximum <i>inclusive</i> value if
98      * there is one, else it returns the maximum <i>exclusive</i> value.
99      *
100      * @return Maximum date value
101      */

102     public Date JavaDoc getMaximum() {
103         if(this.m_dMinInclusive!=null) {
104             return this.m_dMaxInclusive;
105         } else {
106             return this.m_dMaxExclusive;
107         }
108     }
109
110     /**
111      * Sets the minimum <i>inclusive</i> date value.
112      *
113      * @param dt Date value
114      */

115     public void setMinimum(Date JavaDoc dt) {
116         this.m_dMinInclusive = dt;
117     }
118
119     /**
120      * Sets the miaximum <i>inclusive</i> date value.
121      *
122      * @param dt Date value
123      */

124     public void setMaximum(Date JavaDoc dt) {
125         this.m_dMaxInclusive = dt;
126     }
127
128     /* (non-Javadoc)
129      * @see com.simulacramedia.vfs.metadata.range.AbstractRange#validate(java.lang.String)
130      */

131     public ValidationResult validate(ValueInstance value) {
132         boolean bIsValid = true;
133         Date JavaDoc maxDt = getMaximum();
134         Date JavaDoc minDt = getMinimum();
135         Date JavaDoc curDate = getDate(((DateTimeValue)value).getValue());
136         
137         if(maxDt != null && curDate.after(maxDt)) {
138             bIsValid = false;
139         }
140         
141         if(bIsValid == true && minDt != null && curDate.before(minDt)) {
142             bIsValid = false;
143         }
144         
145         return new ValidationResult(bIsValid,"");
146     }
147
148     /* (non-Javadoc)
149      * @see com.simulacramedia.vfs.metadata.Range#instantiate(org.w3c.dom.Element)
150      */

151     public void instantiate(Element JavaDoc elRange) {
152         Element JavaDoc elRestriction = XMLUtils.getFirstElementChild(elRange);
153         NodeList JavaDoc nl = elRestriction.getChildNodes();
154         for(int i=0; i<nl.getLength();i++) {
155             Node JavaDoc node = nl.item(i);
156             if(node.getNodeType()==Node.ELEMENT_NODE) {
157                 Element JavaDoc element = (Element JavaDoc)node;
158                 if(element.getLocalName().equalsIgnoreCase("minInclusive")) {
159                     Node JavaDoc node2 = element.getFirstChild();
160                     if( node2.getNodeType()==Node.TEXT_NODE) {
161                         java.text.SimpleDateFormat JavaDoc df = new java.text.SimpleDateFormat JavaDoc(
162                                                                 XSD_DATE_FORMAT);
163                         try {
164                             this.m_dMinInclusive = df.parse( ((Text JavaDoc)node2).getNodeValue() );
165                         } catch (DOMException JavaDoc e) {
166                             e.printStackTrace();
167                         } catch (ParseException JavaDoc e) {
168                             e.printStackTrace();
169                         }
170                     }
171                 } else if(element.getLocalName().equalsIgnoreCase("maxInclusive")) {
172                     Node JavaDoc node2 = element.getFirstChild();
173                     if( node2.getNodeType()==Node.TEXT_NODE) {
174                         java.text.SimpleDateFormat JavaDoc df = new java.text.SimpleDateFormat JavaDoc(
175                                                                 XSD_DATE_FORMAT);
176                         try {
177                             this.m_dMaxInclusive = df.parse( ((Text JavaDoc)node2).getNodeValue() );
178                         } catch (DOMException JavaDoc e) {
179                             e.printStackTrace();
180                         } catch (ParseException JavaDoc e) {
181                             e.printStackTrace();
182                         }
183                     }
184                 } else if(element.getLocalName().equalsIgnoreCase("minExclusive")) {
185                     Node JavaDoc node2 = element.getFirstChild();
186                     if( node2.getNodeType()==Node.TEXT_NODE) {
187                         java.text.SimpleDateFormat JavaDoc df = new java.text.SimpleDateFormat JavaDoc(
188                                                                 XSD_DATE_FORMAT);
189                         try {
190                             this.m_dMinExclusive = df.parse( ((Text JavaDoc)node2).getNodeValue() );
191                         } catch (DOMException JavaDoc e) {
192                             e.printStackTrace();
193                         } catch (ParseException JavaDoc e) {
194                             e.printStackTrace();
195                         }
196                     }
197                 } else if(element.getLocalName().equalsIgnoreCase("maxExclusive")) {
198                     Node JavaDoc node2 = element.getFirstChild();
199                     if( node2.getNodeType()==Node.TEXT_NODE) {
200                         java.text.SimpleDateFormat JavaDoc df = new java.text.SimpleDateFormat JavaDoc(
201                                                                 XSD_DATE_FORMAT);
202                         try {
203                             this.m_dMaxExclusive = df.parse( ((Text JavaDoc)node2).getNodeValue() );
204                         } catch (DOMException JavaDoc e) {
205                             e.printStackTrace();
206                         } catch (ParseException JavaDoc e) {
207                             e.printStackTrace();
208                         }
209                     }
210                 }
211             }
212         }
213     }
214
215     public String JavaDoc toString() {
216         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
217         
218         java.text.SimpleDateFormat JavaDoc df = new java.text.SimpleDateFormat JavaDoc(
219                                                                         XSD_DATE_FORMAT);
220         
221         sBuff.append("DateTimeRange:\n");
222         if(this.m_dMinInclusive!=null) {
223             sBuff.append("minInclusive: ").append(df.format(this.m_dMinInclusive)).append("\n");
224         }
225         if(this.m_dMaxInclusive!=null) {
226             sBuff.append("maxInclusive: ").append(df.format(this.m_dMaxInclusive)).append("\n");
227         }
228         if(this.m_dMinExclusive!=null) {
229             sBuff.append("minExclusive: ").append(df.format(this.m_dMinExclusive)).append("\n");
230         }
231         if(this.m_dMaxExclusive!=null) {
232             sBuff.append("maxExclusive: ").append(df.format(this.m_dMaxExclusive)).append("\n");
233         }
234         
235         return sBuff.toString();
236     }
237
238     /**
239      * Returns a {@link Date} object from formatted text.
240      *
241      * @param sValue Date formatted text
242      * @return Date
243      */

244     private Date JavaDoc getDate(String JavaDoc sValue) {
245         Date JavaDoc date = null;
246     
247         try {
248             if(sValue != null && sValue.length() > 0) {
249                 date = df.parse(sValue);
250             }
251         } catch (ParseException JavaDoc e) {
252             e.printStackTrace();
253         }
254
255         return date;
256     }
257
258 }
259
Popular Tags