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] Labo-Java-IO #1

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
40 changes: 35 additions & 5 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.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -90,6 +89,7 @@ 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 @@ -123,8 +123,33 @@ 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.");
StringBuilder filepath = new StringBuilder(WORKSPACE_DIRECTORY).append("/");

// Sub-folders depending on tags
for (String tag : quote.getTags()) {
filepath.append(tag).append("/");
}

// Create file dir
File dir = new File(filepath.toString());
if (!dir.exists() && dir.mkdirs())
LOG.info(filepath + " created");

// Create file
filepath.append(filename);
File file = new File(filepath.toString());
if (!file.exists() && file.createNewFile())
LOG.info(filepath + " created");

// Write into file
Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
writer.write(quote.getQuote());
writer.flush();
writer.close();
LOG.info("Quote written");
//throw new UnsupportedOperationException("The student has not implemented this method yet.");
}


/**
* This method uses a IFileExplorer to explore the file system and prints the name of each
Expand All @@ -140,6 +165,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) {
LOG.log(Level.SEVERE, null, ex);
}
}
});
}
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
@@ -1,5 +1,6 @@
package ch.heigvd.res.labio.impl;

import java.util.Arrays;
import java.util.logging.Logger;

/**
Expand All @@ -20,7 +21,16 @@ 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[] lineSplit = lines.split("(?<=(\\r\\n))", 2);
if (lineSplit.length == 1) {
lineSplit = lines.split("(?<=([\\r\\n]))", 2);

if (lineSplit.length == 1) {
lineSplit = new String[] {"", lineSplit[0]};
}
}

return lineSplit;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import ch.heigvd.res.labio.interfaces.IFileExplorer;
import ch.heigvd.res.labio.interfaces.IFileVisitor;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

/**
* This implementation of the IFileExplorer interface performs a depth-first
Expand All @@ -15,8 +17,24 @@
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) {
File[] sub = rootDirectory.listFiles();

visitor.visit(rootDirectory);
if (sub != null) {
for (final File file : sub) {
ArrayList<File> subdirectories = new ArrayList<>();
if (!file.isDirectory()) {
visitor.visit(file);
} else {
subdirectories.add(file);
}

for (File subdirectory : subdirectories) {
explore(subdirectory, 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,6 +18,8 @@
* @author Olivier Liechti
*/
public class FileNumberingFilterWriter extends FilterWriter {
private int lineNumber = 0;
private boolean writeNewLine = true;

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

Expand All @@ -25,17 +29,46 @@ public FileNumberingFilterWriter(Writer out) {

@Override
public void write(String str, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
String[] lineSplit = Utils.getNextLine(str.substring(off, off + len));
if (writeNewLine) {
writeLineBeginning();
}

while (!lineSplit[0].equals("")) {
String newLine = lineSplit[0] + getLineBeginning();
super.write(newLine, 0, newLine.length());

lineSplit = Utils.getNextLine(lineSplit[1]);
}

super.write(lineSplit[1], 0, lineSplit[1].length());
}

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

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
if (writeNewLine && c != '\n') {
writeLineBeginning();
} else if (c == '\r' || c == '\n') {
writeNewLine = true;
}
super.write(c);
}

private void writeLineBeginning() throws IOException {
String newLine = getLineBeginning();
super.write(newLine, 0, newLine.length());
writeNewLine = false;
}

private String getLineBeginning() {
return ++lineNumber + "\t";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ 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.");
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(c));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.heigvd.res.labio.impl.transformers;

import ch.heigvd.res.labio.impl.filters.*;

import java.io.Writer;

/**
Expand All @@ -15,16 +17,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
* input files. So, the input is first prefixed with line numbers, then transformed to uppercase, then sent to the output file.
*/
//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 @@ -58,6 +58,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.
*/

while (true) {
final int c = reader.read();
if (c == -1) break;

writer.write(c);
}

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

}