/**
   * Build an <code>AttributeType</code> from the given <code>obj</code> type. If the given <code>
   * obj</code> class match with an <code>AttributeType</code> type, returns this type. Returns
   * <code>null</code> otherwise.
   *
   * <p>For instance if <b>obj instanceof Float</b> equals <b>true</b>, returns <code>
   * AttributeType.FLOAT</code>.
   *
   * @param obj the object that is to be parsed
   * @return the compatible <code>AttributeType</code>, or <code>null</code>
   */
  public static AttributeType parse(Object obj) {
    Class<?> type = obj.getClass();

    for (AttributeType attributeType : values()) {
      if (type.equals(attributeType.getType())) return attributeType;
    }

    return null;
  }
  private void writeAttributes(
      XMLStreamWriter xmlWriter, AttributeColumn[] cols, String mode, String attClass)
      throws Exception {
    if (exportAttributes) {
      xmlWriter.writeStartElement(ATTRIBUTES);
      xmlWriter.writeAttribute(ATTRIBUTES_CLASS, attClass);
      xmlWriter.writeAttribute(ATTRIBUTES_MODE, mode);

      for (AttributeColumn col : cols) {
        if (!col.getOrigin().equals(AttributeOrigin.PROPERTY)
            || (exportDynamic
                && col.getOrigin().equals(AttributeOrigin.PROPERTY)
                && col.getIndex() == PropertiesColumn.EDGE_WEIGHT.getIndex())) {
          xmlWriter.writeStartElement(ATTRIBUTE);
          xmlWriter.writeAttribute(ATTRIBUTE_ID, col.getId());
          xmlWriter.writeAttribute(ATTRIBUTE_TITLE, col.getTitle());
          if (col.getType().equals(AttributeType.INT)) {
            xmlWriter.writeAttribute(ATTRIBUTE_TYPE, "integer");
          } else if (col.getType().isListType()) {
            if (col.getType().equals(AttributeType.LIST_INTEGER)) {
              xmlWriter.writeAttribute(ATTRIBUTE_TYPE, "listint");
            } else if (col.getType().equals(AttributeType.LIST_CHARACTER)) {
              xmlWriter.writeAttribute(ATTRIBUTE_TYPE, "listchar");
            } else {
              xmlWriter.writeAttribute(
                  ATTRIBUTE_TYPE, col.getType().getTypeString().toLowerCase().replace("_", ""));
            }
          } else if (col.getType().isDynamicType()) {
            AttributeType staticType = TypeConvertor.getStaticType(col.getType());
            if (staticType.equals(AttributeType.INT)) {
              xmlWriter.writeAttribute(ATTRIBUTE_TYPE, "integer");
            } else {
              xmlWriter.writeAttribute(ATTRIBUTE_TYPE, staticType.getTypeString().toLowerCase());
            }
          } else {
            xmlWriter.writeAttribute(ATTRIBUTE_TYPE, col.getType().getTypeString().toLowerCase());
          }
          if (col.getDefaultValue() != null) {
            xmlWriter.writeStartElement(ATTRIBUTE_DEFAULT);
            xmlWriter.writeCharacters(col.getDefaultValue().toString());
            xmlWriter.writeEndElement();
          }
          xmlWriter.writeEndElement();
        }
      }

      xmlWriter.writeEndElement();
    }
  }
Пример #3
0
 public void setValue(String column, Object value) {
   if (column == null) {
     throw new NullPointerException("Column is null");
   }
   AttributeColumn attributeColumn = attributeTable.getColumn(column);
   if (attributeColumn != null) {
     setValue(attributeColumn, value);
   } else {
     // add column
     AttributeType type = AttributeType.parse(value);
     // System.out.println("parsed value type: " + value.getClass());
     if (type != null) {
       attributeColumn = attributeTable.addColumn(column, type);
       setValue(attributeColumn, value);
     }
   }
 }
 private void writeAttValues(
     XMLStreamWriter xmlWriter, AttributeRow row, TimeInterval visibleInterval) throws Exception {
   xmlWriter.writeStartElement(ATTVALUES);
   for (AttributeValue val : row.getValues()) {
     AttributeColumn col = val.getColumn();
     if (!col.getOrigin().equals(AttributeOrigin.PROPERTY)
         || (exportDynamic
             && col.getOrigin().equals(AttributeOrigin.PROPERTY)
             && col.getIndex() == PropertiesColumn.EDGE_WEIGHT.getIndex())) {
       AttributeType type = col.getType();
       if (type.isDynamicType()) {
         DynamicType dynamicValue = (DynamicType) val.getValue();
         if (dynamicValue != null && visibleInterval != null && exportDynamic) {
           List<Interval<?>> intervals =
               dynamicValue.getIntervals(visibleInterval.getLow(), visibleInterval.getHigh());
           for (Interval<?> interval : intervals) {
             Object value = interval.getValue();
             if (value != null) {
               xmlWriter.writeStartElement(ATTVALUE);
               xmlWriter.writeAttribute(ATTVALUE_FOR, col.getId());
               xmlWriter.writeAttribute(ATTVALUE_VALUE, value.toString());
               if (!Double.isInfinite(interval.getLow())) {
                 String intervalLow = formatTime(interval.getLow());
                 xmlWriter.writeAttribute(
                     interval.isLowExcluded() ? START_OPEN : START, intervalLow);
               }
               if (!Double.isInfinite(interval.getHigh())) {
                 String intervalHigh = formatTime(interval.getHigh());
                 xmlWriter.writeAttribute(
                     interval.isHighExcluded() ? END_OPEN : END, intervalHigh);
               }
               xmlWriter.writeEndElement();
             }
           }
         } else if (dynamicValue != null) {
           TimeInterval interval = visibleInterval;
           if (interval == null) {
             interval = new TimeInterval();
           }
           Object value =
               DynamicUtilities.getDynamicValue(
                   dynamicValue, interval.getLow(), interval.getHigh());
           if (value != null) {
             xmlWriter.writeStartElement(ATTVALUE);
             xmlWriter.writeAttribute(ATTVALUE_FOR, val.getColumn().getId());
             xmlWriter.writeAttribute(ATTVALUE_VALUE, value.toString());
             xmlWriter.writeEndElement();
           }
         }
       } else {
         if (val.getValue() != null) {
           xmlWriter.writeStartElement(ATTVALUE);
           xmlWriter.writeAttribute(ATTVALUE_FOR, col.getId());
           xmlWriter.writeAttribute(ATTVALUE_VALUE, val.getValue().toString());
           xmlWriter.writeEndElement();
         }
       }
     }
   }
   xmlWriter.writeEndElement();
 }