Skip to content

Commit 3dc4e94

Browse files
committed
#171 re
1 parent 5a8b56b commit 3dc4e94

2 files changed

Lines changed: 64 additions & 24 deletions

File tree

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

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import javax.xml.parsers.DocumentBuilderFactory;
4646
import javax.xml.parsers.ParserConfigurationException;
4747
import javax.xml.transform.OutputKeys;
48+
import javax.xml.transform.Result;
4849
import javax.xml.transform.Source;
4950
import javax.xml.transform.Transformer;
5051
import javax.xml.transform.TransformerConfigurationException;
@@ -475,7 +476,8 @@ private <T> T fetch(final String query, final Class<T> type)
475476
} else {
476477
throw new IllegalArgumentException(
477478
String.format(
478-
"Unsupported type: %s", type.getName()
479+
"Unsupported type: %s",
480+
type.getName()
479481
)
480482
);
481483
}
@@ -490,26 +492,40 @@ private <T> T fetch(final String query, final Class<T> type)
490492
*/
491493
private static String asString(final Node node) {
492494
final StringWriter writer = new StringWriter();
495+
final Transformer trans;
493496
try {
494-
final Transformer trans;
495497
synchronized (XMLDocument.class) {
496498
trans = XMLDocument.TFACTORY.newTransformer();
497499
}
498-
trans.setOutputProperty(OutputKeys.INDENT, "yes");
499-
trans.setOutputProperty(OutputKeys.VERSION, "1.0");
500-
if (!(node instanceof Document)) {
501-
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
502-
}
500+
} catch (final TransformerConfigurationException ex) {
501+
throw new IllegalArgumentException(
502+
String.format(
503+
"Failed to create transformer by %s",
504+
XMLDocument.TFACTORY.getClass().getName()
505+
),
506+
ex
507+
);
508+
}
509+
trans.setOutputProperty(OutputKeys.INDENT, "yes");
510+
trans.setOutputProperty(OutputKeys.VERSION, "1.0");
511+
if (!(node instanceof Document)) {
512+
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
513+
}
514+
final Source source = new DOMSource(node);
515+
final Result result = new StreamResult(writer);
516+
try {
503517
synchronized (node) {
504-
trans.transform(
505-
new DOMSource(node),
506-
new StreamResult(writer)
507-
);
518+
trans.transform(source, result);
508519
}
509-
} catch (final TransformerConfigurationException ex) {
510-
throw new IllegalStateException(ex);
511520
} catch (final TransformerException ex) {
512-
throw new IllegalArgumentException(ex);
521+
throw new IllegalArgumentException(
522+
String.format(
523+
"Failed to transform %s to %s",
524+
source.getClass().getName(),
525+
result.getClass().getName()
526+
),
527+
ex
528+
);
513529
}
514530
return writer.toString();
515531
}
@@ -528,7 +544,14 @@ private static Node transform(final Source source) {
528544
}
529545
trans.transform(source, result);
530546
} catch (final TransformerException ex) {
531-
throw new IllegalStateException(ex);
547+
throw new IllegalArgumentException(
548+
String.format(
549+
"Failed to transform %s to %s",
550+
source.getClass().getName(),
551+
result.getClass().getName()
552+
),
553+
ex
554+
);
532555
}
533556
return result.getNode();
534557
}

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@
3939
import java.io.UnsupportedEncodingException;
4040
import java.net.URI;
4141
import java.net.URL;
42+
import java.nio.charset.StandardCharsets;
4243
import java.nio.file.Path;
4344
import java.util.HashMap;
4445
import java.util.Map;
46+
import javax.xml.parsers.DocumentBuilder;
4547
import javax.xml.parsers.DocumentBuilderFactory;
4648
import javax.xml.parsers.ParserConfigurationException;
4749
import javax.xml.transform.Result;
@@ -323,7 +325,13 @@ public static XSL make(final URL url) {
323325
try {
324326
return new XSLDocument(url);
325327
} catch (final IOException ex) {
326-
throw new IllegalStateException(ex);
328+
throw new IllegalStateException(
329+
String.format(
330+
"Failed to read from URL '%s'",
331+
url
332+
),
333+
ex
334+
);
327335
}
328336
}
329337

@@ -334,13 +342,20 @@ public String toString() {
334342

335343
@Override
336344
public XML transform(final XML xml) {
337-
final Document target;
345+
final DocumentBuilder builder;
338346
try {
339-
target = XSLDocument.DFACTORY.newDocumentBuilder().newDocument();
340-
this.transformInto(xml, new DOMResult(target));
347+
builder = XSLDocument.DFACTORY.newDocumentBuilder();
341348
} catch (final ParserConfigurationException ex) {
342-
throw new IllegalStateException(ex);
349+
throw new IllegalArgumentException(
350+
String.format(
351+
"Failed to create new XML document by %s",
352+
XSLDocument.DFACTORY.getClass().getName()
353+
),
354+
ex
355+
);
343356
}
357+
final Document target = builder.newDocument();
358+
this.transformInto(xml, new DOMResult(target));
344359
return new XMLDocument(target);
345360
}
346361

@@ -349,9 +364,12 @@ public String applyTo(final XML xml) {
349364
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
350365
this.transformInto(xml, new StreamResult(baos));
351366
try {
352-
return baos.toString("UTF-8");
367+
return baos.toString(StandardCharsets.UTF_8.name());
353368
} catch (final UnsupportedEncodingException ex) {
354-
throw new IllegalArgumentException(ex);
369+
throw new IllegalArgumentException(
370+
"Failed to convert bytes into UTF-8 string",
371+
ex
372+
);
355373
}
356374
}
357375

@@ -392,8 +410,7 @@ private void transformInto(final XML xml, final Result result) {
392410
);
393411
}
394412
XSLDocument.prepare(trans);
395-
for (final Map.Entry<String, Object> ent
396-
: this.params.entrySet()) {
413+
for (final Map.Entry<String, Object> ent : this.params.entrySet()) {
397414
trans.setParameter(ent.getKey(), ent.getValue());
398415
}
399416
try {

0 commit comments

Comments
 (0)