Search in sources :

Example 1 with ResourcesScanner

use of org.reflections.scanners.ResourcesScanner in project deeplearning4j by deeplearning4j.

the class DefaultI18N method loadLanguageResources.

private synchronized void loadLanguageResources(String languageCode) {
    if (loadedLanguages.contains(languageCode))
        return;
    //Scan classpath for resources in the /dl4j_i18n/ directory...
    URL url = this.getClass().getResource("/" + DEFAULT_I8N_RESOURCES_DIR + "/");
    Reflections reflections = new Reflections(new ConfigurationBuilder().setScanners(new ResourcesScanner()).setUrls(url));
    String pattern = ".*" + languageCode;
    Set<String> resources = reflections.getResources(Pattern.compile(pattern));
    Map<String, String> messages = new HashMap<>();
    for (String s : resources) {
        if (!s.endsWith(languageCode))
            continue;
        log.trace("Attempting to parse file: {}", s);
        parseFile(s, messages);
    }
    messagesByLanguage.put(languageCode, messages);
    loadedLanguages.add(languageCode);
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ResourcesScanner(org.reflections.scanners.ResourcesScanner) URL(java.net.URL) Reflections(org.reflections.Reflections)

Example 2 with ResourcesScanner

use of org.reflections.scanners.ResourcesScanner in project swagger-core by swagger-api.

the class BeanConfig method classes.

@Override
public Set<Class<?>> classes() {
    ConfigurationBuilder config = new ConfigurationBuilder();
    Set<String> acceptablePackages = new HashSet<String>();
    boolean allowAllPackages = false;
    if (resourcePackage != null && !"".equals(resourcePackage)) {
        String[] parts = resourcePackage.split(",");
        for (String pkg : parts) {
            if (!"".equals(pkg)) {
                acceptablePackages.add(pkg);
                config.addUrls(ClasspathHelper.forPackage(pkg));
            }
        }
    } else {
        allowAllPackages = true;
    }
    config.setScanners(new ResourcesScanner(), new TypeAnnotationsScanner(), new SubTypesScanner());
    final Reflections reflections = new Reflections(config);
    Set<Class<?>> classes = reflections.getTypesAnnotatedWith(javax.ws.rs.Path.class);
    Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(SwaggerDefinition.class);
    classes.addAll(typesAnnotatedWith);
    /*
         * Find concrete types annotated with @Api, but with a supertype annotated with @Path.
         * This would handle split resources where the interface has jax-rs annotations
         * and the implementing class has Swagger annotations 
         */
    for (Class<?> cls : reflections.getTypesAnnotatedWith(Api.class)) {
        for (Class<?> intfc : TypeToken.of(cls).getTypes().interfaces().rawTypes()) {
            Annotation ann = intfc.getAnnotation(javax.ws.rs.Path.class);
            if (ann != null) {
                classes.add(cls);
                break;
            }
        }
    }
    Set<Class<?>> output = new HashSet<Class<?>>();
    for (Class<?> cls : classes) {
        if (allowAllPackages) {
            output.add(cls);
        } else {
            for (String pkg : acceptablePackages) {
                if (cls.getPackage().getName().startsWith(pkg)) {
                    output.add(cls);
                }
            }
        }
    }
    return output;
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ResourcesScanner(org.reflections.scanners.ResourcesScanner) Annotation(java.lang.annotation.Annotation) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) HashSet(java.util.HashSet) Reflections(org.reflections.Reflections)

Example 3 with ResourcesScanner

use of org.reflections.scanners.ResourcesScanner in project Gaffer by gchq.

the class StreamUtil method openStreams.

public static InputStream[] openStreams(final Class clazz, final String folderPath, final boolean logErrors) {
    if (null == folderPath) {
        return new InputStream[0];
    }
    String folderPathChecked = folderPath;
    if (!folderPathChecked.endsWith("/")) {
        folderPathChecked = folderPathChecked + "/";
    }
    if (folderPathChecked.startsWith("/")) {
        folderPathChecked = folderPathChecked.substring(1);
    }
    final Set<String> schemaFiles = new Reflections(new ConfigurationBuilder().setScanners(new ResourcesScanner()).setUrls(ClasspathHelper.forClass(clazz))).getResources(Pattern.compile(".*"));
    final Iterator<String> itr = schemaFiles.iterator();
    while (itr.hasNext()) {
        if (!itr.next().startsWith(folderPathChecked)) {
            itr.remove();
        }
    }
    int index = 0;
    final InputStream[] schemas = new InputStream[schemaFiles.size()];
    for (final String schemaFile : schemaFiles) {
        schemas[index] = openStream(clazz, schemaFile, logErrors);
        index++;
    }
    return schemas;
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) InputStream(java.io.InputStream) ResourcesScanner(org.reflections.scanners.ResourcesScanner) Reflections(org.reflections.Reflections)

Example 4 with ResourcesScanner

use of org.reflections.scanners.ResourcesScanner in project swagger-core by swagger-api.

the class ReflectiveJaxrsScanner method getReflections.

protected Reflections getReflections() {
    if (reflections == null) {
        ConfigurationBuilder config = new ConfigurationBuilder();
        acceptablePackages = new HashSet<String>();
        if (resourcePackage != "") {
            String[] parts = resourcePackage.split(",");
            for (String pkg : parts) {
                if (!"".equals(pkg)) {
                    acceptablePackages.add(pkg);
                    config.addUrls(ClasspathHelper.forPackage(pkg));
                }
            }
        }
        config.setScanners(new ResourcesScanner(), new TypeAnnotationsScanner(), new SubTypesScanner());
        this.reflections = new Reflections(config);
    }
    return this.reflections;
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) ResourcesScanner(org.reflections.scanners.ResourcesScanner) Reflections(org.reflections.Reflections)

Example 5 with ResourcesScanner

use of org.reflections.scanners.ResourcesScanner in project sling by apache.

the class OsgiMetadataUtil method initMetadataDocumentCache.

/**
     * Reads all SCR metadata XML documents located at OSGI-INF/ and caches them with quick access by implementation class.
     * @return Cache map
     */
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);
    }
    Reflections reflections = new Reflections(METADATA_PATH, new ResourcesScanner());
    Set<String> paths = reflections.getResources(Pattern.compile("^.*\\.xml$"));
    for (String path : paths) {
        parseMetadataDocuments(cacheMap, path, xpathExpression);
    }
    return cacheMap;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) HashMap(java.util.HashMap) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ResourcesScanner(org.reflections.scanners.ResourcesScanner) Document(org.w3c.dom.Document) Reflections(org.reflections.Reflections)

Aggregations

Reflections (org.reflections.Reflections)5 ResourcesScanner (org.reflections.scanners.ResourcesScanner)5 ConfigurationBuilder (org.reflections.util.ConfigurationBuilder)4 SubTypesScanner (org.reflections.scanners.SubTypesScanner)2 TypeAnnotationsScanner (org.reflections.scanners.TypeAnnotationsScanner)2 InputStream (java.io.InputStream)1 Annotation (java.lang.annotation.Annotation)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 XPath (javax.xml.xpath.XPath)1 XPathExpression (javax.xml.xpath.XPathExpression)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 Document (org.w3c.dom.Document)1