KickJava   Java API By Example, From Geeks To Geeks.

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


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 type properties.
37  *
38  * @author Matthew Large
39  * @version $Revision: 1.1 $
40  *
41  */

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

47     public static final String JavaDoc XSD_DATE_FORMAT = "yyyy-MM-dd 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 DateRange() {
79         super();
80     }
81
82     /* (non-Javadoc)
83      * @see com.simulacramedia.vfs.metadata.range.AbstractRange#validate(java.lang.String)
84      */

85     public ValidationResult validate(ValueInstance value) {
86         boolean bIsValid = true;
87         Date JavaDoc maxDt = getMaximum();
88         Date JavaDoc minDt = getMinimum();
89         Date JavaDoc curDate = getDate(((DateValue)value).getValue());
90         
91         if(maxDt != null && curDate.after(maxDt)) {
92             bIsValid = false;
93         }
94         
95         if(bIsValid == true && minDt != null && curDate.before(minDt)) {
96             bIsValid = false;
97         }
98         
99         return new ValidationResult(bIsValid,"");
100     }
101     
102     /**
103      * Returns a {@link Date} object from formatted text.
104      *
105      * @param sValue Date formatted text
106      * @return Date
107      */

108     private Date JavaDoc getDate(String JavaDoc sValue) {
109         Date JavaDoc date = null;
110         
111         try {
112             if(sValue != null && sValue.length() > 0) {
113                 date = df.parse(sValue);
114             }
115             
116         } catch (ParseException JavaDoc e) {
117             e.printStackTrace();
118         }
119     
120         return date;
121     }
122
123     /**
124      * Returns the minimum date value, will use minimum <i>inclusive</i> value if
125      * there is one, else it returns the minimum <i>exclusive</i> value.
126      *
127      * @return Minimum date value
128      */

129     public Date JavaDoc getMinimum() {
130         if(this.m_dMinInclusive!=null) {
131             return this.m_dMinInclusive;
132         } else {
133             return this.m_dMinExclusive;
134         }
135     }
136
137     /**
138      * Returns the maximum date value, will use maximum <i>inclusive</i> value if
139      * there is one, else it returns the maximum <i>exclusive</i> value.
140      *
141      * @return Maximum date value
142      */

143     public Date JavaDoc getMaximum() {
144         if(this.m_dMinInclusive!=null) {
145             return this.m_dMaxInclusive;
146         } else {
147             return this.m_dMaxExclusive;
148         }
149     }
150     
151     /**
152      * Sets the minimum <i>inclusive</i> date value.
153      *
154      * @param dt Date value
155      */

156     public void setMinimum(Date JavaDoc dt) {
157         this.m_dMinInclusive = dt;
158     }
159     
160     /**
161      * Sets the miaximum <i>inclusive</i> date value.
162      *
163      * @param dt Date value
164      */

165     public void setMaximum(Date JavaDoc dt) {
166         this.m_dMaxInclusive = dt;
167     }
168
169     /* (non-Javadoc)
170      * @see com.simulacramedia.vfs.metadata.Range#instantiate(org.w3c.dom.Element)
171      */

172     public void instantiate(Element JavaDoc elRange) {
173         Element JavaDoc elRestriction = XMLUtils.getFirstElementChild(elRange);
174         NodeList JavaDoc nl = elRestriction.getChildNodes();
175         for(int i=0; i<nl.getLength();i++) {
176             Node JavaDoc node = nl.item(i);
177             if(node.getNodeType()==Node.ELEMENT_NODE) {
178                 Element JavaDoc element = (Element JavaDoc)node;
179                 if(element.getLocalName().equalsIgnoreCase("minInclusive")) {
180                     Node JavaDoc node2 = element.getFirstChild();
181                     if( node2.getNodeType()==Node.TEXT_NODE) {
182                         java.text.SimpleDateFormat JavaDoc df = new java.text.SimpleDateFormat JavaDoc(
183                                                                 XSD_DATE_FORMAT);
184                         try {
185                             this.m_dMinInclusive = df.parse( ((Text JavaDoc)node2).getNodeValue() );
186                         } catch (DOMException JavaDoc e) {
187                             e.printStackTrace();
188                         } catch (ParseException JavaDoc e) {
189                             e.printStackTrace();
190                         }
191                     }
192                 } else if(element.getLocalName().equalsIgnoreCase("maxInclusive")) {
193                     Node JavaDoc node2 = element.getFirstChild();
194                     if( node2.getNodeType()==Node.TEXT_NODE) {
195                         java.text.SimpleDateFormat JavaDoc df = new java.text.SimpleDateFormat JavaDoc(
196                                                                 XSD_DATE_FORMAT);
197                         try {
198                             this.m_dMaxInclusive = df.parse( ((Text JavaDoc)node2).getNodeValue() );
199                         } catch (DOMException JavaDoc e) {
200                             e.printStackTrace();
201                         } catch (ParseException JavaDoc e) {
202                             e.printStackTrace();
203                         }
204                     }
205                 } else if(element.getLocalName().equalsIgnoreCase("minExclusive")) {
206                     Node JavaDoc node2 = element.getFirstChild();
207                     if( node2.getNodeType()==Node.TEXT_NODE) {
208                         java.text.SimpleDateFormat JavaDoc df = new java.text.SimpleDateFormat JavaDoc(
209                                                                 XSD_DATE_FORMAT);
210                         try {
211                             this.m_dMinExclusive = df.parse( ((Text JavaDoc)node2).getNodeValue() );
212                         } catch (DOMException JavaDoc e) {
213                             e.printStackTrace();
214                         } catch (ParseException JavaDoc e) {
215                             e.printStackTrace();
216                         }
217                     }
218                 } else if(element.getLocalName().equalsIgnoreCase("maxExclusive")) {
219                     Node JavaDoc node2 = element.getFirstChild();
220                     if( node2.getNodeType()==Node.TEXT_NODE) {
221                         java.text.SimpleDateFormat JavaDoc df = new java.text.SimpleDateFormat JavaDoc(
222                                                                 XSD_DATE_FORMAT);
223                         try {
224                             this.m_dMaxExclusive = df.parse( ((Text JavaDoc)node2).getNodeValue() );
225                         } catch (DOMException JavaDoc e) {
226                             e.printStackTrace();
227                         } catch (ParseException JavaDoc e) {
228                             e.printStackTrace();
229                         }
230                     }
231                 }
232             }
233         }
234     }
235
236     public String JavaDoc toString() {
237         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
238         
239         java.text.SimpleDateFormat JavaDoc df = new java.text.SimpleDateFormat JavaDoc(
240                                                                         XSD_DATE_FORMAT);
241         
242         sBuff.append("DateRange:\n");
243         if(this.m_dMinInclusive!=null) {
244             sBuff.append("minInclusive: ").append(df.format(this.m_dMinInclusive)).append("\n");
245         }
246         if(this.m_dMaxInclusive!=null) {
247             sBuff.append("maxInclusive: ").append(df.format(this.m_dMaxInclusive)).append("\n");
248         }
249         if(this.m_dMinExclusive!=null) {
250             sBuff.append("minExclusive: ").append(df.format(this.m_dMinExclusive)).append("\n");
251         }
252         if(this.m_dMaxExclusive!=null) {
253             sBuff.append("maxExclusive: ").append(df.format(this.m_dMaxExclusive)).append("\n");
254         }
255         
256         return sBuff.toString();
257     }
258
259 }
260
Popular Tags