1 19 package org.openharmonise.webdav.client.value; 20 21 import java.text.*; 22 import java.util.*; 23 24 import org.openharmonise.commons.xml.namespace.*; 25 import org.openharmonise.vfs.metadata.*; 26 import org.openharmonise.vfs.metadata.range.*; 27 import org.openharmonise.vfs.metadata.value.*; 28 import org.w3c.dom.*; 29 30 31 38 public class DAVRangeValue extends RangeValue { 39 40 43 private static final String XSD_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"; 44 45 48 private static final String XSD_DATE_FORMAT = "yyyy-MM-dd"; 49 50 53 public DAVRangeValue() { 54 super(); 55 } 56 57 62 public DAVRangeValue(Range range) { 63 super(range); 64 } 65 66 73 public static void toXML(Document xmlDoc, Element elProp, List aValues) { 74 if(aValues.size()>0) { 75 RangeValue value = (RangeValue) aValues.get(0); 76 Range range = value.getRange(); 77 if(range != null ) { 78 if(range instanceof BooleanRange) { 79 Element elRestriction = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "restriction"); 80 elProp.appendChild(elRestriction); 81 elRestriction.setAttributeNS(NamespaceType.XML_SCHEMA.getURI(), "base", NamespaceType.XML_SCHEMA.getPrefix()+":boolean"); 82 83 Element elLabels = xmlDoc.createElementNS(NamespaceType.OHRM.getURI(), "labels"); 84 elRestriction.appendChild(elLabels); 85 86 elLabels.setAttributeNS(NamespaceType.OHRM.getURI(), "trueLabel", ((BooleanRange)range).getTrueText()); 87 elLabels.setAttributeNS(NamespaceType.OHRM.getURI(), "falseLabel", ((BooleanRange)range).getFalseText()); 88 } else if(range instanceof CollectionRange) { 89 Element elResourceType = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "resourcetype"); 90 elProp.appendChild(elResourceType); 91 92 Element elPropRes = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "collection"); 93 elResourceType.appendChild(elPropRes); 94 95 List aHREFs = ((CollectionRange)range).getHREFs(); 96 Iterator itor = aHREFs.iterator(); 97 while (itor.hasNext()) { 98 String sHREF = (String ) itor.next(); 99 Element elHREF = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "href"); 100 elProp.appendChild(elHREF); 101 Text txt = xmlDoc.createTextNode(sHREF); 102 elHREF.appendChild(txt); 103 } 104 } else if(range instanceof DateRange) { 105 Element elRestriction = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "restriction"); 106 elProp.appendChild(elRestriction); 107 elRestriction.setAttributeNS(NamespaceType.XML_SCHEMA.getURI(), "base", NamespaceType.XML_SCHEMA.getPrefix()+":date"); 108 DateRange actualRange = (DateRange) range; 109 if(actualRange.getMinimum()!=null) { 110 SimpleDateFormat format = new SimpleDateFormat(DateRange.XSD_DATE_FORMAT); 111 String sValue = format.format(actualRange.getMinimum()); 112 Element elTemp = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "minInclusive"); 113 elRestriction.appendChild(elTemp); 114 Text txt = xmlDoc.createTextNode(sValue); 115 elTemp.appendChild(txt); 116 } 117 if(actualRange.getMaximum()!=null) { 118 SimpleDateFormat format = new SimpleDateFormat(DateRange.XSD_DATE_FORMAT); 119 String sValue = format.format(actualRange.getMaximum()); 120 Element elTemp = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "maxInclusive"); 121 elRestriction.appendChild(elTemp); 122 Text txt = xmlDoc.createTextNode(sValue); 123 elTemp.appendChild(txt); 124 } 125 } else if(range instanceof DateTimeRange) { 126 Element elRestriction = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "restriction"); 127 elProp.appendChild(elRestriction); 128 elRestriction.setAttributeNS(NamespaceType.XML_SCHEMA.getURI(), "base", NamespaceType.XML_SCHEMA.getPrefix()+":dateTime"); 129 DateTimeRange actualRange = (DateTimeRange) range; 130 if(actualRange.getMinimum()!=null) { 131 SimpleDateFormat format = new SimpleDateFormat(DateTimeRange.XSD_DATE_FORMAT); 132 String sValue = format.format(actualRange.getMinimum()); 133 Element elTemp = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "minInclusive"); 134 elRestriction.appendChild(elTemp); 135 Text txt = xmlDoc.createTextNode(sValue); 136 elTemp.appendChild(txt); 137 } 138 if(actualRange.getMaximum()!=null) { 139 SimpleDateFormat format = new SimpleDateFormat(DateTimeRange.XSD_DATE_FORMAT); 140 String sValue = format.format(actualRange.getMaximum()); 141 Element elTemp = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "maxInclusive"); 142 elRestriction.appendChild(elTemp); 143 Text txt = xmlDoc.createTextNode(sValue); 144 elTemp.appendChild(txt); 145 } 146 } else if(range instanceof FloatRange) { 147 Element elRestriction = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "restriction"); 148 elProp.appendChild(elRestriction); 149 elRestriction.setAttributeNS(NamespaceType.XML_SCHEMA.getURI(), "base", NamespaceType.XML_SCHEMA.getPrefix()+":float"); 150 FloatRange actualRange = (FloatRange) range; 151 if(actualRange.getMinimum()!=null) { 152 String sValue = actualRange.getMinimum().toString(); 153 Element elTemp = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "minInclusive"); 154 elRestriction.appendChild(elTemp); 155 Text txt = xmlDoc.createTextNode(sValue); 156 elTemp.appendChild(txt); 157 } 158 if(actualRange.getMaximum()!=null) { 159 String sValue = actualRange.getMaximum().toString(); 160 Element elTemp = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "maxInclusive"); 161 elRestriction.appendChild(elTemp); 162 Text txt = xmlDoc.createTextNode(sValue); 163 elTemp.appendChild(txt); 164 } 165 } else if(range instanceof IntegerRange) { 166 Element elRestriction = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "restriction"); 167 elProp.appendChild(elRestriction); 168 elRestriction.setAttributeNS(NamespaceType.XML_SCHEMA.getURI(), "base", NamespaceType.XML_SCHEMA.getPrefix()+":integer"); 169 IntegerRange actualRange = (IntegerRange) range; 170 if(actualRange.getMinimum()!=null) { 171 String sValue = actualRange.getMinimum().toString(); 172 Element elTemp = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "minInclusive"); 173 elRestriction.appendChild(elTemp); 174 Text txt = xmlDoc.createTextNode(sValue); 175 elTemp.appendChild(txt); 176 } 177 if(actualRange.getMaximum()!=null) { 178 String sValue = actualRange.getMaximum().toString(); 179 Element elTemp = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "maxInclusive"); 180 elRestriction.appendChild(elTemp); 181 Text txt = xmlDoc.createTextNode(sValue); 182 elTemp.appendChild(txt); 183 } 184 } else if(range instanceof PropertyRange) { 185 Element elResourceType = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "resourcetype"); 186 elProp.appendChild(elResourceType); 187 188 Element elPropRes = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "property-resource"); 189 elResourceType.appendChild(elPropRes); 190 191 List aHREFs = ((PropertyRange)range).getHREFs(); 192 Iterator itor = aHREFs.iterator(); 193 while (itor.hasNext()) { 194 String sHREF = (String ) itor.next(); 195 Element elHREF = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "href"); 196 elProp.appendChild(elHREF); 197 Text txt = xmlDoc.createTextNode(sHREF); 198 elHREF.appendChild(txt); 199 } 200 } else if(range instanceof ResourceRange) { 201 Element elResourceType = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "resourcetype"); 202 elProp.appendChild(elResourceType); 203 204 Element elPropRes = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "resource"); 205 elResourceType.appendChild(elPropRes); 206 207 List aHREFs = ((ResourceRange)range).getHREFs(); 208 Iterator itor = aHREFs.iterator(); 209 while (itor.hasNext()) { 210 String sHREF = (String ) itor.next(); 211 Element elHREF = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "href"); 212 elProp.appendChild(elHREF); 213 Text txt = xmlDoc.createTextNode(sHREF); 214 elHREF.appendChild(txt); 215 } 216 217 } else if(range instanceof StringRange) { 218 Element elRestriction = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "restriction"); 219 elProp.appendChild(elRestriction); 220 elRestriction.setAttributeNS(NamespaceType.XML_SCHEMA.getURI(), "base", NamespaceType.XML_SCHEMA.getPrefix()+":string"); 221 StringRange actualRange = (StringRange) range; 222 223 String sValue = Integer.toString(actualRange.getMinLength()); 224 Element elTemp = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "minLength"); 225 elRestriction.appendChild(elTemp); 226 Text txt = xmlDoc.createTextNode(sValue); 227 elTemp.appendChild(txt); 228 229 230 sValue = Integer.toString(actualRange.getMaxLength()); 231 elTemp = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "maxLength"); 232 elRestriction.appendChild(elTemp); 233 txt = xmlDoc.createTextNode(sValue); 234 elTemp.appendChild(txt); 235 236 } else if(range instanceof URIRange) { 237 Element elRestriction = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "restriction"); 238 elProp.appendChild(elRestriction); 239 elRestriction.setAttributeNS(NamespaceType.XML_SCHEMA.getURI(), "base", NamespaceType.XML_SCHEMA.getPrefix()+":anyURI"); 240 } else if(range instanceof ValueRange) { 241 Element elResourceType = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "resourcetype"); 242 elProp.appendChild(elResourceType); 243 244 Element elPropRes = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "value"); 245 elResourceType.appendChild(elPropRes); 246 247 List aHREFs = ((ValueRange)range).getHREFs(); 248 Iterator itor = aHREFs.iterator(); 249 while (itor.hasNext()) { 250 String sHREF = (String ) itor.next(); 251 Element elHREF = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "href"); 252 elProp.appendChild(elHREF); 253 Text txt = xmlDoc.createTextNode(sHREF); 254 elHREF.appendChild(txt); 255 } 256 } 257 } 258 } 259 } 260 261 268 public static void fromXML(PropertyInstance propInst, Element propEl) { 269 String sResourceType = null; 270 ArrayList aContentTypes = new ArrayList(); 271 ArrayList aHREFs = new ArrayList(); 272 Element elRestriction = null; 273 274 NodeList nl = propEl.getChildNodes(); 275 for (int i = 0; i < nl.getLength(); i++) { 276 Node node = nl.item(i); 277 if(node.getNodeType()==Node.ELEMENT_NODE) { 278 Element elTemp = (Element) node; 279 if(elTemp.getLocalName().equals("resourcetype")) { 280 Node node2 = elTemp.getFirstChild(); 281 if(node2!=null && node2.getNodeType()==Node.ELEMENT_NODE) { 282 Element elResourceType = (Element) node2; 283 if(elResourceType.getLocalName().equals("value")) { 284 sResourceType = "value"; 285 } else if(elResourceType.getLocalName().equals("property-resource")) { 286 sResourceType = "property-resource"; 287 } else if(elResourceType.getLocalName().equals("collection")) { 288 sResourceType = "collection"; 289 } else { 290 sResourceType = "resource"; 291 } 292 } 293 } else if(elTemp.getLocalName().equals("href")) { 294 Node node2 = elTemp.getFirstChild(); 295 if(node2!=null && node2.getNodeType()==Node.TEXT_NODE) { 296 aHREFs.add(node2.getNodeValue()); 297 } 298 } else if(elTemp.getLocalName().equals("contenttype")) { 299 Node node2 = elTemp.getFirstChild(); 300 if(node2!=null && node2.getNodeType()==Node.TEXT_NODE) { 301 aContentTypes.add(node2.getNodeValue()); 302 } 303 } else if(elTemp.getLocalName().equals("restriction")) { 304 elRestriction = elTemp; 305 } 306 } 307 } 308 309 Range range = null; 310 if(elRestriction!=null) { 311 range = DAVRangeValue.getRestrictionRange(elRestriction); 312 } else if(sResourceType!=null) { 313 if(sResourceType.equals("value")) { 314 range = new ValueRange(); 315 ((ValueRange)range).setValueGroups(aHREFs); 316 } else if(sResourceType.equals("property-resource")) { 317 range = new PropertyRange(); 318 ((PropertyRange)range).setHREFs(aHREFs); 319 } else if(sResourceType.equals("collection")) { 320 range = new CollectionRange(); 321 ((CollectionRange)range).setHREFs(aHREFs); 322 } else if(sResourceType.equals("resource")) { 323 range = new ResourceRange(); 324 ((ResourceRange)range).setHREFs(aHREFs); 325 } 326 } else { 327 range = new StringRange(); 328 } 329 330 DAVRangeValue value = (DAVRangeValue) propInst.getNewValueInstance(); 331 value.setRange(range); 332 propInst.addValueWithoutFiringVirtualFileEvent(value); 333 } 334 335 private static Range getRestrictionRange(Element elRestriction) { 336 Range range = null; 337 338 if(elRestriction.hasAttributeNS(NamespaceType.XML_SCHEMA.getURI(), "base")) { 339 String sBase = elRestriction.getAttributeNS(NamespaceType.XML_SCHEMA.getURI(), "base"); 340 if(sBase!=null && !sBase.equals("")) { 341 if(sBase.equals(NamespaceType.XML_SCHEMA.getPrefix()+":boolean")) { 342 range = new BooleanRange(); 343 344 NodeList nl = elRestriction.getChildNodes(); 345 for (int i = 0; i < nl.getLength(); i++) { 346 Node node = nl.item(i); 347 System.out.println("Checking node under restriction"); 348 if(node.getNodeType()==Node.ELEMENT_NODE) { 349 Element elTemp = (Element) node; 350 System.out.println("Node is an element"); 351 if(elTemp.getLocalName().equals("labels")) { 352 System.out.println("Found labels node"); 353 String sTrueText = elTemp.getAttributeNS(NamespaceType.OHRM.getURI(), "trueLabel"); 354 String sFalseText = elTemp.getAttributeNS(NamespaceType.OHRM.getURI(), "falseLabel"); 355 ((BooleanRange)range).setTrueText(sTrueText); 356 ((BooleanRange)range).setFalseText(sFalseText); 357 } 358 } 359 } 360 361 } else if(sBase.equals(NamespaceType.XML_SCHEMA.getPrefix()+":dateTime")) { 362 range = new DateTimeRange(); 363 364 String sMax = null; 365 String sMin = null; 366 367 NodeList nl = elRestriction.getChildNodes(); 368 for (int i = 0; i < nl.getLength(); i++) { 369 Node node = nl.item(i); 370 if(node.getNodeType()==Node.ELEMENT_NODE) { 371 Element elTemp = (Element) node; 372 if(elTemp.getLocalName().equals("maxInclusive") 373 || elTemp.getLocalName().equals("maxExclusive") ) { 374 Node node2 = elTemp.getFirstChild(); 375 if(node2!=null && node2.getNodeType()==Node.TEXT_NODE) { 376 sMax = node2.getNodeValue(); 377 } 378 } else if(elTemp.getLocalName().equals("minInclusive") 379 || elTemp.getLocalName().equals("minExclusive") ) { 380 Node node2 = elTemp.getFirstChild(); 381 if(node2!=null && node2.getNodeType()==Node.TEXT_NODE) { 382 sMin = node2.getNodeValue(); 383 } 384 } 385 } 386 } 387 388 if(sMax!=null) { 389 SimpleDateFormat format = new SimpleDateFormat(DAVRangeValue.XSD_DATE_TIME_FORMAT); 390 Date dt; 391 try { 392 dt = format.parse(sMax); 393 ((DateTimeRange)range).setMaximum(dt); 394 } catch (ParseException e) { 395 range=null; 396 e.printStackTrace(); 397 } 398 } 399 if(sMin!=null) { 400 SimpleDateFormat format = new SimpleDateFormat(DAVRangeValue.XSD_DATE_TIME_FORMAT); 401 Date dt; 402 try { 403 dt = format.parse(sMin); 404 ((DateTimeRange)range).setMinimum(dt); 405 } catch (ParseException e) { 406 range=null; 407 e.printStackTrace(); 408 } 409 } 410 411 } else if(sBase.equals(NamespaceType.XML_SCHEMA.getPrefix()+":date")) { 412 range = new DateRange(); 413 414 String sMax = null; 415 String sMin = null; 416 417 NodeList nl = elRestriction.getChildNodes(); 418 for (int i = 0; i < nl.getLength(); i++) { 419 Node node = nl.item(i); 420 if(node.getNodeType()==Node.ELEMENT_NODE) { 421 Element elTemp = (Element) node; 422 if(elTemp.getLocalName().equals("maxInclusive") 423 || elTemp.getLocalName().equals("maxExclusive") ) { 424 Node node2 = elTemp.getFirstChild(); 425 if(node2!=null && node2.getNodeType()==Node.TEXT_NODE) { 426 sMax = node2.getNodeValue(); 427 } 428 } else if(elTemp.getLocalName().equals("minInclusive") 429 || elTemp.getLocalName().equals("minExclusive") ) { 430 Node node2 = elTemp.getFirstChild(); 431 if(node2!=null && node2.getNodeType()==Node.TEXT_NODE) { 432 sMin = node2.getNodeValue(); 433 } 434 } 435 } 436 } 437 438 if(sMax!=null) { 439 SimpleDateFormat format = new SimpleDateFormat(DAVRangeValue.XSD_DATE_FORMAT); 440 Date dt; 441 try { 442 dt = format.parse(sMax); 443 ((DateRange)range).setMaximum(dt); 444 } catch (ParseException e) { 445 range=null; 446 e.printStackTrace(); 447 } 448 } 449 if(sMin!=null) { 450 SimpleDateFormat format = new SimpleDateFormat(DAVRangeValue.XSD_DATE_FORMAT); 451 Date dt; 452 try { 453 dt = format.parse(sMin); 454 ((DateRange)range).setMinimum(dt); 455 } catch (ParseException e) { 456 range=null; 457 e.printStackTrace(); 458 } 459 } 460 } else if(sBase.equals(NamespaceType.XML_SCHEMA.getPrefix()+":float")) { 461 range = new FloatRange(); 462 463 String sMax = null; 464 String sMin = null; 465 466 NodeList nl = elRestriction.getChildNodes(); 467 for (int i = 0; i < nl.getLength(); i++) { 468 Node node = nl.item(i); 469 if(node.getNodeType()==Node.ELEMENT_NODE) { 470 Element elTemp = (Element) node; 471 if(elTemp.getLocalName().equals("maxInclusive") 472 || elTemp.getLocalName().equals("maxExclusive") ) { 473 Node node2 = elTemp.getFirstChild(); 474 if(node2!=null && node2.getNodeType()==Node.TEXT_NODE) { 475 sMax = node2.getNodeValue(); 476 } 477 } else if(elTemp.getLocalName().equals("minInclusive") 478 || elTemp.getLocalName().equals("minExclusive") ) { 479 Node node2 = elTemp.getFirstChild(); 480 if(node2!=null && node2.getNodeType()==Node.TEXT_NODE) { 481 sMin = node2.getNodeValue(); 482 } 483 } 484 } 485 } 486 487 if(sMax!=null) { 488 ((FloatRange)range).setMaximum(Float.parseFloat(sMax)); 489 } 490 if(sMin!=null) { 491 ((FloatRange)range).setMinimum(Float.parseFloat(sMin)); 492 } 493 } else if(sBase.equals(NamespaceType.XML_SCHEMA.getPrefix()+":integer")) { 494 range = new IntegerRange(); 495 496 String sMax = null; 497 String sMin = null; 498 499 NodeList nl = elRestriction.getChildNodes(); 500 for (int i = 0; i < nl.getLength(); i++) { 501 Node node = nl.item(i); 502 if(node.getNodeType()==Node.ELEMENT_NODE) { 503 Element elTemp = (Element) node; 504 if(elTemp.getLocalName().equals("maxInclusive") 505 || elTemp.getLocalName().equals("maxExclusive") ) { 506 Node node2 = elTemp.getFirstChild(); 507 if(node2!=null && node2.getNodeType()==Node.TEXT_NODE) { 508 sMax = node2.getNodeValue(); 509 } 510 } else if(elTemp.getLocalName().equals("minInclusive") 511 || elTemp.getLocalName().equals("minExclusive") ) { 512 Node node2 = elTemp.getFirstChild(); 513 if(node2!=null && node2.getNodeType()==Node.TEXT_NODE) { 514 sMin = node2.getNodeValue(); 515 } 516 } 517 } 518 } 519 520 if(sMax!=null) { 521 ((IntegerRange)range).setMaximum(Integer.parseInt(sMax)); 522 } 523 if(sMin!=null) { 524 ((IntegerRange)range).setMinimum(Integer.parseInt(sMin)); 525 } 526 } else if(sBase.equals(NamespaceType.XML_SCHEMA.getPrefix()+":string")) { 527 range = new StringRange(); 528 529 String sMax = null; 530 String sMin = null; 531 532 NodeList nl = elRestriction.getChildNodes(); 533 for (int i = 0; i < nl.getLength(); i++) { 534 Node node = nl.item(i); 535 if(node.getNodeType()==Node.ELEMENT_NODE) { 536 Element elTemp = (Element) node; 537 if(elTemp.getLocalName().equals("maxLength") ) { 538 Node node2 = elTemp.getFirstChild(); 539 if(node2!=null && node2.getNodeType()==Node.TEXT_NODE) { 540 sMax = node2.getNodeValue(); 541 } 542 } else if(elTemp.getLocalName().equals("minLength") ) { 543 Node node2 = elTemp.getFirstChild(); 544 if(node2!=null && node2.getNodeType()==Node.TEXT_NODE) { 545 sMin = node2.getNodeValue(); 546 } 547 } 548 } 549 } 550 551 if(sMax!=null) { 552 ((StringRange)range).setMaxLength(Integer.parseInt(sMax)); 553 } 554 if(sMin!=null) { 555 ((StringRange)range).setMinLength(Integer.parseInt(sMin)); 556 } 557 } else if(sBase.equals(NamespaceType.XML_SCHEMA.getPrefix()+":anyURI")) { 558 range = new URIRange(); 559 } 560 } 561 } 562 563 if(range==null) { 564 range = new StringRange(); 565 } 566 567 return range; 568 } 569 570 } 571 | Popular Tags |