Skip to content

Commit 303620d

Browse files
committed
#422 XMLDocument.make()
1 parent 29d8f80 commit 303620d

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

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

+75
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,81 @@ private XMLDocument(
264264
this.cache = cache;
265265
}
266266

267+
/**
268+
* Factory method, to avoid checked exceptions.
269+
* @param file The path of the XML
270+
* @return XML
271+
* @since 0.35.0
272+
*/
273+
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
274+
public static XML make(final File file) {
275+
try {
276+
return new XMLDocument(file);
277+
} catch (final FileNotFoundException ex) {
278+
throw new IllegalArgumentException(ex);
279+
}
280+
}
281+
282+
/**
283+
* Factory method, to avoid checked exceptions.
284+
* @param file The path of the XML
285+
* @return XML
286+
* @since 0.35.0
287+
*/
288+
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
289+
public static XML make(final Path file) {
290+
try {
291+
return new XMLDocument(file);
292+
} catch (final FileNotFoundException ex) {
293+
throw new IllegalArgumentException(ex);
294+
}
295+
}
296+
297+
/**
298+
* Factory method, to avoid checked exceptions.
299+
* @param uri The URI of the XML
300+
* @return XML
301+
* @since 0.35.0
302+
*/
303+
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
304+
public static XML make(final URI uri) {
305+
try {
306+
return new XMLDocument(uri);
307+
} catch (final IOException ex) {
308+
throw new IllegalArgumentException(ex);
309+
}
310+
}
311+
312+
/**
313+
* Factory method, to avoid checked exceptions.
314+
* @param url The URL of the XML
315+
* @return XML
316+
* @since 0.35.0
317+
*/
318+
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
319+
public static XML make(final URL url) {
320+
try {
321+
return new XMLDocument(url);
322+
} catch (final IOException ex) {
323+
throw new IllegalArgumentException(ex);
324+
}
325+
}
326+
327+
/**
328+
* Factory method, to avoid checked exceptions.
329+
* @param stream The input stream with the XML
330+
* @return XML
331+
* @since 0.35.0
332+
*/
333+
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
334+
public static XML make(final InputStream stream) {
335+
try {
336+
return new XMLDocument(stream);
337+
} catch (final IOException ex) {
338+
throw new IllegalArgumentException(ex);
339+
}
340+
}
341+
267342
@Override
268343
public String toString() {
269344
return XMLDocument.asString(this.cache);

src/test/java/com/jcabi/xml/XMLDocumentTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ void doesNotFailOnFetchingInMultipleThreadsFromDifferentDocuments() {
140140

141141
@RepeatedTest(60)
142142
void doesNotFailOnXsdValidationInMultipleThreadsWithTheSameDocumentAndXsd() {
143-
final XML xml = new XMLDocument("<root>passesValidXmlThrough</root>");
144-
final XML xsd = new XMLDocument(XMLDocumentTest.XSD);
145143
Assertions.assertDoesNotThrow(
146144
new Together<>(
147-
thread -> xml.validate(xsd)
145+
thread -> new XMLDocument("<root>passesValidXmlThrough</root>").validate(
146+
new XMLDocument(XMLDocumentTest.XSD)
147+
)
148148
)::asList,
149149
"XMLDocument should not fail on validation in multiple threads with the same document and XSD"
150150
);
@@ -214,7 +214,7 @@ void findsWithXpathWithCustomNamespace() throws Exception {
214214
file
215215
)
216216
).value();
217-
final XML doc = new XMLDocument(file).registerNs("f", "urn:foo");
217+
final XML doc = XMLDocument.make(file).registerNs("f", "urn:foo");
218218
MatcherAssert.assertThat(
219219
doc.nodes("/f:a/f:b[.='\u0433!']"),
220220
Matchers.hasSize(1)
@@ -264,8 +264,8 @@ void printsWithNamespace() {
264264
}
265265

266266
@Test
267-
void retrievesDomNode() throws Exception {
268-
final XML doc = new XMLDocument(
267+
void retrievesDomNode() {
268+
final XML doc = XMLDocument.make(
269269
this.getClass().getResource("simple.xml")
270270
);
271271
MatcherAssert.assertThat(

0 commit comments

Comments
 (0)