|
43 | 43 | import java.nio.file.Path; |
44 | 44 | import java.util.HashMap; |
45 | 45 | import java.util.Map; |
| 46 | +import java.util.concurrent.atomic.AtomicReference; |
46 | 47 | import javax.xml.parsers.DocumentBuilder; |
47 | 48 | import javax.xml.parsers.DocumentBuilderFactory; |
48 | 49 | import javax.xml.parsers.ParserConfigurationException; |
@@ -139,6 +140,13 @@ public final class XSLDocument implements XSL { |
139 | 140 | */ |
140 | 141 | private final transient String sid; |
141 | 142 |
|
| 143 | + /** |
| 144 | + * Cached transformer. |
| 145 | + * @since 0.26 |
| 146 | + */ |
| 147 | + private final transient AtomicReference<Transformer> cached = |
| 148 | + new AtomicReference<>(); |
| 149 | + |
142 | 150 | /** |
143 | 151 | * Public ctor, from XML as a source. |
144 | 152 | * @param src XSL document body |
@@ -417,27 +425,30 @@ private void transformInto(final XML xml, final Result result) { |
417 | 425 | * @return The transformer |
418 | 426 | */ |
419 | 427 | private Transformer transformer() { |
420 | | - final Transformer trans; |
421 | | - synchronized (XSLDocument.TFACTORY) { |
422 | | - XSLDocument.TFACTORY.setURIResolver(this.sources); |
423 | | - try { |
424 | | - trans = XSLDocument.TFACTORY.newTransformer( |
425 | | - new StreamSource(new StringReader(this.xsl), this.sid) |
426 | | - ); |
427 | | - } catch (final TransformerConfigurationException ex) { |
428 | | - throw new IllegalArgumentException( |
429 | | - String.format( |
430 | | - "Failed to create transformer by %s", |
431 | | - XSLDocument.TFACTORY.getClass().getName() |
432 | | - ), |
433 | | - ex |
434 | | - ); |
| 428 | + if (this.cached.get() == null) { |
| 429 | + final Transformer trans; |
| 430 | + synchronized (XSLDocument.TFACTORY) { |
| 431 | + XSLDocument.TFACTORY.setURIResolver(this.sources); |
| 432 | + try { |
| 433 | + trans = XSLDocument.TFACTORY.newTransformer( |
| 434 | + new StreamSource(new StringReader(this.xsl), this.sid) |
| 435 | + ); |
| 436 | + } catch (final TransformerConfigurationException ex) { |
| 437 | + throw new IllegalArgumentException( |
| 438 | + String.format( |
| 439 | + "Failed to create transformer by %s", |
| 440 | + XSLDocument.TFACTORY.getClass().getName() |
| 441 | + ), |
| 442 | + ex |
| 443 | + ); |
| 444 | + } |
435 | 445 | } |
| 446 | + for (final Map.Entry<String, Object> ent : this.params.entrySet()) { |
| 447 | + trans.setParameter(ent.getKey(), ent.getValue()); |
| 448 | + } |
| 449 | + this.cached.set(XSLDocument.forSaxon(trans)); |
436 | 450 | } |
437 | | - for (final Map.Entry<String, Object> ent : this.params.entrySet()) { |
438 | | - trans.setParameter(ent.getKey(), ent.getValue()); |
439 | | - } |
440 | | - return XSLDocument.forSaxon(trans); |
| 451 | + return this.cached.get(); |
441 | 452 | } |
442 | 453 |
|
443 | 454 | /** |
|
0 commit comments