T loadOne()

in tika-core/src/main/java/org/apache/tika/config/TikaConfig.java [718:834]


        T loadOne(Element element, MimeTypes mimeTypes, ServiceLoader loader)
                throws TikaException, IOException {
            String name = element.getAttribute("class");
            if (name.isBlank()) {
                throw new TikaConfigException("class attribute must not be empty: " + element);
            }
            String initProbHandler = element.getAttribute("initializableProblemHandler");
            InitializableProblemHandler initializableProblemHandler;
            if (initProbHandler.isBlank()) {
                initializableProblemHandler = loader.getInitializableProblemHandler();
            } else {
                initializableProblemHandler = getInitializableProblemHandler(initProbHandler);
            }

            T loaded;

            try {
                Class<? extends T> loadedClass = loader.getServiceClass(getLoaderClass(), name);

                // Do pre-load checks and short-circuits
                //TODO : allow duplicate instances with different configurations
                loaded = preLoadOne(loadedClass, name, mimeTypes);
                if (loaded != null) {
                    return loaded;
                }

                // Get any parameters / settings for the parser
                Map<String, Param> params = null;
                try {
                    params = getParams(element);
                } catch (Exception e) {
                    throw new TikaConfigException(e.getMessage(), e);
                }

                // Is this a composite or decorated class? If so, support recursion
                if (isComposite(loadedClass)) {
                    // Get the child objects for it
                    List<T> children = new ArrayList<>();
                    NodeList childNodes = element.getElementsByTagName(getLoaderTagName());
                    if (childNodes.getLength() > 0) {
                        for (int i = 0; i < childNodes.getLength(); i++) {
                            T loadedChild =
                                    loadOne((Element) childNodes.item(i), mimeTypes, loader);
                            if (loadedChild != null) {
                                children.add(loadedChild);
                            }
                        }
                    }

                    // Get the list of children to exclude
                    Set<Class<? extends T>> excludeChildren = new HashSet<>();
                    NodeList excludeChildNodes =
                            element.getElementsByTagName(getLoaderTagName() + "-exclude");
                    if (excludeChildNodes.getLength() > 0) {
                        for (int i = 0; i < excludeChildNodes.getLength(); i++) {
                            Element excl = (Element) excludeChildNodes.item(i);
                            String exclName = excl.getAttribute("class");
                            try {
                                excludeChildren
                                        .add(loader.getServiceClass(getLoaderClass(), exclName));
                            } catch (ClassNotFoundException e) {
                                //TIKA-3268 -- This should stop the world.
                                throw new TikaConfigException(
                                        "Class not found in -exclude list: " + exclName);
                            }
                        }
                    }

                    // Create the Composite
                    loaded = createComposite(loadedClass, children, excludeChildren, params,
                            mimeTypes, loader);

                    // Default constructor fallback
                    if (loaded == null) {
                        loaded = newInstance(loadedClass);
                    }
                } else {
                    // Regular class, create as-is
                    loaded = newInstance(loadedClass);
                    // TODO Support arguments, needed for Translators etc
                    // See the thread "Configuring parsers and translators" for details
                }

                //Assigning the params to bean fields/setters
                AnnotationUtils.assignFieldParams(loaded, params);
                if (loaded instanceof Initializable) {
                    ((Initializable) loaded).initialize(params);
                    ((Initializable) loaded).checkInitialization(initializableProblemHandler);
                }
                // Have any decoration performed, eg explicit mimetypes
                loaded = decorate(loaded, element);
                // All done with setup
                return loaded;
            } catch (ClassNotFoundException e) {
                if (loader.getLoadErrorHandler() == LoadErrorHandler.THROW) {
                    // Use a different exception signature here
                    throw new TikaConfigException(
                            "Unable to find a " + getLoaderTagName() + " class: " + name, e);
                }
                // Report the problem
                loader.getLoadErrorHandler().handleLoadError(name, e);
                return null;
            } catch (IllegalAccessException e) {
                throw new TikaException(
                        "Unable to access a " + getLoaderTagName() + " class: " + name, e);
            } catch (InvocationTargetException e) {
                throw new TikaException(
                        "Unable to create a " + getLoaderTagName() + " class: " + name, e);
            } catch (InstantiationException e) {
                throw new TikaException(
                        "Unable to instantiate a " + getLoaderTagName() + " class: " + name, e);
            } catch (NoSuchMethodException e) {
                throw new TikaException(
                        "Unable to find the right constructor for " + getLoaderTagName() +
                                " class: " + name, e);
            }
        }