1 19 package org.openharmonise.vfs.metadata.range; 20 21 import java.text.*; 22 import java.text.ParseException ; 23 import java.util.Date ; 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 ; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.NodeList ; 32 import org.w3c.dom.Text ; 33 34 35 42 public class DateRange extends AbstractRange implements Range { 43 44 47 public static final String XSD_DATE_FORMAT = "yyyy-MM-dd G"; 48 49 52 private static SimpleDateFormat df = 53 new java.text.SimpleDateFormat (XSD_DATE_FORMAT); 54 55 58 private Date m_dMinInclusive = null; 59 60 63 private Date m_dMaxInclusive = null; 64 65 68 private Date m_dMinExclusive = null; 69 70 73 private Date m_dMaxExclusive = null; 74 75 78 public DateRange() { 79 super(); 80 } 81 82 85 public ValidationResult validate(ValueInstance value) { 86 boolean bIsValid = true; 87 Date maxDt = getMaximum(); 88 Date minDt = getMinimum(); 89 Date 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 108 private Date getDate(String sValue) { 109 Date date = null; 110 111 try { 112 if(sValue != null && sValue.length() > 0) { 113 date = df.parse(sValue); 114 } 115 116 } catch (ParseException e) { 117 e.printStackTrace(); 118 } 119 120 return date; 121 } 122 123 129 public Date getMinimum() { 130 if(this.m_dMinInclusive!=null) { 131 return this.m_dMinInclusive; 132 } else { 133 return this.m_dMinExclusive; 134 } 135 } 136 137 143 public Date getMaximum() { 144 if(this.m_dMinInclusive!=null) { 145 return this.m_dMaxInclusive; 146 } else { 147 return this.m_dMaxExclusive; 148 } 149 } 150 151 156 public void setMinimum(Date dt) { 157 this.m_dMinInclusive = dt; 158 } 159 160 165 public void setMaximum(Date dt) { 166 this.m_dMaxInclusive = dt; 167 } 168 169 172 public void instantiate(Element elRange) { 173 Element elRestriction = XMLUtils.getFirstElementChild(elRange); 174 NodeList nl = elRestriction.getChildNodes(); 175 for(int i=0; i<nl.getLength();i++) { 176 Node node = nl.item(i); 177 if(node.getNodeType()==Node.ELEMENT_NODE) { 178 Element element = (Element )node; 179 if(element.getLocalName().equalsIgnoreCase("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.parse( ((Text )node2).getNodeValue() ); 186 } catch (DOMException e) { 187 e.printStackTrace(); 188 } catch (ParseException e) { 189 e.printStackTrace(); 190 } 191 } 192 } else if(element.getLocalName().equalsIgnoreCase("maxInclusive")) { 193 Node node2 = element.getFirstChild(); 194 if( node2.getNodeType()==Node.TEXT_NODE) { 195 java.text.SimpleDateFormat df = new java.text.SimpleDateFormat ( 196 XSD_DATE_FORMAT); 197 try { 198 this.m_dMaxInclusive = df.parse( ((Text )node2).getNodeValue() ); 199 } catch (DOMException e) { 200 e.printStackTrace(); 201 } catch (ParseException e) { 202 e.printStackTrace(); 203 } 204 } 205 } else if(element.getLocalName().equalsIgnoreCase("minExclusive")) { 206 Node node2 = element.getFirstChild(); 207 if( node2.getNodeType()==Node.TEXT_NODE) { 208 java.text.SimpleDateFormat df = new java.text.SimpleDateFormat ( 209 XSD_DATE_FORMAT); 210 try { 211 this.m_dMinExclusive = df.parse( ((Text )node2).getNodeValue() ); 212 } catch (DOMException e) { 213 e.printStackTrace(); 214 } catch (ParseException e) { 215 e.printStackTrace(); 216 } 217 } 218 } else if(element.getLocalName().equalsIgnoreCase("maxExclusive")) { 219 Node node2 = element.getFirstChild(); 220 if( node2.getNodeType()==Node.TEXT_NODE) { 221 java.text.SimpleDateFormat df = new java.text.SimpleDateFormat ( 222 XSD_DATE_FORMAT); 223 try { 224 this.m_dMaxExclusive = df.parse( ((Text )node2).getNodeValue() ); 225 } catch (DOMException e) { 226 e.printStackTrace(); 227 } catch (ParseException e) { 228 e.printStackTrace(); 229 } 230 } 231 } 232 } 233 } 234 } 235 236 public String toString() { 237 StringBuffer sBuff = new StringBuffer (); 238 239 java.text.SimpleDateFormat df = new java.text.SimpleDateFormat ( 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 |