• 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

97.73
/xmlunit-core/src/main/java/org/xmlunit/builder/Input.java
1
/*
2
  This file is licensed to You under the Apache License, Version 2.0
3
  (the "License"); you may not use this file except in compliance with
4
  the License.  You may obtain a copy of the License at
5

6
  http://www.apache.org/licenses/LICENSE-2.0
7

8
  Unless required by applicable law or agreed to in writing, software
9
  distributed under the License is distributed on an "AS IS" BASIS,
10
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
  See the License for the specific language governing permissions and
12
  limitations under the License.
13
*/
14
package org.xmlunit.builder;
15

16
import java.io.ByteArrayInputStream;
17
import java.io.ByteArrayOutputStream;
18
import java.io.File;
19
import java.io.InputStream;
20
import java.io.IOException;
21
import java.io.Reader;
22
import java.io.StringReader;
23
import java.net.URI;
24
import java.net.URISyntaxException;
25
import java.net.URL;
26
import java.nio.channels.Channels;
27
import java.nio.channels.ReadableByteChannel;
28
import java.nio.file.Path;
29

30
import javax.xml.transform.Source;
31
import javax.xml.transform.dom.DOMSource;
32
import javax.xml.transform.stream.StreamSource;
33
import org.xmlunit.XMLUnitException;
34
import org.w3c.dom.Document;
35
import org.w3c.dom.Node;
36

37
/**
38
 * Fluent API to create Source instances.
39
 */
40
public class Input {
41

42
    private Input() { /* no instances */ }
43

44
    /**
45
     * Interface for fluent builders of {@link Source}s.
46
     */
47
    public interface Builder {
48
        /**
49
         * build the actual {@link Source} instance.
50
         * @return the Source
51
         */
52
        Source build();
53
    }
54

55
    private static class SourceHoldingBuilder implements Builder {
1✔
56
        protected final Source source;
57
        protected SourceHoldingBuilder(Source source) {
1✔
58
            this.source = source;
1✔
59
        }
1✔
60
        @Override
61
        public Source build() {
62
            assert source != null;
1!
63
            return source;
1✔
64
        }
65
    }
66

67
    /**
68
     * Build a Source from a DOM Document.
69
     * @param d the document to use as source
70
     * @return a new builder
71
     */
72
    public static Builder fromDocument(Document d) {
73
        return new SourceHoldingBuilder(new DOMSource(d));
1✔
74
    }
75

76
    /**
77
     * Build a Source from a DOM Node.
78
     * @param n the node to use as source
79
     * @return a new builder
80
     */
81
    public static Builder fromNode(Node n) {
82
        return new SourceHoldingBuilder(new DOMSource(n));
1✔
83
    }
84

85
    private static class StreamBuilder extends SourceHoldingBuilder {
86
        private StreamBuilder(File f) {
87
            super(new StreamSource(f));
1✔
88
        }
1✔
89
        private StreamBuilder(InputStream s) {
90
            super(new StreamSource(s));
1✔
91
        }
1✔
92
        private StreamBuilder(Reader r) {
93
            super(new StreamSource(r));
1✔
94
        }
1✔
95
        private StreamBuilder(final byte[] b) {
96
            super(new StreamSource() {
1✔
97
                    @Override
98
                    public InputStream getInputStream() {
99
                        return new ByteArrayInputStream(b);
1✔
100
                    }
101
                });
102
        }
1✔
103
        private StreamBuilder(final String s) {
104
            super(new StreamSource() {
1✔
105
                    @Override
106
                    public Reader getReader() {
107
                        return new StringReader(s);
1✔
108
                    }
109
                });
110
        }
1✔
111
        void setSystemId(String id) {
112
            if (id != null) {
1!
113
                source.setSystemId(id);
1✔
114
            }
115
        }
1✔
116
    }
117

118
    /**
119
     * Return the matching Builder for the supported types: {@link Source}, {@link Builder}, {@link Document}, {@link Node},
120
     * byte[] (XML as byte[]), {@link String} (XML as String), {@link File} (contains XML),
121
     * {@link URL} (to an XML-Document), {@link URI} (to an XML-Document), {@link InputStream},
122
     * {@link ReadableByteChannel}, {@link Path},
123
     * Jaxb-{@link Object} (marshal-able with {@code JAXB}.marshal(...))
124
     * @param object the object to use as source
125
     * @return a new builder
126
     */
127
    public static Builder from(Object object) {
128
        Builder xml;
129
        if (object instanceof Source) {
1✔
130
            xml = new SourceHoldingBuilder((Source) object);
1✔
131
        } else if (object instanceof Builder) {
1✔
132
            xml = (Builder) object;
1✔
133
        } else if (object instanceof Document) {
1✔
134
            xml = Input.fromDocument((Document) object);
1✔
135
        } else if (object instanceof Node) {
1✔
136
            xml = Input.fromNode((Node) object);
1✔
137
        } else if (object instanceof byte[]) {
1✔
138
            xml = Input.fromByteArray((byte[]) object);
1✔
139
        } else if (object instanceof String) {
1✔
140
            xml = Input.fromString((String) object);
1✔
141
        } else if (object instanceof File) {
1✔
142
            xml = Input.fromFile((File) object);
1✔
143
        } else if (object instanceof URL) {
1✔
144
            xml = Input.fromURL((URL) object);
1✔
145
        } else if (object instanceof URI) {
1✔
146
            xml = Input.fromURI((URI) object);
1✔
147
        } else if (object instanceof InputStream) {
1✔
148
            xml = Input.fromStream((InputStream) object);
1✔
149
        } else if (object instanceof ReadableByteChannel) {
1✔
150
            xml = Input.fromChannel((ReadableByteChannel) object);
1✔
151
        } else if (object instanceof Path) {
1✔
152
            xml = Input.fromPath((Path) object);
1✔
153
        } else if (object instanceof Reader) {
1✔
154
            xml = Input.fromReader((Reader) object);
1✔
155
        } else {
156
            // assume it is a JaxB-Object.
157
            xml = Input.fromJaxb(object);
1✔
158
        }
159
        return xml;
1✔
160
    }
161

162
    /**
163
     * Build a Source from a Jaxb-Object.
164
     * @param jaxbObject the object to use as source
165
     * @return a new builder
166
     */
167
    public static JaxbBuilder fromJaxb(Object jaxbObject) {
168
        return JaxbBuilderFactoryLocator.getFactory().create(jaxbObject);
1✔
169
    }
170

171
    /**
172
     * Build a Source from a file.
173
     * @param f the file to use as source
174
     * @return a new builder
175
     */
176
    public static Builder fromFile(File f) {
177
        return new StreamBuilder(f);
1✔
178
    }
179

180
    /**
181
     * Build a Source from a named file.
182
     * @param name name of the file to use as source
183
     * @return a new builder
184
     */
185
    public static Builder fromFile(String name) {
186
        return new StreamBuilder(new File(name));
1✔
187
    }
188

189
    /**
190
     * Build a Source from a stream.
191
     * @param s the stream to use as source
192
     * @return a new builder
193
     */
194
    public static Builder fromStream(InputStream s) {
195
        return new StreamBuilder(s);
1✔
196
    }
197

198
    /**
199
     * Build a Source from a reader.
200
     * @param r the reader to use as source
201
     * @return a new builder
202
     */
203
    public static Builder fromReader(Reader r) {
204
        return new StreamBuilder(r);
1✔
205
    }
206

207
    /**
208
     * Build a Source from a string.
209
     * @param s the string to use as source
210
     * @return a new builder
211
     */
212
    public static Builder fromString(String s) {
213
        return new StreamBuilder(s);
1✔
214
    }
215

216
    /**
217
     * Build a Source from an array of bytes.
218
     * @param b the bytes to use as source
219
     * @return a new builder
220
     */
221
    public static Builder fromByteArray(byte[] b) {
222
        return new StreamBuilder(b);
1✔
223
    }
224

225
    /**
226
     * Build a Source from a channel.
227
     * @param c the channel to use as source
228
     * @return a new builder
229
     */
230
    public static Builder fromChannel(ReadableByteChannel c) {
231
        return fromStream(Channels.newInputStream(c));
1✔
232
    }
233

234
    /**
235
     * Build a Source from an URL.
236
     * @param url the url to use as source
237
     * @return a new builder
238
     */
239
    public static Builder fromURL(URL url) {
240
        try {
241
            try (InputStream in = url.openStream()) {
1✔
242
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
1✔
243
                int read = -1;
1✔
244
                byte[] buf = new byte[4096];
1✔
245
                while ((read = in.read(buf)) >= 0) {
1✔
246
                    if (read > 0) {
1!
247
                        baos.write(buf, 0, read);
1✔
248
                    }
249
                }
250
                StreamBuilder b =
1✔
251
                    (StreamBuilder) fromByteArray(baos.toByteArray());
1✔
252
                try {
253
                    b.setSystemId(url.toURI().toString());
1✔
254
                } catch (URISyntaxException use) {
×
255
                    // impossible - shouldn't have been an URL in the
256
                    // first place
257
                    b.setSystemId(url.toString());
×
258
                }
1✔
259
                return b;
1✔
260
            }
261
        } catch (IOException ex) {
1✔
262
            throw new XMLUnitException(ex);
1✔
263
        }
264
    }
265

266
    /**
267
     * Build a Source from an URI.
268
     * @param uri must represent a valid URL
269
     * @return a new builder
270
     */
271
    public static Builder fromURI(URI uri) {
272
        try {
273
            return fromURL(uri.toURL());
1✔
274
        } catch (java.net.MalformedURLException ex) {
1✔
275
            throw new IllegalArgumentException("uri " + uri + " is not an URL",
1✔
276
                                               ex);
277
        }
278
    }
279

280
    /**
281
     * Build a Source from an URI.
282
     * @param uri must represent a valid URL
283
     * @return a new builder
284
     */
285
    public static Builder fromURI(String uri) {
286
        try {
287
            return fromURI(new URI(uri));
1✔
288
        } catch (URISyntaxException ex) {
1✔
289
            throw new IllegalArgumentException("uri " + uri + " is not an URI",
1✔
290
                                               ex);
291
        }
292
    }
293

294
    /**
295
     * Build a Source from a Path.
296
     * @param path a Path
297
     * @return a new builder
298
     * @since XMLUnit 2.8.0
299
     */
300
    public static Builder fromPath(final Path path) {
301
        return fromURI(path.toUri());
1✔
302
    }
303

304
    /**
305
     * Builds {@link Source}s by transforming other sources.
306
     */
307
    public interface TransformationBuilder
308
        extends TransformationBuilderBase<TransformationBuilder>, Builder {
309
        /**
310
         * Sets the stylesheet to use.
311
         * @param b builder holding the styleSheet
312
         * @return a new builder
313
         */
314
        TransformationBuilder withStylesheet(Builder b);
315
    }
316

317
    private static class Transformation
318
        extends AbstractTransformationBuilder<TransformationBuilder>
319
        implements TransformationBuilder {
320

321
        private Transformation(Source s) {
322
            super(s);
1✔
323
        }
1✔
324
        @Override
325
        public TransformationBuilder withStylesheet(Builder b) {
326
            return withStylesheet(b.build());
1✔
327
        }
328
        @Override
329
        public Source build() {
330
            return new DOMSource(getHelper().transformToDocument());
1✔
331
        }
332
    }
333

334
    /**
335
     * Build a Source by XSLT transforming a different Source.
336
     * @param s the source to transform
337
     * @return a new builder
338
     */
339
    public static TransformationBuilder byTransforming(Source s) {
340
        return new Transformation(s);
1✔
341
    }
342

343
    /**
344
     * Build a Source by XSLT transforming a different Source.
345
     * @param b builder providing the source to transform
346
     * @return a new builder
347
     */
348
    public static TransformationBuilder byTransforming(Builder b) {
349
        return byTransforming(b.build());
1✔
350
    }
351
}
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