public Iterator getOutput()

in src/main/java/org/apache/sling/pipes/internal/inputstream/CsvPipe.java [59:100]


    public Iterator<Resource> getOutput(InputStream inputStream) {
        Iterator<Resource> output = EMPTY_ITERATOR;
        String separator = properties.get(PN_SEPARATOR, DEFAULT_SEPARATOR);
        reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        try {
            String headersLine = reader.readLine();
            final String[] headers = headersLine.split(separator);
            if (headers.length > 0){
                nextLine = reader.readLine();
                final Resource inputResource = getInput();
                output = new Iterator<Resource>() {
                    @Override
                    public boolean hasNext() {
                        return StringUtils.isNotBlank(nextLine);
                    }
                    @Override
                    public Resource next() {
                        try {
                            String[] values = nextLine.split(separator);
                            if (values.length < headers.length){
                                throw new IllegalArgumentException("wrong format line " + index + " should have at least the same number of columns than the headers");
                            }
                            Map<String, String> map = new HashMap<>();
                            for (int i = 0; i < headers.length; i ++){
                                map.put(headers[i], values[i]);
                            }
                            binding = map;
                            nextLine = reader.readLine();
                        } catch (IOException e) {
                            logger.error("Unable to retrieve {}nth line of csv file", index, e);
                            nextLine = null;
                            throw new NoSuchElementException();
                        }
                        return inputResource;
                    }
                };
            }
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
        return output;
    }