Coverage Report - com.jcabi.log.DomDecor
 
Classes in this File Line Coverage Branch Coverage Complexity
DomDecor
0%
0/22
0%
0/18
5
 
 1  
 /**
 2  
  * Copyright (c) 2012-2015, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.log;
 31  
 
 32  
 import java.io.StringWriter;
 33  
 import java.util.Formattable;
 34  
 import java.util.Formatter;
 35  
 import javax.xml.transform.OutputKeys;
 36  
 import javax.xml.transform.Transformer;
 37  
 import javax.xml.transform.TransformerConfigurationException;
 38  
 import javax.xml.transform.TransformerException;
 39  
 import javax.xml.transform.TransformerFactory;
 40  
 import javax.xml.transform.dom.DOMSource;
 41  
 import javax.xml.transform.stream.StreamResult;
 42  
 import lombok.EqualsAndHashCode;
 43  
 import lombok.ToString;
 44  
 import org.w3c.dom.Node;
 45  
 
 46  
 /**
 47  
  * Decorates XML Document.
 48  
  *
 49  
  * @author Yegor Bugayenko (yegor@teamed.io)
 50  
  * @version $Id: b6eb80a8595551a2ae12c133ca596befda51c116 $
 51  
  * @since 0.1
 52  
  */
 53  0
 @ToString
 54  0
 @EqualsAndHashCode(of = "node")
 55  
 final class DomDecor implements Formattable {
 56  
 
 57  
     /**
 58  
      * DOM transformer factory, DOM.
 59  
      */
 60  0
     private static final TransformerFactory FACTORY =
 61  
         TransformerFactory.newInstance();
 62  
 
 63  
     /**
 64  
      * The document.
 65  
      */
 66  
     private final transient Node node;
 67  
 
 68  
     /**
 69  
      * Public ctor.
 70  
      * @param doc The document
 71  
      * @throws DecorException If some problem with it
 72  
      */
 73  0
     DomDecor(final Object doc) throws DecorException {
 74  0
         if (doc != null && !(doc instanceof Node)) {
 75  0
             throw new DecorException(
 76  
                 String.format(
 77  
                     "Instance of org.w3c.dom.Node required, while %s provided",
 78  
                     doc.getClass().getName()
 79  
                 )
 80  
             );
 81  
         }
 82  0
         this.node = (Node) doc;
 83  0
     }
 84  
 
 85  
     /**
 86  
      * {@inheritDoc}
 87  
      * @checkstyle ParameterNumber (4 lines)
 88  
      */
 89  
     @Override
 90  
     public void formatTo(final Formatter formatter, final int flags,
 91  
         final int width, final int precision) {
 92  0
         final StringWriter writer = new StringWriter();
 93  0
         if (this.node == null) {
 94  0
             writer.write("NULL");
 95  
         } else {
 96  
             try {
 97  0
                 final Transformer trans = DomDecor.FACTORY.newTransformer();
 98  0
                 trans.setOutputProperty(OutputKeys.INDENT, "yes");
 99  0
                 trans.setOutputProperty(OutputKeys.STANDALONE, "no");
 100  0
                 trans.transform(
 101  
                     new DOMSource(this.node),
 102  
                     new StreamResult(writer)
 103  
                 );
 104  0
             } catch (final TransformerConfigurationException ex) {
 105  0
                 throw new IllegalStateException(ex);
 106  0
             } catch (final TransformerException ex) {
 107  0
                 throw new IllegalStateException(ex);
 108  0
             }
 109  
         }
 110  0
         formatter.format("%s", writer.toString());
 111  0
     }
 112  
 
 113  
 }