Search in sources :

Example 6 with CsvColumnKey

use of org.simpleflatmapper.csv.CsvColumnKey in project SimpleFlatMapper by arnaudroger.

the class CsvMapperCellHandlerBuilder method createTargetSetterFactory.

public static byte[] createTargetSetterFactory(String factoryName, String className, Type target) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    String factoryType = AsmUtils.toAsmType(factoryName);
    String classType = AsmUtils.toAsmType(className);
    String targetType = AsmUtils.toAsmType(target);
    cw.visit(Opcodes.V1_6, Opcodes.ACC_FINAL + Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, factoryType, "L" + CELL_HANDLER_FACTORY_TYPE + "<L" + targetType + ";>;", CELL_HANDLER_FACTORY_TYPE, null);
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(" + AsmUtils.toTargetTypeDeclaration(Instantiator.class) + AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class) + AsmUtils.toTargetTypeDeclaration(ParsingContextFactory.class) + AsmUtils.toTargetTypeDeclaration(FieldMapperErrorHandler.class) + ")V", "(" + "L" + AsmUtils.toAsmType(Instantiator.class) + "<L" + AsmUtils.toAsmType(CsvMapperCellHandler.class) + "<L" + targetType + ";>;L" + targetType + ";>;" + AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class) + AsmUtils.toTargetTypeDeclaration(ParsingContextFactory.class) + "L" + AsmUtils.toAsmType(FieldMapperErrorHandler.class) + "<L" + AsmUtils.toAsmType(CsvColumnKey.class) + ";>;" + ")V", null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitVarInsn(Opcodes.ALOAD, 3);
        mv.visitVarInsn(Opcodes.ALOAD, 4);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, CELL_HANDLER_FACTORY_TYPE, "<init>", "(" + AsmUtils.toTargetTypeDeclaration(Instantiator.class) + AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class) + AsmUtils.toTargetTypeDeclaration(ParsingContextFactory.class) + AsmUtils.toTargetTypeDeclaration(FieldMapperErrorHandler.class) + ")V", false);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(5, 5);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "newInstance", "(" + AsmUtils.toTargetTypeDeclaration(DelayedCellSetter[].class) + AsmUtils.toTargetTypeDeclaration(CellSetter[].class) + ")" + AsmUtils.toTargetTypeDeclaration(CsvMapperCellHandler.class), "(" + "[L" + DELAYED_CELL_SETTER_TYPE + "<L" + targetType + ";*>;" + "[L" + CELL_SETTER_TYPE + "<L" + targetType + ";>;" + ")" + "L" + AsmUtils.toAsmType(CsvMapperCellHandler.class) + "<L" + targetType + ";>;", null);
        mv.visitCode();
        mv.visitTypeInsn(Opcodes.NEW, classType);
        mv.visitInsn(Opcodes.DUP);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, factoryType, "instantiator", AsmUtils.toTargetTypeDeclaration(Instantiator.class));
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, factoryType, "keys", AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class));
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, factoryType, "parsingContextFactory", AsmUtils.toTargetTypeDeclaration(ParsingContextFactory.class));
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, AsmUtils.toAsmType(ParsingContextFactory.class), "newContext", "()" + AsmUtils.toTargetTypeDeclaration(ParsingContext.class), false);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, factoryType, "fieldErrorHandler", AsmUtils.toTargetTypeDeclaration(FieldMapperErrorHandler.class));
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, classType, "<init>", "(" + AsmUtils.toTargetTypeDeclaration(Instantiator.class) + AsmUtils.toTargetTypeDeclaration(DelayedCellSetter[].class) + AsmUtils.toTargetTypeDeclaration(CellSetter[].class) + AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class) + AsmUtils.toTargetTypeDeclaration(ParsingContext.class) + AsmUtils.toTargetTypeDeclaration(FieldMapperErrorHandler.class) + ")V", false);
        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(8, 3);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : CellSetter(org.simpleflatmapper.csv.mapper.CellSetter) DelayedCellSetter(org.simpleflatmapper.csv.mapper.DelayedCellSetter) ParsingContext(org.simpleflatmapper.csv.ParsingContext) DelayedCellSetter(org.simpleflatmapper.csv.mapper.DelayedCellSetter) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) ParsingContextFactory(org.simpleflatmapper.csv.ParsingContextFactory) Instantiator(org.simpleflatmapper.reflect.Instantiator) CsvMapperCellHandler(org.simpleflatmapper.csv.mapper.CsvMapperCellHandler) FieldMapperErrorHandler(org.simpleflatmapper.map.FieldMapperErrorHandler) ClassWriter(org.simpleflatmapper.ow2asm.ClassWriter) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 7 with CsvColumnKey

use of org.simpleflatmapper.csv.CsvColumnKey in project SimpleFlatMapper by arnaudroger.

the class ColumnsMapperKeyBuilderCellConsumer method newCell.

@Override
public void newCell(char[] chars, int offset, int length) {
    if (!hasNoData)
        throw new IllegalArgumentException("Already consume the headers");
    columns.add(new CsvColumnKey(new String(chars, offset, length), index));
    index++;
}
Also used : CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey)

Example 8 with CsvColumnKey

use of org.simpleflatmapper.csv.CsvColumnKey in project SimpleFlatMapper by arnaudroger.

the class CsvParserTest method testDSLIgnoreField.

@Test
public void testDSLIgnoreField() throws Exception {
    Iterator<Tuple2<String, String>> iterator = CsvParser.mapTo(String.class, String.class).columnDefinition(new Predicate<CsvColumnKey>() {

        @Override
        public boolean test(CsvColumnKey csvColumnKey) {
            return csvColumnKey.getIndex() != 1 && csvColumnKey.getIndex() != 2;
        }
    }, CsvColumnDefinition.ignoreDefinition()).iterator(new StringReader("-1,0,1,2\nv0,v1,v2,v3"));
    Tuple2<String, String> tuple = iterator.next();
    assertEquals("v1", tuple.first());
    assertEquals("v2", tuple.second());
}
Also used : Tuple2(org.simpleflatmapper.tuple.Tuple2) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) StringReader(java.io.StringReader) Predicate(org.simpleflatmapper.util.Predicate) Test(org.junit.Test)

Example 9 with CsvColumnKey

use of org.simpleflatmapper.csv.CsvColumnKey in project SimpleFlatMapper by arnaudroger.

the class CsvWriterBuilderTest method testWriteCsvOnDbObject.

@Test
public void testWriteCsvOnDbObject() throws Exception {
    MapperConfig<CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>> config = MapperConfig.<CsvColumnKey>fieldMapperConfig();
    CsvWriterBuilder<DbObject> builder = CsvWriterBuilder.newBuilder(DbObject.class);
    Mapper<DbObject, Appendable> mapper = builder.addColumn("id").addColumn("name").addColumn("email").addColumn("creation_time", new DateFormatProperty("dd/MM/yyyy HH:mm:ss")).addColumn("type_ordinal", new EnumOrdinalFormatProperty()).addColumn("type_name").mapper();
    DbObject dbObject = CsvWriterTest.newDbObject();
    assertEquals("13,name,email,06/06/2015 17:46:23,1,type3\r\n", mapper.map(dbObject).toString());
    dbObject.setEmail("email,e\" ");
    assertEquals("13,name,\"email,e\"\" \",06/06/2015 17:46:23,1,type3\r\n", mapper.map(dbObject).toString());
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) DbObject(org.simpleflatmapper.test.beans.DbObject) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) EnumOrdinalFormatProperty(org.simpleflatmapper.map.property.EnumOrdinalFormatProperty) DateFormatProperty(org.simpleflatmapper.map.property.DateFormatProperty) Test(org.junit.Test)

Example 10 with CsvColumnKey

use of org.simpleflatmapper.csv.CsvColumnKey in project SimpleFlatMapper by arnaudroger.

the class FieldMapperToAppendableFactory method setterFromFactory.

@SuppressWarnings("unchecked")
private <S, P> Setter<Appendable, ? super P> setterFromFactory(PropertyMapping<S, P, CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>> pm) {
    Setter<Appendable, ? super P> setter = null;
    final SetterFactoryProperty setterFactoryProperty = pm.getColumnDefinition().lookFor(SetterFactoryProperty.class);
    if (setterFactoryProperty != null) {
        final SetterFactory<Appendable, PropertyMapping<S, P, CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>>> setterFactory = (SetterFactory<Appendable, PropertyMapping<S, P, CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>>>) setterFactoryProperty.getSetterFactory();
        setter = setterFactory.getSetter(pm);
    }
    if (setter == null) {
        if (!pm.getPropertyMeta().isSelf()) {
            final ClassMeta<P> classMeta = pm.getPropertyMeta().getPropertyClassMeta();
            if (classMeta instanceof ObjectClassMeta) {
                ObjectClassMeta<P> ocm = (ObjectClassMeta<P>) classMeta;
                if (ocm.getNumberOfProperties() == 1) {
                    PropertyMeta<P, ?> subProp = ocm.getFirstProperty();
                    Setter<Appendable, Object> subSetter = (Setter<Appendable, Object>) setterFromFactory(pm.propertyMeta(subProp));
                    if (subSetter != null) {
                        setter = new SetterOnGetter<Appendable, Object, P>(subSetter, (Getter<P, Object>) subProp.getGetter());
                    } else {
                        return new ObjectToStringSetter<P>(subProp.getGetter());
                    }
                }
            }
        }
    }
    return setter;
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) CharacterGetter(org.simpleflatmapper.reflect.primitive.CharacterGetter) ShortGetter(org.simpleflatmapper.reflect.primitive.ShortGetter) LongGetter(org.simpleflatmapper.reflect.primitive.LongGetter) BooleanGetter(org.simpleflatmapper.reflect.primitive.BooleanGetter) Getter(org.simpleflatmapper.reflect.Getter) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter) FloatGetter(org.simpleflatmapper.reflect.primitive.FloatGetter) SetterOnGetter(org.simpleflatmapper.reflect.setter.SetterOnGetter) ByteGetter(org.simpleflatmapper.reflect.primitive.ByteGetter) DoubleGetter(org.simpleflatmapper.reflect.primitive.DoubleGetter) SetterFactory(org.simpleflatmapper.reflect.SetterFactory) SetterFactoryProperty(org.simpleflatmapper.map.property.SetterFactoryProperty) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) Setter(org.simpleflatmapper.reflect.Setter) ObjectClassMeta(org.simpleflatmapper.reflect.meta.ObjectClassMeta)

Aggregations

CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)16 Test (org.junit.Test)10 DateFormatProperty (org.simpleflatmapper.map.property.DateFormatProperty)4 Type (java.lang.reflect.Type)3 ParsingContext (org.simpleflatmapper.csv.ParsingContext)3 StringReader (java.io.StringReader)2 CellValueReader (org.simpleflatmapper.csv.CellValueReader)2 CellValueReaderFactory (org.simpleflatmapper.csv.CellValueReaderFactory)2 CsvColumnDefinition (org.simpleflatmapper.csv.CsvColumnDefinition)2 ParsingContextFactoryBuilder (org.simpleflatmapper.csv.ParsingContextFactoryBuilder)2 EnumOrdinalFormatProperty (org.simpleflatmapper.map.property.EnumOrdinalFormatProperty)2 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)2 BooleanGetter (org.simpleflatmapper.reflect.primitive.BooleanGetter)2 ByteGetter (org.simpleflatmapper.reflect.primitive.ByteGetter)2 CharacterGetter (org.simpleflatmapper.reflect.primitive.CharacterGetter)2 DoubleGetter (org.simpleflatmapper.reflect.primitive.DoubleGetter)2 FloatGetter (org.simpleflatmapper.reflect.primitive.FloatGetter)2 IntGetter (org.simpleflatmapper.reflect.primitive.IntGetter)2 LongGetter (org.simpleflatmapper.reflect.primitive.LongGetter)2 ShortGetter (org.simpleflatmapper.reflect.primitive.ShortGetter)2