1 package org.owasp.dependencycheck.utils;
2
3 import org.xml.sax.InputSource;
4
5 import java.io.Closeable;
6 import java.io.FileNotFoundException;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.InputStreamReader;
10 import java.io.Reader;
11 import java.nio.charset.StandardCharsets;
12 import java.nio.file.Path;
13
14 public class AutoCloseableInputSource extends InputSource implements AutoCloseable {
15 public AutoCloseableInputSource(InputStream inputStream) {
16 super(inputStream);
17 }
18
19 public AutoCloseableInputSource(Reader reader) {
20 super(reader);
21 }
22
23 @Override
24 public void close() throws IOException {
25 closeIfNecessary(super.getByteStream());
26 closeIfNecessary(super.getCharacterStream());
27 }
28
29 private void closeIfNecessary(Closeable closeable) throws IOException {
30 if (closeable != null) {
31 closeable.close();
32 }
33 }
34
35 public static AutoCloseableInputSource fromResource(String resourceLocation) throws FileNotFoundException {
36 AutoCloseableInputSource inputSource = new AutoCloseableInputSource(new InputStreamReader(FileUtils.getResourceAsStream(resourceLocation), StandardCharsets.UTF_8));
37 inputSource.setSystemId(Path.of(resourceLocation).getFileName().toString());
38 return inputSource;
39 }
40 }