Example #1
0
  public Collection<T> parseAll(String string, boolean isOnline) {
    Collection<T> elements = new ArrayList<T>();
    try {

      DataEntity entity = AnnotationUtils.findAnnotation(persistentClass, DataEntity.class);

      String[] joinSplit = {string};
      if (isOnline) {
        joinSplit = StringUtil.joinSplit(string, entity.regex());
      }

      for (String record : joinSplit) {
        elements.add(parse(persistentClass.newInstance(), record, isOnline));
      }

    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
    return elements;
  }
Example #2
0
  @SuppressWarnings({"unchecked", "rawtypes"})
  public T parse(T instance, String string, boolean isOnline) {
    try {
      DataEntity entity = AnnotationUtils.findAnnotation(persistentClass, DataEntity.class);
      Field[] fields = persistentClass.getDeclaredFields();

      String record = string;
      if (isOnline && !entity.regex().equals(DataEntity.NESTED)) {
        record = StringUtil.substringAfterFirstOccurrence(string, entity.regex());
      }

      for (Field field : fields) {
        DataField dataField = field.getAnnotation(DataField.class);

        if (dataField != null) {
          int start = 0;
          int end = 0;
          Object value = null;

          if (field.getType().getSuperclass() == Enum.class) {
            start = isOnline ? dataField.startRangeOnline() : dataField.startRangeBatch();
            end = start + (isOnline ? dataField.lengthOnline() : dataField.lenghtBatch());
            Method method = field.getType().getMethod("decode", String.class);
            if (end <= record.length()) {
              String tmpVal = record.substring(start, end);
              value = method.invoke(null, new Object[] {tmpVal});
            }
          } else if (isComplexType(field.getType())) {
            start = isOnline ? dataField.startRangeOnline() : dataField.startRangeBatch();
            end = start + (isOnline ? dataField.lengthOnline() : dataField.lenghtBatch());
            if (end <= record.length()) {
              String substring;
              try {
                if (!dataField.isAggregate()) {
                  substring = record.substring(start, end);
                } else {
                  substring = record;
                }
              } catch (StringIndexOutOfBoundsException e) {
                logger.debug(
                    "StringIndexOutOfBoundsException: field=[{}] string=[{}]",
                    field.getName(),
                    record);
                throw e;
              }
              if (substring.trim().length() != 0) {
                Class<?> type = field.getType();
                DomainStringParser domainStringParser = new DomainStringParser(type);
                value = domainStringParser.parse(substring, isOnline);
              }
            }
          } else {
            start = isOnline ? dataField.startRangeOnline() : dataField.startRangeBatch();
            end = start + (isOnline ? dataField.lengthOnline() : dataField.lenghtBatch());
            if (end <= record.length()) {
              String substring = record.substring(start, end).trim();
              if (field.getType().equals(Calendar.class)) {
                value = parseLegacyDate(substring);
              } else {
                value = substring;
              }
            }
          }
          setField(instance, field, value);
        }
      }
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (SecurityException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    }
    return instance;
  }