| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| StringSource |
|
| 2.3333333333333335;2.333 |
| 1 | /** | |
| 2 | * Copyright (c) 2011-2014, 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.matchers; | |
| 31 | ||
| 32 | import com.jcabi.xml.XMLDocument; | |
| 33 | import java.io.StringWriter; | |
| 34 | import java.util.Locale; | |
| 35 | import javax.xml.transform.OutputKeys; | |
| 36 | import javax.xml.transform.Transformer; | |
| 37 | import javax.xml.transform.TransformerException; | |
| 38 | import javax.xml.transform.TransformerFactory; | |
| 39 | import javax.xml.transform.dom.DOMSource; | |
| 40 | import javax.xml.transform.stream.StreamResult; | |
| 41 | import lombok.EqualsAndHashCode; | |
| 42 | import org.w3c.dom.Node; | |
| 43 | ||
| 44 | /** | |
| 45 | * Private class for DOM to String converting. | |
| 46 | * | |
| 47 | * <p>Objects of this class are immutable and thread-safe. | |
| 48 | * | |
| 49 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
| 50 | * @version $Id$ | |
| 51 | */ | |
| 52 | 0 | @EqualsAndHashCode(callSuper = false, of = "xml") |
| 53 | final class StringSource extends DOMSource { | |
| 54 | ||
| 55 | /** | |
| 56 | * The XML itself. | |
| 57 | */ | |
| 58 | private final transient String xml; | |
| 59 | ||
| 60 | /** | |
| 61 | * Public ctor. | |
| 62 | * @param text The content of the document | |
| 63 | */ | |
| 64 | StringSource(final String text) { | |
| 65 | 35 | super(); |
| 66 | 35 | this.xml = text; |
| 67 | 35 | super.setNode(new XMLDocument(text).node()); |
| 68 | 35 | } |
| 69 | ||
| 70 | /** | |
| 71 | * Public ctor. | |
| 72 | * @param node The node | |
| 73 | */ | |
| 74 | StringSource(final Node node) { | |
| 75 | 2 | super(); |
| 76 | 2 | final StringWriter writer = new StringWriter(); |
| 77 | try { | |
| 78 | 2 | final Transformer transformer = |
| 79 | TransformerFactory.newInstance().newTransformer(); | |
| 80 | 2 | transformer.setOutputProperty( |
| 81 | // @checkstyle MultipleStringLiteralsCheck (1 line) | |
| 82 | OutputKeys.OMIT_XML_DECLARATION, "yes" | |
| 83 | ); | |
| 84 | 2 | transformer.setOutputProperty( |
| 85 | OutputKeys.INDENT, "yes" | |
| 86 | ); | |
| 87 | 2 | transformer.transform( |
| 88 | new DOMSource(node), | |
| 89 | new StreamResult(writer) | |
| 90 | ); | |
| 91 | 0 | } catch (final TransformerException ex) { |
| 92 | 0 | throw new IllegalStateException(ex); |
| 93 | 2 | } |
| 94 | 2 | this.xml = writer.toString(); |
| 95 | 2 | this.setNode(node); |
| 96 | 2 | } |
| 97 | ||
| 98 | @Override | |
| 99 | public String toString() { | |
| 100 | 3 | final StringBuilder buf = new StringBuilder(); |
| 101 | 3 | final int length = this.xml.length(); |
| 102 | 378 | for (int pos = 0; pos < length; ++pos) { |
| 103 | 375 | final char chr = this.xml.charAt(pos); |
| 104 | // @checkstyle MagicNumber (1 line) | |
| 105 | 375 | if (chr > 0x7f) { |
| 106 | 6 | buf.append("&#").append( |
| 107 | Integer.toHexString(chr).toUpperCase(Locale.ENGLISH) | |
| 108 | ).append(';'); | |
| 109 | } else { | |
| 110 | 369 | buf.append(chr); |
| 111 | } | |
| 112 | } | |
| 113 | 3 | return buf.toString(); |
| 114 | } | |
| 115 | } |