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

Turning in labo01 - not all tests pass #111

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
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -82,6 +87,8 @@ public static void main(String[] args) {

@Override
public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException {
final String DEFAULT_FILENAME = "quote-",
EXTENSION = ".utf8";
clearOutputDirectory();
QuoteClient client = new QuoteClient();
for (int i = 0; i < numberOfQuotes; i++) {
Expand All @@ -92,6 +99,8 @@ 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, DEFAULT_FILENAME + i + EXTENSION);

LOG.info("Received a new joke with " + quote.getTags().size() + " tags.");
for (String tag : quote.getTags()) {
LOG.info("> " + tag);
Expand Down Expand Up @@ -125,30 +134,54 @@ 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.");
final String ENCODING = "UTF-8";
List<String> tags = quote.getTags();
String path = WORKSPACE_DIRECTORY;

//Creating the path for the direcotries that are necessary to store the quote file
for (String directory : tags)
{
path += File.separator + directory;
}

//Creating the direcotries that are necessary to store the quote file
Files.createDirectories(Paths.get(path));

//Preparing the file to create
path += File.separator + filename;

//Writing the quote in the file -> filename, with encoding -> ENCODING
Writer writer = new OutputStreamWriter(new FileOutputStream(path), ENCODING);
writer.write(quote.getQuote());
writer.flush();
writer.close();
}

/**
* This method uses a IFileExplorer to explore the file system and prints the name of each
* encountered file and directory.
*/
void printFileNames(final Writer writer) {
IFileExplorer explorer = new DFSFileExplorer();
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).
*/
}
});
}
/**
* This method uses a IFileExplorer to explore the file system and prints
* the name of each encountered file and directory.
*/
void printFileNames(final Writer writer)
{
IFileExplorer explorer = new DFSFileExplorer();
explorer.explore(new File(WORKSPACE_DIRECTORY), new IFileVisitor() {
@Override
public void visit(File file)
{
try
{
writer.write(file.getAbsolutePath());
} catch (IOException e)
{
System.out.println(e.getMessage());
}
}
});
}

@Override
public String getAuthorEmail() {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
return "[email protected]";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,68 @@ public class Utils {

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

/**
* This method looks for the next new line separators (\r, \n, \r\n) to extract
* 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.");
}
/**
* This method looks for the next new line separators (\r, \n, \r\n) to
* extract 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)
{
/*
We should be using System.lineSeparator() to be able to separate
the string in several lines independently of the system, but the unit tests
don't allow us to do so.
*/

int indexLinux = lines.indexOf('\n');
int indexMac = lines.indexOf('\r');
int realIndex = -1;

if (indexLinux > -1)
{
if (indexMac > -1)
{
if (indexMac > indexLinux)
{
realIndex = indexLinux;
}
else if (indexMac == indexLinux - 1)
{
realIndex = indexLinux;
}
else
{
realIndex = indexMac;
}
}
else
{
realIndex = indexLinux;
}
}
else if (indexMac > -1)
{
realIndex = indexMac;
}

String[] result = new String[2];
if (realIndex > -1)
{
result[0] = lines.substring(0, realIndex + 1);
result[1] = lines.substring(realIndex + 1);
}
else
{
result[0] = "";
result[1] = lines;
}

return result;
}

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

/**
* This implementation of the IFileExplorer interface performs a depth-first
Expand All @@ -12,11 +13,32 @@
*
* @author Olivier Liechti
*/
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 class DFSFileExplorer implements IFileExplorer
{
@Override
public void explore(File rootDirectory, IFileVisitor vistor)
{
//We visit each node that we encounter
vistor.visit(rootDirectory);

if (rootDirectory.isDirectory())
{
//We get the list of files and directories contained in rootDirectory and sort them
File[] files = rootDirectory.listFiles();
Arrays.sort(files);

//We first visit the files of the directory and then explore the sub-directories
for (File file : files)
{
if (file.isFile())
vistor.visit(file);
}

for (File subDirectory : files)
{
if (subDirectory.isDirectory())
explore(subDirectory, vistor);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.heigvd.res.lab01.impl.filters;

import ch.heigvd.res.lab01.impl.Utils;
import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;
Expand All @@ -17,25 +18,56 @@
*/
public class FileNumberingFilterWriter extends FilterWriter {

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

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

@Override
public void write(String str, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
}
@Override
public void write(String str, int off, int len) throws IOException
{
final String TAB = "\t";
str = str.substring(off, off + len);

String[] lines = Utils.getNextLine(str);

//if it's the first time we write, we must provide the first line number
if (linesWritten == 0)
{
String temp = ++linesWritten + TAB;
super.write(temp.toCharArray(), 0, temp.length());
}

//We consume the string until we reach the end of it
//We write new lines until we don't find any separators anymore
while (!lines[0].isEmpty())
{
String line = lines[0] + (++linesWritten) + TAB;
super.write(line.toCharArray(), 0, line.length());
lines = Utils.getNextLine(lines[1]);
}

//We write the rest of the string wich can be separatorless
if(!lines[1].isEmpty())
super.write(lines[1].toCharArray(), 0, lines[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.");
}
@Override
public void write(char[] cbuf, int off, int len) throws IOException
{
String lines = String.valueOf(cbuf);
write(lines, off, len);
}

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
}
@Override
public void write(int c) throws IOException
{
char[] buf = Character.toChars(c);
write(buf, 0, 1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,32 @@
*/
public class UpperCaseFilterWriter extends FilterWriter {

public UpperCaseFilterWriter(Writer wrappedWriter) {
super(wrappedWriter);
}
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.");
}
@Override
public void write(String str, int off, int len) throws IOException
{
//Using the write method of the UpperCaseFilterWriter class
write(str.toCharArray(), 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.");
}

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
}
@Override
public void write(char[] cbuf, int off, int len) throws IOException
{
for (int i = off; i < len + off; i++)
{
//Using the write method of the UpperCaseFilterWriter class
write(cbuf[i]);
}
}

@Override
public void write(int c) throws IOException
{
//Writing in the out attribute of the extended class.
super.write(Character.toUpperCase(c));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.heigvd.res.lab01.impl.transformers;

import ch.heigvd.res.lab01.impl.filters.FileNumberingFilterWriter;
import ch.heigvd.res.lab01.impl.filters.UpperCaseFilterWriter;
import java.io.Writer;

/**
Expand All @@ -15,16 +17,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
Loading