25
25
import java .io .InputStreamReader ;
26
26
import java .io .Reader ;
27
27
import java .io .StringWriter ;
28
+ import java .nio .charset .StandardCharsets ;
28
29
import java .util .HashSet ;
29
30
import java .util .Properties ;
30
31
import java .util .Set ;
43
44
import org .apache .velocity .util .introspection .SecureUberspector ;
44
45
import org .docx4j .openpackaging .exceptions .Docx4JException ;
45
46
import org .docx4j .openpackaging .packages .WordprocessingMLPackage ;
47
+ import org .jsoup .Jsoup ;
48
+ import org .jsoup .nodes .Document .OutputSettings ;
49
+ import org .jsoup .safety .Whitelist ;
46
50
import org .slf4j .Logger ;
47
51
import org .slf4j .LoggerFactory ;
48
52
49
53
import de .symeda .sormas .api .docgeneneration .DocumentTemplateException ;
50
54
import de .symeda .sormas .api .docgeneneration .DocumentVariables ;
51
55
import de .symeda .sormas .api .i18n .I18nProperties ;
52
56
import de .symeda .sormas .api .i18n .Strings ;
57
+ import de .symeda .sormas .api .utils .HtmlHelper ;
53
58
import fr .opensagres .xdocreport .core .XDocReportException ;
54
59
import fr .opensagres .xdocreport .document .IXDocReport ;
55
60
import fr .opensagres .xdocreport .document .registry .XDocReportRegistry ;
63
68
public class TemplateEngine {
64
69
65
70
private static final Pattern VARIABLE_PATTERN = Pattern .compile ("([{] *(!)? *([A-Za-z0-9._]+) *[}]| *(!)? *([A-Za-z0-9._]+) *)" );
71
+ private static final Whitelist HTML_TEMPLATE_WHITELIST =
72
+ HtmlHelper .EVENTACTION_WHITELIST .addAttributes ("div" , "class" ).addAttributes ("span" , "class" ).addAttributes ("table" , "class" );
66
73
private static final Logger logger = LoggerFactory .getLogger (TemplateEngine .class );
67
74
68
- private Properties xdocVelocityProperties ;
75
+ private final Properties xdocVelocityProperties ;
69
76
70
77
public TemplateEngine () {
71
78
xdocVelocityProperties = new Properties ();
@@ -136,6 +143,9 @@ public String generateDocumentTxt(Properties properties, File templateFile) {
136
143
velocityEngine .setProperty (RuntimeConstants .UBERSPECT_CLASSNAME , SecureUberspector .class .getCanonicalName ());
137
144
// Disable Includes
138
145
velocityEngine .setProperty (RuntimeConstants .EVENTHANDLER_INCLUDE , NoIncludesEventHandler .class .getCanonicalName ());
146
+ // Clean Html
147
+ velocityEngine .setProperty (RuntimeConstants .EVENTHANDLER_REFERENCEINSERTION , CleanHtmlReference .class .getCanonicalName ());
148
+
139
149
velocityEngine .setProperty (RuntimeConstants .RESOURCE_LOADER , "file" );
140
150
velocityEngine .setProperty (RuntimeConstants .FILE_RESOURCE_LOADER_PATH , FilenameUtils .getFullPathNoEndSeparator (templateFile .getPath ()));
141
151
Template template = velocityEngine .getTemplate (templateFile .getName ());
@@ -152,7 +162,10 @@ public String generateDocumentTxt(Properties properties, File templateFile) {
152
162
153
163
StringWriter stringWriter = new StringWriter ();
154
164
template .merge (velocityContext , stringWriter );
155
- return stringWriter .toString ();
165
+ OutputSettings outputSettings = new OutputSettings ();
166
+ outputSettings .prettyPrint (false );
167
+ outputSettings .charset (StandardCharsets .UTF_8 );
168
+ return Jsoup .clean (stringWriter .toString (), "" , HTML_TEMPLATE_WHITELIST , outputSettings );
156
169
}
157
170
158
171
public void validateTemplateDocx (InputStream templateInputStream ) throws DocumentTemplateException {
0 commit comments