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] Junod #29

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
7962798
Init commit New branch
JunodChristophe Mar 5, 2021
5418010
Code pour passer InvokeApplication() de ApplicationTest
JunodChristophe Mar 5, 2021
5f04833
Changement dans le code pour passer tout les test de ApplicationTest
JunodChristophe Mar 5, 2021
1b82110
Implémentation dans le code pour passer le test de itShouldBePossible…
JunodChristophe Mar 11, 2021
37bebc3
Implémentation dans le code pour passer le test de itShouldBePossible…
JunodChristophe Mar 11, 2021
c86ecc2
Implémentation pour passer le test itShouldWorkWhenWritingAString()
JunodChristophe Mar 11, 2021
ec213f9
Implémentation pour passer le test itShouldWorkWhenWritingACharArray()
JunodChristophe Mar 11, 2021
be108c7
Implémentation pour passer le test itShouldWorkWhenWritingASingleChar()
JunodChristophe Mar 11, 2021
cec2f71
Implémentation pour passer le test itShouldDuplicateATextFile()
JunodChristophe Mar 11, 2021
5ddb8c2
Implémentation pour passer le test itShouldPrintANumberForFileWithOne…
JunodChristophe Mar 12, 2021
6860549
Implémentation pour passer le test itShouldHandleWriteWithAnInt()
JunodChristophe Mar 12, 2021
fb43f3b
Refactoring et compléter une fonction non implémenté
JunodChristophe Mar 12, 2021
5f40189
Correction d'une fonction write() mal implémentée
JunodChristophe Mar 12, 2021
081aee0
Correction de la fonction explorer(File rootDirectory, IFileVisitor v…
JunodChristophe Mar 12, 2021
7460cc2
Correction du chemin vers le répertoire des citations.
JunodChristophe Mar 12, 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
42 changes: 26 additions & 16 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,10 +9,7 @@
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.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -92,12 +89,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-" + Integer.toString(i));
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 +125,25 @@ 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.");
// Build the complete path.
String path = WORKSPACE_DIRECTORY + "/" + String.join("/", quote.getTags()) + "/" + filename + ".utf8";

// This create both directories and file
File file = new File(path);
file.getParentFile().mkdirs();
file.createNewFile();

try (
// Try with resource to automatically close everything.
// Open and manage file.
FileOutputStream fos = new FileOutputStream(file);
// Set encoding
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
// Write to the file.
BufferedWriter writer = new BufferedWriter(osw);
) {
writer.write(quote.getQuote());
}
}

/**
Expand All @@ -145,11 +155,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.getPath()+"\n");
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
Expand Down
12 changes: 11 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 @@ -20,7 +20,17 @@ 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.");
String[] s = new String[2];
// For Linux or Windows
int index = lines.indexOf("\n");
if (index == -1) { // If nothing search for MacOS
index = lines.indexOf("\r");
}
index++; // The line separator is included.
// If index was -1 then it becomes 0 which leads to an empty subString for s[0].
s[0] = lines.substring(0,index);
s[1] = lines.substring(index); // Remaining after the new line
return s;
}

}
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 @@ -17,7 +18,18 @@ 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);
if (rootDirectory.isDirectory()) { // Explore the directory
File[] content = rootDirectory.listFiles(); // get content directory
if (content != null) { // if not empty
Arrays.sort(content); // sort in alphabetic order.
for (File file : content) { // for each files/directories
explore(file, vistor); // recursion
}
}
}

}

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

import ch.heigvd.res.labio.impl.Utils;

import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.logging.Logger;

/**
Expand All @@ -18,24 +21,70 @@
public class FileNumberingFilterWriter extends FilterWriter {

private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName());
private int countLine = 1; // To count the number of line processed.

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.");
// Process preparation
String[] subStr = new String[2];
subStr[1] = str.substring(off,off+len);
StringBuilder resultStr = new StringBuilder();

/* There are 2 situations where we implement the line number.
* For the first line and when there is a line separator. */
if (countLine == 1) { // For the first line.
resultStr.append(getNumberingString());
}
subStr = Utils.getNextLine(subStr[1]);
while(!subStr[0].isEmpty()) { // Check if there is a line separator.
resultStr.append(subStr[0]).append(getNumberingString());
subStr = Utils.getNextLine(subStr[1]); // Check for another line separator.
}
resultStr.append(subStr[1]); // The remaining string

this.out.write(resultStr.toString());
}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
// call to the function Write(String str,int off, int len) for the same process.
this.write(String.valueOf(cbuf), off, len);
}

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
boolean sendString = false; // Check if there are more than 1 character to process.
StringBuilder resultStr = new StringBuilder(); // In case there are more than 1 character.

if (countLine == 1) { // For the first line.
resultStr.append(getNumberingString());
sendString = true;
}
resultStr.append(Character.toString(c));

// Catch end of line based on OS.
char separator = System.lineSeparator().charAt(System.lineSeparator().length()-1);
if (Character.toString(c).equals(Character.toString(separator))) {
resultStr.append(getNumberingString());
sendString = true;
}

if (sendString) {
this.out.write(resultStr.toString());
} else {
this.out.write(c);
}
}

/**
* Return the number of line and a tabulation.
* @return a simple String containing the line count and a tabulation.
*/
private String getNumberingString() {
return countLine++ + "\t";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
this.out.write(str.substring(off,off+len).toUpperCase());
}

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

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
this.out.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 @@ -15,16 +18,7 @@ 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 @@ -47,11 +47,7 @@ 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.
*/
reader.transferTo(writer);

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;
}

}