public static Iterator findSitemapRoots()

in src/main/java/org/apache/sling/sitemap/SitemapUtil.java [209:254]


    public static Iterator<Resource> findSitemapRoots(ResourceResolver resolver, String searchPath) {
        String correctedSearchPath = searchPath == null ? "/" : searchPath;
        StringBuilder query = new StringBuilder(correctedSearchPath.length() + 35);
        query.append("/jcr:root").append(ISO9075.encodePath(correctedSearchPath));
        if (!correctedSearchPath.endsWith("/")) {
            query.append('/');
        }
        query.append("/*[@").append(SitemapService.PROPERTY_SITEMAP_ROOT).append('=').append(Boolean.TRUE).append(']');
        query.append(" option(index tag slingSitemaps)");

        return new Iterator<Resource>() {
            private final Iterator<Resource> hits = resolver.findResources(query.toString(), Query.XPATH);
            private Resource next = seek();

            private Resource seek() {
                while (hits.hasNext()) {
                    Resource nextHit = normalizeSitemapRoot(hits.next());
                    // skip a hit on the given searchPath itself. This may be when a search is done for descendant
                    // sitemaps given the normalized sitemap root path and the sitemap root's jcr:content is in the
                    // result set.
                    if (nextHit == null
                        || nextHit.getPath().equals(correctedSearchPath)
                        || nextHit.getPath().startsWith(JCR_SYSTEM_PATH)) {
                        continue;
                    }
                    return nextHit;
                }
                return null;
            }

            @Override
            public boolean hasNext() {
                return next != null;
            }

            @Override
            public Resource next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                Resource ret = next;
                next = seek();
                return ret;
            }
        };
    }