Search in sources :

Example 1 with IteratorChain

use of org.apache.commons.collections4.iterators.IteratorChain in project sling by apache.

the class ResourceResolverControl method listChildren.

/**
     * This method asks all matching resource providers for the children iterators,
     * merges them, adds {@link SyntheticResource}s (see
     * {@link #getResource(String, Resource, Map, boolean)} for more details),
     * filters out the duplicates and returns the resulting iterator. All
     * transformations are done lazily, during the {@link Iterator#hasNext()}
     * invocation on the result.
     */
@SuppressWarnings("unchecked")
public Iterator<Resource> listChildren(final ResourceResolverContext context, final Resource parent) {
    final String parentPath = parent.getPath();
    // 3 sources are combined: children of the provider which owns 'parent',
    // providers which are directly mounted at a child path,
    // synthetic resources for providers mounted at a lower level
    // children of the 'parent' provider
    Iterator<Resource> realChildren = null;
    final AuthenticatedResourceProvider provider = this.getBestMatchingProvider(context, parentPath);
    if (provider != null) {
        realChildren = provider.listChildren(parent);
    }
    final Set<String> visitedNames = new HashSet<>();
    IteratorChain chain = new IteratorChain();
    if (realChildren != null) {
        chain.addIterator(realChildren);
    }
    // synthetic and providers are done in one loop
    final Node<ResourceProviderHandler> node = getResourceProviderStorage().getTree().getNode(parent.getPath());
    if (node != null) {
        final List<Resource> syntheticList = new ArrayList<>();
        final List<Resource> providerList = new ArrayList<>();
        for (final Entry<String, Node<ResourceProviderHandler>> entry : node.getChildren().entrySet()) {
            final String name = entry.getKey();
            final ResourceProviderHandler handler = entry.getValue().getValue();
            PathBuilder pathBuilder = new PathBuilder(parent.getPath());
            pathBuilder.append(name);
            final String childPath = pathBuilder.toString();
            if (handler == null) {
                syntheticList.add(new SyntheticResource(context.getResourceResolver(), childPath, ResourceProvider.RESOURCE_TYPE_SYNTHETIC));
            } else {
                Resource rsrc = null;
                try {
                    final AuthenticatedResourceProvider rp = context.getProviderManager().getOrCreateProvider(handler, this);
                    rsrc = rp == null ? null : rp.getResource(childPath, parent, null);
                } catch (final LoginException ignore) {
                // ignore
                }
                if (rsrc != null) {
                    providerList.add(rsrc);
                } else {
                    // otherwise we need to make sure that no one else is providing this child
                    if (entry.getValue().getChildren().isEmpty()) {
                        syntheticList.add(new SyntheticResource(context.getResourceResolver(), childPath, ResourceProvider.RESOURCE_TYPE_SYNTHETIC));
                    } else {
                        visitedNames.add(name);
                    }
                }
            }
        }
        if (!providerList.isEmpty()) {
            chain.addIterator(providerList.iterator());
        }
        if (!syntheticList.isEmpty()) {
            chain.addIterator(syntheticList.iterator());
        }
    }
    if (chain.size() == 0) {
        return Collections.EMPTY_LIST.iterator();
    }
    return new UniqueResourceIterator(visitedNames, chain);
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) Node(org.apache.sling.resourceresolver.impl.providers.tree.Node) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) ArrayList(java.util.ArrayList) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) AuthenticatedResourceProvider(org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider) IteratorChain(org.apache.commons.collections4.iterators.IteratorChain) PathBuilder(org.apache.sling.api.resource.path.PathBuilder) LoginException(org.apache.sling.api.resource.LoginException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with IteratorChain

use of org.apache.commons.collections4.iterators.IteratorChain in project jena by apache.

the class GenericSpatialPropertyFunction method checkBound.

private boolean checkBound(ExecutionContext execCxt, Node subject) {
    try {
        Graph graph = execCxt.getActiveGraph();
        IteratorChain<Triple> spatialTriples = new IteratorChain<>();
        // Check for Geometry and so GeometryLiterals.
        if (graph.contains(subject, Geo.HAS_GEOMETRY_NODE, null)) {
            // A Feature can have many geometries so add each of them. The check Geo.HAS_DEFAULT_GEOMETRY_NODE will only return one but requires the data to have these present.
            Iterator<Triple> geometryTriples = graph.find(subject, Geo.HAS_GEOMETRY_NODE, null);
            while (geometryTriples.hasNext()) {
                Node geometry = geometryTriples.next().getObject();
                ExtendedIterator<Triple> iter = graph.find(geometry, Geo.HAS_SERIALIZATION_NODE, null);
                // Check for asWKT
                if (!iter.hasNext()) {
                    iter = graph.find(geometry, Geo.AS_WKT_NODE, null);
                }
                // Check for asGML
                if (!iter.hasNext()) {
                    iter = graph.find(geometry, Geo.AS_GML_NODE, null);
                }
                spatialTriples.addIterator(iter);
            }
        } else {
            // Check for Geo predicates against the feature when no geometry literals found.
            if (graph.contains(subject, SpatialExtension.GEO_LAT_NODE, null) && graph.contains(subject, SpatialExtension.GEO_LON_NODE, null)) {
                Node lat = graph.find(subject, SpatialExtension.GEO_LAT_NODE, null).next().getObject();
                Node lon = graph.find(subject, SpatialExtension.GEO_LON_NODE, null).next().getObject();
                Node latLonGeometryLiteral = ConvertLatLon.toNode(lat, lon);
                Triple triple = new Triple(subject, Geo.HAS_GEOMETRY_NODE, latLonGeometryLiteral);
                spatialTriples.addIterator(Arrays.asList(triple).iterator());
            }
        }
        // Check through each Geometry and stop if one is accepted.
        boolean isMatched = false;
        while (spatialTriples.hasNext()) {
            Triple triple = spatialTriples.next();
            Node geometryLiteral = triple.getObject();
            GeometryWrapper targetGeometryWrapper = GeometryWrapper.extract(geometryLiteral);
            isMatched = checkSecondFilter(spatialArguments, targetGeometryWrapper);
            if (isMatched) {
                // Stop checking when match is true.
                break;
            }
        }
        return isMatched;
    } catch (DatatypeFormatException ex) {
        throw new ExprEvalException(ex.getMessage(), ex);
    }
}
Also used : Triple(org.apache.jena.graph.Triple) DatatypeFormatException(org.apache.jena.datatypes.DatatypeFormatException) Graph(org.apache.jena.graph.Graph) Node(org.apache.jena.graph.Node) GeometryWrapper(org.apache.jena.geosparql.implementation.GeometryWrapper) IteratorChain(org.apache.commons.collections4.iterators.IteratorChain) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Aggregations

IteratorChain (org.apache.commons.collections4.iterators.IteratorChain)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 DatatypeFormatException (org.apache.jena.datatypes.DatatypeFormatException)1 GeometryWrapper (org.apache.jena.geosparql.implementation.GeometryWrapper)1 Graph (org.apache.jena.graph.Graph)1 Node (org.apache.jena.graph.Node)1 Triple (org.apache.jena.graph.Triple)1 ExprEvalException (org.apache.jena.sparql.expr.ExprEvalException)1 LoginException (org.apache.sling.api.resource.LoginException)1 Resource (org.apache.sling.api.resource.Resource)1 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)1 PathBuilder (org.apache.sling.api.resource.path.PathBuilder)1 ResourceProviderHandler (org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler)1 AuthenticatedResourceProvider (org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider)1 Node (org.apache.sling.resourceresolver.impl.providers.tree.Node)1