Examples of BindyFixedLengthFactory


Examples of org.apache.camel.dataformat.bindy.BindyFixedLengthFactory

    /**
     * Method used to create the singleton of the BindyCsvFactory
     */
    public BindyFixedLengthFactory getFactory(PackageScanClassResolver resolver) throws Exception {
        if (modelFactory == null) {
            modelFactory = new BindyFixedLengthFactory(resolver, packages);
        }
        return modelFactory;
    }
View Full Code Here

Examples of org.apache.camel.dataformat.bindy.BindyFixedLengthFactory

    }

    @SuppressWarnings("unchecked")
    public void marshal(Exchange exchange, Object body, OutputStream outputStream) throws Exception {
        PackageScanClassResolver resolver = exchange.getContext().getPackageScanClassResolver();
        BindyFixedLengthFactory factory = (BindyFixedLengthFactory) getFactory(resolver);
        ObjectHelper.notNull(factory, "not instantiated");

        // Get CRLF
        byte[] bytesCRLF = ConverterUtils.getByteReturn(factory.getCarriageReturn());

        List<Map<String, Object>> models;

        // the body is not a prepared list so help a bit here and create one for us
        if (exchange.getContext().getTypeConverter().convertTo(List.class, body) == null) {
            models = new ArrayList<Map<String, Object>>();
            Iterator<?> it = ObjectHelper.createIterator(body);
            while (it.hasNext()) {
                Object model = it.next();
                String name = model.getClass().getName();
                Map<String, Object> row = new HashMap<String, Object>();
                row.put(name, body);
                models.add(row);
            }
        } else {
            // cast to the expected type
            models = (List<Map<String, Object>>) body;
        }
       
        // add the header if it is in the exchange header
        Map<String, Object> headerRow = (Map<String, Object>) exchange.getIn().getHeader(CAMEL_BINDY_FIXED_LENGTH_HEADER);
        if (headerRow != null) {
            models.add(0, headerRow);
        }
       
        // add the footer if it is in the exchange header
        Map<String, Object> footerRow = (Map<String, Object>) exchange.getIn().getHeader(CAMEL_BINDY_FIXED_LENGTH_FOOTER);
        if (footerRow != null) {
            models.add(models.size(), footerRow);
        }

        int row = 0;
        for (Map<String, Object> model : models) {
            row++;
            String result = null;
           
            if (row == 1 && headerFactory != null) {
                // marshal the first row as a header if the models match
                Set<String> modelClassNames = model.keySet();
                // only use the header factory if the row is the header
                if (headerFactory.supportsModel(modelClassNames)) {
                    if (factory.skipHeader())  {
                        LOG.info("Skipping marshal of header row; 'skipHeader=true'");
                        continue;
                    } else {
                        result = headerFactory.unbind(model);
                    }   
                }
            } else if (row == models.size() && footerFactory != null) {
                // marshal the last row as a footer if the models match
                Set<String> modelClassNames = model.keySet();
                // only use the header factory if the row is the header
                if (footerFactory.supportsModel(modelClassNames)) {
                    if (factory.skipFooter()) {
                        LOG.info("Skipping marshal of footer row; 'skipFooter=true'");
                        continue;
                    } else {
                        result = footerFactory.unbind(model);
                    }
                }
            }
           
            if (result == null) {
                // marshal as a normal / default row
                result = factory.unbind(model);
            }
           
            byte[] bytes = exchange.getContext().getTypeConverter().convertTo(byte[].class, exchange, result);
            outputStream.write(bytes);
View Full Code Here

Examples of org.apache.camel.dataformat.bindy.BindyFixedLengthFactory

        }
    }

    public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
        PackageScanClassResolver resolver = exchange.getContext().getPackageScanClassResolver();
        BindyFixedLengthFactory factory = (BindyFixedLengthFactory) getFactory(resolver);
        ObjectHelper.notNull(factory, "not instantiated");
       
        // List of Pojos
        List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();

        // Pojos of the model
        Map<String, Object> model;

        InputStreamReader in = new InputStreamReader(inputStream, IOHelper.getCharsetName(exchange));

        // Scanner is used to read big file
        Scanner scanner = new Scanner(in);

        AtomicInteger count = new AtomicInteger(0);

        try {

            // Parse the header if it exists
            if (scanner.hasNextLine() && factory.hasHeader()) {
               
                // Read the line (should not trim as its fixed length)
                String line = getNextNonEmptyLine(scanner, count);
               
                if (!factory.skipHeader()) {
                    Map<String, Object> headerObjMap = createModel(headerFactory, line, count.intValue());
                    exchange.getOut().setHeader(CAMEL_BINDY_FIXED_LENGTH_HEADER, headerObjMap);
                }
            }

            String thisLine = getNextNonEmptyLine(scanner, count);

            String nextLine = null;
            if (thisLine != null) {
                nextLine = getNextNonEmptyLine(scanner, count);
            }

            // Parse the main file content
            while (thisLine != null && nextLine != null) {
               
                model = createModel(factory, thisLine, count.intValue());

                // Add objects graph to the list
                models.add(model);

                thisLine = nextLine;
                nextLine = getNextNonEmptyLine(scanner, count);
            }
           
            // this line should be the last non-empty line from the file
            // optionally parse the line as a footer
            if (thisLine != null) {
                if (factory.hasFooter()) {
                    if (!factory.skipFooter()) {
                        Map<String, Object> footerObjMap = createModel(footerFactory, thisLine, count.intValue());
                        exchange.getOut().setHeader(CAMEL_BINDY_FIXED_LENGTH_FOOTER, footerObjMap);
                    }
                } else {
                    model = createModel(factory, thisLine, count.intValue());
View Full Code Here

Examples of org.apache.camel.dataformat.bindy.BindyFixedLengthFactory

                FixedLengthRecord record = type.getAnnotation(FixedLengthRecord.class);
                return record != null && !record.isFooter() && !record.isHeader();
            }
        };

        BindyFixedLengthFactory factory;
        if (getClassType() != null) {
            factory = new BindyFixedLengthFactory(resolver, defaultRecordScanFilter, getClassType());
        } else {
            factory = new BindyFixedLengthFactory(resolver, defaultRecordScanFilter, getPackages());
        }
       
        // Optionally initialize the header factory... using header model classes
        if (factory.hasHeader()) {
            PackageScanFilter headerScanFilter = new PackageScanFilter() {
                @Override
                public boolean matches(Class<?> type) {
                    FixedLengthRecord record = type.getAnnotation(FixedLengthRecord.class);
                    return record != null && record.isHeader();
                }
            };
           
            if (getClassType() != null) {
                this.headerFactory = new BindyFixedLengthFactory(resolver, headerScanFilter, getClassType());
            } else {
                this.headerFactory = new BindyFixedLengthFactory(resolver, headerScanFilter, getPackages());
            }
        }
       
        // Optionally initialize the footer factory... using footer model classes
        if (factory.hasFooter()) {
           
            PackageScanFilter footerScanFilter = new PackageScanFilter() {
                @Override
                public boolean matches(Class<?> type) {
                    FixedLengthRecord record = type.getAnnotation(FixedLengthRecord.class);
                    return record != null && record.isFooter();
                }
            };
           
            if (getClassType() != null) {
                this.footerFactory = new BindyFixedLengthFactory(resolver, footerScanFilter, getClassType());
            } else {
                this.footerFactory = new BindyFixedLengthFactory(resolver, footerScanFilter, getPackages());
            }
        }
       
        return factory;
    }
View Full Code Here

Examples of org.apache.camel.dataformat.bindy.BindyFixedLengthFactory

    }

    @SuppressWarnings("unchecked")
    public void marshal(Exchange exchange, Object body, OutputStream outputStream) throws Exception {

        BindyFixedLengthFactory factory = getFactory(exchange.getContext().getPackageScanClassResolver());
        ObjectHelper.notNull(factory, "not instantiated");

        // Get CRLF
        byte[] bytesCRLF = Converter.getByteReturn(factory.getCarriageReturn());

        List<Map<String, Object>> models;

        // the body is not a prepared list so help a bit here and create one for us
        if (exchange.getContext().getTypeConverter().convertTo(List.class, body) == null) {
            models = new ArrayList<Map<String, Object>>();
            Iterator it = ObjectHelper.createIterator(body);
            while (it.hasNext()) {
                Object model = it.next();
                String name = model.getClass().getName();
                Map<String, Object> row = new HashMap<String, Object>();
                row.put(name, body);
                models.add(row);
            }
        } else {
            // cast to the expected type
            models = (List<Map<String, Object>>) body;
        }

        for (Map<String, Object> model : models) {

            String result = factory.unbind(model);

            byte[] bytes = exchange.getContext().getTypeConverter().convertTo(byte[].class, exchange, result);
            outputStream.write(bytes);

            // Add a carriage return
View Full Code Here

Examples of org.apache.camel.dataformat.bindy.BindyFixedLengthFactory

            outputStream.write(bytesCRLF);
        }
    }

    public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
        BindyFixedLengthFactory factory = getFactory(exchange.getContext().getPackageScanClassResolver());
        ObjectHelper.notNull(factory, "not instantiated");

        // List of Pojos
        List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();

        // Pojos of the model
        Map<String, Object> model;

        InputStreamReader in = new InputStreamReader(inputStream);

        // Scanner is used to read big file
        Scanner scanner = new Scanner(in);

        int count = 0;

        try {

            // TODO Test if we have a Header
            // TODO Test if we have a Footer (containing by example checksum)

            while (scanner.hasNextLine()) {
                String line;
               
                // Read the line (should not trim as its fixed length)
                line = scanner.nextLine();

                if (ObjectHelper.isEmpty(line)) {
                    // skip if line is empty
                    continue;
                }

                // Increment counter
                count++;
               
                // Check if the record length corresponds to the parameter
                // provided in the @FixedLengthRecord
                if ((line.length() < factory.recordLength()) || (line.length() > factory.recordLength())) {
                    throw new java.lang.IllegalArgumentException("Size of the record : " + line.length() + " is not equal to the value provided in the model : " + factory.recordLength() + " !");
                }

                // Create POJO where Fixed data will be stored
                model = factory.factory();
               
                // Bind data from Fixed record with model classes
                factory.bind(line, model, count);

                // Link objects together
                factory.link(model);

                // Add objects graph to the list
                models.add(model);

                if (LOG.isDebugEnabled()) {
View Full Code Here

Examples of org.apache.camel.dataformat.bindy.BindyFixedLengthFactory

    /**
     * Method used to create the singleton of the BindyCsvFactory
     */
    public BindyFixedLengthFactory getFactory(PackageScanClassResolver resolver) throws Exception {
        if (modelFactory == null) {
            modelFactory = new BindyFixedLengthFactory(resolver, packages);
        }
        return modelFactory;
    }
View Full Code Here

Examples of org.apache.camel.dataformat.bindy.BindyFixedLengthFactory

    }

    @SuppressWarnings("unchecked")
    public void marshal(Exchange exchange, Object body, OutputStream outputStream) throws Exception {
        PackageScanClassResolver resolver = exchange.getContext().getPackageScanClassResolver();
        BindyFixedLengthFactory factory = (BindyFixedLengthFactory) getFactory(resolver);
        ObjectHelper.notNull(factory, "not instantiated");

        // Get CRLF
        byte[] bytesCRLF = ConverterUtils.getByteReturn(factory.getCarriageReturn());

        List<Map<String, Object>> models;

        // the body is not a prepared list so help a bit here and create one for us
        if (exchange.getContext().getTypeConverter().convertTo(List.class, body) == null) {
            models = new ArrayList<Map<String, Object>>();
            Iterator<?> it = ObjectHelper.createIterator(body);
            while (it.hasNext()) {
                Object model = it.next();
                String name = model.getClass().getName();
                Map<String, Object> row = new HashMap<String, Object>();
                row.put(name, body);
                models.add(row);
            }
        } else {
            // cast to the expected type
            models = (List<Map<String, Object>>) body;
        }
       
        // add the header if it is in the exchange header
        Map<String, Object> headerRow = (Map<String, Object>) exchange.getIn().getHeader(CAMEL_BINDY_FIXED_LENGTH_HEADER);
        if (headerRow != null) {
            models.add(0, headerRow);
        }
       
        // add the footer if it is in the exchange header
        Map<String, Object> footerRow = (Map<String, Object>) exchange.getIn().getHeader(CAMEL_BINDY_FIXED_LENGTH_FOOTER);
        if (footerRow != null) {
            models.add(models.size(), footerRow);
        }

        int row = 0;
        for (Map<String, Object> model : models) {
            row++;
            String result = null;
           
            if (row == 1 && headerFactory != null) {
                // marshal the first row as a header if the models match
                Set<String> modelClassNames = model.keySet();
                // only use the header factory if the row is the header
                if (headerFactory.supportsModel(modelClassNames)) {
                    if (factory.skipHeader())  {
                        LOG.info("Skipping marshal of header row; 'skipHeader=true'");
                        continue;
                    } else {
                        result = headerFactory.unbind(model);
                    }   
                }
            } else if (row == models.size() && footerFactory != null) {
                // marshal the last row as a footer if the models match
                Set<String> modelClassNames = model.keySet();
                // only use the header factory if the row is the header
                if (footerFactory.supportsModel(modelClassNames)) {
                    if (factory.skipFooter()) {
                        LOG.info("Skipping marshal of footer row; 'skipFooter=true'");
                        continue;
                    } else {
                        result = footerFactory.unbind(model);
                    }
                }
            }
           
            if (result == null) {
                // marshal as a normal / default row
                result = factory.unbind(model);
            }
           
            byte[] bytes = exchange.getContext().getTypeConverter().convertTo(byte[].class, exchange, result);
            outputStream.write(bytes);
View Full Code Here

Examples of org.apache.camel.dataformat.bindy.BindyFixedLengthFactory

        }
    }

    public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
        PackageScanClassResolver resolver = exchange.getContext().getPackageScanClassResolver();
        BindyFixedLengthFactory factory = (BindyFixedLengthFactory) getFactory(resolver);
        ObjectHelper.notNull(factory, "not instantiated");
       
        // List of Pojos
        List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();

        // Pojos of the model
        Map<String, Object> model;

        InputStreamReader in = new InputStreamReader(inputStream, IOHelper.getCharsetName(exchange));

        // Scanner is used to read big file
        Scanner scanner = new Scanner(in);

        AtomicInteger count = new AtomicInteger(0);

        try {

            // Parse the header if it exists
            if (scanner.hasNextLine() && factory.hasHeader()) {
               
                // Read the line (should not trim as its fixed length)
                String line = getNextNonEmptyLine(scanner, count);
               
                if (!factory.skipHeader()) {
                    Map<String, Object> headerObjMap = createModel(headerFactory, line, count.intValue());
                    exchange.getOut().setHeader(CAMEL_BINDY_FIXED_LENGTH_HEADER, headerObjMap);
                }
            }

            String thisLine = getNextNonEmptyLine(scanner, count);

            String nextLine = null;
            if (thisLine != null) {
                nextLine = getNextNonEmptyLine(scanner, count);
            }

            // Parse the main file content
            while (thisLine != null && nextLine != null) {
               
                model = createModel(factory, thisLine, count.intValue());

                // Add objects graph to the list
                models.add(model);

                thisLine = nextLine;
                nextLine = getNextNonEmptyLine(scanner, count);
            }
           
            // this line should be the last non-empty line from the file
            // optionally parse the line as a footer
            if (thisLine != null) {
                if (factory.hasFooter()) {
                    if (!factory.skipFooter()) {
                        Map<String, Object> footerObjMap = createModel(footerFactory, thisLine, count.intValue());
                        exchange.getOut().setHeader(CAMEL_BINDY_FIXED_LENGTH_FOOTER, footerObjMap);
                    }
                } else {
                    model = createModel(factory, thisLine, count.intValue());
View Full Code Here

Examples of org.apache.camel.dataformat.bindy.BindyFixedLengthFactory

                FixedLengthRecord record = type.getAnnotation(FixedLengthRecord.class);
                return record != null && !record.isFooter() && !record.isHeader();
            }
        };

        BindyFixedLengthFactory factory;
        if (getClassType() != null) {
            factory = new BindyFixedLengthFactory(resolver, defaultRecordScanFilter, getClassType());
        } else {
            factory = new BindyFixedLengthFactory(resolver, defaultRecordScanFilter, getPackages());
        }
       
        // Optionally initialize the header factory... using header model classes
        if (factory.hasHeader()) {
            PackageScanFilter headerScanFilter = new PackageScanFilter() {
                @Override
                public boolean matches(Class<?> type) {
                    FixedLengthRecord record = type.getAnnotation(FixedLengthRecord.class);
                    return record != null && record.isHeader();
                }
            };
           
            if (getClassType() != null) {
                this.headerFactory = new BindyFixedLengthFactory(resolver, headerScanFilter, getClassType());
            } else {
                this.headerFactory = new BindyFixedLengthFactory(resolver, headerScanFilter, getPackages());
            }
        }
       
        // Optionally initialize the footer factory... using footer model classes
        if (factory.hasFooter()) {
           
            PackageScanFilter footerScanFilter = new PackageScanFilter() {
                @Override
                public boolean matches(Class<?> type) {
                    FixedLengthRecord record = type.getAnnotation(FixedLengthRecord.class);
                    return record != null && record.isFooter();
                }
            };
           
            if (getClassType() != null) {
                this.footerFactory = new BindyFixedLengthFactory(resolver, footerScanFilter, getClassType());
            } else {
                this.footerFactory = new BindyFixedLengthFactory(resolver, footerScanFilter, getPackages());
            }
        }
       
        return factory;
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.