From 9bf78c87fa569cbb37f4b4d4db4488657b6d83cf Mon Sep 17 00:00:00 2001 From: doeni98 Date: Sun, 8 Mar 2020 10:18:31 +0100 Subject: [PATCH 1/6] DFSFileExplorer: implemented and passing test --- .../labio/impl/explorers/DFSFileExplorer.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) 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..f75268d 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,7 @@ import ch.heigvd.res.labio.interfaces.IFileExplorer; 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 @@ -11,12 +12,23 @@ * files in the directory and then moves into the subdirectories. * * @author Olivier Liechti + * modified 08.03.2020 Denis Bourqui */ 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) { + visitor.visit(rootDirectory); + if(rootDirectory.isDirectory()){ + File[] subFiles = rootDirectory.listFiles(); + Arrays.sort(subFiles); + + for (File file: subFiles) { + if(file.isDirectory()){ + explore(file, visitor); + } + } + } } } From 48f18065db5abf386f81f5586ee428b9743f88f5 Mon Sep 17 00:00:00 2001 From: doeni98 Date: Sun, 8 Mar 2020 10:40:50 +0100 Subject: [PATCH 2/6] passing ApplicationTest --- .../ch/heigvd/res/labio/impl/Application.java | 28 +++++++++++++++---- .../labio/impl/explorers/DFSFileExplorer.java | 2 ++ .../transformers/CompleteFileTransformer.java | 9 +++--- .../transformers/NoOpFileTransformer.java | 4 +-- 4 files changed, 32 insertions(+), 11 deletions(-) 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..3c76d83 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,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). */ + String filename = "quote-" + (i + 1) + ".utf8"; + storeQuote(quote, filename); LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); @@ -123,7 +123,20 @@ 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 path = WORKSPACE_DIRECTORY + "/"; + for (String tag: quote.getTags()) { + path += tag + "/"; + } + System.out.println(path); + + File quoteFile = new File(path + filename); + // creating path to quote + quoteFile.getParentFile().mkdirs(); + + //writing file + FileWriter fw = new FileWriter(quoteFile); + fw.write(quote.getQuote()); + fw.close(); } /** @@ -140,6 +153,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 exception){ + exception.printStackTrace(); + } } }); } 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 f75268d..2133631 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 @@ -26,6 +26,8 @@ public void explore(File rootDirectory, IFileVisitor visitor) { for (File file: subFiles) { if(file.isDirectory()){ explore(file, visitor); + }else{ + visitor.visit(file); } } } 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..ba39561 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,14 @@ 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/NoOpFileTransformer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java index 5971a30..b9513dd 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,14 @@ public class NoOpFileTransformer extends FileTransformer { @Override public Writer decorateWithFilters(Writer writer) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + //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; } } From 5d7b710474da86cfab289b49fa3a22560ceab974 Mon Sep 17 00:00:00 2001 From: doeni98 Date: Sun, 8 Mar 2020 10:57:32 +0100 Subject: [PATCH 3/6] Utils tests pass --- .../java/ch/heigvd/res/labio/impl/Utils.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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..82ae5b4 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,24 @@ 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 lineSeparator; + if(lines.contains("\r\n")){ + lineSeparator = "\r\n"; + }else if(lines.contains("\r")){ + lineSeparator = "\r"; + }else if(lines.contains("\n")){ + lineSeparator = "\n"; + }else{ + // no line separator + return new String[]{"", lines}; + } + + String[] linesArray = lines.split(lineSeparator); + String rest =""; + for (int i = 1; i < linesArray.length; i++) { + rest += linesArray[i] + lineSeparator; + } + return new String[]{linesArray[0] + lineSeparator, rest}; } } From a788c9e013ef9eba7d61ec06f9f716db770f8f3c Mon Sep 17 00:00:00 2001 From: doeni98 Date: Sun, 8 Mar 2020 11:17:57 +0100 Subject: [PATCH 4/6] passing test UpperCaseFilter --- .../res/labio/impl/filters/UpperCaseFilterWriter.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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..9570693 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,19 @@ 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."); + String str = new String(cbuf); + str = str.toUpperCase(); + super.write(str.toCharArray(),off,len); } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + super.write((char)Character.toUpperCase(c)); } } From 4e3efc477ba3848ba0236e2b18ab65fcae714d01 Mon Sep 17 00:00:00 2001 From: doeni98 Date: Sun, 8 Mar 2020 11:27:48 +0100 Subject: [PATCH 5/6] passing File transformer test --- .../heigvd/res/labio/impl/transformers/FileTransformer.java | 5 +++++ 1 file changed, 5 insertions(+) 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..6360ee2 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,11 @@ 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. */ + int readVal = -1; + + while ( reader.ready() && ((readVal = reader.read()) != -1 )){ + writer.write(readVal); + } reader.close(); writer.flush(); From 3bbb36c0a82a8484f76511e75594b1ad86354500 Mon Sep 17 00:00:00 2001 From: doeni98 Date: Sun, 8 Mar 2020 12:05:10 +0100 Subject: [PATCH 6/6] passing fileNumbering & all tests ok --- .../filters/FileNumberingFilterWriter.java | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) 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..1f73125 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 @@ -11,13 +11,20 @@ * It then sends the line number and a tab character, before resuming the write * process. * - * Hello\n\World -> 1\Hello\n2\tWorld + * Hello\n\World -> 1\tHello\n2\tWorld * * @author Olivier Liechti + * + * \n -> \n[number]\t + * \r -> \r[number]\t + * \r\n -> \r\n[number]\t */ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); + private int linesCounter = 1; + private boolean newLine = true; + private int charBefore = 0; public FileNumberingFilterWriter(Writer out) { super(out); @@ -25,17 +32,37 @@ 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."); + for(int i = off; i < len + off; 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 < len + off; i++){ + write(cbuf[i]); + } } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + if(newLine){ //used for first line only + writeLinePrefix(); + newLine = false; + } + if(c != '\n' && charBefore == '\r'){ + writeLinePrefix(); + } + super.write(c); + + if(c == '\n'){ + writeLinePrefix(); + } + charBefore = c; + } + public void writeLinePrefix() throws IOException { + super.write(linesCounter + "\t", 0, (int) (Math.log10(linesCounter) + 2)); + linesCounter++; } }