@Test public void parseValue() { final EventParser eventParser = new EventParser(); assertThat(eventParser.parseValue(definition(MongoDataType.STRING), "foo")).isEqualTo("foo"); assertThat(eventParser.parseValue(definition(MongoDataType.INT32), "32")).isEqualTo(32); assertThat(eventParser.parseValue(definition(MongoDataType.INT64), "64")).isEqualTo(64L); assertThat(eventParser.parseValue(definition(MongoDataType.DOUBLE), "1.0")).isEqualTo(1.0); assertThat(eventParser.parseValue(definition(MongoDataType.BOOLEAN), "true")).isEqualTo(true); final DateTime now = DateTime.now().toDateTime(DateTimeZone.UTC); assertThat(eventParser.parseValue(definition(MongoDataType.DATE), Long.toString(now.getMillis()))).isEqualTo( now.toDate()); assertThat((eventParser.parseValue(definition(MongoDataType.DATE), ISODateTimeFormat.dateTime().print(now)))) .isEqualTo( now.toDate()); assertThat(eventParser.parseValue(definition(MongoDataType.NULL), "full")).isNull(); assertThat(eventParser.parseValue(definition(MongoDataType.OBJECTID), "507c7f79bcf86cd7994f6c0e")).isEqualTo( new ObjectId("507c7f79bcf86cd7994f6c0e")); BasicDBList dbList = new BasicDBList(); dbList.add(1); dbList.add(2); dbList.add(3); DBObject dbObject = new BasicDBObject(); dbObject.put("abc", 123); dbObject.put("myArray", dbList); assertThat(eventParser.parseValue(definition(MongoDataType.OBJECT), "{ \"abc\": 123, \"myArray\": [1, 2, 3] }")) .isEqualTo( dbObject); assertThat(eventParser.parseValue(definition(MongoDataType.BINARY), "U3RyYXRpbw==")).isEqualTo( "Stratio".getBytes(Charsets.UTF_8)); }
public Object parseValue(final FieldDefinition fd, final String stringValue) { if (fd == null || fd.getType() == null) { try { return JSON.parse(stringValue); } catch (JSONParseException ex) { log.trace("Could not parse as JSON, defaulting to String: {}", stringValue); return stringValue; } } switch (fd.getType()) { case DOUBLE: return Double.parseDouble(stringValue); case STRING: return stringValue; case OBJECT: case ARRAY: return JSON.parse(stringValue); case BINARY: SimpleFieldDefinition sfd = (SimpleFieldDefinition) fd; final String encoding = (sfd.getEncoding() == null) ? DEFAULT_BINARY_ENCODING : sfd.getEncoding() .toLowerCase(Locale.ENGLISH); if ("base64".equals(encoding)) { return BaseEncoding.base64().decode(stringValue); } else { throw new UnsupportedOperationException("Unsupported encoding for binary type: " + encoding); } case OBJECTID: return new ObjectId(stringValue); case BOOLEAN: return Boolean.parseBoolean(stringValue); case DATE: DateFormat dateFormat = ((DateFieldDefinition) fd).getDateFormat(); if (dateFormat == null) { if (NumberUtils.isNumber(stringValue)) { return new Date(Long.parseLong(stringValue)); } else { return ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(stringValue).toDate(); } } else { try { return dateFormat.parse(stringValue); } catch (ParseException ex) { log.warn("Could not parse date, defaulting to String: {}", stringValue); return stringValue; } } case NULL: return null; case INT32: return Integer.parseInt(stringValue); case INT64: return Long.parseLong(stringValue); case DOCUMENT: return populateDocument((DocumentFieldDefinition) fd, stringValue); default: throw new UnsupportedOperationException("Unsupported type: " + fd.getType().name()); } }
EventParser { public Object parseValue(final FieldDefinition fd, final String stringValue) { if (fd == null || fd.getType() == null) { try { return JSON.parse(stringValue); } catch (JSONParseException ex) { log.trace("Could not parse as JSON, defaulting to String: {}", stringValue); return stringValue; } } switch (fd.getType()) { case DOUBLE: return Double.parseDouble(stringValue); case STRING: return stringValue; case OBJECT: case ARRAY: return JSON.parse(stringValue); case BINARY: SimpleFieldDefinition sfd = (SimpleFieldDefinition) fd; final String encoding = (sfd.getEncoding() == null) ? DEFAULT_BINARY_ENCODING : sfd.getEncoding() .toLowerCase(Locale.ENGLISH); if ("base64".equals(encoding)) { return BaseEncoding.base64().decode(stringValue); } else { throw new UnsupportedOperationException("Unsupported encoding for binary type: " + encoding); } case OBJECTID: return new ObjectId(stringValue); case BOOLEAN: return Boolean.parseBoolean(stringValue); case DATE: DateFormat dateFormat = ((DateFieldDefinition) fd).getDateFormat(); if (dateFormat == null) { if (NumberUtils.isNumber(stringValue)) { return new Date(Long.parseLong(stringValue)); } else { return ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(stringValue).toDate(); } } else { try { return dateFormat.parse(stringValue); } catch (ParseException ex) { log.warn("Could not parse date, defaulting to String: {}", stringValue); return stringValue; } } case NULL: return null; case INT32: return Integer.parseInt(stringValue); case INT64: return Long.parseLong(stringValue); case DOCUMENT: return populateDocument((DocumentFieldDefinition) fd, stringValue); default: throw new UnsupportedOperationException("Unsupported type: " + fd.getType().name()); } } }
EventParser { public Object parseValue(final FieldDefinition fd, final String stringValue) { if (fd == null || fd.getType() == null) { try { return JSON.parse(stringValue); } catch (JSONParseException ex) { log.trace("Could not parse as JSON, defaulting to String: {}", stringValue); return stringValue; } } switch (fd.getType()) { case DOUBLE: return Double.parseDouble(stringValue); case STRING: return stringValue; case OBJECT: case ARRAY: return JSON.parse(stringValue); case BINARY: SimpleFieldDefinition sfd = (SimpleFieldDefinition) fd; final String encoding = (sfd.getEncoding() == null) ? DEFAULT_BINARY_ENCODING : sfd.getEncoding() .toLowerCase(Locale.ENGLISH); if ("base64".equals(encoding)) { return BaseEncoding.base64().decode(stringValue); } else { throw new UnsupportedOperationException("Unsupported encoding for binary type: " + encoding); } case OBJECTID: return new ObjectId(stringValue); case BOOLEAN: return Boolean.parseBoolean(stringValue); case DATE: DateFormat dateFormat = ((DateFieldDefinition) fd).getDateFormat(); if (dateFormat == null) { if (NumberUtils.isNumber(stringValue)) { return new Date(Long.parseLong(stringValue)); } else { return ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(stringValue).toDate(); } } else { try { return dateFormat.parse(stringValue); } catch (ParseException ex) { log.warn("Could not parse date, defaulting to String: {}", stringValue); return stringValue; } } case NULL: return null; case INT32: return Integer.parseInt(stringValue); case INT64: return Long.parseLong(stringValue); case DOCUMENT: return populateDocument((DocumentFieldDefinition) fd, stringValue); default: throw new UnsupportedOperationException("Unsupported type: " + fd.getType().name()); } } EventParser();  EventParser(final MappingDefinition mappingDefinition); }
EventParser { public Object parseValue(final FieldDefinition fd, final String stringValue) { if (fd == null || fd.getType() == null) { try { return JSON.parse(stringValue); } catch (JSONParseException ex) { log.trace("Could not parse as JSON, defaulting to String: {}", stringValue); return stringValue; } } switch (fd.getType()) { case DOUBLE: return Double.parseDouble(stringValue); case STRING: return stringValue; case OBJECT: case ARRAY: return JSON.parse(stringValue); case BINARY: SimpleFieldDefinition sfd = (SimpleFieldDefinition) fd; final String encoding = (sfd.getEncoding() == null) ? DEFAULT_BINARY_ENCODING : sfd.getEncoding() .toLowerCase(Locale.ENGLISH); if ("base64".equals(encoding)) { return BaseEncoding.base64().decode(stringValue); } else { throw new UnsupportedOperationException("Unsupported encoding for binary type: " + encoding); } case OBJECTID: return new ObjectId(stringValue); case BOOLEAN: return Boolean.parseBoolean(stringValue); case DATE: DateFormat dateFormat = ((DateFieldDefinition) fd).getDateFormat(); if (dateFormat == null) { if (NumberUtils.isNumber(stringValue)) { return new Date(Long.parseLong(stringValue)); } else { return ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(stringValue).toDate(); } } else { try { return dateFormat.parse(stringValue); } catch (ParseException ex) { log.warn("Could not parse date, defaulting to String: {}", stringValue); return stringValue; } } case NULL: return null; case INT32: return Integer.parseInt(stringValue); case INT64: return Long.parseLong(stringValue); case DOCUMENT: return populateDocument((DocumentFieldDefinition) fd, stringValue); default: throw new UnsupportedOperationException("Unsupported type: " + fd.getType().name()); } } EventParser();  EventParser(final MappingDefinition mappingDefinition); Object parseValue(final FieldDefinition fd, final String stringValue); DBObject parse(Event event); List<DBObject> parse(List<Event> events); }
EventParser { public Object parseValue(final FieldDefinition fd, final String stringValue) { if (fd == null || fd.getType() == null) { try { return JSON.parse(stringValue); } catch (JSONParseException ex) { log.trace("Could not parse as JSON, defaulting to String: {}", stringValue); return stringValue; } } switch (fd.getType()) { case DOUBLE: return Double.parseDouble(stringValue); case STRING: return stringValue; case OBJECT: case ARRAY: return JSON.parse(stringValue); case BINARY: SimpleFieldDefinition sfd = (SimpleFieldDefinition) fd; final String encoding = (sfd.getEncoding() == null) ? DEFAULT_BINARY_ENCODING : sfd.getEncoding() .toLowerCase(Locale.ENGLISH); if ("base64".equals(encoding)) { return BaseEncoding.base64().decode(stringValue); } else { throw new UnsupportedOperationException("Unsupported encoding for binary type: " + encoding); } case OBJECTID: return new ObjectId(stringValue); case BOOLEAN: return Boolean.parseBoolean(stringValue); case DATE: DateFormat dateFormat = ((DateFieldDefinition) fd).getDateFormat(); if (dateFormat == null) { if (NumberUtils.isNumber(stringValue)) { return new Date(Long.parseLong(stringValue)); } else { return ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(stringValue).toDate(); } } else { try { return dateFormat.parse(stringValue); } catch (ParseException ex) { log.warn("Could not parse date, defaulting to String: {}", stringValue); return stringValue; } } case NULL: return null; case INT32: return Integer.parseInt(stringValue); case INT64: return Long.parseLong(stringValue); case DOCUMENT: return populateDocument((DocumentFieldDefinition) fd, stringValue); default: throw new UnsupportedOperationException("Unsupported type: " + fd.getType().name()); } } EventParser();  EventParser(final MappingDefinition mappingDefinition); Object parseValue(final FieldDefinition fd, final String stringValue); DBObject parse(Event event); List<DBObject> parse(List<Event> events);  }