Skip to content

Commit 6a60def

Browse files
lsoaresKevinGilmore
authored andcommitted
Remove Apache Commons IO dependency and split into smaller methods (eugenp#942)
* Fix the requested changes * Split into smaller methods * Split into smaller methods * Remove apache dependency and split into smaller methods * Add unit tests
1 parent 729c899 commit 6a60def

3 files changed

Lines changed: 71 additions & 32 deletions

File tree

‎jsoup/pom.xml‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
<version>${jsoup.version}</version>
1616
</dependency>
1717
<dependency>
18-
<groupId>commons-io</groupId>
19-
<artifactId>commons-io</artifactId>
20-
<version>${commons.io.version}</version>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
<scope>test</scope>
2122
</dependency>
2223
</dependencies>
2324
<properties>
2425
<maven.compiler.source>1.8</maven.compiler.source>
2526
<maven.compiler.target>1.8</maven.compiler.target>
26-
<commons.io.version>2.5</commons.io.version>
27+
2728
<jsoup.version>1.10.1</jsoup.version>
2829
</properties>
2930
</project>

jsoup/src/main/java/com/baeldung/jsoup/JsoupExample.java renamed to jsoup/src/main/java/com/baeldung/jsoup/JsoupParser.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,33 @@
11
package com.baeldung.jsoup;
22

3-
import java.io.File;
43
import java.io.IOException;
5-
import org.apache.commons.io.FileUtils;
6-
import org.jsoup.HttpStatusException;
74
import org.jsoup.Jsoup;
85
import org.jsoup.nodes.Document;
96
import org.jsoup.nodes.Element;
107
import org.jsoup.parser.Tag;
118
import org.jsoup.select.Elements;
129

13-
public class JsoupExample {
10+
public class JsoupParser {
1411

15-
public static void main(String[] args) throws IOException {
16-
scrapeSpringBlog();
17-
}
18-
19-
static void scrapeSpringBlog() throws IOException {
20-
String blogUrl = "https://spring.io/blog";
21-
Document doc = Jsoup.connect(blogUrl).get();
22-
23-
try {
24-
Document doc404 = Jsoup.connect("https://spring.io/will-not-be-found").get();
25-
} catch (HttpStatusException ex) {
26-
System.out.println(ex.getMessage());
27-
}
12+
Document doc;
2813

29-
Document docCustomConn = Jsoup.connect(blogUrl).userAgent("Mozilla").get();
30-
docCustomConn = Jsoup.connect(blogUrl).timeout(5000).get();
31-
docCustomConn = Jsoup.connect(blogUrl).cookie("cookiename", "val234").get();
32-
// docCustomConn = Jsoup.connect(blogUrl).data("datakey", "datavalue").post();
33-
docCustomConn = Jsoup.connect(blogUrl).header("headersecurity", "xyz123").get();
14+
public void loadDocument(String blogUrl) throws IOException {
15+
doc = Jsoup.connect(blogUrl).get();
16+
}
3417

35-
docCustomConn = Jsoup.connect(blogUrl)
18+
void loadDocumentCustomized(String blogUrl) throws IOException {
19+
doc = Jsoup.connect(blogUrl)
3620
.userAgent("Mozilla")
3721
.timeout(5000)
3822
.cookie("cookiename", "val234")
3923
.cookie("anothercookie", "ilovejsoup")
24+
.referrer("http://google.com")
4025
.header("headersecurity", "xyz123")
4126
.get();
27+
}
4228

29+
void examplesSelectors() {
4330
Elements links = doc.select("a");
44-
Elements sections = doc.select("section");
4531
Elements logo = doc.select(".spring-logo--container");
4632
Elements pagination = doc.select("#pagination_control");
4733
Elements divsDescendant = doc.select("header div");
@@ -50,6 +36,14 @@ static void scrapeSpringBlog() throws IOException {
5036
Element pag = doc.getElementById("pagination_control");
5137
Elements desktopOnly = doc.getElementsByClass("desktopOnly");
5238

39+
Elements sections = doc.select("section");
40+
Element firstSection = sections.first();
41+
Elements sectionParagraphs = firstSection.select(".paragraph");
42+
}
43+
44+
void examplesTraversing() {
45+
Elements sections = doc.select("section");
46+
5347
Element firstSection = sections.first();
5448
Element lastSection = sections.last();
5549
Element secondSection = sections.get(2);
@@ -59,17 +53,24 @@ static void scrapeSpringBlog() throws IOException {
5953
Elements siblings = firstSection.siblingElements();
6054

6155
sections.stream().forEach(el -> System.out.println("section: " + el));
56+
}
6257

63-
Elements sectionParagraphs = firstSection.select(".paragraph");
64-
58+
void examplesExtracting() {
6559
Element firstArticle = doc.select("article").first();
6660
Element timeElement = firstArticle.select("time").first();
6761
String dateTimeOfFirstArticle = timeElement.attr("datetime");
6862
Element sectionDiv = firstArticle.select("section div").first();
6963
String sectionDivText = sectionDiv.text();
7064
String articleHtml = firstArticle.html();
7165
String outerHtml = firstArticle.outerHtml();
66+
}
7267

68+
void examplesModifying() {
69+
Element firstArticle = doc.select("article").first();
70+
Element timeElement = firstArticle.select("time").first();
71+
Element sectionDiv = firstArticle.select("section div").first();
72+
73+
String dateTimeOfFirstArticle = timeElement.attr("datetime");
7374
timeElement.attr("datetime", "2016-12-16 15:19:54.3");
7475
sectionDiv.text("foo bar");
7576
firstArticle.select("h2").html("<div><span></span></div>");
@@ -82,8 +83,9 @@ static void scrapeSpringBlog() throws IOException {
8283

8384
doc.select("li.navbar-link").remove();
8485
firstArticle.select("img").remove();
86+
}
8587

86-
File indexFile = new File("/tmp", "spring_blog_home.html");
87-
FileUtils.writeStringToFile(indexFile, doc.html(), doc.charset());
88+
String getTidyHtml() {
89+
return doc.html();
8890
}
8991
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.jsoup;
2+
3+
import java.io.IOException;
4+
import org.jsoup.HttpStatusException;
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertTrue;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
public class JsoupParserTest {
11+
12+
JsoupParser jsoupParser;
13+
14+
@Before
15+
public void setUp() {
16+
jsoupParser = new JsoupParser();
17+
}
18+
19+
@Test
20+
public void test404() throws IOException {
21+
try {
22+
jsoupParser.loadDocument("https://spring.io/will-not-be-found");
23+
} catch (HttpStatusException ex) {
24+
assertEquals(404, ex.getStatusCode());
25+
}
26+
}
27+
28+
@Test
29+
public void testChange() throws IOException {
30+
jsoupParser.loadDocument("http://spring.io/blog");
31+
32+
jsoupParser.examplesModifying();
33+
34+
assertTrue(jsoupParser.getTidyHtml().contains("http://baeldung.com"));
35+
}
36+
}

0 commit comments

Comments
 (0)