Example #1
0
 /**
  * Builds the property.
  *
  * @param document the document
  * @param parent the parent
  * @param field the field
  * @param dateVal the dateVal
  * @throws IOException Signals that an I/O exception has occurred.
  */
 private static void buildProperty(Document document, Element parent, Field field, Object value)
     throws IOException {
   Type type = field.getType();
   // no need to qualify each element name as namespace is already added
   String propName = field.getName().getLocalName();
   Element element = document.createElement(propName);
   parent.appendChild(element);
   // extract the element content
   if (type.isSimpleType()) {
     // Avoid returning scientific notation representations of
     // very large or very small decimal values. See CSPACE-4691.
     if (isNuxeoDecimalType(type) && valueMatchesNuxeoType(type, value)) {
       element.setTextContent(nuxeoDecimalValueToDecimalString(value));
       /*
       * We need a way to produce just a Date when the specified data
       * type is an xs:date vs. xs:datetime. Nuxeo maps both to a Calendar. Sigh.
       if(logger.isTraceEnabled() && isDateType(type)) {
           String dateValType = "unknown";
           if (value instanceof java.util.Date) {
               dateValType = "java.util.Date";
           } else if (value instanceof java.util.Calendar) {
               dateValType = "java.util.Calendar";
           }
           logger.trace("building XML for date type: "+type.getName()
                   +" value type: "+dateValType
                   +" encoded: "+encodedVal);
       }
       */
     } else {
       String encodedVal = type.encode(value);
       element.setTextContent(encodedVal);
     }
   } else if (type.isComplexType()) {
     ComplexType ctype = (ComplexType) type;
     if (ctype.getName().equals(TypeConstants.CONTENT)) {
       throw new RuntimeException("Unexpected schema type: BLOB for field: " + propName);
     } else {
       buildComplex(document, element, ctype, (Map) value);
     }
   } else if (type.isListType()) {
     if (value instanceof List) {
       buildList(document, element, (ListType) type, (List) value);
     } else if (value.getClass().getComponentType() != null) {
       buildList(document, element, (ListType) type, PrimitiveArrays.toList(value));
     } else {
       throw new IllegalArgumentException(
           "A value of list type is neither list neither array: " + value);
     }
   }
 }
 private void writeScalarPropertyValue(JsonGenerator jg, Type type, Object value)
     throws IOException {
   if (value == null) {
     jg.writeNull();
   } else if (type instanceof BooleanType) {
     jg.writeBoolean((Boolean) value);
   } else if (type instanceof LongType) {
     jg.writeNumber((Long) value);
   } else if (type instanceof DoubleType) {
     jg.writeNumber((Double) value);
   } else if (type instanceof IntegerType) {
     jg.writeNumber((Integer) value);
   } else if (type instanceof BinaryType) {
     jg.writeBinary((byte[]) value);
   } else {
     jg.writeString(type.encode(value));
   }
 }