Example usage for org.apache.lucene.analysis.util CharFilterFactory forName

List of usage examples for org.apache.lucene.analysis.util CharFilterFactory forName

Introduction

In this page you can find the example usage for org.apache.lucene.analysis.util CharFilterFactory forName.

Prototype

public static CharFilterFactory forName(String name, Map<String, String> args) 

Source Link

Document

looks up a charfilter by name from context classpath

Usage

From source file:org.apache.jackrabbit.oak.plugins.index.lucene.NodeStateAnalyzerFactory.java

License:Apache License

private CharFilterFactory[] loadCharFilterFactories(NodeState charFiltersState) {
    List<CharFilterFactory> result = newArrayList();

    //Need to read children in order
    Tree tree = TreeFactory.createReadOnlyTree(charFiltersState);
    for (Tree t : tree.getChildren()) {
        NodeState state = charFiltersState.getChildNode(t.getName());

        String factoryType = getFactoryType(state, t.getName());
        Map<String, String> args = convertNodeState(state);
        CharFilterFactory cf = CharFilterFactory.forName(factoryType, args);
        init(cf, state);//from w  w  w  .jav  a2 s  .c  o m
        result.add(cf);
    }

    return result.toArray(new CharFilterFactory[result.size()]);
}

From source file:org.apache.tika.eval.tokens.AnalyzerDeserializer.java

License:Apache License

private static CharFilterFactory[] buildCharFilters(JsonElement el, String analyzerName) throws IOException {
    if (el == null || el.isJsonNull()) {
        return null;
    }//ww w  .  j  av a2s  .  com
    if (!el.isJsonArray()) {
        throw new IllegalArgumentException(
                "Expecting array for charfilters, but got:" + el.toString() + " for " + analyzerName);
    }
    JsonArray jsonArray = (JsonArray) el;
    List<CharFilterFactory> ret = new LinkedList<CharFilterFactory>();
    for (JsonElement filterMap : jsonArray) {
        if (!(filterMap instanceof JsonObject)) {
            throw new IllegalArgumentException(
                    "Expecting a map with \"factory\" string and \"params\" map in char filter factory;"
                            + " not: " + filterMap.toString() + " in " + analyzerName);
        }
        JsonElement factoryEl = ((JsonObject) filterMap).get(FACTORY);
        if (factoryEl == null || !factoryEl.isJsonPrimitive()) {
            throw new IllegalArgumentException(
                    "Expecting value for factory in char filter factory builder in:" + analyzerName);
        }
        String factoryName = factoryEl.getAsString();
        factoryName = factoryName.replaceAll("oala.", "org.apache.lucene.analysis.");

        JsonElement paramsEl = ((JsonObject) filterMap).get(PARAMS);
        Map<String, String> params = mapify(paramsEl);
        String spiName = "";
        for (String s : CharFilterFactory.availableCharFilters()) {
            Class clazz = CharFilterFactory.lookupClass(s);
            if (clazz.getName().equals(factoryName)) {
                spiName = s;
                break;
            }
        }
        if (spiName.equals("")) {
            throw new IllegalArgumentException(
                    "A SPI class of type org.apache.lucene.analysis.util.CharFilterFactory with name" + "'"
                            + factoryName + "' does not exist.");
        }

        try {
            CharFilterFactory charFilterFactory = CharFilterFactory.forName(spiName, params);
            if (charFilterFactory instanceof ResourceLoaderAware) {
                ((ResourceLoaderAware) charFilterFactory)
                        .inform(new ClasspathResourceLoader(AnalyzerDeserializer.class));
            }
            ret.add(charFilterFactory);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("While trying to load " + analyzerName + ": " + e.getMessage(),
                    e);
        }
    }
    if (ret.size() == 0) {
        return new CharFilterFactory[0];
    }
    return ret.toArray(new CharFilterFactory[ret.size()]);
}

From source file:org.tallison.gramreaper.ingest.schema.AnalyzerDeserializer.java

License:Apache License

private static CharFilterFactory[] buildCharFilters(JsonElement el, String analyzerName) throws IOException {
    if (el == null || el.isJsonNull()) {
        return null;
    }// w  w  w  . j  a v a 2  s.  c om
    if (!el.isJsonArray()) {
        throw new IllegalArgumentException(
                "Expecting array for charfilters, but got:" + el.toString() + " for " + analyzerName);
    }
    JsonArray jsonArray = (JsonArray) el;
    List<CharFilterFactory> ret = new LinkedList<CharFilterFactory>();
    for (JsonElement filterMap : jsonArray) {
        if (!(filterMap instanceof JsonObject)) {
            throw new IllegalArgumentException(
                    "Expecting a map with \"factory\" string and \"params\" map in char filter factory;"
                            + " not: " + filterMap.toString() + " in " + analyzerName);
        }
        JsonElement factoryEl = ((JsonObject) filterMap).get(FACTORY);
        if (factoryEl == null || !factoryEl.isJsonPrimitive()) {
            throw new IllegalArgumentException(
                    "Expecting value for factory in char filter factory builder in:" + analyzerName);
        }
        String factoryName = factoryEl.getAsString();
        factoryName = factoryName.replaceAll("oala.", "org.apache.lucene.analysis.");

        JsonElement paramsEl = ((JsonObject) filterMap).get(PARAMS);
        Map<String, String> params = mapify(paramsEl);
        for (Map.Entry<String, String> e : params.entrySet()) {
            System.out.println("PARAM: " + e.getKey() + " : " + e.getValue());
        }
        String spiName = "";
        for (String s : CharFilterFactory.availableCharFilters()) {
            Class clazz = CharFilterFactory.lookupClass(s);
            if (clazz.getName().equals(factoryName)) {
                spiName = s;
                break;
            }
        }
        try {
            CharFilterFactory charFilterFactory = CharFilterFactory.forName(spiName, params);
            if (charFilterFactory instanceof ResourceLoaderAware) {
                ((ResourceLoaderAware) charFilterFactory)
                        .inform(new ClasspathResourceLoader(AnalyzerDeserializer.class));
            }
            ret.add(charFilterFactory);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("While trying to load " + analyzerName + ": " + e.getMessage(),
                    e);
        }
    }
    if (ret.size() == 0) {
        return new CharFilterFactory[0];
    }
    return ret.toArray(new CharFilterFactory[ret.size()]);
}