All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.thematchbox.river.docs.DocumentReaderImpl Maven / Gradle / Ivy

Go to download

This project contains an abstract implementation of an ElasticSearch River and is used as a basis for custom river implementations.

There is a newer version: 1.1.3
Show newest version
package com.thematchbox.river.docs;

/* Copyright 2015 theMatchBox

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/


import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.ByteOrderMarkDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import org.apache.commons.io.IOUtils;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.poi.hslf.extractor.PowerPointExtractor;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.rtf.RTFEditorKit;
import java.io.*;
import java.nio.charset.Charset;

public class DocumentReaderImpl implements DocumentReader {

    private final CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();

    public DocumentReaderImpl() {
        detector.add(new ByteOrderMarkDetector());
        detector.add(JChardetFacade.getInstance());
        detector.add(ASCIIDetector.getInstance());
    }

    public String read(InputStream inputStream, DocumentType documentType) throws IOException, BadLocationException {
        switch (documentType) {
            case DOC: {
                WordExtractor wordExtractor = new WordExtractor(inputStream);
                String text = wordExtractor.getText();
                wordExtractor.close();
                return text;
            }
            case DOCX: {
                XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
                XWPFWordExtractor extractor = new XWPFWordExtractor(xwpfDocument);
                String text = extractor.getText();
                extractor.close();
                return text;
            }
            case PPT: {
                PowerPointExtractor extractor = new PowerPointExtractor(inputStream);
                String text = extractor.getText();
                extractor.close();
                return text;
            }
            case PPTX: {
                XMLSlideShow xmlSlideShow = new XMLSlideShow(inputStream);
                XSLFPowerPointExtractor extractor = new XSLFPowerPointExtractor(xmlSlideShow);
                String text = extractor.getText();
                extractor.close();
                return text;
            }
            case XLS: {
                HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream);
                ExcelExtractor extractor = new ExcelExtractor(hssfWorkbook);
                String text = extractor.getText();
                extractor.close();
                return text;
            }
            case XLSX: {
                XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
                XSSFExcelExtractor extractor = new XSSFExcelExtractor(xssfWorkbook);
                String text = extractor.getText();
                extractor.close();
                return text;
            }
            case RTF: {
                RTFEditorKit rtfParser = new RTFEditorKit();
                Document document = rtfParser.createDefaultDocument();
                rtfParser.read(inputStream, document, 0);
                String text = document.getText(0, document.getLength());
                inputStream.close();
                return text;
            }
            case TXT:
            case CSV: {
                byte[] bytes = IOUtils.toByteArray(inputStream);
                inputStream.close();
                Charset charSet = getCharSet(bytes);
                BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes), charSet));
                StringBuilder builder = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line).append("\n");
                }

                return builder.toString();

            }
            case PDF: {
                PDFParser pdfParser = new PDFParser(inputStream);
                pdfParser.parse();
                PDDocument pdDocument = pdfParser.getPDDocument();
                PDFTextStripper textStripper = new PDFTextStripper();
                String text = textStripper.getText(pdDocument);
                pdDocument.close();
                return text;
            }
        }

        return null;
    }

    private Charset getCharSet(byte[] bytes) throws IOException {
        ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
        Charset charset = detector.detectCodepage(byteStream, bytes.length);
        if (charset == null) {
            throw new IOException("Unable to detect code page for this file.");
        }
        switch (charset.name()) {
            case "UTF-8":
            case "windows-1252":
                break;
            default:
                charset = Charset.forName("windows-1252");
                break;
        }
        return charset;
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy