From 681bc48fa28a548e51672dc0e49a8dffc629dd12 Mon Sep 17 00:00:00 2001 From: FionaGbn Date: Fri, 5 Mar 2021 15:06:32 +0100 Subject: [PATCH 01/10] test --- test.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.txt diff --git a/test.txt b/test.txt new file mode 100644 index 00000000..e69de29b From f1a7ef34cbe8b8fb30c4aff42d8149954b3febc8 Mon Sep 17 00:00:00 2001 From: Tevaearai Date: Fri, 5 Mar 2021 15:18:18 +0100 Subject: [PATCH 02/10] Add test2.txt file to test git MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sur la branche fb-dev-reb Modifications qui seront validées : nouveau fichier : test2.txt --- test2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 test2.txt diff --git a/test2.txt b/test2.txt new file mode 100644 index 00000000..3b18e512 --- /dev/null +++ b/test2.txt @@ -0,0 +1 @@ +hello world From 2e3867e7f072975cc32f67e300b9138d7d2fcfe9 Mon Sep 17 00:00:00 2001 From: Tevaearai Date: Fri, 5 Mar 2021 16:09:59 +0100 Subject: [PATCH 03/10] p --- test3.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test3.txt diff --git a/test3.txt b/test3.txt new file mode 100644 index 00000000..e69de29b From 6a6634afeeb5d563118f2e0cc9c0f81047120d15 Mon Sep 17 00:00:00 2001 From: FionaGbn Date: Fri, 5 Mar 2021 16:42:50 +0100 Subject: [PATCH 04/10] =?UTF-8?q?UpperCaseFilterWriter=20compl=C3=A9t?= =?UTF-8?q?=C3=A9,=20les=20tests=20fonctionnent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../labio/impl/filters/UpperCaseFilterWriter.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 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 0f41a5dd..741e7062 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 @@ -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)); } } From 7827d8e380b8272610cabd6279913a6a94010f07 Mon Sep 17 00:00:00 2001 From: FionaGbn Date: Sat, 6 Mar 2021 14:04:11 +0100 Subject: [PATCH 05/10] =?UTF-8?q?package=20transformers=20modifi=C3=A9,=20?= =?UTF-8?q?FileTransformerTest=20fonctionnent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../labio/impl/transformers/CompleteFileTransformer.java | 8 ++++---- .../res/labio/impl/transformers/FileTransformer.java | 7 +++++++ .../res/labio/impl/transformers/NoOpFileTransformer.java | 3 +-- 3 files changed, 12 insertions(+), 6 deletions(-) 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 4beca482..319f0ac8 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 bde833e8..31fae660 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 @@ -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(); 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 5971a302..e0651e6a 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; } } From 87688d59489bb0ef94f92ad1da43c6f62039ff5a Mon Sep 17 00:00:00 2001 From: FionaGbn Date: Fri, 12 Mar 2021 14:22:49 +0100 Subject: [PATCH 06/10] DFSFileExplorer fini, les tests fonctionnent --- .../labio/impl/explorers/DFSFileExplorer.java | 18 ++++++++++++++++-- 1 file changed, 16 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 b97c4a72..1d09042d 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 @@ -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 @@ -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); + } + } } } From fbbe20796140aaa1bcbb049e0f0b35e3cac03ee7 Mon Sep 17 00:00:00 2001 From: FionaGbn Date: Wed, 17 Mar 2021 22:41:57 +0100 Subject: [PATCH 07/10] =?UTF-8?q?classe=20Utils=20compl=C3=A9t=C3=A9e,=20t?= =?UTF-8?q?ests=20fonctionnent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ch/heigvd/res/labio/impl/Utils.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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 c8a3a5ad..80c6f145 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,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; + } } From 2cbf0b454ad24809e025a83add97705e38c0c231 Mon Sep 17 00:00:00 2001 From: FionaGbn Date: Fri, 19 Mar 2021 13:06:12 +0100 Subject: [PATCH 08/10] =?UTF-8?q?FileNumberFilterWriter=20compl=C3=A9t?= =?UTF-8?q?=C3=A9,=20tests=20fonctionnent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filters/FileNumberingFilterWriter.java | 32 ++++++++++++++++--- 1 file changed, 28 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 976c9462..21e85f4e 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 @@ -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); + + } } + From 2fa2f36102ec6ab685cf28b67f535c8ce6883405 Mon Sep 17 00:00:00 2001 From: Tevaearai Date: Sat, 20 Mar 2021 16:39:30 +0100 Subject: [PATCH 09/10] Application fin --- .../ch/heigvd/res/labio/impl/Application.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 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 25e835c4..d134e668 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 @@ -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; @@ -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); } } - } } @@ -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 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); + } } /** @@ -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(); + } } }); } From 2f42bd056e07ca02c8b2477727a5a1b9ca77bfd7 Mon Sep 17 00:00:00 2001 From: Tevaearai Date: Sat, 20 Mar 2021 16:47:27 +0100 Subject: [PATCH 10/10] Removed test.txt files --- test.txt | 0 test2.txt | 1 - test3.txt | 0 3 files changed, 1 deletion(-) delete mode 100644 test.txt delete mode 100644 test2.txt delete mode 100644 test3.txt diff --git a/test.txt b/test.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/test2.txt b/test2.txt deleted file mode 100644 index 3b18e512..00000000 --- a/test2.txt +++ /dev/null @@ -1 +0,0 @@ -hello world diff --git a/test3.txt b/test3.txt deleted file mode 100644 index e69de29b..00000000