示例#1
0
  @SuppressWarnings("unchecked")
  protected void writeProperty(
      XMLWriter2 writer,
      String name,
      EdmType type,
      Object value,
      boolean isDocumentElement,
      boolean writeType) {

    writer.startElement(new QName2(d, name, "d"));

    if (isDocumentElement) {
      writer.writeNamespace("m", m);
      writer.writeNamespace("d", d);
    }

    String sValue = null;

    if (!type.isSimple()) {
      if (writeType) {
        String typename = type.getFullyQualifiedTypeName();
        if (value instanceof OCollection) {
          EdmCollectionType collectionType = (EdmCollectionType) type;
          typename = "Bag(" + collectionType.getItemType().getFullyQualifiedTypeName() + ")";
        }
        writer.writeAttribute(new QName2(m, "type", "m"), typename);
      }
      // complex or collection
      if (value instanceof OCollection) {
        writeCollection(writer, name, (OCollection<? extends OObject>) value);
      } else if (value instanceof OComplexObject) {
        writeProperties(writer, ((OComplexObject) value).getProperties());
      } else {
        // deprecated form of a complex object.
        List<OProperty<?>> complexProperties = (List<OProperty<?>>) value;
        if (complexProperties != null) {
          writeProperties(writer, complexProperties);
        }
      }
    } else {
      // simple
      // write the type attribute if requested and not a string
      if (writeType && type != EdmSimpleType.STRING) {
        writer.writeAttribute(new QName2(m, "type", "m"), type.getFullyQualifiedTypeName());
      }
      // now write the value
      if (type == EdmSimpleType.INT32) {
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmSimpleType.INT16) {
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmSimpleType.INT64) {
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmSimpleType.BOOLEAN) {
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmSimpleType.BYTE) {
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmSimpleType.SBYTE) {
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmSimpleType.DECIMAL) {
        if (value != null) {
          sValue = ((BigDecimal) value).toPlainString();
        }
      } else if (type == EdmSimpleType.SINGLE) {
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmSimpleType.DOUBLE) {
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmSimpleType.STRING) {
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmSimpleType.DATETIME) {
        if (value != null) sValue = InternalUtil.formatDateTimeForXml((LocalDateTime) value);
      } else if (type == EdmSimpleType.BINARY) {
        byte[] bValue = (byte[]) value;
        if (value != null) {
          sValue = Base64.encodeBase64String(bValue);
        }
      } else if (type == EdmSimpleType.GUID) {
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmSimpleType.TIME) {
        if (value != null) {
          sValue = InternalUtil.formatTimeForXml((LocalTime) value);
        }
      } else if (type == EdmSimpleType.DATETIMEOFFSET) {
        // Edm.DateTimeOffset '-'? yyyy '-' mm '-' dd 'T' hh ':' mm
        // ':' ss ('.' s+)? (zzzzzz)?
        if (value != null) {
          sValue = InternalUtil.formatDateTimeOffsetForXml((DateTime) value);
        }
      } else {
        throw new UnsupportedOperationException("Implement " + type);
      }
    }

    if (value == null) {
      writer.writeAttribute(new QName2(m, "null", "m"), "true");
    } else if (sValue != null) {
      writer.writeText(sValue);
    }

    writer.endElement(name);
  }
  @SuppressWarnings("unchecked")
  protected void writeProperty(XMLWriter2 writer, OProperty<?> prop, boolean isDocumentElement) {

    String name = prop.getName();
    EdmType type = prop.getType();
    Object value = prop.getValue();

    if (isDocumentElement) writer.startElement(new QName2(name), d);
    else writer.startElement(new QName2(d, name, "d"));

    String sValue = null;

    if (!type.isSimple()) {
      writer.writeAttribute(new QName2(m, "type", "m"), type.toTypeString());
      // complex
      List<OProperty<?>> complexProperties = (List<OProperty<?>>) value;
      if (complexProperties != null) {
        writeProperties(writer, complexProperties);
      }
    } else {
      // simple
      if (type == EdmType.INT32) {
        writer.writeAttribute(new QName2(m, "type", "m"), type.toTypeString());
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmType.INT16) {
        writer.writeAttribute(new QName2(m, "type", "m"), type.toTypeString());
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmType.INT64) {
        writer.writeAttribute(new QName2(m, "type", "m"), type.toTypeString());
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmType.BOOLEAN) {
        writer.writeAttribute(new QName2(m, "type", "m"), type.toTypeString());
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmType.BYTE) {
        writer.writeAttribute(new QName2(m, "type", "m"), type.toTypeString());
        if (value != null) {
          sValue = Hex.encodeHexString(new byte[] {(Byte) value});
        }
      } else if (type == EdmType.DECIMAL) {
        writer.writeAttribute(new QName2(m, "type", "m"), type.toTypeString());
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmType.SINGLE) {
        writer.writeAttribute(new QName2(m, "type", "m"), type.toTypeString());
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmType.DOUBLE) {
        writer.writeAttribute(new QName2(m, "type", "m"), type.toTypeString());
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmType.STRING) {
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmType.DATETIME) {
        writer.writeAttribute(new QName2(m, "type", "m"), type.toTypeString());
        if (value != null) sValue = InternalUtil.formatDateTime((LocalDateTime) value);
      } else if (type == EdmType.BINARY) {
        writer.writeAttribute(new QName2(m, "type", "m"), type.toTypeString());
        byte[] bValue = (byte[]) value;
        if (value != null) {
          sValue = Base64.encodeBase64String(bValue);
        }
      } else if (type == EdmType.GUID) {
        writer.writeAttribute(new QName2(m, "type", "m"), type.toTypeString());
        if (value != null) {
          sValue = value.toString();
        }
      } else if (type == EdmType.TIME) {
        writer.writeAttribute(new QName2(m, "type", "m"), type.toTypeString());
        if (value != null) {
          sValue = InternalUtil.toString((LocalTime) value);
        }
      } else if (type == EdmType.DATETIMEOFFSET) {
        // Edm.DateTimeOffset '-'? yyyy '-' mm '-' dd 'T' hh ':' mm
        // ':' ss ('.' s+)? (zzzzzz)?
        writer.writeAttribute(new QName2(m, "type", "m"), type.toTypeString());
        if (value != null) {
          sValue = InternalUtil.toString((DateTime) value);
        }
      } else {
        throw new UnsupportedOperationException("Implement " + type);
      }
    }

    if (value == null) {
      writer.writeAttribute(new QName2(m, "null", "m"), "true");
    } else {
      writer.writeText(sValue);
    }
    writer.endElement(name);
  }