Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

Commit

Permalink
feat(licenseinfo): allows velocity to load templates from etc/sw360
Browse files Browse the repository at this point in the history
  • Loading branch information
maierthomas committed Mar 21, 2018
1 parent 53fc3bc commit 051a6d9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
Expand Up @@ -17,13 +17,15 @@
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.*;
import org.eclipse.sw360.datahandler.thrift.licenses.License;
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.*;
Expand All @@ -48,28 +50,33 @@ public DocxGenerator(OutputFormatVariant outputFormatVariant, String description
@Override
public byte[] generateOutputFile(Collection<LicenseInfoParsingResult> 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<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());
}
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<LicenseInfoParsingResult> projectLicenseInfoResults,
Expand Down
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit 051a6d9

Please sign in to comment.