Skip to content

Commit 6c093bc

Browse files
committed
fix(#185): remove synchronization in XSLDocument
1 parent df340cf commit 6c093bc

1 file changed

Lines changed: 24 additions & 43 deletions

File tree

src/main/java/com/jcabi/xml/XSLDocument.java

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,6 @@ public final class XSLDocument implements XSL {
107107
XSL.class.getResourceAsStream("strip.xsl")
108108
);
109109

110-
/**
111-
* DOM document builder factory.
112-
*/
113-
private static final DocumentBuilderFactory DFACTORY =
114-
DocumentBuilderFactory.newInstance();
115-
116-
/**
117-
* Transformer factory.
118-
*/
119-
private static final TransformerFactory TFACTORY =
120-
TransformerFactory.newInstance();
121-
122110
/**
123111
* XSL document.
124112
*/
@@ -402,16 +390,15 @@ public String toString() {
402390

403391
@Override
404392
public XML transform(final XML xml) {
393+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
405394
final DocumentBuilder builder;
406395
try {
407-
synchronized (XSLDocument.DFACTORY) {
408-
builder = XSLDocument.DFACTORY.newDocumentBuilder();
409-
}
396+
builder = factory.newDocumentBuilder();
410397
} catch (final ParserConfigurationException ex) {
411398
throw new IllegalArgumentException(
412399
String.format(
413400
"Failed to create new XML document by %s",
414-
XSLDocument.DFACTORY.getClass().getName()
401+
factory.getClass().getName()
415402
),
416403
ex
417404
);
@@ -446,17 +433,15 @@ public String applyTo(final XML xml) {
446433
* @param xml XML
447434
* @param result Result
448435
* @since 0.11
449-
* @link <a href="https://stackoverflow.com/questions/4695489">Relevant SO question</a>
436+
* @link https://stackoverflow.com/questions/4695489/capture-xslmessage-output-in-java
450437
*/
451438
private void transformInto(final XML xml, final Result result) {
452439
final Transformer trans = this.transformer();
453440
final ConsoleErrorListener errors = new ConsoleErrorListener();
454441
trans.setErrorListener(errors);
455442
final long start = System.nanoTime();
456443
try {
457-
synchronized (XSLDocument.DFACTORY) {
458-
trans.transform(new DOMSource(xml.node()), result);
459-
}
444+
trans.transform(new DOMSource(xml.node()), result);
460445
} catch (final TransformerException ex) {
461446
final StringBuilder summary = new StringBuilder(
462447
String.join("; ", errors.summary())
@@ -488,30 +473,26 @@ private void transformInto(final XML xml, final Result result) {
488473
* @return The transformer
489474
*/
490475
private Transformer transformer() {
491-
synchronized (XSLDocument.TFACTORY) {
492-
if (this.cached.get() == null) {
493-
XSLDocument.TFACTORY.setURIResolver(this.sources);
494-
final Transformer trans;
495-
try {
496-
trans = XSLDocument.TFACTORY.newTransformer(
497-
new StreamSource(new StringReader(this.xsl), this.sid)
498-
);
499-
} catch (final TransformerConfigurationException ex) {
500-
throw new IllegalArgumentException(
501-
String.format(
502-
"Failed to create transformer by %s",
503-
XSLDocument.TFACTORY.getClass().getName()
504-
),
505-
ex
506-
);
507-
}
508-
for (final Map.Entry<String, Object> ent : this.params.entrySet()) {
509-
trans.setParameter(ent.getKey(), ent.getValue());
510-
}
511-
this.cached.set(XSLDocument.forSaxon(trans));
512-
}
476+
final TransformerFactory factory = TransformerFactory.newInstance();
477+
factory.setURIResolver(this.sources);
478+
Transformer trans;
479+
try {
480+
trans = factory.newTransformer(
481+
new StreamSource(new StringReader(this.xsl), this.sid)
482+
);
483+
} catch (final TransformerConfigurationException ex) {
484+
throw new IllegalArgumentException(
485+
String.format(
486+
"Failed to create transformer by %s",
487+
factory.getClass().getName()
488+
),
489+
ex
490+
);
513491
}
514-
return this.cached.get();
492+
for (final Map.Entry<String, Object> ent : this.params.entrySet()) {
493+
trans.setParameter(ent.getKey(), ent.getValue());
494+
}
495+
return trans;
515496
}
516497

517498
/**

0 commit comments

Comments
 (0)