From 7995d2928c87cce37ff3084133cbca435b4ab77f Mon Sep 17 00:00:00 2001 From: Damien Carnal Date: Sun, 12 Mar 2017 16:20:45 +0100 Subject: [PATCH] Rendu labo01 --- .gitignore | 2 +- .../ch/heigvd/res/lab01/impl/Application.java | 61 +++++++++----- .../java/ch/heigvd/res/lab01/impl/Utils.java | 19 ++++- .../lab01/impl/explorers/DFSFileExplorer.java | 26 +++++- .../filters/FileNumberingFilterWriter.java | 80 +++++++++++++++---- .../impl/filters/UpperCaseFilterWriter.java | 61 +++++++++----- .../transformers/CompleteFileTransformer.java | 20 ++--- .../transformers/NoOpFileTransformer.java | 16 +--- 8 files changed, 202 insertions(+), 83 deletions(-) diff --git a/.gitignore b/.gitignore index 9ae1b7b..d7ec736 100644 --- a/.gitignore +++ b/.gitignore @@ -23,7 +23,7 @@ release.properties dependency-reduced-pom.xml buildNumber.properties .mvn/timing.properties - +*.DS_Store # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 diff --git a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Application.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Application.java index b33a15c..c927747 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Application.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Application.java @@ -5,21 +5,18 @@ import ch.heigvd.res.lab01.interfaces.IApplication; import ch.heigvd.res.lab01.interfaces.IFileExplorer; import ch.heigvd.res.lab01.interfaces.IFileVisitor; -import ch.heigvd.res.lab01.quotes.QuoteClient; import ch.heigvd.res.lab01.quotes.Quote; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.StringWriter; -import java.io.Writer; -import java.util.logging.Level; -import java.util.logging.Logger; +import ch.heigvd.res.lab01.quotes.QuoteClient; import org.apache.commons.io.FileUtils; +import java.io.*; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * * @author Olivier Liechti + * @author Damien Carnal */ public class Application implements IApplication { @@ -30,6 +27,8 @@ public class Application implements IApplication { public static String WORKSPACE_DIRECTORY = "./workspace/quotes"; private static final Logger LOG = Logger.getLogger(Application.class.getName()); + + private int quoteNumber = 1; public static void main(String[] args) { @@ -86,12 +85,7 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException { QuoteClient client = new QuoteClient(); for (int i = 0; i < numberOfQuotes; i++) { Quote quote = client.fetchQuote(); - /* There is a missing piece here! - * As you can see, this method handles the first part of the lab. It uses the web service - * client to fetch quotes. We have removed a single line from this method. It is a call to - * 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, WORKSPACE_DIRECTORY); LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); @@ -125,7 +119,28 @@ 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 = filename; + List listTags = quote.getTags(); + String q = quote.getQuote(); + + new File(path).mkdir(); + path += '/'; + + for (int i = 0; i < listTags.size(); i++){ + path += listTags.get(i) + '/'; + new File(path).mkdirs(); + } + + path += "quote-" + quoteNumber + ".utf8"; + + new File(path).createNewFile(); + + try (Writer writer = new BufferedWriter(new OutputStreamWriter( + new FileOutputStream(path), "utf-8"))) + { + writer.write(q); + } + quoteNumber++; } /** @@ -137,18 +152,20 @@ void printFileNames(final Writer writer) { 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). - */ + try{ + writer.write(file.getPath()); + writer.write('\n'); + } catch (IOException e){ + e.printStackTrace(); + } + } }); } @Override public String getAuthorEmail() { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + return "random.guy@heig-vd.ch"; } @Override diff --git a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Utils.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Utils.java index ebededd..6ac6784 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Utils.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Utils.java @@ -5,6 +5,7 @@ /** * * @author Olivier Liechti + * @author Damien Carnal */ public class Utils { @@ -20,7 +21,23 @@ 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]; + + // si on a pas de nouvelle ligne on retourne la ligne actuel et un string vide + if (lines.indexOf('\n') == -1 && lines.indexOf('\r') == -1){ + result[0] = new String(""); + result[1] = lines; + } + //si il y a un caractère de saut de ligne on parse + else if (lines.contains("\n")){ + result[0] = lines.substring(0, lines.indexOf('\n') + 1); + result[1] = lines.substring(lines.indexOf('\n') + 1); + }else{ + result[0] = lines.substring(0, lines.indexOf('\r') + 1); + result[1] = lines.substring(lines.indexOf('\r') + 1); + } + + return result; } } diff --git a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/explorers/DFSFileExplorer.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/explorers/DFSFileExplorer.java index 0322ffc..7a00ffa 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/explorers/DFSFileExplorer.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/explorers/DFSFileExplorer.java @@ -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 @@ -11,12 +12,35 @@ * files in the directory and then moves into the subdirectories. * * @author Olivier Liechti + * @author Damien Carnal */ 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.isDirectory()){ + // Creation d'un tableau avec le contenu du fichier + File[] files = rootDirectory.listFiles(); + Arrays.sort(files); + + // On visite d'abord les fichier + for (File file : files){ + if (file.isFile()){ + vistor.visit(file); + } + } + + // ensuite on explore en profondeur + for (File file : files){ + if (file.isDirectory()){ + explore(file, vistor); + } + } + } + } } diff --git a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/FileNumberingFilterWriter.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/FileNumberingFilterWriter.java index 509843d..3de6062 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/FileNumberingFilterWriter.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/FileNumberingFilterWriter.java @@ -14,28 +14,78 @@ * Hello\n\World -> 1\Hello\n2\tWorld * * @author Olivier Liechti + * @author Damien Carnal */ 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()); + + + // Numéro de linge + private int counter; + private boolean firstCall; + private boolean previousWasCR; - public FileNumberingFilterWriter(Writer out) { - super(out); - } + public FileNumberingFilterWriter(Writer out) { + super(out); + counter = 1; + firstCall = true; + previousWasCR = false; + } @Override - public void write(String str, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); - } + public void write(String str, int off, int len) throws IOException { + if (str == null){ + return; + } + // regard si l'oofset sort du string + if (off + len > str.length()){ + return; + } - @Override - public void write(char[] cbuf, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); - } + String string = str.substring(off, off + len); + char[] cbuf = string.toCharArray(); + write(cbuf, 0, cbuf.length); + } - @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 { + if (cbuf == null){ + return; + } + // Envoie des caractères individuellement + for (char c : cbuf){ + write((int) c); + } + } + + @Override + public void write(int c) throws IOException { + String str; + if (firstCall){ + // ajoute le numéro et le tab en début + str = counter++ + "\t"; + super.write(str, 0, str.length()); + firstCall = false; + } + + if (c == '\n') { + str = "\n" + counter++ + "\t"; + previousWasCR = false; + super.write(str, 0, str.length()); + } else if (c == '\r') { + str = "\r"; + previousWasCR = true; + super.write(str, 0, str.length()); + } else { + // si c'est un \r on dois écrire le no de linge et le tab + if (previousWasCR){ + str = counter++ + "\t"; + super.write(str, 0, str.length()); + } + super.write(c); + previousWasCR = false; + } + } } diff --git a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/UpperCaseFilterWriter.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/UpperCaseFilterWriter.java index 1be272e..2a1c295 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/UpperCaseFilterWriter.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/UpperCaseFilterWriter.java @@ -7,26 +7,51 @@ /** * * @author Olivier Liechti + * @author Damien Carnal */ 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."); - } - - @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."); - } + public UpperCaseFilterWriter(Writer wrappedWriter) { + super(wrappedWriter); + } + + @Override + public void write(String str, int off, int len) throws IOException { + + if (str == null){ + return; + } + if (off + len > str.length()){ + return; + } + + String substring = str.substring(off, off + len); + substring = substring.toUpperCase(); + super.write(substring, 0, substring.length()); + } + + @Override + public void write(char[] cbuf, int off, int len) throws IOException { + if (cbuf == null){ + return; + } + if (off + len > cbuf.length){ + return; + } + + for (int i = off; i < off + len; i++){ + char c = cbuf[i]; + write(c); + } + } + + @Override + public void write(int c) throws IOException { + // conversion en majuscule + if (Character.isLowerCase(c)){ + c = Character.toUpperCase(c); + } + super.write(c); + } } diff --git a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/CompleteFileTransformer.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/CompleteFileTransformer.java index 86fff79..40e07e9 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/CompleteFileTransformer.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/CompleteFileTransformer.java @@ -1,5 +1,8 @@ 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; /** @@ -13,19 +16,10 @@ */ public class CompleteFileTransformer extends FileTransformer { - @Override - public Writer decorateWithFilters(Writer writer) { - if (true) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + @Override + public Writer decorateWithFilters(Writer writer) { + writer = new UpperCaseFilterWriter(new FileNumberingFilterWriter(writer)); + return writer; } - /* - * 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)); - return writer; - } } diff --git a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/NoOpFileTransformer.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/NoOpFileTransformer.java index 41670aa..5e424d9 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/NoOpFileTransformer.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/NoOpFileTransformer.java @@ -11,16 +11,8 @@ */ 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; - } - + @Override + public Writer decorateWithFilters(Writer writer) { + return writer; + } }