Skip to content

Commit

Permalink
Improve debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
Hronom committed Nov 18, 2015
1 parent 7ca25e5 commit 4b76524
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.github.hronom</groupId>
<artifactId>scrape-dat-website</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
<packaging>jar</packaging>

<name>scrape-dat-website</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,38 @@ public class ScrapeDatWebsiteApp {

public static void main(String[] args) {
logger.info(ScrapeDatWebsiteApp.class.getSimpleName());
printSystemInfo();

ScrapeView scrapeView = new ScrapeView();
new ScrapeButtonController(scrapeView);
new ScrapeMainView(scrapeView);
}

private static void printSystemInfo() {
logger.info("Java version: " + System.getProperty("java.version"));
logger.info("Java vendor: " + System.getProperty("java.vendor"));
logger.info("Java vendor url: " + System.getProperty("java.vendor.url"));
logger.info("Java home: " + System.getProperty("java.home"));
logger.info("Java vm specification version: " + System.getProperty("java.vm.specification.version"));
logger.info("Java vm specification vendor: " + System.getProperty("java.vm.specification.vendor"));
logger.info("Java vm specification name: " + System.getProperty("java.vm.specification.name"));
logger.info("Java vm version: " + System.getProperty("java.vm.version"));
logger.info("Java vm vendor: " + System.getProperty("java.vm.vendor"));
logger.info("Java vm name: " + System.getProperty("java.vm.name"));
logger.info("Java specification version: " + System.getProperty("java.specification.version"));
logger.info("Java specification vendor: " + System.getProperty("java.specification.vendor"));
logger.info("Java specification name: " + System.getProperty("java.specification.name"));
logger.info("Java class.version: " + System.getProperty("java.class.version"));
logger.info("Java class.path: " + System.getProperty("java.class.path"));
logger.info("Java library.path: " + System.getProperty("java.library.path"));
logger.info("Java io.tmpdir: " + System.getProperty("java.io.tmpdir"));
logger.info("Java compiler: " + System.getProperty("java.compiler"));
logger.info("Java ext.dirs: " + System.getProperty("java.ext.dirs"));
logger.info("OS name: " + System.getProperty("os.name"));
logger.info("OS arch: " + System.getProperty("os.arch"));
logger.info("OS version: " + System.getProperty("os.version"));


// Total number of processors or cores available to the JVM.
logger.info("Available processors (cores): " + Runtime.getRuntime().availableProcessors());
// Total amount of free memory available to the JVM.
Expand All @@ -33,9 +65,5 @@ public static void main(String[] args) {
logger.info("Free space (bytes): " + root.getFreeSpace());
logger.info("Usable space (bytes): " + root.getUsableSpace());
}

ScrapeView scrapeView = new ScrapeView();
new ScrapeButtonController(scrapeView);
new ScrapeMainView(scrapeView);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public ActionListener createScrapeButtonActionListener() {
public void actionPerformed(ActionEvent event) {
Executors.newSingleThreadExecutor().submit(new Runnable() {
public void run() {
// Disable fields in view.
scrapeView.setWebsiteUrlTextFieldEnabled(false);
scrapeView.setSelectorTextFieldEnabled(false);
scrapeView.setScrapeButtonEnabled(false);
scrapeView.setWorkInProgress(true);
scrapeView.setOutput("");

scrapeView.setProgressBarTaskText("initializing");
logger.info("Start processing...");
long beginTime = System.currentTimeMillis();

Expand All @@ -64,36 +72,37 @@ public void run() {
scrapeView.getSelector() + "\"");
}

// Disable fields in view.
scrapeView.setWebsiteUrlTextFieldEnabled(false);
scrapeView.setSelectorTextFieldEnabled(false);
scrapeView.setScrapeButtonEnabled(false);
scrapeView.setWorkInProgress(true);
scrapeView.setOutput("");

// Process.
try {
URL url = new URL(scrapeView.getWebsiteUrl());
scrapeView.setProgressBarTaskText("requesting page");
logger.info("Requesting page...");
HtmlPage page = webClient.getPage(url);
logger.info("Requesting of page completed.");


scrapeView.setProgressBarTaskText("viewing page as XML");
logger.info("View page as XML");
String xml = page.asXml();

// Unescape html.
scrapeView.setProgressBarTaskText("unescaping HTML");
logger.info("Unescape html");
xml = StringEscapeUtils.unescapeHtml4(xml);

logger.info("Get selector");
String selector = scrapeView.getSelector();
if (!xml.isEmpty() && !selector.isEmpty()) {
logger.info("Parse XML");
scrapeView.setProgressBarTaskText("parsing HTML");
logger.info("Parse HTML");
Document doc = Jsoup.parse(xml);

scrapeView.setProgressBarTaskText("selecting elements in HTML");
logger.info("select elements in HTML");
Elements selectedElements = doc.select(selector);

if (!selectedElements.isEmpty()) {
scrapeView.setProgressBarTaskText("parsing selected elements");
logger.info("Parse extracted elements");
StringBuilder sb = new StringBuilder();
for (Element element : selectedElements) {
Expand All @@ -105,22 +114,22 @@ public void run() {
scrapeView.setOutput(sb.toString());
}
}
} catch (IOException e) {
} catch (Exception e) {
logger.error(e);
}

logger.info("Close WebClient.");
webClient.close();

long endTime = System.currentTimeMillis();
logger.info("Process time: " + (endTime - beginTime) + " ms.");
logger.info("Processing complete.");

// Enable fields in view.
scrapeView.setWorkInProgress(false);
scrapeView.setScrapeButtonEnabled(true);
scrapeView.setSelectorTextFieldEnabled(true);
scrapeView.setWebsiteUrlTextFieldEnabled(true);

long endTime = System.currentTimeMillis();
logger.info("Process time: " + (endTime - beginTime) + " ms.");
logger.info("Processing complete.");
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import javax.swing.*;

public class ScrapeView extends JPanel {
private final String progressBarTextPrefix = "Working, please wait...";

private final JLabel websiteUrlLabel;
private final JTextField websiteUrlTextField;
private final JLabel selectorLabel;
Expand Down Expand Up @@ -115,7 +117,7 @@ public ScrapeView() {

{
progressBar = new JProgressBar();
progressBar.setString("Working, please wait...");
progressBar.setString(progressBarTextPrefix);
progressBar.setStringPainted(true);
progressBar.setIndeterminate(true);
progressBar.setVisible(false);
Expand Down Expand Up @@ -162,4 +164,8 @@ public void setOutput(String text) {
public void setWorkInProgress(boolean working){
progressBar.setVisible(working);
}

public void setProgressBarTaskText(String taskText) {
progressBar.setString(progressBarTextPrefix + " (" + taskText + ")");
}
}

0 comments on commit 4b76524

Please sign in to comment.