Skip to content

Commit

Permalink
extra synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Jan 12, 2014
1 parent 2a4a291 commit 485c5fc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<version>1.0.1</version>
</parent>
<artifactId>jcabi-xml</artifactId>
<version>1.0-SNAPSHOT</version>
<version>0.7.2</version>
<name>jcabi-xml</name>
<description>XML Parsing and Transforming</description>
<issueManagement>
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/com/jcabi/xml/XMLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ private XMLDocument(final Node node, final XPathContext ctx) {
public String toString() {
final StringWriter writer = new StringWriter();
try {
final Transformer trans = XMLDocument.TFACTORY.newTransformer();
final Transformer trans;
synchronized (XMLDocument.class) {
trans = XMLDocument.TFACTORY.newTransformer();
}
// @checkstyle MultipleStringLiterals (1 line)
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty(OutputKeys.VERSION, "1.0");
Expand Down Expand Up @@ -359,7 +362,10 @@ public XML merge(@NotNull(message = "context can't be NULL")
private NodeList nodelist(final String query) {
final NodeList nodes;
try {
final XPath xpath = XMLDocument.XFACTORY.newXPath();
final XPath xpath;
synchronized (XMLDocument.class) {
xpath = XMLDocument.XFACTORY.newXPath();
}
xpath.setNamespaceContext(this.context);
nodes = (NodeList) xpath.evaluate(
query, this.dom, XPathConstants.NODESET
Expand All @@ -380,7 +386,11 @@ private NodeList nodelist(final String query) {
private static Node transform(final Source source) {
final DOMResult result = new DOMResult();
try {
XMLDocument.TFACTORY.newTransformer().transform(source, result);
final Transformer trans;
synchronized (XMLDocument.class) {
trans = XMLDocument.TFACTORY.newTransformer();
}
trans.transform(source, result);
} catch (TransformerConfigurationException ex) {
throw new IllegalStateException(ex);
} catch (TransformerException ex) {
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/jcabi/xml/XSDDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ public Collection<SAXParseException> validate(
@NotNull(message = "XML can't be NULL") final Source xml) {
final Schema schema;
try {
schema = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
.newSchema(new StreamSource(new StringReader(this.xsd)));
synchronized (XSDDocument.class) {
schema = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
.newSchema(new StreamSource(new StringReader(this.xsd)));
}
} catch (SAXException ex) {
throw new IllegalStateException(
String.format("failed to create XSD schema from %s", this.xsd),
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/jcabi/xml/XSLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,20 @@ public XML transform(@NotNull(message = "XML can't be NULL")
final XML xml) {
final Transformer trans;
try {
trans = XSLDocument.TFACTORY.newTransformer(
new StreamSource(new StringReader(this.xsl))
);
synchronized (XSLDocument.class) {
trans = XSLDocument.TFACTORY.newTransformer(
new StreamSource(new StringReader(this.xsl))
);
}
} catch (TransformerConfigurationException ex) {
throw new IllegalStateException(ex);
}
final Document target;
try {
target = XSLDocument.DFACTORY.newDocumentBuilder().newDocument();
synchronized (XSLDocument.class) {
target = XSLDocument.DFACTORY.newDocumentBuilder()
.newDocument();
}
} catch (ParserConfigurationException ex) {
throw new IllegalStateException(ex);
}
Expand Down

0 comments on commit 485c5fc

Please sign in to comment.