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

[TOREVIEW] Maillard_Maillard #28

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
117dcd7
initial commit
mathias-maillard Mar 5, 2021
cd02fa6
Implemented UpperCase for single char
mathias-maillard Mar 5, 2021
966faa6
implemented UpperCase for char[]
mathias-maillard Mar 5, 2021
068d27b
All tests working for UpperCaseFilterWriter
mathias-maillard Mar 5, 2021
aa0dbd6
Utils implementation, still not working for MacOS
mathias-maillard Mar 5, 2021
26bf113
Correction of Utils with all tests working
mathias-maillard Mar 5, 2021
3d82026
First version of Application with ApplicationTests failing
mathias-maillard Mar 12, 2021
04a2187
First implementation of Application
mathias-maillard Mar 12, 2021
a9e121f
Some FileNumberingFilterWriter tests working, still needs work
mathias-maillard Mar 12, 2021
1b5122a
Implement DFSFileExplorer to pass tests
JoanMaillard Mar 22, 2021
a2a57ea
Remove trailing spaces from application.java
JoanMaillard Mar 22, 2021
94aeed3
FileNumberingFilterWriter now passes tests.
JoanMaillard Mar 23, 2021
920606b
Correction FileNumberingFilterWriter
JoanMaillard Mar 23, 2021
3ca5d61
Activation CompleteFileTransformer code
JoanMaillard Mar 23, 2021
ce60607
Implementation FileTransformer class missing piece
JoanMaillard Mar 23, 2021
05f928e
Remove UnsupportedOperationException of already-supported FileTransfo…
JoanMaillard Mar 23, 2021
98aa4d2
Adjusted DFSFileExplorer to sort content files. Removed useless import.
JoanMaillard Mar 23, 2021
d4ebf99
Application now prints correct filenames
mathias-maillard Mar 23, 2021
b0834bc
All tests working!
mathias-maillard Mar 23, 2021
22cecd2
Code cleanup
mathias-maillard Mar 23, 2021
36dafc3
Removed useless piece of code
mathias-maillard Mar 23, 2021
ddff313
Retrying to pass unit tests
mathias-maillard Mar 23, 2021
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
79 changes: 45 additions & 34 deletions LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
import java.io.StringWriter;
import java.io.Writer;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
*
* @author Olivier Liechti
Expand All @@ -28,52 +33,52 @@ public class Application implements IApplication {
* to where the Java application is invoked.
*/
public static String WORKSPACE_DIRECTORY = "./workspace/quotes";

private static final Logger LOG = Logger.getLogger(Application.class.getName());

public static void main(String[] args) {

/*
* I prefer to have LOG output on a single line, it's easier to read. Being able
* to change the formatting of console outputs is one of the reasons why it is
* better to use a Logger rather than using System.out.println
*/
System.setProperty("java.util.logging.SimpleFormatter.format", "%4$s: %5$s%6$s%n");


int numberOfQuotes = 0;
try {
numberOfQuotes = Integer.parseInt(args[0]);
} catch (Exception e) {
System.err.println("The command accepts a single numeric argument (number of quotes to fetch)");
System.exit(-1);
}

Application app = new Application();
try {
/*
* Step 1 : clear the output directory
*/
app.clearOutputDirectory();

/*
* Step 2 : use the QuotesClient to fetch quotes; store each quote in a file
*/
app.fetchAndStoreQuotes(numberOfQuotes);

/*
* Step 3 : use a file explorer to traverse the file system; print the name of each directory and file
*/
Writer writer = new StringWriter(); // we create a special writer that will send characters into a string (memory)
app.printFileNames(writer); // we hand over this writer to the printFileNames method
LOG.info(writer.toString()); // we dump the whole result on the console

/*
* Step 4 : process the quote files, by applying 2 transformations to their content
* (convert to uppercase and add line numbers)
*/
app.processQuoteFiles();

} catch (IOException ex) {
LOG.log(Level.SEVERE, "Could not fetch quotes. {0}", ex.getMessage());
ex.printStackTrace();
Expand All @@ -92,12 +97,7 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException {
e.printStackTrace();
}
if (quote != null) {
/* There is a missing piece here!
* As you can see, this method handles the first part of the lab. It uses the web service
* client to fetch quotes. We have removed a single line from this method. It is a call to
* one method provided by this class, which is responsible for storing the content of the
* quote in a text file (and for generating the directories based on the tags).
*/
storeQuote(quote, "quote-" + i + ".utf8");
LOG.info("Received a new joke with " + quote.getTags().size() + " tags.");
for (String tag : quote.getTags()) {
LOG.info("> " + tag);
Expand All @@ -106,36 +106,45 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException {

}
}

/**
* This method deletes the WORKSPACE_DIRECTORY and its content. It uses the
* apache commons-io library. You should call this method in the main method.
*
* @throws IOException
*
* @throws IOException
*/
void clearOutputDirectory() throws IOException {
FileUtils.deleteDirectory(new File(WORKSPACE_DIRECTORY));
FileUtils.deleteDirectory(new File(WORKSPACE_DIRECTORY));
}

/**
* This method stores the content of a quote in the local file system. It has
* 2 responsibilities:
*
* 2 responsibilities:
*
* - with quote.getTags(), it gets a list of tags and uses
* it to create sub-folders (for instance, if a quote has three tags "A", "B" and
* "C", it will be stored in /quotes/A/B/C/quotes-n.utf8.
*
*
* - with quote.getQuote(), it has access to the text of the quote. It stores
* this text in UTF-8 file.
*
*
* @param quote the quote object, with tags and text
* @param filename the name of the file to create and where to store the quote text
* @throws IOException
* @throws IOException
*/
void storeQuote(Quote quote, String filename) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
List<String> tags = quote.getTags();
String folder_name = "/";
for (String tag : tags) {
folder_name += tag + "/";
}

String quote_text = quote.getQuote();
File my_quote = new File("./workspace/quotes/"+folder_name + filename);
FileUtils.writeStringToFile(my_quote, quote_text, "UTF-8");

}

/**
* This method uses a IFileExplorer to explore the file system and prints the name of each
* encountered file and directory.
Expand All @@ -144,20 +153,22 @@ void printFileNames(final Writer writer) {
IFileExplorer explorer = new DFSFileExplorer();
explorer.explore(new File(WORKSPACE_DIRECTORY), new IFileVisitor() {
@Override
public void visit(File file) {
/*
* There is a missing piece here. Notice how we use an anonymous class here. We provide the implementation
* of the the IFileVisitor interface inline. You just have to add the body of the visit method, which should
* be pretty easy (we want to write the filename, including the path, to the writer passed in argument).
*/
public void visit(File file) {

try {
writer.write(file.getPath()+System.lineSeparator());
}
catch(IOException e) {
System.err.println("Invalid File");
}
}
});
}

@Override
public void processQuoteFiles() throws IOException {
IFileExplorer explorer = new DFSFileExplorer();
explorer.explore(new File(WORKSPACE_DIRECTORY), new CompleteFileTransformer());
explorer.explore(new File(WORKSPACE_DIRECTORY), new CompleteFileTransformer());
}

}
28 changes: 27 additions & 1 deletion LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/**
*
* @author Olivier Liechti
* Modified by Joan Maillard and Mathias Maillard
*/
public class Utils {

Expand All @@ -20,7 +21,32 @@ public class Utils {
* contain any line separator, then the first element is an empty string.
*/
public static String[] getNextLine(String lines) {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
char lsMac = '\r';
char lsUnix = '\n';
String lsWindows = "\r\n";
String[] result = new String[2];
int index;
//Check for \r\n
if ( (index = lines.indexOf(lsWindows)) != -1) {
result[0] = lines.substring(0, index+2);
result[1] = lines.substring(index+2);
}
//check for \r
else if ((index = lines.indexOf(lsMac)) != -1) {
result[0] = lines.substring(0, index+1);
result[1] = lines.substring(index+1);
}
//check for \n
else if ((index = lines.indexOf(lsUnix)) != -1) {
result[0] = lines.substring(0, index+1);
result[1] = lines.substring(index+1);
}
//No line separators
else {
result[0] = "";
result[1] = lines;
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,36 @@
import ch.heigvd.res.labio.interfaces.IFileVisitor;

import java.io.File;
import java.util.Arrays;

/**
* This implementation of the IFileExplorer interface performs a depth-first
* exploration of the file system and invokes the visitor for every encountered
* node (file and directory). When the explorer reaches a directory, it visits all
* files in the directory and then moves into the subdirectories.
*
*
* @author Olivier Liechti
*
* Modified by Joan Maillard and Mathias Maillard
*/
public class DFSFileExplorer implements IFileExplorer {

@Override
public void explore(File rootDirectory, IFileVisitor vistor) {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
public void explore(File rootDirectory, IFileVisitor visitor) {
// Avoid errors if file is null
if(rootDirectory == null){
return;
}
// Visit file
visitor.visit(rootDirectory);
// Check if file is dir
if (rootDirectory.isDirectory()) {
// if so is the case, list all files inside and explore within
File[] dirContentFiles = rootDirectory.listFiles();
Arrays.sort(dirContentFiles);
for (File dirFile : dirContentFiles) {
explore(dirFile, visitor);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ch.heigvd.res.labio.impl.filters;

import ch.heigvd.res.labio.impl.Utils;
import jdk.jshell.execution.Util;

import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;
Expand All @@ -14,28 +17,69 @@
* Hello\n\World -> 1\Hello\n2\tWorld
*
* @author Olivier Liechti
*
* Modified by Joan Maillard and Mathias Maillard
*/
public class FileNumberingFilterWriter extends FilterWriter {

private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName());
private int count = 1;
private boolean hadAnR;
private boolean newLine;

public FileNumberingFilterWriter(Writer out) {
super(out);
hadAnR = true;
newLine = true;
}

private void writeLineNumToken() throws IOException {
if(newLine) {
newLine = false;
out.write(count + "\t");
count++;
}
}

@Override
public void write(String str, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
String lines[] = Utils.getNextLine(str.substring(off, off+len));
while (!lines[0].equals("")) {
writeLineNumToken();
out.write(lines[0]);
newLine = true;
lines = Utils.getNextLine(lines[1]);
}
writeLineNumToken();
out.write(lines[1]);
}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
write(new String(cbuf), off, len);
}

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
if (c != '\n' && hadAnR) {
writeLineNumToken();
}
out.write(c);
if (c == '\n') {
if (!hadAnR) {
newLine = true;
writeLineNumToken();
}
else {
writeLineNumToken();
}
}
if (c == '\r') {
hadAnR = true;
newLine = true;
}
else {
hadAnR = false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/**
*
* @author Olivier Liechti
* Modified by Joan Maillard and Mathias Maillard
*/
public class UpperCaseFilterWriter extends FilterWriter {

Expand All @@ -16,17 +17,17 @@ public UpperCaseFilterWriter(Writer wrappedWriter) {

@Override
public void write(String str, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
out.write(str.toUpperCase(), off, len);
}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
out.write(String.copyValueOf(cbuf).toUpperCase(), off, len);
}

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
out.write(Character.toUpperCase((char) c));
}

}
Loading