diff --git a/src/main/java/domain/URLContentLoader.java b/src/main/java/domain/URLContentLoader.java new file mode 100644 index 0000000..c448989 --- /dev/null +++ b/src/main/java/domain/URLContentLoader.java @@ -0,0 +1,39 @@ +package domain; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; + +public class URLContentLoader { + private String urlPath; + + public String loadURLContent() { + StringBuilder text = new StringBuilder(); + try { + URL url = new URL(urlPath); + try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) { + String line; + while ((line = br.readLine()) != null) { + text.append(line).append("\n"); + } + } + } catch (MalformedURLException mue) { + System.err.println("Invalid URL: " + urlPath); + mue.printStackTrace(); + } catch (IOException ioe) { + System.err.println("Error reading from URL: " + urlPath); + ioe.printStackTrace(); + } + return text.toString(); + } + + public String getUrlPath() { + return urlPath; + } + + public void setUrlPath(String urlPath) { + this.urlPath = urlPath; + } +} \ No newline at end of file diff --git a/src/main/java/facade/WordCloudManager.java b/src/main/java/facade/WordCloudManager.java index 3f804b3..a234b1f 100644 --- a/src/main/java/facade/WordCloudManager.java +++ b/src/main/java/facade/WordCloudManager.java @@ -1,6 +1,7 @@ package facade; import domain.FileLoader; +import domain.URLContentLoader; import domain.WordCloudCreator; import domain.TextProcessing; @@ -10,11 +11,13 @@ public class WordCloudManager { private FileLoader fl; private TextProcessing tp; private WordCloudCreator wcm; + private URLContentLoader urll; public WordCloudManager() { fl = new FileLoader(); tp = new TextProcessing(); wcm = new WordCloudCreator(); + urll = new URLContentLoader(); } public boolean loadFile() { @@ -26,6 +29,16 @@ public class WordCloudManager { } } + public boolean setUrlPath(String urlPath) { + urll.setUrlPath(urlPath); + if(!urll.getUrlPath().isEmpty()) { + return true; + } + else { + return false; + } + } + public boolean loadStopwords() { if(fl.getStopwordsPath() != null) { String stopwordfile = tp.formatToText(fl.getStopwordsPath(), fl.getStopwordFormat()); @@ -53,14 +66,28 @@ public class WordCloudManager { } public boolean createWordCloud() { - if(wcm.insertWordsIntoTemplate(tp.tokenizingFile(tp.formatToText(fl.getFilePath(),fl.getFileFormat())))) { - fl = null; - tp = null; - fl = new FileLoader(); - tp = new TextProcessing(); - return true; + if(!(fl.getFilePath() == null)) { + if (wcm.insertWordsIntoTemplate(tp.tokenizingFile(tp.formatToText(fl.getFilePath(), fl.getFileFormat())))) { + resetComponents(); + return true; + } + } + if(!urll.getUrlPath().isEmpty()) { + if(wcm.insertWordsIntoTemplate(tp.tokenizingFile(urll.loadURLContent()))) { + resetComponents(); + return true; + } } return false; } + + public void resetComponents() { + fl = null; + tp = null; + urll = null; + fl = new FileLoader(); + urll = new URLContentLoader(); + tp = new TextProcessing(); + } } diff --git a/src/main/java/tui/TUI.java b/src/main/java/tui/TUI.java index 2190cd3..11ed149 100644 --- a/src/main/java/tui/TUI.java +++ b/src/main/java/tui/TUI.java @@ -36,11 +36,22 @@ public class TUI { } break; case(1): - //URL Input + //Load URLContent + System.out.println("Insert URL path: "); + String url = scan.nextLine(); + if(wcm.setUrlPath(url)) { + System.out.println("URL successful set!\n"); + fMenu = true; + fileMenu(); + } + else { + System.out.println("URL cannot be set!\n"); + } break; case(2): //Exit isRunning = false; + scan.close(); System.out.println("Close Program!"); break; } @@ -58,13 +69,14 @@ public class TUI { // Load stopwords file if(wcm.loadFile()) { if(wcm.loadStopwords()) { - System.out.println("File loaded successful!"); + System.out.println("File loaded successful!\n"); } else{ - System.out.println("Cannot load one more File!"); + System.out.println("Cannot load one more File!\n"); } - } else { - System.out.println("File cannot be loaded!"); + } + else { + System.out.println("File cannot be loaded!\n"); } break; case(1): @@ -92,7 +104,7 @@ public class TUI { if(wcm.createWordCloud()) { System.out.println("HTML File created!\n"); fMenu = false; - System.out.println("Close Program!"); + System.out.println("Close Program!\n"); } else { System.out.println("HTML FIle not created!\n");