4545import javax .xml .parsers .DocumentBuilderFactory ;
4646import javax .xml .parsers .ParserConfigurationException ;
4747import javax .xml .transform .OutputKeys ;
48+ import javax .xml .transform .Result ;
4849import javax .xml .transform .Source ;
4950import javax .xml .transform .Transformer ;
5051import 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 }
0 commit comments