コード例 #1
0
 static OProperty<?> buildPropery(
     String propName, EdmType type, Object value, String invalidCharacterReplacement)
     throws TransformationException, SQLException, IOException {
   if (!(type instanceof EdmSimpleType)) {
     if (type instanceof EdmCollectionType) {
       EdmCollectionType collectionType = (EdmCollectionType) type;
       EdmType componentType = collectionType.getItemType();
       Builder<OObject> b = OCollections.newBuilder(componentType);
       if (value instanceof Array) {
         value = ((Array) value).getArray();
       }
       int length = java.lang.reflect.Array.getLength(value);
       for (int i = 0; i < length; i++) {
         Object o = java.lang.reflect.Array.get(value, i);
         OProperty p = buildPropery("x", componentType, o, invalidCharacterReplacement);
         if (componentType instanceof EdmSimpleType) {
           b.add(OSimpleObjects.create((EdmSimpleType) componentType, p.getValue()));
         } else {
           throw new AssertionError("Multi-dimensional arrays are not yet supported.");
           // b.add((OCollection)p.getValue());
         }
       }
       return OProperties.collection(propName, collectionType, b.build());
     }
     throw new AssertionError("non-simple types are not yet supported");
   }
   EdmSimpleType expectedType = (EdmSimpleType) type;
   if (value == null) {
     return OProperties.null_(propName, expectedType);
   }
   Class<?> sourceType = DataTypeManager.getRuntimeType(value.getClass());
   Class<?> targetType =
       DataTypeManager.getDataTypeClass(
           ODataTypeManager.teiidType(expectedType.getFullyQualifiedTypeName()));
   if (sourceType != targetType) {
     Transform t = DataTypeManager.getTransform(sourceType, targetType);
     if (t == null && BlobType.class == targetType) {
       if (sourceType == ClobType.class) {
         return OProperties.binary(propName, ClobType.getString((Clob) value).getBytes());
       }
       if (sourceType == SQLXML.class) {
         return OProperties.binary(propName, ((SQLXML) value).getString().getBytes());
       }
     }
     value = DataTypeManager.convertToRuntimeType(value, true);
     value = t != null ? t.transform(value, targetType) : value;
     value = replaceInvalidCharacters(expectedType, value, invalidCharacterReplacement);
     if (value instanceof BinaryType) {
       value = ((BinaryType) value).getBytesDirect();
     }
     return OProperties.simple(propName, expectedType, value);
   }
   value = replaceInvalidCharacters(expectedType, value, invalidCharacterReplacement);
   return OProperties.simple(propName, expectedType, value);
 }
コード例 #2
0
ファイル: XmlFormatWriter.java プロジェクト: lazy404/odata4j
  @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);
  }