implements URL read from Path
parent
30654cc2ab
commit
006c5525c3
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package facade;
|
package facade;
|
||||||
|
|
||||||
import domain.FileLoader;
|
import domain.FileLoader;
|
||||||
|
import domain.URLContentLoader;
|
||||||
import domain.WordCloudCreator;
|
import domain.WordCloudCreator;
|
||||||
import domain.TextProcessing;
|
import domain.TextProcessing;
|
||||||
|
|
||||||
|
@ -10,11 +11,13 @@ public class WordCloudManager {
|
||||||
private FileLoader fl;
|
private FileLoader fl;
|
||||||
private TextProcessing tp;
|
private TextProcessing tp;
|
||||||
private WordCloudCreator wcm;
|
private WordCloudCreator wcm;
|
||||||
|
private URLContentLoader urll;
|
||||||
|
|
||||||
public WordCloudManager() {
|
public WordCloudManager() {
|
||||||
fl = new FileLoader();
|
fl = new FileLoader();
|
||||||
tp = new TextProcessing();
|
tp = new TextProcessing();
|
||||||
wcm = new WordCloudCreator();
|
wcm = new WordCloudCreator();
|
||||||
|
urll = new URLContentLoader();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean loadFile() {
|
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() {
|
public boolean loadStopwords() {
|
||||||
if(fl.getStopwordsPath() != null) {
|
if(fl.getStopwordsPath() != null) {
|
||||||
String stopwordfile = tp.formatToText(fl.getStopwordsPath(), fl.getStopwordFormat());
|
String stopwordfile = tp.formatToText(fl.getStopwordsPath(), fl.getStopwordFormat());
|
||||||
|
@ -53,14 +66,28 @@ public class WordCloudManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean createWordCloud() {
|
public boolean createWordCloud() {
|
||||||
if(wcm.insertWordsIntoTemplate(tp.tokenizingFile(tp.formatToText(fl.getFilePath(),fl.getFileFormat())))) {
|
if(!(fl.getFilePath() == null)) {
|
||||||
fl = null;
|
if (wcm.insertWordsIntoTemplate(tp.tokenizingFile(tp.formatToText(fl.getFilePath(), fl.getFileFormat())))) {
|
||||||
tp = null;
|
resetComponents();
|
||||||
fl = new FileLoader();
|
return true;
|
||||||
tp = new TextProcessing();
|
}
|
||||||
return true;
|
}
|
||||||
|
if(!urll.getUrlPath().isEmpty()) {
|
||||||
|
if(wcm.insertWordsIntoTemplate(tp.tokenizingFile(urll.loadURLContent()))) {
|
||||||
|
resetComponents();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void resetComponents() {
|
||||||
|
fl = null;
|
||||||
|
tp = null;
|
||||||
|
urll = null;
|
||||||
|
fl = new FileLoader();
|
||||||
|
urll = new URLContentLoader();
|
||||||
|
tp = new TextProcessing();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,11 +36,22 @@ public class TUI {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case(1):
|
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;
|
break;
|
||||||
case(2):
|
case(2):
|
||||||
//Exit
|
//Exit
|
||||||
isRunning = false;
|
isRunning = false;
|
||||||
|
scan.close();
|
||||||
System.out.println("Close Program!");
|
System.out.println("Close Program!");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -58,13 +69,14 @@ public class TUI {
|
||||||
// Load stopwords file
|
// Load stopwords file
|
||||||
if(wcm.loadFile()) {
|
if(wcm.loadFile()) {
|
||||||
if(wcm.loadStopwords()) {
|
if(wcm.loadStopwords()) {
|
||||||
System.out.println("File loaded successful!");
|
System.out.println("File loaded successful!\n");
|
||||||
}
|
}
|
||||||
else{
|
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;
|
break;
|
||||||
case(1):
|
case(1):
|
||||||
|
@ -92,7 +104,7 @@ public class TUI {
|
||||||
if(wcm.createWordCloud()) {
|
if(wcm.createWordCloud()) {
|
||||||
System.out.println("HTML File created!\n");
|
System.out.println("HTML File created!\n");
|
||||||
fMenu = false;
|
fMenu = false;
|
||||||
System.out.println("Close Program!");
|
System.out.println("Close Program!\n");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
System.out.println("HTML FIle not created!\n");
|
System.out.println("HTML FIle not created!\n");
|
||||||
|
|
Loading…
Reference in New Issue