All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.simy4.xpath.expr.PathExpr Maven / Gradle / Ivy

There is a newer version: 2.3.9
Show newest version
package com.github.simy4.xpath.expr;

import com.github.simy4.xpath.XmlBuilderException;
import com.github.simy4.xpath.navigator.Node;
import com.github.simy4.xpath.util.FlatteningIterator;
import com.github.simy4.xpath.util.Function;
import com.github.simy4.xpath.util.TransformingIterator;
import com.github.simy4.xpath.view.IterableNodeView;
import com.github.simy4.xpath.view.NodeSetView;
import com.github.simy4.xpath.view.NodeView;
import com.github.simy4.xpath.view.View;
import com.github.simy4.xpath.view.ViewContext;

import java.util.Iterator;
import java.util.List;

public class PathExpr implements Expr {

    private final List pathExpr;

    public PathExpr(List pathExpr) {
        this.pathExpr = pathExpr;
    }

    @Override
    public  View resolve(final ViewContext context) throws XmlBuilderException {
        IterableNodeView children = context.getCurrent();
        for (final StepExpr stepExpr : pathExpr) {
            final IterableNodeView currentChildren = children;
            children = new NodeSetView(new Iterable>() {
                @Override
                public Iterator> iterator() {
                    final Iterator> iterator = currentChildren.iterator();
                    return new FlatteningIterator>(
                            new TransformingIterator, Iterator>>(iterator,
                                    new StepResolver(context, iterator, stepExpr)));
                }
            });
        }
        return children;
    }

    @Override
    public String toString() {
        final Iterator pathExprIterator = pathExpr.iterator();
        final StringBuilder stringBuilder = new StringBuilder();
        if (pathExprIterator.hasNext()) {
            stringBuilder.append(pathExprIterator.next());
            while (pathExprIterator.hasNext()) {
                stringBuilder.append('/').append(pathExprIterator.next());
            }
        }
        return stringBuilder.toString();
    }

    private static final class StepResolver implements Function, Iterator>> {

        private final ViewContext parentContext;
        private final Iterator> iterator;
        private final StepExpr stepExpr;
        private int position = 1;

        private StepResolver(ViewContext parentContext, Iterator> iterator, StepExpr stepExpr) {
            this.parentContext = parentContext;
            this.iterator = iterator;
            this.stepExpr = stepExpr;
        }

        @Override
        public Iterator> apply(NodeView view) {
            final ViewContext context = new ViewContext(parentContext.getNavigator(), view,
                    parentContext.isGreedy(), iterator.hasNext(), position++);
            return stepExpr.resolve(context).iterator();
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy