by.creepid.docsreporter.converter.PoiConverterAdapter.java Source code

Java tutorial

Introduction

Here is the source code for by.creepid.docsreporter.converter.PoiConverterAdapter.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package by.creepid.docsreporter.converter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.poi.xwpf.converter.core.IXWPFConverter;
import org.apache.poi.xwpf.converter.core.Options;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import org.odftoolkit.odfdom.converter.xhtml.XHTMLConverter;

/**
 *
 * @author rusakovich
 */
public abstract class PoiConverterAdapter<S extends DocFormat, E extends DocFormat> implements DocConverterAdapter {

    protected final IXWPFConverter converter;
    protected E format;

    PoiConverterAdapter(IXWPFConverter converter) {
        this.converter = converter;
    }

    private XWPFDocument createDocument(InputStream in) throws IOException {
        return new XWPFDocument(in);
    }

    public OutputStream convert(DocFormat targetFormat, InputStream in) throws IOException {
        if (targetFormat != getTargetFormat()) {
            throw new IllegalArgumentException(
                    "Target must have " + getTargetFormat().getExts()[0] + " extension!");
        }

        XWPFDocument doc = createDocument(in);
        OutputStream out = new ByteArrayOutputStream();

        synchronized (converter) {
            converter.convert(doc, out, getOptions());
        }

        return out;
    }

    abstract Options getOptions();

    @Override
    public abstract E getTargetFormat();

    @Override
    public DocFormat getSourceFormat() {
        return DocFormat.DOCX;
    }
}