Ir al contenido

Crear su propio Text extractor

Puede crear su propio Text Extractor.

Condiciones:

  • La nueva clase Text Extractor debe implementar la interfaz “TextExtractor”.
  • La nueva clase Text Extractor debe declararse en el paquete “com.openkm.plugin.extractor”.
  • La nueva clase Text Extractor debe anotarse con@PluginImplementation”.
  • La nueva clase Text Extractor debe extender “AbstractTextExtractor”.

Interfaz Text Extractor:

package com.openkm.plugin.extractor;
import net.xeoh.plugins.base.Plugin;
import java.io.IOException;
import java.io.InputStream;
public interface TextExtractor extends Plugin {
String[] getContentTypes();
String extractText(InputStream stream, String mimeType, String encoding) throws IOException;
}

La nueva clase debe cargarse en el paquete com.openkm.plugin.extractor porque el sistema de plugins de la aplicación intentará cargarla desde ahí.

Método Tipo Descripción
getContentTypes() String[] Devuelve los tipos MIME admitidos por este extractor. Las cadenas devueltas deben estar en minúsculas, y el array devuelto no debe estar vacío.
extractText(InputStream stream, String mimeType, String encoding) String Devuelve un reader para el contenido de texto del documento binario dado. El tipo de contenido y la codificación de caracteres (si están disponibles y aplican) se pasan como argumentos.

Ejemplo de implementación de Text Extractor

Sección titulada «Ejemplo de implementación de Text Extractor»
package com.openkm.plugin.extractor;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
/**
* Text extractor for plain text.
*/
@PluginImplementation
public class PlainTextExtractor extends AbstractTextExtractor {
private static final Logger log = LoggerFactory.getLogger(PlainTextExtractor.class);
/**
* Creates a new <code>PlainTextExtractor</code> instance.
*/
public PlainTextExtractor() {
super(new String[]{"text/plain"});
}
// -------------------------------------------------------< TextExtractor >
/**
* Wraps the given input stream to an {@link InputStreamReader} using the
* given encoding, or the platform default encoding if the encoding is not
* given or is unsupported. Closes the stream and returns an empty reader if
* the given encoding is not supported.
*
* @param stream binary stream
* @param type ignored
* @param encoding character encoding, optional
* @return reader for the plain text content
* @throws IOException if the binary stream can not be closed in case of an
* encoding issue
*/
@Override
public String extractText(InputStream stream, String type, String encoding) throws IOException {
log.debug("extractText({}, {}, {})", stream, type, encoding);
try {
if (encoding != null) {
return IOUtils.toString(stream, encoding);
}
} catch (UnsupportedEncodingException e) {
log.warn("Unsupported encoding '{}', using default ({}) instead.", encoding, System.getProperty("file.encoding"));
}
return IOUtils.toString(stream, StandardCharsets.UTF_8);
}
}