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] all tests pass #42

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
36 changes: 19 additions & 17 deletions LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
import ch.heigvd.res.labio.interfaces.IFileVisitor;
import ch.heigvd.res.labio.quotes.QuoteClient;
import ch.heigvd.res.labio.quotes.Quote;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;

/**
*
* @author Olivier Liechti
* @author Olivier Liechti, Dupont Maxime
*/
public class Application implements IApplication {

Expand Down Expand Up @@ -84,12 +82,7 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException {
QuoteClient client = new QuoteClient();
for (int i = 0; i < numberOfQuotes; i++) {
Quote quote = client.fetchQuote();
/* 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 Down Expand Up @@ -123,7 +116,16 @@ 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.");
StringBuilder pathBuilder = new StringBuilder(WORKSPACE_DIRECTORY);
for(String tag : quote.getTags()){
pathBuilder.append('/').append(tag); // tags drole , fun : path */drole/fun
}
//reste à ajouter le nom du fichier à notre stringbuilder :
pathBuilder.append('/').append(filename);

File temp = new File(pathBuilder.toString());
FileUtils.writeStringToFile(temp,quote.getQuote());
}

/**
Expand All @@ -135,11 +137,11 @@ void printFileNames(final Writer writer) {
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).
*/
try {
writer.write(file.toString() + '\n');
}catch (IOException ex){
ex.printStackTrace();
}
}
});
}
Expand Down
22 changes: 18 additions & 4 deletions LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,29 @@ public class Utils {

/**
* This method looks for the next new line separators (\r, \n, \r\n) to extract
* the next line in the string passed in arguments.
*
* the next line in the string passed in arguments.
* @param lines a string that may contain 0, 1 or more lines
* @return an array with 2 elements; the first element is the next line with
* the line separator, the second element is the remaining text. If the argument does not
* 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.");
//throw new UnsupportedOperationException("The student has not implemented this method yet.");
String[] nextLine = new String[2];
for(int i = 0; i < lines.length(); ++i) {
if (i + 1 < lines.length() && lines.charAt(i + 1) == '\n' && lines.charAt(i) == '\r') {
nextLine[0] = lines.substring(0, i + 2);
nextLine[1] = lines.substring(i + 2);
return nextLine;
}
if (lines.charAt(i) == '\n' || lines.charAt(i) == '\r') {
nextLine[0] = lines.substring(0, i + 1);
nextLine[1] = lines.substring(i + 1);
return nextLine;
}
}
nextLine[0] = "";
nextLine[1] = lines;
return nextLine;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
import ch.heigvd.res.labio.interfaces.IFileExplorer;
import ch.heigvd.res.labio.interfaces.IFileVisitor;
import java.io.File;
import java.nio.file.Files;
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
* @author Olivier Liechti, Dupont Maxime
*/
public class DFSFileExplorer implements IFileExplorer {

@Override
public void explore(File rootDirectory, IFileVisitor vistor) {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
}
vistor.visit(rootDirectory); // on visite le dossier actuel, donc DFS pre-ordre, nous assure que chaque fichier est visité
File[] subFiles = rootDirectory.listFiles(); // on liste les sous-fichiers

//On va procéder de manière récursive, donc tout d'abord le cas basique
if(subFiles == null){
return; //basique, si on a pas de sous fichiers, on s'arrete
}
Arrays.sort(subFiles);
//ensuite la partie récursive
for (File f :subFiles){
explore(f,vistor);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,61 @@
*
* Hello\n\World -> 1\Hello\n2\tWorld
*
* @author Olivier Liechti
* @author Olivier Liechti, Dupont Maxime
*/
public class FileNumberingFilterWriter extends FilterWriter {

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

private int lineCount = 0;
private char lastChar = '\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.");
//throw new UnsupportedOperationException("The student has not implemented this method yet.");
this.write(str.toCharArray(), 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.");
//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.");
//throw new UnsupportedOperationException("The student has not implemented this method yet.");
//Tout le traitement se fait ici, les deux autres méthodes se contentent de rediriger
String result = "";

//On commence :
if(lastChar == '\0'){
result += ++lineCount + "\t";
result +=((char) c);
}else if (c == '\r'){
lastChar = (char) c;
return;
}else if( lastChar == '\r'){
result += lastChar;
if( c == '\n'){
result +=(char) c ;
result += ++lineCount + "\t";
}else{
result += ++lineCount + "\t";
result += (char) c;
}
}else if (c == '\n'){
result += ((char) c);
result += ++lineCount + "\t";
}else {
result +=((char) c);
}
out.write(result);
lastChar = (char) c;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
*
* @author Olivier Liechti
* @author Olivier Liechti, Dupont Maxime
*/
public class UpperCaseFilterWriter extends FilterWriter {

Expand All @@ -16,17 +16,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.");
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.");
super.write(new String(cbuf).toUpperCase(), 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,5 +1,8 @@
package ch.heigvd.res.labio.impl.transformers;

import ch.heigvd.res.labio.impl.filters.FileNumberingFilterWriter;
import ch.heigvd.res.labio.impl.filters.UpperCaseFilterWriter;

import java.io.Writer;

/**
Expand All @@ -9,22 +12,13 @@
* generate an output file with 1) uppercase letters and 2) line numbers at the
* beginning of each line.
*
* @author Olivier Liechti
* @author Olivier Liechti,Dupont Maxime
*/
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 @@ -53,11 +53,12 @@ public void visit(File file) {
Writer writer = new OutputStreamWriter(new FileOutputStream(file.getPath()+ ".out"), "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 buffSize = 4096;
char[] buff = new char[buffSize];
int length;
while((length = reader.read(buff,0,buffSize)) != -1){
writer.write(buff,0,length);
}

reader.close();
writer.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ 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;
}

}