示例#1
0
  /**
   * Gets the element data.
   *
   * @param element the element
   * @param type the type
   * @return the element data
   */
  @SuppressWarnings("unchecked")
  private static Object getElementData(org.dom4j.Element element, Type type, ServiceContext ctx)
      throws Exception {
    Object result = null;
    String dateStr = "";

    if (type.isSimpleType()) {
      if (isNuxeoDateType(type)) {
        String dateVal = element.getText();
        if (dateVal == null || dateVal.trim().isEmpty()) {
          result = type.decode("");
        } else {
          // Dates or date/times in any ISO 8601-based representations
          // directly supported by Nuxeo will be successfully decoded.
          result = type.decode(dateVal);
          // All other date or date/time values must first be converted
          // to a supported ISO 8601-based representation.
          if (result == null) {
            dateStr = DateTimeFormatUtils.toIso8601Timestamp(dateVal, ctx.getTenantId());
            if (dateStr != null) {
              result = type.decode(dateStr);
            } else {
              throw new IllegalArgumentException(
                  "Unrecognized date value '" + dateVal + "' in field '" + element.getName() + "'");
            }
          }
        }
      } else {
        String textValue = element.getText();
        if (textValue != null && textValue.trim().isEmpty()) {
          result = null;
        } else {
          result = type.decode(textValue);
        }
      }
    } else if (type.isListType()) {
      ListType ltype = (ListType) type;
      List<Object> list = new ArrayList<Object>();
      Iterator<org.dom4j.Element> it = element.elementIterator();
      while (it.hasNext()) {
        org.dom4j.Element el = it.next();
        list.add(getElementData(el, ltype.getFieldType(), ctx));
      }
      Type ftype = ltype.getFieldType();
      if (ftype.isSimpleType()) { // these are stored as arrays
        Class klass = JavaTypes.getClass(ftype);
        if (klass.isPrimitive()) {
          return PrimitiveArrays.toPrimitiveArray(list, klass);
        } else {
          return list.toArray((Object[]) Array.newInstance(klass, list.size()));
        }
      }
      result = list;
    } else {
      ComplexType ctype = (ComplexType) type;
      if (ctype.getName().equals(TypeConstants.CONTENT)) {
        //				String mimeType = element.elementText(ExportConstants.BLOB_MIME_TYPE);
        //				String encoding = element.elementText(ExportConstants.BLOB_ENCODING);
        //				String content = element.elementTextTrim(ExportConstants.BLOB_DATA);
        //				if ((content == null || content.length() == 0)
        //						&& (mimeType == null || mimeType.length() == 0)) {
        //					return null; // remove blob
        //				}
        //				Blob blob = null;
        //				if (xdoc.hasExternalBlobs()) {
        //					blob = xdoc.getBlob(content);
        //				}
        //				if (blob == null) { // may be the blob is embedded like a Base64
        //					// encoded data
        //					byte[] bytes = Base64.decode(content);
        //					blob = new StreamingBlob(new ByteArraySource(bytes));
        //				}
        //				blob.setMimeType(mimeType);
        //				blob.setEncoding(encoding);
        //				return blob;
      } else { // a complex type
        Map<String, Object> map = new HashMap<String, Object>();
        Iterator<org.dom4j.Element> it = element.elementIterator();
        while (it.hasNext()) {
          org.dom4j.Element el = it.next();
          String name = el.getName();
          Object value = getElementData(el, ctype.getField(el.getName()).getType(), ctx);
          map.put(name, value);
        }
        result = map;
      }
    }
    return result;
  }