Example #1
0
  /**
   * Returns an appropriate XQuery type for the specified Java object.
   *
   * @param o object
   * @return item type or {@code null} if no appropriate type was found
   */
  private static Type type(final Object o) {
    final Type t = type(o.getClass());
    if (t != null) return t;

    if (o instanceof Element) return NodeType.ELM;
    if (o instanceof Document) return NodeType.DOC;
    if (o instanceof DocumentFragment) return NodeType.DOC;
    if (o instanceof Attr) return NodeType.ATT;
    if (o instanceof Comment) return NodeType.COM;
    if (o instanceof ProcessingInstruction) return NodeType.PI;
    if (o instanceof Text) return NodeType.TXT;

    if (o instanceof Duration) {
      final Duration d = (Duration) o;
      return !d.isSet(DatatypeConstants.YEARS) && !d.isSet(DatatypeConstants.MONTHS)
          ? AtomType.DTD
          : !d.isSet(DatatypeConstants.HOURS)
                  && !d.isSet(DatatypeConstants.MINUTES)
                  && !d.isSet(DatatypeConstants.SECONDS)
              ? AtomType.YMD
              : AtomType.DUR;
    }

    if (o instanceof XMLGregorianCalendar) {
      final QName type = ((XMLGregorianCalendar) o).getXMLSchemaType();
      if (type == DatatypeConstants.DATE) return AtomType.DAT;
      if (type == DatatypeConstants.DATETIME) return AtomType.DTM;
      if (type == DatatypeConstants.TIME) return AtomType.TIM;
      if (type == DatatypeConstants.GYEARMONTH) return AtomType.YMO;
      if (type == DatatypeConstants.GMONTHDAY) return AtomType.MDA;
      if (type == DatatypeConstants.GYEAR) return AtomType.YEA;
      if (type == DatatypeConstants.GMONTH) return AtomType.MON;
      if (type == DatatypeConstants.GDAY) return AtomType.DAY;
    }
    return null;
  }
 /*
  * Test Duration.isSet(Field) throws NPE if the field parameter is null.
  */
 @Test(expectedExceptions = NullPointerException.class)
 public void checkDurationIsSetNeg() {
   Duration duration = datatypeFactory.newDuration(true, 0, 0, 0, 0, 0, 0);
   duration.isSet(null);
 }
  /*
   * Test for Duration.isSet().
   */
  @Test
  public void checkDurationIsSet() {
    Duration duration1 = datatypeFactory.newDuration(true, 1, 1, 1, 1, 1, 1);
    Duration duration2 = datatypeFactory.newDuration(true, 0, 0, 0, 0, 0, 0);

    assertTrue(duration1.isSet(YEARS));
    assertTrue(duration1.isSet(MONTHS));
    assertTrue(duration1.isSet(DAYS));
    assertTrue(duration1.isSet(HOURS));
    assertTrue(duration1.isSet(MINUTES));
    assertTrue(duration1.isSet(SECONDS));

    assertTrue(duration2.isSet(YEARS));
    assertTrue(duration2.isSet(MONTHS));
    assertTrue(duration2.isSet(DAYS));
    assertTrue(duration2.isSet(HOURS));
    assertTrue(duration2.isSet(MINUTES));
    assertTrue(duration2.isSet(SECONDS));

    Duration duration66 = datatypeFactory.newDuration(true, null, null, zero, null, null, null);
    assertFalse(duration66.isSet(YEARS));
    assertFalse(duration66.isSet(MONTHS));
    assertFalse(duration66.isSet(HOURS));
    assertFalse(duration66.isSet(MINUTES));
    assertFalse(duration66.isSet(SECONDS));

    Duration duration3 = datatypeFactory.newDuration("P1D");
    assertFalse(duration3.isSet(YEARS));
    assertFalse(duration3.isSet(MONTHS));
    assertFalse(duration3.isSet(HOURS));
    assertFalse(duration3.isSet(MINUTES));
    assertFalse(duration3.isSet(SECONDS));
  }