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 ok #61

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
39 changes: 23 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,16 +83,11 @@ 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).
*/
LOG.info("Received a new joke with " + quote.getTags().size() + " tags.");
for (String tag : quote.getTags()) {
LOG.info("> " + tag);
}
storeQuote(quote, "quote-" + i);
}
}

Expand Down Expand Up @@ -123,7 +117,19 @@ 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();
StringBuilder directory = new StringBuilder(Application.WORKSPACE_DIRECTORY + '/');

for(String tag : tags){
directory.append(tag).append('/');
}

new File(directory.toString()).mkdirs();
String filepath = directory + filename + ".utf8";

Writer writer = new OutputStreamWriter(new FileOutputStream(filepath), "UTF-8");
writer.write(quote.getQuote());
writer.close();
}

/**
Expand All @@ -135,11 +141,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() + '\n');
}
catch (java.io.IOException e){
System.out.println(e.getMessage());
}
}
});
}
Expand Down
37 changes: 36 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 @@ -9,6 +9,9 @@
public class Utils {

private static final Logger LOG = Logger.getLogger(Utils.class.getName());
private static final String WINDOWS_ENDLINE = "\r\n";
private static final String MAC_ENDLINE = "\r";
private static final String LINUX_ENDLINE = "\n";

/**
* This method looks for the next new line separators (\r, \n, \r\n) to extract
Expand All @@ -20,7 +23,39 @@ 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.");
int nextLineIndex = getNextLineIndex(lines);
String[] division = new String[2];

division[0] = lines.substring(0, nextLineIndex);
division[1] = lines.substring(nextLineIndex);

return division;
}

/**
* This method looks for the next new line separators (\r, \n, \r\n) to get the index
* of the next line.
*
* @param lines lines a string that may contain 0, 1 or more lines
* @return the index at which the next line starts
*/
private static int getNextLineIndex(String lines){
int posWindowsEndLine = lines.indexOf(WINDOWS_ENDLINE);
int posLinuxEndLine = lines.indexOf(LINUX_ENDLINE);
int posMacEndLine = lines.indexOf(MAC_ENDLINE);

if(posWindowsEndLine >= 0){
return posWindowsEndLine + WINDOWS_ENDLINE.length();
}
if(posMacEndLine >= 0){
return posMacEndLine + MAC_ENDLINE.length();
}
if(posLinuxEndLine>= 0){
return posLinuxEndLine + LINUX_ENDLINE.length();
}
return 0;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
* @author Olivier Liechti
*/
public class DFSFileExplorer implements IFileExplorer {
@Override
public void explore(File rootDirectory, IFileVisitor visitor) {
visitor.visit(rootDirectory);
File[] childFiles = rootDirectory.listFiles();

@Override
public void explore(File rootDirectory, IFileVisitor vistor) {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
if (childFiles != null) {
for (File childFile : childFiles) {
explore(childFile, visitor);
}
}
}

}
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 @@ -16,26 +18,40 @@
* @author Olivier Liechti
*/
public class FileNumberingFilterWriter extends FilterWriter {

private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName());
private int lineCount = 0;
private int prevChar = -1;

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 < off + len; ++i){
this.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 < off + len; ++i){
this.write(cbuf[i]);
}
}

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
if(lineCount == 0 || (prevChar == '\r' && c != '\n')){ //début du doc ou après séparation Mac
out.write(Integer.toString(++lineCount) + '\t');
}

out.write(c);

this.prevChar = c;
if(prevChar == '\n') { //après séparation Windows ou Unix
out.write(Integer.toString(++lineCount) + '\t');
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,30 @@
* @author Olivier Liechti
*/
public class UpperCaseFilterWriter extends FilterWriter {

public UpperCaseFilterWriter(Writer wrappedWriter) {
super(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);

/* // ALTERNATIVE
for(int i = off; i < len + off; ++i) {
this.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) {
this.write(cbuf[i]);
}
}

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
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 @@ -53,11 +53,11 @@ 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 c = reader.read();
while(c != -1){
writer.write(c);
c = reader.read();
}

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

}