java Requirements Checklist These are the items you should complete by the end o
ID: 3805164 • Letter: J
Question
java
Requirements Checklist These are the items you should complete by the end of Part 2 Connect to a webpage and return a Jsoup Document Extract Links from a Jsoup Document Extract and return lower case body text of a Jsoup Document Throw your custom exception for outlined cases Match framework outlined in the Javadoc Test thoroughly with provided JUnit test cases as well as your own Overview This section gives an overview of what parsing is, what we are actually parsing, and a quick tutorial of how we are going to perform this parsing with JSoup Parsing itself is simply taking an input and performing some operations f or as a result of the input. In the context of this project we are taking a web page as input and extracting text and links from it like below Hello World Khtmb chead> ktitle Hello World JSoup https://purdue.edu Another paragraph ca hrefm"https://purdue.eduExplanation / Answer
Below is the implementation of the WebParser that implements the functionalities given in the required checklist. I have added comments to make the implementation self explanatory. Make sure you test the implementation with the JUnit tests provided to you. I also wrote a sample test client to test the implementation manually.
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
class WebParser {
// Connect to a webpage and return a JSoup Document
public static Document getDocument(String url) throws ParseException {
if (null == url) { // check for null parameter
throw new ParseException("getDocument() failed: String url is null.");
}
if ("".isEmpty(url)) { // check for empty parameter
throw new ParseException("getDocument() failed: String url is empty.");
}
Document document = null;
try {
document = Jsoup.connect(url).get();
}
catch (Exception e) {
throw new ParseException(e.message);
}
if (null == document) {
throw new ParseException("getDocument() failed: Document is null.")
}
return document;
}
// Extract and return lower case body text of a JSoup Document
public static Element getBody(Document document) throws ParseException {
if (null == document) {
throw new ParseException("getBody() failed: Document parameter is null.");
}
return document.body();
}
// Extract links from a Jsoup Document
public static Elements getLinks(Document document) throws ParseException {
if (null == document) {
throw new ParseException("getLinks() failed: Document parameter is null.");
}
return links = document.select("a[href]");
}
}
// Custom Exception
class ParseException extends Exception {
ParseException() { super(); }
ParseException(String message) { super(message); }
}
// Sample test client.
class TestClient {
public static void main(String[] args) {
// Connect to a webpage and return a JSoup Document
Document doc = WebParser.getDocument("https://www.wikipedia.org");
// Extract links from a Jsoup Document
Elements links = WebParser.getLinks(doc);
// Print the links
for (Element link : links) {
System.out.println(link.text());
}
// Extract and return lower case body text of a JSoup Document
Element body = WebParser.getBody(doc).text();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.