Skip to content
Open
Show file tree
Hide file tree
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
31 changes: 26 additions & 5 deletions LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@
import ch.heigvd.res.labio.quotes.QuoteClient;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.io.*;
import java.net.URISyntaxException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Olivier Liechti
* Modifed by Blanc Jean-Luc
*/
public class Application implements IApplication {

Expand Down Expand Up @@ -98,6 +97,7 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException {
* 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 Down Expand Up @@ -133,7 +133,22 @@ void clearOutputDirectory() throws IOException {
* @throws IOException
*/
void storeQuote(Quote quote, String filename) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
//throw new UnsupportedOperationException("The student has not implemented this method yet.");

List<String> tags = quote.getTags();
String quoteText = quote.getQuote();

String pathToRep = WORKSPACE_DIRECTORY;
for(String tag : tags){
pathToRep += "/" + tag;
}
File fileDir = new File(pathToRep);
fileDir.mkdirs();

pathToRep += "/" + filename;
FileWriter fileWriter = new FileWriter(pathToRep);
fileWriter.write(quoteText);
fileWriter.close();
}

/**
Expand All @@ -150,6 +165,12 @@ public void visit(File file) {
* 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).
*/
try{
writer.write(file.getPath() + "\n");
}
catch(IOException e){
e.printStackTrace();
}
}
});
}
Expand Down
25 changes: 24 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,8 @@
/**
*
* @author Olivier Liechti
*
* Modified by Blanc Jean-Luc
*/
public class Utils {

Expand All @@ -20,7 +22,28 @@ 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.");

//First we want to know the index of the line separators
int indexR = lines.indexOf("\r");//MAC OS
int indexN = lines.indexOf("\n");//UNIX
int indexRN = lines.indexOf("\r\n");//WINDOWS

//Then we want to check if those indexes exist or not,
// if the index = -1 then the line separator is not in the "lines" string
// Once we know if they exist, we want to start forming our return element String[]
// and actually return it if the line separator exists
// we return something too if we don't find any line separators
//here we add 2 to the index because the line separator is 2 characters
if(indexRN != -1)
return new String[]{lines.substring(0, indexRN + 2), lines.substring(indexRN + 2)};

if(indexR != -1)
return new String[]{lines.substring(0, indexR + 1), lines.substring(indexR + 1)};

if(indexN != -1)
return new String[]{lines.substring(0, indexN + 1), lines.substring(indexN + 1)};

return new String[]{"", lines};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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
Expand All @@ -12,12 +13,32 @@
* files in the directory and then moves into the subdirectories.
*
* @author Olivier Liechti
*
* Modified by Blanc Jean-Luc
*/
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) {

//Checking that rootDirectory does exist before going further
if(rootDirectory == null){
return;
}

//if it exists, then we can visit it
visitor.visit(rootDirectory);

//if the file is actually a directory, then we need to go further in
if(rootDirectory.isDirectory()){
//we create an array that contains the directory content, we then sort this content in order to display it
File[] folderContent = rootDirectory.listFiles();
Arrays.sort(folderContent);
//adding recursive here
for(File file : folderContent){
explore(file, visitor);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,59 @@
* It then sends the line number and a tab character, before resuming the write
* process.
*
* Hello\n\World -> 1\Hello\n2\tWorld
* Hello\n\World -> 1\tHello\n2\tWorld
*
* @author Olivier Liechti
* Modified by Blanc Jean-Luc
*/
public class FileNumberingFilterWriter extends FilterWriter {

private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName());
private int counter = 0;
private int lastC = '\0';

public FileNumberingFilterWriter(Writer out) {
super(out);
}

@Override
public void write(String str, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
for(int i = off; i < len+off; i++){
write(str.charAt(i));
}
}

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

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
//here we want to check if we are on the first line, if that's not the case then we want to check
// the various "c" cases regarding the OS
// UNIX = \n MAC OS = \r Windows = \r\n
// after that we write down the counter, insert a tab
// then we write down the char (while not forgetting to save the last char
if(counter == 0){
out.write(++counter + "\t");
}
else if(lastC == '\r' && c != '\n'){
out.write(++counter + "\t");
}

out.write(c);

if(c == '\n' && lastC == '\r'){
out.write(++counter + "\t");
}
else if(c == '\n' && lastC != '\r'){
out.write(++counter + "\t");
}

lastC = c;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Locale;

/**
*
* @author Olivier Liechti
*
* Modified by Blanc Jean-Luc
*/

//basically here we only have to implement last class methods using "super" while making sure we put everything to upper case
public class UpperCaseFilterWriter extends FilterWriter {

public UpperCaseFilterWriter(Writer wrappedWriter) {
Expand All @@ -16,17 +21,20 @@ 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.");
super.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.");
for(int i = 0; i < cbuf.length; i++){
cbuf[i] = Character.toUpperCase(cbuf[i]);
}
super.write(cbuf, off, len);
}

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

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

import java.io.Writer;
import ch.heigvd.res.labio.impl.filters.FileNumberingFilterWriter;
import ch.heigvd.res.labio.impl.filters.UpperCaseFilterWriter;

/**
* This class returns a writer decorated with two filters: an instance of
Expand All @@ -10,21 +12,20 @@
* beginning of each line.
*
* @author Olivier Liechti
*
* Modified by Blanc Jean-Luc
*/
public class CompleteFileTransformer extends FileTransformer {

@Override
public Writer decorateWithFilters(Writer writer) {
if (true) {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
}
/*
* If you uncomment the following line (and get rid of th 3 previous lines...), you will restore the decoration
* of the writer (connected to the file. You can see that you first decorate the writer with an UpperCaseFilterWriter, which you then
* decorate with a FileNumberingFilterWriter. The resulting writer is used by the abstract class to write the characters read from the
* input files. So, the input is first prefixed with line numbers, then transformed to uppercase, then sent to the output file.f
*/
//writer = new FileNumberingFilterWriter(new UpperCaseFilterWriter(writer));
writer = new FileNumberingFilterWriter(new UpperCaseFilterWriter(writer));
return writer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* a list of filters and decorates the output writer with them.
*
* @author Olivier Liechti
*
* Modified by Blanc Jean-Luc
*/
public abstract class FileTransformer implements IFileVisitor {

Expand Down Expand Up @@ -47,11 +49,11 @@ public void visit(File file) {
Writer writer = new OutputStreamWriter(new FileOutputStream(file.getPath()+ ".out"), StandardCharsets.UTF_8); // the bug fix by teacher
writer = decorateWithFilters(writer);

/*
* There is a missing piece here: you have an input reader and an ouput writer (notice how the
* writer has been decorated by the concrete subclass!). You need to write a loop to read the
* characters and write them to the writer.
*/

int c; //character we want to read and write
while((c = reader.read()) != -1){ //-1 here means end of flux
writer.write(c);
}

reader.close();
writer.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@
* the content of the input file into the output file.
*
* @author Olivier Liechti
*
* Modified by Blanc Jean-Luc
*/
public class NoOpFileTransformer extends FileTransformer {

@Override
public Writer decorateWithFilters(Writer writer) {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
/*
* The NoOpFileTransformer does not apply any transformation of the character stream
* (no uppercase, no line number, etc.). So, we don't need to decorate the writer connected to
* the output file at all. Just uncomment the following line and get rid of the UnsupportedOperationException and
* you will be all set.
*/
//return writer;
return writer;
}

}