public Iterator getOutput()

in src/main/java/org/apache/sling/pipes/internal/inputstream/RegexpPipe.java [55:104]


    public Iterator<Resource> getOutput(InputStream inputStream) {
        Iterator<Resource> output = EMPTY_ITERATOR;
        try {
            String patternString = bindings.instantiateExpression(properties.get(PN_PATTERN, String.class));
            if (patternString == null){
                logger.debug("pattern {} evaluates as empty.", properties.get(PN_PATTERN, String.class));
                return output;
            }
            final Collection<String> names = getGroupNames(patternString);
            if (names.isEmpty()){
                logger.debug("no name defined, will take the whole match");
            }
            Pattern pattern = Pattern.compile(patternString);
            String text = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
            logger.trace("about to parse {}", text);
            Matcher matcher = pattern.matcher(text);
            if (matcher.find()) {
                final Resource next = getInput();
                output = new Iterator<Resource>() {
                    boolean hasNext = true;
                    @Override
                    public boolean hasNext() {
                        return hasNext;
                    }

                    @Override
                    public Resource next() {
                        if (! hasNext) {
                            throw new NoSuchElementException();
                        }
                        if (!names.isEmpty()){
                            Map<String, Object> map = new HashMap<>();
                            for (String name : names) {
                                map.put(name, matcher.group(name));
                            }
                            binding = map;
                        } else {
                            //no group names defined, we take the whole match
                            binding = matcher.group(0);
                        }
                        hasNext = matcher.find();
                        return next;
                    }
                };
            }
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
        return output;
    }