diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java index ad87a7d..824b74c 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java @@ -7,10 +7,8 @@ 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.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.FileUtils; @@ -90,6 +88,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 + ".utf8"); LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); @@ -123,7 +122,18 @@ 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."); + String filePath = WORKSPACE_DIRECTORY; + + for (String tag : quote.getTags()) + filePath += "/" + tag; + + File file = new File(filePath); + file.mkdirs(); + + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath + "/" + filename), "UTF-8"); + outputStreamWriter.write(quote.getQuote()); + outputStreamWriter.flush(); + outputStreamWriter.close(); } /** @@ -140,6 +150,12 @@ 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"); + writer.flush(); + } catch (IOException exception) { + throw new RuntimeException(exception); + } } }); } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java index c8a3a5a..c6e1ff8 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java @@ -20,7 +20,22 @@ 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 result[] = new String[2]; + result[0] = ""; + result[1] = lines; + int count = 0; + + while (lines.charAt(count) != '\r' && lines.charAt(count) != '\n') + if(++count == lines.length()) + return result; + + if(count < (lines.length() - 1) && lines.charAt(count + 1) == '\n') + count++; + + result[0] = lines.substring(0, ++count); + result[1] = lines.substring(count); + + return result; } } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java index 83f8e61..b74ee7d 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java @@ -3,6 +3,9 @@ import ch.heigvd.res.labio.interfaces.IFileExplorer; import ch.heigvd.res.labio.interfaces.IFileVisitor; import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.logging.Level; /** * This implementation of the IFileExplorer interface performs a depth-first @@ -16,7 +19,22 @@ public class DFSFileExplorer implements IFileExplorer { @Override public void explore(File rootDirectory, IFileVisitor vistor) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + vistor.visit(rootDirectory); + + if(!rootDirectory.exists()) + return; + + File[] files = rootDirectory.listFiles(); + + Arrays.sort(files); + + for (File file : + files) { + if(file.isFile()) + vistor.visit(file); + if(file.isDirectory()) + explore(file, vistor); + } } } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java index 976c946..2c636aa 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java @@ -19,23 +19,38 @@ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); + private char previousCharacter = '\0', + currentCharacter = '\0'; + private int counter = 0; + 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++) + 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."); + currentCharacter = (char) c; + if (previousCharacter == '\0' || (currentCharacter != '\n' && previousCharacter == '\r')) + out.write(++counter + "\t" + currentCharacter); + else + out.write(currentCharacter); + + if(currentCharacter == '\n') + out.write(++counter + "\t"); + + previousCharacter = currentCharacter; } } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java index 0f41a5d..e806ac4 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java @@ -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."); + str = str.toUpperCase(); + super.write(str, 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 character = 0; character < (off + len); ++character) + cbuf[character] = Character.toUpperCase(cbuf[character]); + 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)); } } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformer.java index 4beca48..319f0ac 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformer.java @@ -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; /** @@ -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; } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/FileTransformer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/FileTransformer.java index 18e3f14..e612549 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/FileTransformer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/FileTransformer.java @@ -58,6 +58,8 @@ 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 (reader.ready()) + writer.write(reader.read()); reader.close(); writer.flush(); diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java index 5971a30..e0651e6 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java @@ -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; } } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileExplorer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileExplorer.java index 7d387e7..1443208 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileExplorer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileExplorer.java @@ -1,6 +1,7 @@ package ch.heigvd.res.labio.interfaces; import java.io.File; +import java.io.IOException; /** * This interface is used to perform one operation on each element (file and diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileVisitor.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileVisitor.java index b0abe27..73010a5 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileVisitor.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileVisitor.java @@ -1,6 +1,7 @@ package ch.heigvd.res.labio.interfaces; import java.io.File; +import java.io.IOException; /** * This interface is used together with the IFileExplorer interface. It defines