/** * Converts JsonElement into a corresponded {@link DataModelReflection}. * @param definition structure of {@link DataModelReflection} * @param element conversion target * @return the converted object * @throws IOException if failed to convert * @throws IllegalArgumentException if some parameters were {@code null} */ public static DataModelReflection convert( DataModelDefinition<?> definition, JsonElement element) throws IOException { if ((element instanceof JsonObject) == false) { throw new IOException(MessageFormat.format( Messages.getString("JsonObjectDriver.errorInvalidJsonValue"), //$NON-NLS-1$ element)); } JsonObjectDriver driver = new JsonObjectDriver(definition.newReflection()); try { driver.scan(definition, (JsonObject) element); } catch (RuntimeException e) { throw new IOException(MessageFormat.format( Messages.getString("JsonObjectDriver.errorFailedToConvert"), //$NON-NLS-1$ element), e); } return driver.builder.build(); }
@Override public void datetimeProperty(PropertyName name, JsonObject context) throws IOException { JsonElement prop = property(context, name); if (prop == null) { return; } String string = prop.getAsString(); Matcher matcher = DATETIME.matcher(string); if (matcher.matches() == false) { throw new IOException(MessageFormat.format( Messages.getString("JsonObjectDriver.errorInvalidDateTimeFormat"), //$NON-NLS-1$ name, string, "yyyy-mm-dd hh:mm:ss")); //$NON-NLS-1$ } Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, Integer.parseInt(matcher.group(1))); calendar.set(Calendar.MONTH, Integer.parseInt(matcher.group(2)) - 1); calendar.set(Calendar.DATE, Integer.parseInt(matcher.group(3))); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(matcher.group(4))); calendar.set(Calendar.MINUTE, Integer.parseInt(matcher.group(5))); calendar.set(Calendar.SECOND, Integer.parseInt(matcher.group(6))); builder.add(name, calendar); } }
@Override public <V> DataModelSource createSource( DataModelDefinition<V> definition, DirectFileOutputDescription description, TestContext context) throws IOException { DirectIoTestHelper helper = new DirectIoTestHelper(context, description.getBasePath()); if (LOG.isDebugEnabled()) { LOG.debug(MessageFormat.format( "Retrieving Direct I/O output: {0}", //$NON-NLS-1$ description.getClass().getName())); } V object = definition.toObject(definition.newReflection().build()); ModelInput<? super V> input = helper.openInput(definition.getModelClass(), description); return new DataModelSource() { @Override public DataModelReflection next() throws IOException { if (input.readTo(object)) { return definition.toReflection(object); } return null; } @Override public void close() throws IOException { input.close(); } }; } }
@Override public void timeProperty(PropertyName name, JsonObject context) throws IOException { JsonElement prop = property(context, name); if (prop == null) { return; } String string = prop.getAsString(); Matcher matcher = TIME.matcher(string); if (matcher.matches() == false) { throw new IOException(MessageFormat.format( Messages.getString("JsonObjectDriver.errorInvalidTimeFormat"), //$NON-NLS-1$ name, string, "hh:mm:ss")); //$NON-NLS-1$ } Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(matcher.group(1))); calendar.set(Calendar.MINUTE, Integer.parseInt(matcher.group(2)) - 1); calendar.set(Calendar.SECOND, Integer.parseInt(matcher.group(3))); builder.add(name, calendar); }
@Override public void dateProperty(PropertyName name, JsonObject context) throws IOException { JsonElement prop = property(context, name); if (prop == null) { return; } String string = prop.getAsString(); Matcher matcher = DATE.matcher(string); if (matcher.matches() == false) { throw new IOException(MessageFormat.format( Messages.getString("JsonObjectDriver.errorInvalidDateFormat"), //$NON-NLS-1$ name, string, "yyyy-mm-dd")); //$NON-NLS-1$ } Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, Integer.parseInt(matcher.group(1))); calendar.set(Calendar.MONTH, Integer.parseInt(matcher.group(2)) - 1); calendar.set(Calendar.DATE, Integer.parseInt(matcher.group(3))); builder.add(name, calendar); }
@Override public void datetimeProperty(PropertyName name, Cell context) throws IOException { if (context.getCellTypeEnum() != CellType.NUMERIC) { throw exception(name, context, "date/datetime"); //$NON-NLS-1$ } Calendar calendar = Calendar.getInstance(); calendar.setTime(context.getDateCellValue()); Calendar result = Calendar.getInstance(); result.clear(); result.set( calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)); builder.add(name, result); }
@Override public void booleanProperty(PropertyName name, Cell context) throws IOException { if (context.getCellTypeEnum() == CellType.BOOLEAN) { builder.add(name, context.getBooleanCellValue()); } else { String string = context.getStringCellValue(); if (string.equalsIgnoreCase("true")) { //$NON-NLS-1$ builder.add(name, true); } else if (string.equalsIgnoreCase("false")) { //$NON-NLS-1$ builder.add(name, false); } else { throw exception(name, context, "boolean"); //$NON-NLS-1$ } } }
@Override public void stringProperty(PropertyName name, Cell context) throws IOException { if (context.getCellTypeEnum() != CellType.STRING) { throw new IOException(MessageFormat.format( Messages.getString("ExcelDataDriver.errorNotExplicitString"), //$NON-NLS-1$ id, context.getRowIndex() + 1, context.getColumnIndex() + 1)); } String value = context.getStringCellValue(); if (value.isEmpty() && context.getCellStyle().getQuotePrefixed() == false) { // skip empty string cells return; } builder.add(name, value); }
@Override public DataModelReflection toReflection(T object) { Builder<T> builder = newReflection(); try { for (Map.Entry<PropertyName, Method> entry : accessors.entrySet()) { PropertyName property = entry.getKey(); Method accessor = entry.getValue(); Object value = get(object, accessor); builder.add(property, value); } } catch (Exception e) { throw new AssertionError(e); } return builder.build(); }
@Override public void dateProperty(PropertyName name, Cell context) throws IOException { if (context.getCellTypeEnum() != CellType.NUMERIC) { throw exception(name, context, "date/datetime"); //$NON-NLS-1$ } Calendar calendar = Calendar.getInstance(); calendar.setTime(context.getDateCellValue()); Calendar result = Calendar.getInstance(); result.clear(); result.set( calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE)); builder.add(name, result); }
@Override public DataModelReflection toReflection(T object) { Builder<T> builder = newReflection(); for (Map.Entry<PropertyName, Field> entry : fields.entrySet()) { PropertyName name = entry.getKey(); Field field = entry.getValue(); try { Object value = field.get(object); builder.add(name, value); } catch (IllegalAccessException e) { throw new AssertionError(e); } } return builder.build(); }
@Override public void timeProperty(PropertyName name, Cell context) throws IOException { if (context.getCellTypeEnum() != CellType.NUMERIC) { throw exception(name, context, "date/datetime"); //$NON-NLS-1$ } Calendar calendar = Calendar.getInstance(); calendar.setTime(context.getDateCellValue()); Calendar result = Calendar.getInstance(); result.clear(); result.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY)); result.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE)); result.set(Calendar.SECOND, calendar.get(Calendar.SECOND)); builder.add(name, result); }
@Override public void integerProperty(PropertyName name, Cell context) throws IOException { BigDecimal decimal = toDecimal(name, context, "integer"); //$NON-NLS-1$ builder.add(name, decimal.toBigInteger()); }
@Override public void longProperty(PropertyName name, Cell context) throws IOException { long value = toLong(name, context, "long"); //$NON-NLS-1$ builder.add(name, value); }
@Override public void shortProperty(PropertyName name, Cell context) throws IOException { long value = toLong(name, context, "short"); //$NON-NLS-1$ checkRange(name, context, value, Short.MIN_VALUE, Short.MAX_VALUE); builder.add(name, (short) value); }
@Override public void doubleProperty(PropertyName name, JsonObject context) throws IOException { JsonElement prop = property(context, name); if (prop == null) { return; } builder.add(name, prop.getAsDouble()); }
@Override public void floatProperty(PropertyName name, JsonObject context) throws IOException { JsonElement prop = property(context, name); if (prop == null) { return; } builder.add(name, prop.getAsFloat()); }
@Override public void booleanProperty(PropertyName name, JsonObject context) throws IOException { JsonElement prop = property(context, name); if (prop == null) { return; } builder.add(name, prop.getAsBoolean()); }
@Override public Builder<T> newReflection() { return new Builder<>(this); }
@Override public Builder<T> newReflection() { return new Builder<>(this); }