Exemple #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;
  }