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

[WIP] final cleaning #66

Open
wants to merge 8 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
44 changes: 28 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 @@ -7,10 +7,9 @@
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.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -84,12 +83,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, WORKSPACE_DIRECTORY);
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 +117,24 @@ 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.");
List<String> tags = quote.getTags();
String text = quote.getQuote();
String name = filename;
name += "/quotes";
for (String tag: tags) {
name+= '/'+ tag;
}
name+= "/quote-" + quote.getValue().getId() + ".utf8";
File f = new File(name);
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
if (!f.exists()) {
f.createNewFile();
}
PrintWriter writer = new PrintWriter( name, "UTF-8");
writer.println(text);
writer.close();
}

/**
Expand All @@ -135,11 +146,12 @@ 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());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
Expand Down
26 changes: 23 additions & 3 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,35 @@ 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.");
String[] result = new String[2];
int i1 = lines.indexOf("\n");
int i2 = lines.indexOf("\r");
int i3 = lines.indexOf("\r\n");
if (i1 == -1 && i2==-1 && i3==-1) { //Aucune occurence
result[0] = "";
result[1] = lines;
}
else if(i3>i1 && i3 >=i2) { // pour \r\n
result[0] = lines.substring(0, i3 + 3);
result[1] = lines.substring(i3 + 3);
}
else if (i2>i1) { // pour \r
result[0] = lines.substring(0, i2+1);
result[1] = lines.substring(i2+1);
} else { // pour \n
result[0] = lines.substring(0, i1+1);
result[1] = lines.substring(i1+1);
}
return result;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,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.");
public void explore(File rootDirectory, IFileVisitor visitor) {
visitor.visit(rootDirectory);
File[] listOfFiles = rootDirectory.listFiles();
if (listOfFiles != null) { //Si c'est un directory
for (File file : listOfFiles) {
if (file.isDirectory()) {
explore(file, visitor);
} else {
visitor.visit(file);
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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;
Expand All @@ -23,19 +25,42 @@ public FileNumberingFilterWriter(Writer out) {
super(out);
}

/* Méthodes non fonctionelles */
@Override
public void write(String str, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
String str_f = str.substring(off, off+len);
String[] next;
String result = "1\t";
next = Utils.getNextLine(str_f);
result += next[0];
int i =2;
while (next[0].indexOf("\r") != -1 || next[0].indexOf("\n") != -1) {
next = Utils.getNextLine(next[1]);
result += (i+"\t");
result += next[0];
}
super.out.write(result);
}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
String str_f = cbuf.toString().substring(off, off+len);
String[] next;
String result = "1\t";
next = Utils.getNextLine(str_f);
result += next[0];
int i =2;
while (next[0].indexOf("\r") != -1 || next[0].indexOf("\n") != -1) {
next = Utils.getNextLine(next[1]);
result += (i+"\t");
result += next[0];
}
super.out.write(result);
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ 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.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.");
char[] bufUpper = new char[cbuf.length];
for (int i=0; i<cbuf.length; ++i) {
bufUpper[i] = Character.toUpperCase(cbuf[i]);
}
super.out.write(bufUpper, off, len);
}

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
super.out.write((int)Character.toUpperCase((char)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,14 @@ 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,6 +53,14 @@ public void visit(File file) {
Writer writer = new OutputStreamWriter(new FileOutputStream(file.getPath()+ ".out"), "UTF-8"); // the bug fix by teacher
writer = decorateWithFilters(writer);


for (int i=0; i<file.length(); ++i) {
int value = reader.read();
if (value == -1) {
break;
}
writer.write(value);
}
/*
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public class NoOpFileTransformer extends FileTransformer {

@Override
public Writer decorateWithFilters(Writer writer) {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
//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;
}

}
4 changes: 4 additions & 0 deletions LabJavaIO/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Olivier Liechti shot
the sheriff, but
he round house
kicked the deputy.