001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.outerj.daisy.textextraction.impl;
017:
018: import org.outerj.daisy.textextraction.TextExtractorManager;
019: import org.outerj.daisy.textextraction.TextExtractor;
020: import org.outerj.daisy.plugin.PluginRegistry;
021: import org.outerj.daisy.plugin.PluginUser;
022: import org.outerj.daisy.plugin.PluginHandle;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: import javax.annotation.PreDestroy;
027: import java.util.Map;
028: import java.util.HashMap;
029: import java.util.List;
030: import java.util.ArrayList;
031: import java.io.InputStream;
032: import java.io.BufferedInputStream;
033:
034: public class TextExtractorManagerImpl implements TextExtractorManager {
035: private List<PluginHandle<TextExtractor>> textExtractorPlugins = new ArrayList<PluginHandle<TextExtractor>>();
036: private PluginUser<TextExtractor> pluginUser = new MyPluginUser();
037: private PluginRegistry pluginRegistry;
038: private Map<String, TextExtractor> extractorsByMimeType = new HashMap<String, TextExtractor>();
039: private final Log log = LogFactory.getLog(getClass());
040:
041: public TextExtractorManagerImpl(PluginRegistry pluginRegistry) {
042: this .pluginRegistry = pluginRegistry;
043: pluginRegistry.setPluginUser(TextExtractor.class, pluginUser);
044: }
045:
046: @PreDestroy
047: public void destroy() {
048: pluginRegistry.unsetPluginUser(TextExtractor.class, pluginUser);
049: }
050:
051: public String getText(String mimeType, InputStream is)
052: throws Exception {
053: try {
054: TextExtractor textExtractor = extractorsByMimeType
055: .get(mimeType);
056:
057: if (textExtractor != null) {
058: BufferedInputStream bis = new BufferedInputStream(is);
059: return textExtractor.getText(bis);
060: } else {
061: if (log.isDebugEnabled())
062: log
063: .debug("No textextractor registered for mimetype "
064: + mimeType);
065: }
066: return null;
067: } finally {
068: is.close();
069: }
070: }
071:
072: public boolean supportsMimeType(String mimeType) {
073: return extractorsByMimeType.containsKey(mimeType);
074: }
075:
076: private void rebuildIndex() {
077: Map<String, TextExtractor> extractorsByMimeType = new HashMap<String, TextExtractor>();
078:
079: for (PluginHandle<TextExtractor> pluginHandle : textExtractorPlugins) {
080: for (String mimeType : pluginHandle.getPlugin()
081: .getMimeTypes()) {
082: extractorsByMimeType.put(mimeType, pluginHandle
083: .getPlugin());
084: }
085: }
086:
087: this .extractorsByMimeType = extractorsByMimeType;
088: }
089:
090: private class MyPluginUser implements PluginUser<TextExtractor> {
091:
092: public void pluginAdded(PluginHandle<TextExtractor> pluginHandle) {
093: textExtractorPlugins.add(pluginHandle);
094: rebuildIndex();
095: }
096:
097: public void pluginRemoved(
098: PluginHandle<TextExtractor> pluginHandle) {
099: textExtractorPlugins.remove(pluginHandle);
100: rebuildIndex();
101: }
102: }
103: }
|