From 3d6fd2369f97baa3f46d115246e345e2c473da90 Mon Sep 17 00:00:00 2001 From: Quillasp Date: Fri, 5 Mar 2021 12:48:06 +0100 Subject: [PATCH 1/6] Work on Application seems finished, problem with DFS though --- .../ch/heigvd/res/labio/impl/Application.java | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 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..be553aff 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,9 @@ 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.logging.Level; import java.util.logging.Logger; @@ -98,6 +96,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, String.format("quote-%d", i)); LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); @@ -133,7 +132,24 @@ 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."); + // throw new UnsupportedOperationException("The student has not implemented this method yet."); + StringBuilder sbpath = new StringBuilder(); + sbpath.append(WORKSPACE_DIRECTORY).append("/"); + + for (String tag: quote.getTags()) + sbpath.append(tag).append("/"); + + String path = sbpath.toString(); + File f = new File(path); + f.mkdirs(); + + OutputStreamWriter oswriter = new OutputStreamWriter( + new FileOutputStream(path + filename + ".utf-8"), StandardCharsets.UTF_8 + ); + + oswriter.write(quote.getQuote()); + oswriter.flush(); + oswriter.close(); } /** @@ -150,6 +166,15 @@ 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). */ + if (file == null) + return; + + try { + writer.write(file.getPath()); + writer.write(System.lineSeparator()); + } catch (IOException ioe) { + throw new RuntimeException(); + } } }); } From 98124e7fd07b003bd47c12f618a74b3c621b666c Mon Sep 17 00:00:00 2001 From: Quillasp Date: Fri, 5 Mar 2021 12:59:17 +0100 Subject: [PATCH 2/6] Utils done, minor changes in Application, addition of compiler version in pom --- LabJavaIO/pom.xml | 1 + .../ch/heigvd/res/labio/impl/Application.java | 5 ++-- .../java/ch/heigvd/res/labio/impl/Utils.java | 26 ++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/LabJavaIO/pom.xml b/LabJavaIO/pom.xml index 6a5afb94..2b10b44e 100644 --- a/LabJavaIO/pom.xml +++ b/LabJavaIO/pom.xml @@ -55,6 +55,7 @@ org.apache.maven.plugins maven-compiler-plugin + 3.1 11 11 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 be553aff..f4551ea2 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 @@ -140,11 +140,12 @@ void storeQuote(Quote quote, String filename) throws IOException { sbpath.append(tag).append("/"); String path = sbpath.toString(); + File f = new File(path); f.mkdirs(); OutputStreamWriter oswriter = new OutputStreamWriter( - new FileOutputStream(path + filename + ".utf-8"), StandardCharsets.UTF_8 + new FileOutputStream(path + filename + ".utf8"), StandardCharsets.UTF_8 ); oswriter.write(quote.getQuote()); @@ -173,7 +174,7 @@ public void visit(File file) { writer.write(file.getPath()); writer.write(System.lineSeparator()); } catch (IOException ioe) { - throw new RuntimeException(); + throw new RuntimeException(ioe); } } }); 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..62f30490 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 @@ -10,6 +10,23 @@ public class Utils { private static final Logger LOG = Logger.getLogger(Utils.class.getName()); + /** + * découverte des lookahead et lookbehind grâce à : + * - https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html + * pour ce qui est de la structure d'une regex + * - https://www.regular-expressions.info/lookaround.html + * pour l'utilisation des lookahead et lookbehind + * - (?<=X) : si X se trouve bien avant le caractère + * - (?!X) : si X ne se trouve pas après le caractère + * - (?<=X(?!Y)) : si X se trouve avant le caractère mais qu'il n'est pas + * suivi par Y + * - lookahead et lookbehind permettent de faire des comparaisons sur les + * String sans que ceux-ci ne soient consommés : ils permettent de dire + * si le String match ou non la regexe + */ + private static final String REGEX = "(?<=(\r\n))|(?<=\n)|(?<=\r(?!\n))"; + + /** * This method looks for the next new line separators (\r, \n, \r\n) to extract * the next line in the string passed in arguments. @@ -20,7 +37,14 @@ 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."); + // throw new UnsupportedOperationException("The student has not implemented this method yet."); + String[] result = lines.split(REGEX, 2); + + if (result.length == 2) + return result; + else + return new String[]{"", result[0]}; + } } From 2da72816584c8c51d9076a8cdb797061d22d89ff Mon Sep 17 00:00:00 2001 From: Quillasp Date: Fri, 5 Mar 2021 13:05:29 +0100 Subject: [PATCH 3/6] DFSFileExplorer done --- .../labio/impl/explorers/DFSFileExplorer.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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..0355c257 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 @@ -2,6 +2,9 @@ import ch.heigvd.res.labio.interfaces.IFileExplorer; import ch.heigvd.res.labio.interfaces.IFileVisitor; +import java.util.ArrayList; +import java.util.Arrays; + import java.io.File; @@ -17,7 +20,25 @@ public class DFSFileExplorer implements IFileExplorer { @Override public void explore(File rootDirectory, IFileVisitor vistor) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + // throw new UnsupportedOperationException("The student has not implemented this method yet."); + if (rootDirectory == null) + return; + + vistor.visit(rootDirectory); + + File[] childFiles = rootDirectory.listFiles(); + + if(childFiles == null) + return; + + Arrays.sort(childFiles); + + for (File f : childFiles) { + if (f.isDirectory()) + explore(f, vistor); + else + vistor.visit(f); + } } } From 2f46bdb66a80f81d98184449dedb5ec77ee2c580 Mon Sep 17 00:00:00 2001 From: Quillasp Date: Fri, 5 Mar 2021 13:07:49 +0100 Subject: [PATCH 4/6] UpperCaseFilterWriter done --- .../res/labio/impl/filters/UpperCaseFilterWriter.java | 11 ++++++++--- 1 file changed, 8 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 0f41a5dd..dcfc790c 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,22 @@ 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."); + // throw new UnsupportedOperationException("The student has not implemented this method yet."); + out.write(str.substring(off, off + len).toUpperCase()); } @Override public void write(char[] cbuf, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + // throw new UnsupportedOperationException("The student has not implemented this method yet."); + for (int i = 0; i < len; ++i) { + out.write(Character.toUpperCase(cbuf[off + i])); + } } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + // throw new UnsupportedOperationException("The student has not implemented this method yet."); + out.write(Character.toUpperCase(c)); } } From 7eb65fa5ad91d2cdad36eef0155ca743b2a4142e Mon Sep 17 00:00:00 2001 From: Quillasp Date: Fri, 5 Mar 2021 13:24:06 +0100 Subject: [PATCH 5/6] FileNumberingFilterWriter done --- .../filters/FileNumberingFilterWriter.java | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 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..74298a44 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,64 @@ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); + private int lineNumber; + private boolean beginning; + private boolean newline; public FileNumberingFilterWriter(Writer out) { super(out); + lineNumber = 0; + beginning = true; + newline = false; } @Override public void write(String str, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + // throw new UnsupportedOperationException("The student has not implemented this method yet."); + 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."); + // throw new UnsupportedOperationException("The student has not implemented this method yet."); + for (int i = 0; i < len; ++i) + write(cbuf[off + i]); + + // ajout de la dernière ligne (assez artificiel) + write(0); + } + + /** + * Écrit un numéro de ligne suivi d'un tab à chaque nouvelle ligne + * @throws IOException si erreur à l'écriture sur le flux + */ + public void newline() throws IOException { + out.write(++lineNumber + "\t"); } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + // throw new UnsupportedOperationException("The student has not implemented this method yet."); + if (beginning) { + beginning = false; + newline(); + out.write(c); + } else { + if (c == '\r' || c == '\n') { + if (!newline) + newline = true; + + out.write(c); + } else { + if (newline) + newline(); + + newline = false; + + if (c != 0) + out.write(c); + } + } } } From 269ca5d8ec069e4017b1f2323e0cb3897dd1783f Mon Sep 17 00:00:00 2001 From: Quillasp Date: Fri, 5 Mar 2021 13:35:14 +0100 Subject: [PATCH 6/6] transformers done --- .../labio/impl/transformers/CompleteFileTransformer.java | 9 ++++++--- .../res/labio/impl/transformers/FileTransformer.java | 4 ++++ .../res/labio/impl/transformers/NoOpFileTransformer.java | 4 ++-- 3 files changed, 12 insertions(+), 5 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..b35c0005 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,16 @@ public class CompleteFileTransformer extends FileTransformer { @Override public Writer decorateWithFilters(Writer writer) { - if (true) { + /*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..60335845 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,10 @@ 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 c; + while ((c = reader.read()) != -1) + writer.write(c); 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..71477a0e 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; } }