Skip to content

Commit

Permalink
Merge pull request #43 from spdx/movehtmltemplate
Browse files Browse the repository at this point in the history
Move the html template code from the SPDX tools library
  • Loading branch information
goneall authored Dec 19, 2018
2 parents d04487e + b524015 commit 5b729c5
Show file tree
Hide file tree
Showing 10 changed files with 1,344 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.spdx</groupId>
<artifactId>licenseListPublisher</artifactId>
<version>2.1.16</version>
<version>2.1.17</version>
<name>License List Publisher</name>
<description>Tool that generates license data found in the license-list-data repository from the license-list-XML source</description>
<licenses>
Expand Down
106 changes: 106 additions & 0 deletions src/org/spdx/htmltemplates/ExceptionHtml.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Copyright (c) 2014 Source Auditor Inc.
*
* 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.
*
*/
package org.spdx.htmltemplates;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Map;

import org.spdx.html.InvalidLicenseTemplateException;
import org.spdx.rdfparser.license.ListedLicenseException;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;


/**
* Manages the production of an HTML file based on an SpdxLicenseRestriction (a.k.a License Exception)
* @author Gary O'Neall
*
*/
public class ExceptionHtml {

static final String TEMPLATE_CLASS_PATH = "resources" + "/" + "htmlTemplate";
static final String TEMPLATE_ROOT_PATH = "resources" + File.separator + "htmlTemplate";
static final String HTML_TEMPLATE = "ExceptionHTMLTemplate.html";

Map<String, Object> mustacheMap = Maps.newHashMap();

/**
* @param exception
* @throws InvalidLicenseTemplateException
*/
public ExceptionHtml(ListedLicenseException exception) throws InvalidLicenseTemplateException {
List<String> alSourceUrls = Lists.newArrayList();
String[] sourceUrls = exception.getSeeAlso();
if (sourceUrls != null) {
for (String sourceUrl: sourceUrls) {
alSourceUrls.add(sourceUrl);
}
}
mustacheMap.put("name", exception.getName());
mustacheMap.put("id", exception.getLicenseExceptionId());
mustacheMap.put("text", exception.getExceptionTextHtml());
mustacheMap.put("getSourceUrl", alSourceUrls);
mustacheMap.put("notes", exception.getComment());
mustacheMap.put("deprecated", exception.isDeprecated());
mustacheMap.put("deprecatedVersion", exception.getDeprecatedVersion());
}

/**
* @param exceptionHtmlFile
* @param exceptionHtmlTocReference
* @throws IOException
* @throws MustacheException
*/
public void writeToFile(File exceptionHtmlFile,
String exceptionHtmlTocReference) throws IOException, MustacheException {
mustacheMap.put("exceptionTocReference", exceptionHtmlTocReference);
FileOutputStream stream = null;
OutputStreamWriter writer = null;
if (!exceptionHtmlFile.exists()) {
if (!exceptionHtmlFile.createNewFile()) {
throw(new IOException("Can not create new file "+exceptionHtmlFile.getName()));
}
}
String templateDirName = TEMPLATE_ROOT_PATH;
File templateDirectoryRoot = new File(templateDirName);
if (!(templateDirectoryRoot.exists() && templateDirectoryRoot.isDirectory())) {
templateDirName = TEMPLATE_CLASS_PATH;
}
try {
stream = new FileOutputStream(exceptionHtmlFile);
writer = new OutputStreamWriter(stream, "UTF-8");
DefaultMustacheFactory builder = new DefaultMustacheFactory(templateDirName);
Mustache mustache = builder.compile(HTML_TEMPLATE);
mustache.execute(writer, mustacheMap);
} finally {
if (writer != null) {
writer.close();
}
if (stream != null) {
stream.close();
}
}
}
}
277 changes: 277 additions & 0 deletions src/org/spdx/htmltemplates/ExceptionHtmlToc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
/**
* Copyright (c) 2014 Source Auditor Inc.
*
* 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.
*
*/
package org.spdx.htmltemplates;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringEscapeUtils;
import org.spdx.rdfparser.license.LicenseException;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
* Generates the HTML Table of Contents for License Exceptions
* @author Gary O'Neall
*
*/
public class ExceptionHtmlToc {
static final String TEMPLATE_CLASS_PATH = "resources" + "/" + "htmlTemplate";
static final String TEMPLATE_ROOT_PATH = "resources" + File.separator + "htmlTemplate";
static final String HTML_TEMPLATE = "ExceptionsTocHTMLTemplate.html";

public static class DeprecatedExceptionRow {
private int refNumber;
String licenseExceptionId;
private String reference;
private String exceptionName;
private String deprecatedVersion;

public DeprecatedExceptionRow(String licenseExceptionId, String exceptionName,
int refNumber, String reference, String deprecatedVersion) {
this.licenseExceptionId = licenseExceptionId;
this.exceptionName = exceptionName;
this.refNumber = refNumber;
this.reference = reference;
this.deprecatedVersion = deprecatedVersion;
}

/**
* @return the licenseExceptionId
*/
public String getLicenseExceptionId() {
return licenseExceptionId;
}


/**
* @param licenseExceptionId the licenseExceptionId to set
*/
public void setLicenseExceptionId(String licenseExceptionId) {
this.licenseExceptionId = licenseExceptionId;
}


/**
* @return the refNumber
*/
public int getRefNumber() {
return refNumber;
}

/**
* @param refNumber the refNumber to set
*/
public void setRefNumber(int refNumber) {
this.refNumber = refNumber;
}

/**
* @return the reference
*/
public String getReference() {
return reference;
}

/**
* @param reference the reference to set
*/
public void setReference(String reference) {
this.reference = reference;
}

/**
* @return the exceptionName
*/
public String getExceptionName() {
return exceptionName;
}

/**
* @param exceptionName the exceptionName to set
*/
public void setExceptionName(String exceptionName) {
this.exceptionName = exceptionName;
}

/**
* @return the deprecatedVersion
*/
public String getDeprecatedVersion() {
return deprecatedVersion;
}

/**
* @param deprecatedVersion the deprecatedVersion to set
*/
public void setDeprecatedVersion(String deprecatedVersion) {
this.deprecatedVersion = deprecatedVersion;
}
}

/**
* Holds the data one of the list rows of exceptions
* @author Gary O'Neall
*
*/
public static class ExceptionRow {
private int refNumber;
private String reference;
private String exceptionName;
/**
* @return the refNumber
*/
public int getRefNumber() {
return refNumber;
}

/**
* @param refNumber the refNumber to set
*/
public void setRefNumber(int refNumber) {
this.refNumber = refNumber;
}

/**
* @return the reference
*/
public String getReference() {
return reference;
}

/**
* @param reference the reference to set
*/
public void setReference(String reference) {
this.reference = reference;
}

/**
* @return the exceptionName
*/
public String getExceptionName() {
return exceptionName;
}

/**
* @param exceptionName the exceptionName to set
*/
public void setExceptionName(String exceptionName) {
this.exceptionName = exceptionName;
}

/**
* @return the licenseExceptionId
*/
public String getLicenseExceptionId() {
return licenseExceptionId;
}

/**
* @param licenseExceptionId the licenseExceptionId to set
*/
public void setLicenseExceptionId(String licenseExceptionId) {
this.licenseExceptionId = licenseExceptionId;
}

private String licenseExceptionId;

public ExceptionRow(String licenseExceptionId, String exceptionName,
int refNumber, String reference) {
this.licenseExceptionId = licenseExceptionId;
this.exceptionName = exceptionName;
this.refNumber = refNumber;
this.reference = reference;
}
}

List<ExceptionRow> exceptions = Lists.newArrayList();
List<DeprecatedExceptionRow> deprecatedExceptions = Lists.newArrayList();

int currentRefNum = 1;

/**
* Add an exception to the table of contents
* @param exception
* @param exceptionHTMLReference
*/
public void addException(LicenseException exception,
String exceptionHTMLReference) {
exceptions.add(new ExceptionRow(
StringEscapeUtils.escapeHtml4(exception.getLicenseExceptionId()),
StringEscapeUtils.escapeHtml4(exception.getName()),
currentRefNum++, exceptionHTMLReference));
}

public void addDeprecatedException(LicenseException exception,
String exceptionHTMLReference, String deprecatedVersion) {
deprecatedExceptions.add(new DeprecatedExceptionRow(
StringEscapeUtils.escapeHtml4(exception.getLicenseExceptionId()),
StringEscapeUtils.escapeHtml4(exception.getName()),
currentRefNum++, exceptionHTMLReference, deprecatedVersion));
}

/**
* Creates and writes an Exception Table of Contents file
* @param exceptionTocFile file to write to
* @param version Version of the License List
* @throws IOException
* @throws MustacheException
*/
public void writeToFile(File exceptionTocFile, String version) throws MustacheException, IOException {

Map<String, Object> mustacheMap = Maps.newHashMap();
mustacheMap.put("version", StringEscapeUtils.escapeHtml4(version));
mustacheMap.put("listedExceptions", exceptions);
mustacheMap.put("deprecatedExceptions", deprecatedExceptions);
FileOutputStream stream = null;
OutputStreamWriter writer = null;
if (!exceptionTocFile.exists()) {
if (!exceptionTocFile.createNewFile()) {
throw(new IOException("Can not create new file "+exceptionTocFile.getName()));
}
}
String templateDirName = TEMPLATE_ROOT_PATH;
File templateDirectoryRoot = new File(templateDirName);
if (!(templateDirectoryRoot.exists() && templateDirectoryRoot.isDirectory())) {
templateDirName = TEMPLATE_CLASS_PATH;
}
try {
stream = new FileOutputStream(exceptionTocFile);
writer = new OutputStreamWriter(stream, "UTF-8");
DefaultMustacheFactory builder = new DefaultMustacheFactory(templateDirName);
Mustache mustache = builder.compile(HTML_TEMPLATE);
mustache.execute(writer, mustacheMap);
} finally {
if (writer != null) {
writer.close();
}
if (stream != null) {
stream.close();
}
}
}

}
Loading

0 comments on commit 5b729c5

Please # to comment.