allow generic tinder tasks to override Velocity properties

This commit is contained in:
Bill Denton 2016-12-07 18:35:42 -08:00
parent 6b446d9bd1
commit 5ab36de2e1
6 changed files with 140 additions and 61 deletions

View File

@ -156,6 +156,12 @@ import ca.uhn.fhir.tinder.parser.TargetType;
* <td valign="top" align="center">No. Defaults to same directory as the template file.</td>
* </tr>
* <tr>
* <td valign="top">velocityProperties</td>
* <td valign="top">Specifies the full path to a java properties file
* containing Velocity configuration properties</td>
* <td valign="top" align="center">No.</td>
* </tr>
* <tr>
* <td valign="top">includeResources</td>
* <td valign="top">A list of the names of the resources or composite data types that should
* be used in the file generation</td>
@ -238,6 +244,8 @@ public class TinderGenericMultiFileMojo extends AbstractMojo {
private File templateFile;
@Parameter(required = false)
private String velocityPath;
@Parameter(required = false)
private String velocityProperties;
@Parameter(required = false)
private List<String> includeResources;
@ -319,6 +327,7 @@ public class TinderGenericMultiFileMojo extends AbstractMojo {
rp.setTemplate(template);
rp.setTemplateFile(templateFile);
rp.setVelocityPath(velocityPath);
rp.setVelocityProperties(velocityProperties);
rp.writeAll(targetType, targetDirectory, null, targetPackage);
}
@ -333,6 +342,7 @@ public class TinderGenericMultiFileMojo extends AbstractMojo {
dtp.setTemplate(template);
dtp.setTemplateFile(templateFile);
dtp.setVelocityPath(velocityPath);
dtp.setVelocityProperties(velocityProperties);
dtp.writeAll(targetType, targetDirectory, null, targetPackage);
}
@ -347,6 +357,7 @@ public class TinderGenericMultiFileMojo extends AbstractMojo {
vsp.setTemplate(template);
vsp.setTemplateFile(templateFile);
vsp.setVelocityPath(velocityPath);
vsp.setVelocityProperties(velocityProperties);
vsp.writeMarkedValueSets(targetType, targetDirectory, targetPackage);
}
@ -361,6 +372,7 @@ public class TinderGenericMultiFileMojo extends AbstractMojo {
pp.setTemplate(template);
pp.setTemplateFile(templateFile);
pp.setVelocityPath(velocityPath);
pp.setVelocityProperties(velocityProperties);
pp.writeAll(targetType, targetDirectory, null, targetPackage);
}

View File

@ -151,6 +151,12 @@ import ca.uhn.fhir.tinder.parser.TargetType;
* <td valign="top" align="center">No. Defaults to same directory as the template file.</td>
* </tr>
* <tr>
* <td valign="top">velocityProperties</td>
* <td valign="top">Specifies the full path to a java properties file
* containing Velocity configuration properties</td>
* <td valign="top" align="center">No.</td>
* </tr>
* <tr>
* <td valign="top">includeResources</td>
* <td valign="top">A list of the names of the resources or composite data types that should
* be used in the file generation</td>
@ -227,6 +233,8 @@ public class TinderGenericSingleFileMojo extends AbstractMojo {
private File templateFile;
@Parameter(required = false)
private String velocityPath;
@Parameter(required = false)
private String velocityProperties;
@Parameter(required = false)
private List<String> includeResources;
@ -311,27 +319,13 @@ public class TinderGenericSingleFileMojo extends AbstractMojo {
/*
* Next, deal with the template and initialize velocity
*/
VelocityEngine v = new VelocityEngine();
VelocityEngine v = VelocityHelper.configureVelocityEngine(templateFile, velocityPath, velocityProperties);
InputStream templateIs = null;
if (templateFile != null) {
templateIs = new FileInputStream(templateFile);
v.setProperty("resource.loader", "file");
v.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
if (velocityPath != null) {
v.setProperty("file.resource.loader.path", velocityPath);
} else {
String path = templateFile.getCanonicalFile().getParent();
if (null == path) {
path = ".";
}
v.setProperty("file.resource.loader.path", path);
}
} else {
templateIs = this.getClass().getResourceAsStream(template);
v.setProperty("resource.loader", "cp");
v.setProperty("cp.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
}
v.setProperty("runtime.references.strict", Boolean.TRUE);
InputStreamReader templateReader = new InputStreamReader(templateIs);
/*

View File

@ -60,6 +60,7 @@ public class ValueSetGenerator {
private String myTemplate = null;
private File myTemplateFile = null;
private String myVelocityPath = null;
private String myVelocityProperties = null;
public ValueSetGenerator(String theVersion) {
myVersion = theVersion;
@ -322,6 +323,10 @@ public class ValueSetGenerator {
myVelocityPath = theVelocityPath;
}
public void setVelocityProperties(String theVelocityProperties) {
myVelocityProperties = theVelocityProperties;
}
public void write(Collection<ValueSetTm> theValueSets, File theOutputDirectory, String thePackageBase) throws IOException {
write(TargetType.SOURCE, theValueSets, theOutputDirectory, thePackageBase);
}
@ -364,30 +369,16 @@ public class ValueSetGenerator {
ctx.put("packageBase", thePackageBase);
ctx.put("esc", new EscapeTool());
VelocityEngine v = new VelocityEngine();
VelocityEngine v = VelocityHelper.configureVelocityEngine(myTemplateFile, myVelocityPath, myVelocityProperties);
if (myTemplateFile != null) {
templateIs = new FileInputStream(myTemplateFile);
v.setProperty("resource.loader", "file");
v.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
if (myVelocityPath != null) {
v.setProperty("file.resource.loader.path", myVelocityPath);
} else {
String path = myTemplateFile.getCanonicalFile().getParent();
if (null == path) {
path = ".";
}
v.setProperty("file.resource.loader.path", path);
}
} else {
String templateName = myTemplate;
if (null == templateName) {
templateName = "/vm/valueset.vm";
}
templateIs = this.getClass().getResourceAsStream(templateName);
v.setProperty("resource.loader", "cp");
v.setProperty("cp.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
}
v.setProperty("runtime.references.strict", Boolean.TRUE);
InputStreamReader templateReader = new InputStreamReader(templateIs, "UTF-8");
v.evaluate(ctx, w, "", templateReader);

View File

@ -0,0 +1,88 @@
package ca.uhn.fhir.tinder;
/*
* #%L
* HAPI FHIR Tinder Plug-In
* %%
* Copyright (C) 2014 - 2016 University Health Network
* %%
* 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.
* #L%
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map.Entry;
import java.util.Properties;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
public class VelocityHelper {
public static VelocityEngine configureVelocityEngine (File templateFile, String velocityPath, String propertyFile) throws IOException {
VelocityEngine result = new VelocityEngine();
boolean haveResourceLoader = false;
boolean haveRuntimeReferences = false;
if (propertyFile != null) {
File propFile = new File(propertyFile);
if (propFile.exists() && propFile.isFile() && propFile.canRead()) {
InputStream propsIn = new FileInputStream(propFile);
Properties props = new Properties();
props.load(propsIn);
propsIn.close();
for (Entry<?,?> entry : props.entrySet()) {
String key = (String)entry.getKey();
result.setProperty(key, entry.getValue());
if (RuntimeConstants.RESOURCE_LOADER.equals(key)) {
haveResourceLoader = true;
} else
if (RuntimeConstants.RUNTIME_REFERENCES_STRICT.equals(key)) {
haveRuntimeReferences = true;
}
}
} else {
throw new FileNotFoundException("Velocity property file ["+propertyFile+"] does not exist or is not readable.");
}
}
if (!haveResourceLoader) {
if (templateFile != null) {
result.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
result.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
if (velocityPath != null) {
result.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, velocityPath);
} else {
String path = templateFile.getCanonicalFile().getParent();
if (null == path) {
path = ".";
}
result.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, path);
}
} else {
result.setProperty("resource.loader", "cp");
result.setProperty("cp.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
}
}
if (!haveRuntimeReferences) {
result.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, Boolean.TRUE);
}
return result;
}
}

View File

@ -44,6 +44,7 @@ import ca.uhn.fhir.tinder.GeneratorContext;
import ca.uhn.fhir.tinder.GeneratorContext.ProfileFileDefinition;
import ca.uhn.fhir.tinder.TinderStructuresMojo.ValueSetFileDefinition;
import ca.uhn.fhir.tinder.ValueSetGenerator;
import ca.uhn.fhir.tinder.VelocityHelper;
import ca.uhn.fhir.tinder.parser.BaseStructureSpreadsheetParser;
import ca.uhn.fhir.tinder.parser.DatatypeGeneratorUsingSpreadsheet;
import ca.uhn.fhir.tinder.parser.ProfileParser;
@ -206,6 +207,12 @@ import ca.uhn.fhir.tinder.parser.TargetType;
* <td valign="top" align="center">No. Defaults to same directory as the template file.</td>
* </tr>
* <tr>
* <td valign="top">velocityProperties</td>
* <td valign="top">Specifies the full path to a java properties file
* containing Velocity configuration properties</td>
* <td valign="top" align="center">No.</td>
* </tr>
* <tr>
* <td valign="top">includeResources</td>
* <td valign="top">A list of the names of the resources or composite data types that should
* be used in the file generation</td>
@ -276,6 +283,8 @@ public class TinderGeneratorTask extends Task {
private String velocityPath;
private String velocityProperties;
private List<String> includeResources;
private List<String> excludeResources;
@ -367,27 +376,13 @@ public class TinderGeneratorTask extends Task {
/*
* Next, deal with the template and initialize velocity
*/
VelocityEngine v = new VelocityEngine();
VelocityEngine v = VelocityHelper.configureVelocityEngine(templateFile, velocityPath, velocityProperties);
InputStream templateIs = null;
if (templateFile != null) {
templateIs = new FileInputStream(templateFile);
v.setProperty("resource.loader", "file");
v.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
if (velocityPath != null) {
v.setProperty("file.resource.loader.path", velocityPath);
} else {
String path = templateFile.getCanonicalFile().getParent();
if (null == path) {
path = ".";
}
v.setProperty("file.resource.loader.path", path);
}
} else {
templateIs = this.getClass().getResourceAsStream(template);
v.setProperty("resource.loader", "cp");
v.setProperty("cp.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
}
v.setProperty("runtime.references.strict", Boolean.TRUE);
InputStreamReader templateReader = new InputStreamReader(templateIs);
/*
@ -457,6 +452,7 @@ public class TinderGeneratorTask extends Task {
rp.setTemplate(template);
rp.setTemplateFile(templateFile);
rp.setVelocityPath(velocityPath);
rp.setVelocityProperties(velocityProperties);
rp.writeAll(targetType, targetDirectory, null, targetPackage);
}
@ -471,6 +467,7 @@ public class TinderGeneratorTask extends Task {
dtp.setTemplate(template);
dtp.setTemplateFile(templateFile);
dtp.setVelocityPath(velocityPath);
dtp.setVelocityProperties(velocityProperties);
dtp.writeAll(targetType, targetDirectory, null, targetPackage);
}
@ -485,6 +482,7 @@ public class TinderGeneratorTask extends Task {
vsp.setTemplate(template);
vsp.setTemplateFile(templateFile);
vsp.setVelocityPath(velocityPath);
vsp.setVelocityProperties(velocityProperties);
vsp.writeMarkedValueSets(targetType, targetDirectory, targetPackage);
}
@ -499,6 +497,7 @@ public class TinderGeneratorTask extends Task {
pp.setTemplate(template);
pp.setTemplateFile(templateFile);
pp.setVelocityPath(velocityPath);
pp.setVelocityProperties(velocityProperties);
pp.writeAll(targetType, targetDirectory, null, targetPackage);
}
}
@ -611,6 +610,10 @@ public class TinderGeneratorTask extends Task {
this.velocityPath = velocityPath;
}
public void setVelocityProperties(String velocityProperties) {
this.velocityProperties = velocityProperties;
}
public void setIncludeResources(String names) {
if (null == this.includeResources) {
this.includeResources = new ArrayList<String>();

View File

@ -43,6 +43,7 @@ import ca.uhn.fhir.model.primitive.BoundCodeDt;
import ca.uhn.fhir.model.primitive.BoundCodeableConceptDt;
import ca.uhn.fhir.tinder.TinderStructuresMojo;
import ca.uhn.fhir.tinder.ValueSetGenerator;
import ca.uhn.fhir.tinder.VelocityHelper;
import ca.uhn.fhir.tinder.model.BaseElement;
import ca.uhn.fhir.tinder.model.BaseRootType;
import ca.uhn.fhir.tinder.model.Child;
@ -73,6 +74,7 @@ public abstract class BaseStructureParser {
private String myTemplate = null;
private File myTemplateFile = null;
private String myVelocityPath = null;
private String myVelocityProperties = null;
public BaseStructureParser(String theVersion, String theBaseDir) {
myVersion = theVersion;
@ -475,6 +477,10 @@ public abstract class BaseStructureParser {
myVelocityPath = theVelocityPath;
}
public void setVelocityProperties(String theVelocityProperties) {
myVelocityProperties = theVelocityProperties;
}
private void write(BaseRootType theResource, File theFile, String thePackageBase) throws IOException, MojoFailureException {
FileOutputStream fos = new FileOutputStream(theFile, false);
OutputStreamWriter w = new OutputStreamWriter(fos, "UTF-8");
@ -538,27 +544,12 @@ public abstract class BaseStructureParser {
ctx.put("versionCapitalized", capitalize);
ctx.put("this", theResource);
VelocityEngine v = new VelocityEngine();
VelocityEngine v = VelocityHelper.configureVelocityEngine(getTemplateFile(), getVelocityPath(), myVelocityProperties);
InputStream templateIs = null;
if (getTemplateFile() != null) {
templateIs = new FileInputStream(getTemplateFile());
v.setProperty("resource.loader", "file");
v.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
if (getVelocityPath() != null) {
v.setProperty("file.resource.loader.path", getVelocityPath());
} else {
String path = getTemplateFile().getCanonicalFile().getParent();
if (null == path) {
path = ".";
}
v.setProperty("file.resource.loader.path", path);
}
} else {
templateIs = this.getClass().getResourceAsStream(getTemplate());
v.setProperty("resource.loader", "cp");
v.setProperty("cp.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
}
v.setProperty("runtime.references.strict", Boolean.TRUE);
InputStreamReader templateReader = new InputStreamReader(templateIs);
v.evaluate(ctx, w, "", templateReader);