Search in sources :

Example 1 with REPORT

use of org.eclipse.sw360.datahandler.thrift.licenseinfo.OutputFormatVariant.REPORT in project sw360portal by sw360.

the class DocxGenerator method generateOutputFile.

@Override
public byte[] generateOutputFile(Collection<LicenseInfoParsingResult> projectLicenseInfoResults, String projectName, String projectVersion, String licenseInfoHeaderText) throws SW360Exception {
    ByteArrayOutputStream docxOutputStream = new ByteArrayOutputStream();
    Optional<byte[]> docxTemplateFile = CommonUtils.loadResource(DocxGenerator.class, DOCX_TEMPLATE_FILE);
    if (docxTemplateFile.isPresent()) {
        try {
            XWPFDocument xwpfDocument = new XWPFDocument(new ByteArrayInputStream(docxTemplateFile.get()));
            switch(getOutputVariant()) {
                case DISCLOSURE:
                    fillDocument(xwpfDocument, projectLicenseInfoResults, projectName, projectVersion, licenseInfoHeaderText, false);
                    break;
                case REPORT:
                    fillDocument(xwpfDocument, projectLicenseInfoResults, projectName, projectVersion, licenseInfoHeaderText, true);
                    break;
                default:
                    throw new IllegalArgumentException("Unknown generator variant type: " + getOutputVariant());
            }
            xwpfDocument.write(docxOutputStream);
            docxOutputStream.close();
        } catch (XmlException e) {
            throw new SW360Exception("Got XmlException while generating docx document: " + e.getMessage());
        } catch (IOException e) {
            throw new SW360Exception("Got IOException when generating docx document: " + e.getMessage());
        } catch (TException e) {
            throw new SW360Exception("Error reading sw360 licenses: " + e.getMessage());
        }
        return docxOutputStream.toByteArray();
    } else {
        throw new SW360Exception("Could not load the template for xwpf document: " + DOCX_TEMPLATE_FILE);
    }
}
Also used : TException(org.apache.thrift.TException) ByteArrayInputStream(java.io.ByteArrayInputStream) XmlException(org.apache.xmlbeans.XmlException) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)1 TException (org.apache.thrift.TException)1 XmlException (org.apache.xmlbeans.XmlException)1 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)1