|
27 | 27 | import javax.xml.parsers.DocumentBuilder; |
28 | 28 | import javax.xml.parsers.DocumentBuilderFactory; |
29 | 29 | import javax.xml.parsers.ParserConfigurationException; |
| 30 | +import javax.xml.transform.Templates; |
30 | 31 | import javax.xml.transform.Transformer; |
31 | 32 | import javax.xml.transform.TransformerConfigurationException; |
32 | 33 | import javax.xml.transform.TransformerException; |
|
50 | 51 | import java.util.List; |
51 | 52 | import java.util.Map; |
52 | 53 | import java.util.Set; |
| 54 | +import java.util.concurrent.ConcurrentHashMap; |
53 | 55 |
|
54 | 56 | import javax.xml.XMLConstants; |
55 | 57 |
|
@@ -220,18 +222,39 @@ private void generateComponentClasses(Task task) throws ParserConfigurationExcep |
220 | 222 | } |
221 | 223 | } |
222 | 224 |
|
| 225 | + private final TransformerFactory transformerFactory = new net.sf.saxon.TransformerFactoryImpl(); |
| 226 | + |
| 227 | + private final Map<String, Templates> templatesCache = new ConcurrentHashMap<>(); |
| 228 | + |
223 | 229 | private Transformer createTransformer(Task task, String xsltFile) |
224 | 230 | throws TransformerFactoryConfigurationError, TransformerConfigurationException { |
225 | | - StreamSource styleSource; |
| 231 | + final String cacheKey; |
| 232 | + final StreamSource styleSource; |
226 | 233 | File xslt = new File(task.getTransformDirectory() + "/" + xsltFile); |
227 | 234 | if (xslt.exists()) { |
| 235 | + cacheKey = xslt.getAbsolutePath(); |
228 | 236 | styleSource = new StreamSource(xslt); |
229 | 237 | } else { |
230 | 238 | logInfo("Loading predefined xslt file:" + xsltFile); |
| 239 | + cacheKey = "classpath:" + xsltFile; |
231 | 240 | styleSource = new StreamSource(this.getClass().getResourceAsStream(xsltFile)); |
232 | 241 | } |
233 | | - TransformerFactory transformerFactory = new net.sf.saxon.TransformerFactoryImpl(); |
234 | | - return transformerFactory.newTransformer(styleSource); |
| 242 | + |
| 243 | + try { |
| 244 | + Templates templates = templatesCache.computeIfAbsent(cacheKey, k -> { |
| 245 | + try { |
| 246 | + return transformerFactory.newTemplates(styleSource); |
| 247 | + } catch (TransformerConfigurationException e) { |
| 248 | + throw new CodeGenerationException(e); |
| 249 | + } |
| 250 | + }); |
| 251 | + return templates.newTransformer(); |
| 252 | + } catch (CodeGenerationException e) { |
| 253 | + if (e.getCause() instanceof TransformerConfigurationException) { |
| 254 | + throw (TransformerConfigurationException) e.getCause(); |
| 255 | + } |
| 256 | + throw e; |
| 257 | + } |
235 | 258 | } |
236 | 259 |
|
237 | 260 | private final Map<String, Document> specificationCache = new HashMap<>(); |
|
0 commit comments