Search in sources :

Example 1 with TikaConfigException

use of org.apache.tika.exception.TikaConfigException in project tika by apache.

the class AnnotationUtils method assignFieldParams.

/**
     * Assigns the param values to bean
     * @throws TikaConfigException when an error occurs while assigning params
     */
public static void assignFieldParams(Object bean, Map<String, Param> params) throws TikaConfigException {
    Class<?> beanClass = bean.getClass();
    if (!PARAM_INFO.containsKey(beanClass)) {
        synchronized (TikaConfig.class) {
            if (!PARAM_INFO.containsKey(beanClass)) {
                List<AccessibleObject> aObjs = collectInfo(beanClass, org.apache.tika.config.Field.class);
                List<ParamField> fields = new ArrayList<>(aObjs.size());
                for (AccessibleObject aObj : aObjs) {
                    fields.add(new ParamField(aObj));
                }
                PARAM_INFO.put(beanClass, fields);
            }
        }
    }
    List<ParamField> fields = PARAM_INFO.get(beanClass);
    Set<String> validFieldNames = new HashSet<>();
    for (ParamField field : fields) {
        validFieldNames.add(field.getName());
        Param<?> param = params.get(field.getName());
        if (param != null) {
            if (field.getType().isAssignableFrom(param.getType())) {
                try {
                    field.assignValue(bean, param.getValue());
                } catch (Exception e) {
                    throw new TikaConfigException(e.getMessage(), e);
                }
            } else {
                String msg = String.format(Locale.ROOT, "Value '%s' of type '%s' cant be" + " assigned to field '%s' of defined type '%s'", param.getValue(), param.getValue().getClass(), field.getName(), field.getType());
                throw new TikaConfigException(msg);
            }
        } else if (field.isRequired()) {
            //param not supplied but field is declared as required?
            String msg = String.format(Locale.ROOT, "Param %s is required for %s," + " but it is not given in config.", field.getName(), bean.getClass().getName());
            throw new TikaConfigException(msg);
        } else {
        //FIXME: SLF4j is not showing up for import, fix it and send this to LOG.debug
        //LOG.debug("Param not supplied, field is not mandatory");
        }
    }
}
Also used : ParamField(org.apache.tika.config.ParamField) TikaConfig(org.apache.tika.config.TikaConfig) ArrayList(java.util.ArrayList) TikaConfigException(org.apache.tika.exception.TikaConfigException) TikaConfigException(org.apache.tika.exception.TikaConfigException) AccessibleObject(java.lang.reflect.AccessibleObject) HashSet(java.util.HashSet)

Example 2 with TikaConfigException

use of org.apache.tika.exception.TikaConfigException in project tika by apache.

the class SentimentParser method initialize.

@Override
public void initialize(Map<String, Param> params) throws TikaConfigException {
    LOG.debug("Initializing...");
    if (modelPath == null) {
        throw new TikaConfigException("Parameter 'modelPath' is required but it is not set");
    }
    try {
        URL resolvedUrl = null;
        if (modelPath.startsWith("http://") || modelPath.startsWith("https://")) {
            resolvedUrl = new URL(modelPath);
        } else {
            resolvedUrl = getClass().getClassLoader().getResource(modelPath);
            File file = new File(modelPath);
            if (file.exists()) {
                // file on filesystem gets higher priority
                resolvedUrl = file.toURI().toURL();
            }
        }
        if (resolvedUrl == null) {
            throw new TikaConfigException("Model doesn't exists :" + modelPath);
        }
        LOG.info("Sentiment Model is at {}", resolvedUrl);
        long st = System.currentTimeMillis();
        SentimentModel model = new SentimentModel(resolvedUrl);
        long time = System.currentTimeMillis() - st;
        LOG.debug("time taken to load model {}", time);
        classifier = new SentimentME(model);
    } catch (Exception e) {
        LOG.warn("Failed to load sentiment model from {}" + modelPath);
        throw new TikaConfigException(e.getMessage(), e);
    }
}
Also used : SentimentModel(opennlp.tools.sentiment.SentimentModel) SentimentME(opennlp.tools.sentiment.SentimentME) TikaConfigException(org.apache.tika.exception.TikaConfigException) File(java.io.File) URL(java.net.URL) IOException(java.io.IOException) TikaException(org.apache.tika.exception.TikaException) SAXException(org.xml.sax.SAXException) TikaConfigException(org.apache.tika.exception.TikaConfigException)

Example 3 with TikaConfigException

use of org.apache.tika.exception.TikaConfigException in project tika by apache.

the class AnnotationUtilsTest method testParamValueInheritance.

@Test
public void testParamValueInheritance() {
    class Bean {

        @Field(required = true)
        CharSequence field;
    }
    Bean parser = new Bean();
    Map<String, Param> params = new HashMap<>();
    try {
        String val = "someval";
        params.put("field", new Param<String>("field", String.class, val));
        AnnotationUtils.assignFieldParams(parser, params);
        Assert.assertEquals(val, parser.field);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception not expected, string is assignable to CharSequence");
    }
    try {
        Date val = new Date();
        params.put("field", new Param<Date>("field", Date.class, val));
        AnnotationUtils.assignFieldParams(parser, params);
        Assert.fail("Exception expected, Date is not assignable to CharSequence.");
    } catch (TikaConfigException e) {
    //expected
    }
}
Also used : HashMap(java.util.HashMap) Param(org.apache.tika.config.Param) TikaConfigException(org.apache.tika.exception.TikaConfigException) TikaConfigException(org.apache.tika.exception.TikaConfigException) Date(java.util.Date) Test(org.junit.Test)

Example 4 with TikaConfigException

use of org.apache.tika.exception.TikaConfigException in project tika by apache.

the class DL4JInceptionV3NetTest method recognise.

@Test
@Ignore("until we can make this more robust across platforms")
public void recognise() throws Exception {
    TikaConfig config;
    try {
        config = new TikaConfig(getClass().getResourceAsStream("dl4j-inception3-config.xml"));
    } catch (TikaConfigException e) {
        if (e.getMessage() != null && e.getMessage().contains("Connection refused")) {
            return;
        }
        throw e;
    }
    Tika tika = new Tika(config);
    Metadata md = new Metadata();
    tika.parse(getClass().getResourceAsStream("cat.jpg"), md);
    String[] objects = md.getValues("OBJECT");
    boolean found = false;
    for (String object : objects) {
        if (object.contains("_cat")) {
            found = true;
        }
    }
    assertTrue(found);
}
Also used : TikaConfig(org.apache.tika.config.TikaConfig) TikaConfigException(org.apache.tika.exception.TikaConfigException) Metadata(org.apache.tika.metadata.Metadata) Tika(org.apache.tika.Tika) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with TikaConfigException

use of org.apache.tika.exception.TikaConfigException in project tika by apache.

the class ParameterizedParserTest method testBadValue.

@Test
public void testBadValue() throws Exception {
    boolean ex = false;
    try {
        Metadata m = getMetadata("TIKA-1986-bad-values.xml");
        fail("should have thrown exception");
    } catch (TikaConfigException e) {
        ex = true;
    }
    assertTrue("No TikaConfigException", ex);
}
Also used : Metadata(org.apache.tika.metadata.Metadata) TikaConfigException(org.apache.tika.exception.TikaConfigException) Test(org.junit.Test)

Aggregations

TikaConfigException (org.apache.tika.exception.TikaConfigException)8 IOException (java.io.IOException)4 TikaException (org.apache.tika.exception.TikaException)3 Test (org.junit.Test)3 SAXException (org.xml.sax.SAXException)3 File (java.io.File)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 TikaConfig (org.apache.tika.config.TikaConfig)2 Metadata (org.apache.tika.metadata.Metadata)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Pattern (java.util.regex.Pattern)1 SentimentME (opennlp.tools.sentiment.SentimentME)1