Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring of Parser.java #429

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 52 additions & 28 deletions Parser.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,66 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
* This class is thread safe.
* The Parser class takes a text @see { java.io.File } as constructor argument and allows to read it as a String.
* It also Allows to write a String literal to the file
*
*/
public class Parser {
private File file;
public synchronized void setFile(File f) {
file = f;

private final File file;

public Parser(File file){
this.file = file;
}
public synchronized File getFile() {

public File getFile() {
return file;
}

/**
* Convenience method that returns the String content of a text file including only Unicode characters in the ASCII range
* @return String containing the value of the text file
* @throws IOException
*/
public String getContent() throws IOException {
FileInputStream i = new FileInputStream(file);
String output = "";
int data;
while ((data = i.read()) > 0) {
output += (char) data;
}
return output;
return getContent(false);
}
public String getContentWithoutUnicode() throws IOException {
FileInputStream i = new FileInputStream(file);
String output = "";
int data;
while ((data = i.read()) > 0) {
if (data < 0x80) {
output += (char) data;
}
}
return output;

/**
* This method that reads a file as a text file, including the Unicode characters outside the ASCII range or not.
* @param includeAscii Optional parameter that allows to include or exclude Unicode characters in the ASCII range in the result
* @return String containing the value of the text file
* @throws IOException
*/
public String getContent(boolean includeAscii) throws IOException {
StringBuilder sb = new StringBuilder();
try(BufferedReader br = new BufferedReader(new FileReader(file))){
String line;
while ((line = br.readLine()) != null){
if (!includeAscii){
sb.append(line.chars().filter(c -> c > 0x80));
} else {
sb.append(line);
}
}
}
return sb.toString();
}

/**
* Saves the provided String to the file
* @param content The String to be saved to the file
* @throws IOException
*/
public void saveContent(String content) throws IOException {
FileOutputStream o = new FileOutputStream(file);
for (int i = 0; i < content.length(); i += 1) {
o.write(content.charAt(i));
}
try(BufferedWriter bw = new BufferedWriter(new FileWriter(file))){
bw.write(content);
}
}

}