diff --git a/backend/src/src-licenseinfo/src/main/java/org/eclipse/sw360/licenseinfo/outputGenerators/DocxGenerator.java b/backend/src/src-licenseinfo/src/main/java/org/eclipse/sw360/licenseinfo/outputGenerators/DocxGenerator.java index 6c427eb46..d3d21e83c 100644 --- a/backend/src/src-licenseinfo/src/main/java/org/eclipse/sw360/licenseinfo/outputGenerators/DocxGenerator.java +++ b/backend/src/src-licenseinfo/src/main/java/org/eclipse/sw360/licenseinfo/outputGenerators/DocxGenerator.java @@ -17,6 +17,7 @@ import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.thrift.TException; import org.apache.xmlbeans.XmlException; +import org.eclipse.sw360.datahandler.common.CommonUtils; import org.eclipse.sw360.datahandler.thrift.SW360Exception; import org.eclipse.sw360.datahandler.thrift.ThriftClients; import org.eclipse.sw360.datahandler.thrift.licenseinfo.*; @@ -24,6 +25,7 @@ import org.eclipse.sw360.datahandler.thrift.licenses.LicenseService; import org.eclipse.sw360.datahandler.thrift.licenses.Todo; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.*; @@ -48,28 +50,33 @@ public DocxGenerator(OutputFormatVariant outputFormatVariant, String description @Override public byte[] generateOutputFile(Collection projectLicenseInfoResults, String projectName, String projectVersion, String licenseInfoHeaderText) throws SW360Exception { ByteArrayOutputStream docxOutputStream = new ByteArrayOutputStream(); - try { - XWPFDocument xwpfDocument = new XWPFDocument(this.getClass().getResourceAsStream(DOCX_TEMPLATE_FILE)); - 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()); + Optional 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()); } - 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); } - return docxOutputStream.toByteArray(); } private void fillDocument(XWPFDocument document, Collection projectLicenseInfoResults, diff --git a/backend/src/src-licenseinfo/src/main/java/org/eclipse/sw360/licenseinfo/outputGenerators/OutputGenerator.java b/backend/src/src-licenseinfo/src/main/java/org/eclipse/sw360/licenseinfo/outputGenerators/OutputGenerator.java index 7ce42a6af..47f29a95c 100644 --- a/backend/src/src-licenseinfo/src/main/java/org/eclipse/sw360/licenseinfo/outputGenerators/OutputGenerator.java +++ b/backend/src/src-licenseinfo/src/main/java/org/eclipse/sw360/licenseinfo/outputGenerators/OutputGenerator.java @@ -13,13 +13,16 @@ package org.eclipse.sw360.licenseinfo.outputGenerators; - import com.google.common.collect.Maps; import com.google.common.collect.Sets; import org.apache.commons.io.IOUtils; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; +import org.apache.velocity.runtime.RuntimeConstants; +import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; +import org.apache.velocity.runtime.resource.loader.FileResourceLoader; import org.apache.velocity.tools.ToolManager; +import org.eclipse.sw360.datahandler.common.CommonUtils; import org.eclipse.sw360.datahandler.common.SW360Utils; import org.eclipse.sw360.datahandler.thrift.SW360Exception; import org.eclipse.sw360.datahandler.thrift.licenseinfo.*; @@ -93,8 +96,10 @@ public String getComponentLongName(LicenseInfoParsingResult li) { public VelocityContext getConfiguredVelocityContext() { Properties p = new Properties(); - p.setProperty("resource.loader", "class"); - p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); + p.setProperty(RuntimeConstants.RESOURCE_LOADER, "file, class"); + p.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CommonUtils.SYSTEM_CONFIGURATION_PATH); + p.setProperty("file.resource.loader.class", FileResourceLoader.class.getName()); + p.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName()); Velocity.init(p); ToolManager velocityToolManager = new ToolManager(); velocityToolManager.configure(VELOCITY_TOOLS_FILE); diff --git a/libraries/lib-datahandler/src/main/java/org/eclipse/sw360/datahandler/common/CommonUtils.java b/libraries/lib-datahandler/src/main/java/org/eclipse/sw360/datahandler/common/CommonUtils.java index 3b1120442..aa63245b1 100644 --- a/libraries/lib-datahandler/src/main/java/org/eclipse/sw360/datahandler/common/CommonUtils.java +++ b/libraries/lib-datahandler/src/main/java/org/eclipse/sw360/datahandler/common/CommonUtils.java @@ -55,7 +55,7 @@ */ public class CommonUtils { - private static String SYSTEM_CONFIGURATION_PATH = "/etc/sw360"; + public static final String SYSTEM_CONFIGURATION_PATH = "/etc/sw360"; private CommonUtils() { // Utility class with only static functions