• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xmlunit / xmlunit / 667

pending completion
667

push

travis-ci-com

bodewig
add Cyclone DX SBOM generation to build

5824 of 6326 relevant lines covered (92.06%)

3.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.32
/xmlunit-legacy/src/main/java/org/custommonkey/xmlunit/NodeInputStream.java
1
/*
2
******************************************************************
3
Copyright (c) 2001-2007,2015 Jeff Martin, Tim Bacon
4
All rights reserved.
5

6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
8
are met:
9

10
    * Redistributions of source code must retain the above copyright
11
      notice, this list of conditions and the following disclaimer.
12
    * Redistributions in binary form must reproduce the above
13
      copyright notice, this list of conditions and the following
14
      disclaimer in the documentation and/or other materials provided
15
      with the distribution.
16
    * Neither the name of the XMLUnit nor the names
17
      of its contributors may be used to endorse or promote products
18
      derived from this software without specific prior written
19
      permission.
20

21
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
POSSIBILITY OF SUCH DAMAGE.
33

34
******************************************************************
35
*/
36

37
package org.custommonkey.xmlunit;
38

39
import java.io.ByteArrayOutputStream;
40
import java.io.InputStream;
41
import java.io.IOException;
42
import java.util.Properties;
43
import javax.xml.transform.stream.StreamResult;
44
import org.w3c.dom.Node;
45

46
/**
47
 * Adapter class to present the content of a DOM Node (e.g. a Document) as an
48
 * InputStream using a DOM to Stream transformation.
49
 */
50
public class NodeInputStream extends InputStream {
51
    private final Node rootNode;
52
    private final ByteArrayOutputStream nodeContentBytes;
53
    private final Properties outputProperties;
54
    private int atPos = 0;
4✔
55

56
    /**
57
     * Simple constructor
58
     * @param rootNode the node to be presented as an input stream
59
     */
60
    public NodeInputStream(Node rootNode) {
61
        this(rootNode, null);
4✔
62
    }
4✔
63

64
    /**
65
     * Simple constructor
66
     * @param rootNode the node to be presented as an input stream
67
     */
68
    public NodeInputStream(Node rootNode, Properties outputProperties) {
4✔
69
        this.rootNode = rootNode;
4✔
70
        nodeContentBytes = new ByteArrayOutputStream();
4✔
71
        this.outputProperties = outputProperties;
4✔
72
    }
4✔
73

74
    /**
75
     * Do the actual work of serializing the node to bytes
76
     * @throws IOException if serialization goes awry
77
     */
78
    private void ensureContentAvailable() throws IOException {
79
        if (nodeContentBytes.size() > 0) {
4✔
80
            return;
4✔
81
        }
82
        try {
83
            Transform serializeTransform = new Transform(rootNode);
4✔
84
            if (outputProperties!=null) {
4✔
85
                serializeTransform.setOutputProperties(outputProperties);
4✔
86
            }
87
            StreamResult byteResult = new StreamResult(nodeContentBytes);
4✔
88
            serializeTransform.transformTo(byteResult);
4✔
89
        } catch (Exception e) {
×
90
            throw new IOException("Unable to serialize document to outputstream: "
×
91
                                  + e.toString());
×
92
        }
4✔
93
    }
4✔
94

95
    /**
96
     * InputStream method
97
     * @return byte as read
98
     * @throws IOException
99
     */
100
    public int read() throws IOException {
101
        ensureContentAvailable();
4✔
102
        if (reallyAvailable()==0) {
4✔
103
            return -1;
4✔
104
        }
105
        int contentByte = nodeContentBytes.toByteArray()[atPos];
4✔
106
        atPos++;
4✔
107
        return contentByte;
4✔
108
    }
109

110
    /**
111
     * InputStream method
112
     * Note that calling close allows a repeated read of the content
113
     * @throws IOException
114
     */
115
    public void close() throws IOException {
116
        atPos = 0;
4✔
117
    }
4✔
118

119
    /**
120
     * InputStream method
121
     * @return number of bytes available
122
     */
123
    public int available() throws IOException {
124
        ensureContentAvailable();
4✔
125
        return reallyAvailable();
4✔
126
    }
127

128
    /**
129
     * @return really available
130
     */
131
    private int reallyAvailable() {
132
        return nodeContentBytes.size() - atPos;
4✔
133
    }
134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc