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] Tevaearai_Gamboni #40

Open
wants to merge 16 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
31 changes: 25 additions & 6 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,11 +9,10 @@
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.nio.charset.StandardCharsets;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -98,12 +97,12 @@ 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+1) + ".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 +132,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.");

List<String> tag_list = quote.getTags();
String tmp_path = tag_list.stream().reduce(WORKSPACE_DIRECTORY,(s1, s2) -> s1 + "/" + s2); // construct the path
File f = new File(tmp_path);
f.mkdirs(); // create folder and the potential parent folders
tmp_path += "/" + filename;

// create the file and write the quote in it
try {
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tmp_path), StandardCharsets.UTF_8))) {
writer.write(quote.getQuote());
}
}
catch (IOException e) {
throw new IOException(e);
}
}

/**
Expand All @@ -150,6 +164,11 @@ 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 ex) {
ex.printStackTrace();
}
}
});
}
Expand Down
18 changes: 16 additions & 2 deletions 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,21 @@ 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[] tab = new String[] {"", lines}; // Default case: there is no line separator
int bound;

for (int i = 0; i < lines.length(); ++i) {
if (lines.charAt(i) == '\n' || lines.charAt(i) == '\r') {
if (lines.charAt(i) == '\r' && i + 1 < lines.length() && lines.charAt(i + 1) == '\n') { // Windows case
bound = i + 2;
} else { // Mac and Unix case
bound = i + 1;
}
tab[0] = lines.substring(0, bound);
tab[1] = lines.substring(bound);
break;
}
}
return tab;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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 @@ -15,9 +17,21 @@
*/
public class DFSFileExplorer implements IFileExplorer {

/*
* We start by visiting the root directory, then we get its files.
* If there is some files, we sort them and loop through them
* to explore them recursively.
*/
@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) {
Arrays.sort(listOfFiles);
for (File listOfFile : listOfFiles) {
explore(listOfFile, visitor);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,48 @@
public class FileNumberingFilterWriter extends FilterWriter {

private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName());
private static int cnt;
private boolean backslashR = false;

public FileNumberingFilterWriter(Writer out) {
super(out);
cnt = 0;
}

@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) {
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) {
write(cbuf[i]);
}
}

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

if (cnt == 0) { // First line
out.write(++cnt + "\t");
super.write(c);
return;
} else if (c == '\n') { // Unix and Windows case
super.write(c);
out.write(++cnt + "\t");
backslashR = false;
return;
} else if (backslashR) { // Mac case
out.write(++cnt + "\t");
backslashR = false;
} else if (c == '\r') { // Potential Mac or Windows case : we must wait the next char to find out
backslashR = true;
}
super.write(c);

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,33 @@
import java.io.IOException;
import java.io.Writer;


/**
*
* @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.");
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((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,13 @@ 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 @@ -52,6 +52,13 @@ public void visit(File file) {
* 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 = new BufferedReader(reader);
writer = new BufferedWriter(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,13 @@ 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;
}

}