private static Map initMetadataDocumentCache()

in core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java [142:167]


    private static Map<String,Document> initMetadataDocumentCache() {
        Map<String,Document> cacheMap = new HashMap<>();

        XPath xpath = XPATH_FACTORY.newXPath();
        xpath.setNamespaceContext(NAMESPACE_CONTEXT);
        XPathExpression xpathExpression;
        try {
            xpathExpression = xpath.compile("//*[implementation/@class]");
        }
        catch (XPathExpressionException ex) {
            throw new RuntimeException("Compiling XPath expression failed.", ex);
        }

        // get all OSGI-INF/*.xml files from classpath
        Reflections reflections = new Reflections(METADATA_PATH, Scanners.Resources);
        Pattern xmlFilesPattern = Pattern.compile("^.*\\.xml$");
        Set<String> paths = reflections.getResources(xmlFilesPattern);

        // filter out OSGi metatype files and parse all found XML documents
        Pattern metatypeFilesPattern = Pattern.compile("^" + Pattern.quote(METADATA_METATYPE_PATH )+ ".*$");
        paths.stream()
            .filter(path -> !metatypeFilesPattern.matcher(path).matches())
            .forEach(path -> parseMetadataDocuments(cacheMap, path, xpathExpression));

        return cacheMap;
    }