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

xmlunit / xmlunit / cd160610-9b67-4752-a2c4-e3a0d82d991e

21 Apr 2025 11:55AM UTC coverage: 91.756% (-0.02%) from 91.78%
cd160610-9b67-4752-a2c4-e3a0d82d991e

push

circleci

web-flow
Merge pull request #289 from xmlunit/circleci-project-setup

CircleCI project setup

3996 of 4698 branches covered (85.06%)

11754 of 12810 relevant lines covered (91.76%)

2.35 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,2022 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;
1✔
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);
1✔
62
    }
1✔
63

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

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

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

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

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

130
    /**
131
     * @return really available
132
     */
133
    private int reallyAvailable() {
134
        return nodeContentBytes.size() - atPos;
1✔
135
    }
136
}
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