diff --git a/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/TinderGenericMultiFileMojo.java b/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/TinderGenericMultiFileMojo.java index 27f19b7f722..b34a561d14d 100644 --- a/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/TinderGenericMultiFileMojo.java +++ b/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/TinderGenericMultiFileMojo.java @@ -156,6 +156,12 @@ import ca.uhn.fhir.tinder.parser.TargetType; * No. Defaults to same directory as the template file. * * + * velocityProperties + * Specifies the full path to a java properties file + * containing Velocity configuration properties + * No. + * + * * includeResources * A list of the names of the resources or composite data types that should * be used in the file generation @@ -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 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); } diff --git a/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/TinderGenericSingleFileMojo.java b/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/TinderGenericSingleFileMojo.java index ca8b87aed15..a1d00ad8121 100644 --- a/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/TinderGenericSingleFileMojo.java +++ b/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/TinderGenericSingleFileMojo.java @@ -151,6 +151,12 @@ import ca.uhn.fhir.tinder.parser.TargetType; * No. Defaults to same directory as the template file. * * + * velocityProperties + * Specifies the full path to a java properties file + * containing Velocity configuration properties + * No. + * + * * includeResources * A list of the names of the resources or composite data types that should * be used in the file generation @@ -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 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); /* diff --git a/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/ValueSetGenerator.java b/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/ValueSetGenerator.java index ec92441ff35..d1872813549 100644 --- a/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/ValueSetGenerator.java +++ b/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/ValueSetGenerator.java @@ -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 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); diff --git a/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/VelocityHelper.java b/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/VelocityHelper.java new file mode 100644 index 00000000000..5dca5c06db3 --- /dev/null +++ b/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/VelocityHelper.java @@ -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; + } +} diff --git a/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/ant/TinderGeneratorTask.java b/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/ant/TinderGeneratorTask.java index ec0e8fa8ff6..d71a7db5af1 100644 --- a/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/ant/TinderGeneratorTask.java +++ b/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/ant/TinderGeneratorTask.java @@ -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; * No. Defaults to same directory as the template file. * * + * velocityProperties + * Specifies the full path to a java properties file + * containing Velocity configuration properties + * No. + * + * * includeResources * A list of the names of the resources or composite data types that should * be used in the file generation @@ -276,6 +283,8 @@ public class TinderGeneratorTask extends Task { private String velocityPath; + private String velocityProperties; + private List includeResources; private List 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(); diff --git a/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/parser/BaseStructureParser.java b/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/parser/BaseStructureParser.java index f59e210c761..1c53315fb24 100644 --- a/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/parser/BaseStructureParser.java +++ b/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/tinder/parser/BaseStructureParser.java @@ -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);