Skip to content

Commit a457fb2

Browse files
committedDec 5, 2024
feat(#277): remove redundant code
1 parent 6a7004f commit a457fb2

File tree

2 files changed

+50
-49
lines changed

2 files changed

+50
-49
lines changed
 

‎src/main/java/com/jcabi/xml/DomParser.java

+35-35
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import com.jcabi.log.Logger;
3333
import java.io.ByteArrayInputStream;
34+
import java.io.ByteArrayOutputStream;
3435
import java.io.File;
3536
import java.io.IOException;
3637
import java.io.InputStream;
@@ -100,10 +101,6 @@ final class DomParser {
100101
}
101102

102103

103-
DomParser(final DocumentBuilderFactory fct, final InputStream stream) {
104-
this(fct, new StreamSource(stream));
105-
}
106-
107104
DomParser(final DocumentBuilderFactory fct, final File file) {
108105
this(fct, new FileSource(file));
109106
}
@@ -155,22 +152,33 @@ public Document document() {
155152
return doc;
156153
}
157154

155+
/**
156+
* Source of XML.
157+
* @since 0.32
158+
*/
158159
private interface DocSource {
159160

160161
Document apply(DocumentBuilder builder) throws IOException, SAXException;
161162

162163
long length();
163164
}
164165

166+
/**
167+
* File source of XML from a file.
168+
* @since 0.32
169+
*/
165170
private static class FileSource implements DocSource {
166171

172+
/**
173+
* The file.
174+
*/
167175
private final File file;
168176

169-
private FileSource(final Path path) {
170-
this(path.toFile());
171-
}
172-
173-
private FileSource(final File file) {
177+
/**
178+
* Public ctor.
179+
* @param file The file.
180+
*/
181+
FileSource(final File file) {
174182
this.file = file;
175183
}
176184

@@ -185,38 +193,30 @@ public long length() {
185193
}
186194
}
187195

188-
189-
private static class StreamSource implements DocSource {
190-
191-
private final InputStream stream;
192-
193-
public StreamSource(final InputStream stream) {
194-
this.stream = stream;
195-
}
196-
197-
@Override
198-
public Document apply(final DocumentBuilder builder) throws IOException, SAXException {
199-
final Document res = builder.parse(this.stream);
200-
this.stream.close();
201-
return res;
202-
}
203-
204-
@Override
205-
public long length() {
206-
return 0;
207-
}
208-
}
209-
210-
196+
/**
197+
* Bytes source of XML.
198+
* @since 0.32
199+
*/
211200
private static class BytesSource implements DocSource {
212201

202+
/**
203+
* Bytes of the XML.
204+
*/
213205
private final byte[] xml;
214206

215-
private BytesSource(final String xml) {
207+
/**
208+
* Public ctor.
209+
* @param xml Bytes of the XML.
210+
*/
211+
BytesSource(final String xml) {
216212
this(xml.getBytes(StandardCharsets.UTF_8));
217213
}
218214

219-
private BytesSource(final byte[] xml) {
215+
/**
216+
* Public ctor.
217+
* @param xml Bytes of the XML.
218+
*/
219+
BytesSource(final byte[] xml) {
220220
this.xml = xml;
221221
}
222222

@@ -227,7 +227,7 @@ public Document apply(final DocumentBuilder builder) throws IOException, SAXExce
227227

228228
@Override
229229
public long length() {
230-
return xml.length;
230+
return this.xml.length;
231231
}
232232
}
233233
}

‎src/main/java/com/jcabi/xml/XMLDocument.java

+15-14
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public XMLDocument(final File file) throws FileNotFoundException {
184184
}
185185

186186
/**
187-
* Public ctor, from input stream.
187+
* Public ctor, from XML in a file.
188188
*
189189
* <p>The object is created with a default implementation of
190190
* {@link NamespaceContext}, which already defines a
@@ -193,19 +193,15 @@ public XMLDocument(final File file) throws FileNotFoundException {
193193
* <p>An {@link IllegalArgumentException} is thrown if the parameter
194194
* passed is not in XML format.
195195
*
196-
* <p>The provided input stream will be closed automatically after
197-
* getting data from it.
198-
*
199-
* @param stream The input stream, which will be closed automatically
200-
* @throws IOException In case of I/O problem
196+
* @param file XML file
197+
* @throws FileNotFoundException In case of I/O problems
201198
*/
202-
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
203-
public XMLDocument(final InputStream stream) throws IOException {
204-
this(new DomParser(XMLDocument.configuredDFactory(), stream).document());
199+
public XMLDocument(final Path file) throws FileNotFoundException {
200+
this(new DomParser(XMLDocument.configuredDFactory(), file.toFile()).document());
205201
}
206202

207203
/**
208-
* Public ctor, from XML in a file.
204+
* Public ctor, from input stream.
209205
*
210206
* <p>The object is created with a default implementation of
211207
* {@link NamespaceContext}, which already defines a
@@ -214,11 +210,16 @@ public XMLDocument(final InputStream stream) throws IOException {
214210
* <p>An {@link IllegalArgumentException} is thrown if the parameter
215211
* passed is not in XML format.
216212
*
217-
* @param file XML file
218-
* @throws FileNotFoundException In case of I/O problems
213+
* <p>The provided input stream will be closed automatically after
214+
* getting data from it.
215+
*
216+
* @param stream The input stream, which will be closed automatically
217+
* @throws IOException In case of I/O problem
219218
*/
220-
public XMLDocument(final Path file) throws FileNotFoundException {
221-
this(new DomParser(XMLDocument.configuredDFactory(), file.toFile()).document());
219+
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
220+
public XMLDocument(final InputStream stream) throws IOException {
221+
this(new TextResource(stream).toString());
222+
stream.close();
222223
}
223224

224225
/**

0 commit comments

Comments
 (0)
Please sign in to comment.