|
35 | 35 | import org.apache.velocity.Template; |
36 | 36 | import org.apache.velocity.VelocityContext; |
37 | 37 | import org.apache.velocity.app.VelocityEngine; |
| 38 | +import org.apache.velocity.exception.VelocityException; |
38 | 39 | import org.apache.velocity.runtime.RuntimeConstants; |
39 | 40 | import org.apache.velocity.runtime.RuntimeSingleton; |
40 | 41 | import org.apache.velocity.runtime.parser.ParseException; |
41 | 42 | import org.apache.velocity.runtime.parser.node.SimpleNode; |
42 | 43 | import org.apache.velocity.util.introspection.SecureUberspector; |
43 | 44 | import org.docx4j.openpackaging.exceptions.Docx4JException; |
44 | 45 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
| 46 | +import org.slf4j.Logger; |
| 47 | +import org.slf4j.LoggerFactory; |
45 | 48 |
|
46 | 49 | import de.symeda.sormas.api.docgeneneration.DocumentTemplateException; |
47 | 50 | import de.symeda.sormas.api.docgeneneration.DocumentVariables; |
|
53 | 56 | import fr.opensagres.xdocreport.template.FieldExtractor; |
54 | 57 | import fr.opensagres.xdocreport.template.FieldsExtractor; |
55 | 58 | import fr.opensagres.xdocreport.template.IContext; |
56 | | -import fr.opensagres.xdocreport.template.TemplateEngineKind; |
| 59 | +import fr.opensagres.xdocreport.template.ITemplateEngine; |
57 | 60 | import fr.opensagres.xdocreport.template.velocity.internal.ExtractVariablesVelocityVisitor; |
| 61 | +import fr.opensagres.xdocreport.template.velocity.internal.VelocityTemplateEngine; |
58 | 62 |
|
59 | 63 | public class TemplateEngine { |
60 | 64 |
|
61 | 65 | private static final Pattern VARIABLE_PATTERN = Pattern.compile("([{] *(!)? *([A-Za-z0-9._]+) *[}]| *(!)? *([A-Za-z0-9._]+) *)"); |
| 66 | + private static final Logger logger = LoggerFactory.getLogger(TemplateEngine.class); |
| 67 | + |
| 68 | + private Properties xdocVelocityProperties; |
| 69 | + |
| 70 | + public TemplateEngine() { |
| 71 | + xdocVelocityProperties = new Properties(); |
| 72 | + try { |
| 73 | + xdocVelocityProperties.load(VelocityTemplateEngine.class.getClassLoader().getResourceAsStream("xdocreport-velocity.properties")); |
| 74 | + } catch (IOException e) { |
| 75 | + logger.error("Could not read velocity properties.", e); |
| 76 | + } |
| 77 | + // Disable Reflection and Classloader related methods |
| 78 | + xdocVelocityProperties.setProperty(RuntimeConstants.UBERSPECT_CLASSNAME, SecureUberspector.class.getCanonicalName()); |
| 79 | + // Disable Includes |
| 80 | + xdocVelocityProperties.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE, NoIncludesEventHandler.class.getCanonicalName()); |
| 81 | + } |
62 | 82 |
|
63 | 83 | public DocumentVariables extractTemplateVariablesDocx(File templateFile) throws DocumentTemplateException { |
64 | 84 | try { |
@@ -105,7 +125,7 @@ public byte[] generateDocumentDocx(Properties properties, File templateFile) thr |
105 | 125 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
106 | 126 | report.process(context, outputStream); |
107 | 127 | return outputStream.toByteArray(); |
108 | | - } catch (IOException | XDocReportException e) { |
| 128 | + } catch (IOException | XDocReportException | VelocityException e) { |
109 | 129 | throw new DocumentTemplateException(String.format(I18nProperties.getString(Strings.errorDocumentGeneration), templateFile.getName())); |
110 | 130 | } |
111 | 131 | } |
@@ -161,7 +181,8 @@ protected IXDocReport readXDocReport(InputStream templateInputStream) throws Doc |
161 | 181 |
|
162 | 182 | try { |
163 | 183 | ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray()); |
164 | | - return XDocReportRegistry.getRegistry().loadReport(inStream, TemplateEngineKind.Velocity); |
| 184 | + ITemplateEngine templateEngine = new XDocTemplateEngine(xdocVelocityProperties); |
| 185 | + return XDocReportRegistry.getRegistry().loadReport(inStream, templateEngine); |
165 | 186 | } catch (IOException | XDocReportException | NullPointerException e) { |
166 | 187 | throw new DocumentTemplateException(I18nProperties.getString(Strings.errorProcessingTemplate)); |
167 | 188 | } |
|
0 commit comments