From e899dfba8b02ba8c8a77087315fcaafda517a552 Mon Sep 17 00:00:00 2001 From: Grimlix Date: Fri, 28 Feb 2020 13:50:32 +0100 Subject: [PATCH 01/11] Premier commit test Application --- .../src/main/java/ch/heigvd/res/labio/impl/Application.java | 1 + 1 file changed, 1 insertion(+) 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..f7eebec 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 @@ -90,6 +90,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). */ + Petit penis LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); From 3a5e5cf36e94e99fffba540309f4ead85550dd5e Mon Sep 17 00:00:00 2001 From: Grimlix Date: Fri, 28 Feb 2020 13:55:54 +0100 Subject: [PATCH 02/11] Change in fetchAndStore --- .../src/main/java/ch/heigvd/res/labio/impl/Application.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 f7eebec..5ed5b0f 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 @@ -90,7 +90,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). */ - Petit penis + client.storeQuote(quote, WORKSPACE_DIRECTORY); LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); From 869b2c01a4b7e504ff6ce926f3f1dcc002e4690e Mon Sep 17 00:00:00 2001 From: Grimlix Date: Fri, 28 Feb 2020 14:11:06 +0100 Subject: [PATCH 03/11] fix of storeQuote() --- .../src/main/java/ch/heigvd/res/labio/impl/Application.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 5ed5b0f..bc22398 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 @@ -90,7 +90,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). */ - client.storeQuote(quote, WORKSPACE_DIRECTORY); + storeQuote(quote, WORKSPACE_DIRECTORY); LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); @@ -124,6 +124,7 @@ 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."); } From eb21d2c885cc0250ca03e424bbeb3ce5cbb64efe Mon Sep 17 00:00:00 2001 From: Grimlix Date: Mon, 2 Mar 2020 19:03:51 +0100 Subject: [PATCH 04/11] Passing the first two test of ApplicationTest --- .../ch/heigvd/res/labio/impl/Application.java | 47 ++++++++++++++++--- .../labio/impl/explorers/DFSFileExplorer.java | 7 ++- .../transformers/CompleteFileTransformer.java | 4 +- 3 files changed, 47 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 bc22398..ddd0b5c 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,12 +7,14 @@ 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.nio.charset.StandardCharsets; + +import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; + +import ch.heigvd.res.labio.quotes.TagsGenerator; import org.apache.commons.io.FileUtils; /** @@ -90,7 +92,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). */ - storeQuote(quote, WORKSPACE_DIRECTORY); + String filename = "/quote-" + i + ".utf8"; + storeQuote(quote, filename); LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); @@ -119,13 +122,38 @@ void clearOutputDirectory() throws IOException { * - with quote.getQuote(), it has access to the text of the quote. It stores * this text in UTF-8 file. * - * @param quote the quote object, with tags and text + * @param quote the quote object, with tags a + * nd text * @param filename the name of the file to create and where to store the quote text * @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 + File.separator; + File actualDir = new File(path); + + int tagNum = quote.getTags().size(); + //itbLOG.info(String.valueOf(tagNum)); + if(tagNum != 0){ + for(String tag : quote.getTags()){ + path += tag + File.separator; + actualDir = new File(path); + + //LOG.info(actualDir.getPath()); + + if(actualDir.isDirectory() == false){ + actualDir.mkdirs(); + } + } + } + + File newFile = new File(actualDir, filename); + LOG.info(newFile.getPath()); + FileWriter fileWriter = new FileWriter(newFile); + fileWriter.write(quote.getQuote()); + fileWriter.close(); + + //throw new UnsupportedOperationException("The student has not implemented this method yet."); } /** @@ -142,6 +170,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("\n" + file.getPath()); + }catch(IOException exception){ + System.out.println("Enable to write path of the file"); + } } }); } 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..7f40c6d 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 @@ -16,7 +16,12 @@ 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); + //throw new UnsupportedOperationException("The student has not implemented this method yet."); + + } } 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..53b2328 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 @@ -15,9 +15,7 @@ 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 From 89b397f727cc5740bfbd624ea729fd2f0facde58 Mon Sep 17 00:00:00 2001 From: Grimlix Date: Mon, 2 Mar 2020 19:16:19 +0100 Subject: [PATCH 05/11] Commented + cleaned the code ==> TODO: UpperCaseFilter --- .../ch/heigvd/res/labio/impl/Application.java | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 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 ddd0b5c..224807d 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 @@ -8,13 +8,10 @@ import ch.heigvd.res.labio.quotes.QuoteClient; import ch.heigvd.res.labio.quotes.Quote; -import java.nio.charset.StandardCharsets; - import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; -import ch.heigvd.res.labio.quotes.TagsGenerator; import org.apache.commons.io.FileUtils; /** @@ -86,12 +83,6 @@ 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). - */ String filename = "/quote-" + i + ".utf8"; storeQuote(quote, filename); LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); @@ -133,27 +124,24 @@ void storeQuote(Quote quote, String filename) throws IOException { File actualDir = new File(path); int tagNum = quote.getTags().size(); - //itbLOG.info(String.valueOf(tagNum)); if(tagNum != 0){ for(String tag : quote.getTags()){ + + //creating the new directory with the tag path += tag + File.separator; actualDir = new File(path); - //LOG.info(actualDir.getPath()); - + //if there is already this directory, it is not necessary to create a new one if(actualDir.isDirectory() == false){ actualDir.mkdirs(); } } } + //Adding the quote utf8 file File newFile = new File(actualDir, filename); - LOG.info(newFile.getPath()); FileWriter fileWriter = new FileWriter(newFile); fileWriter.write(quote.getQuote()); - fileWriter.close(); - - //throw new UnsupportedOperationException("The student has not implemented this method yet."); } /** @@ -172,8 +160,8 @@ public void visit(File file) { */ try { writer.write("\n" + file.getPath()); - }catch(IOException exception){ - System.out.println("Enable to write path of the file"); + } catch (IOException e) { + e.printStackTrace(); } } }); From 117d12d2bef86c13c8b0a4d73875008083e754b6 Mon Sep 17 00:00:00 2001 From: Grimlix Date: Wed, 4 Mar 2020 17:50:16 +0100 Subject: [PATCH 06/11] UpperCaseFilterWriterTest passe, clean CompleteFileTransformer --- .../java/ch/heigvd/res/labio/impl/Application.java | 7 ++++++- .../res/labio/impl/explorers/DFSFileExplorer.java | 3 +++ .../labio/impl/filters/UpperCaseFilterWriter.java | 13 ++++++++++--- .../impl/transformers/CompleteFileTransformer.java | 6 ++++-- 4 files changed, 23 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 224807d..27ca137 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 @@ -142,6 +142,10 @@ void storeQuote(Quote quote, String filename) throws IOException { File newFile = new File(actualDir, filename); FileWriter fileWriter = new FileWriter(newFile); fileWriter.write(quote.getQuote()); + + fileWriter.flush(); + fileWriter.close(); + } /** @@ -159,7 +163,8 @@ public void visit(File file) { * be pretty easy (we want to write the filename, including the path, to the writer passed in argument). */ try { - writer.write("\n" + file.getPath()); + writer.write(file.getPath()); + writer.write(System.lineSeparator()); } catch (IOException e) { e.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 7f40c6d..d347840 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 @@ -18,6 +18,9 @@ public class DFSFileExplorer implements IFileExplorer { public void explore(File rootDirectory, IFileVisitor vistor) { + + + vistor.visit(rootDirectory); //throw new UnsupportedOperationException("The student has not implemented this method yet."); 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..72b16b7 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,24 @@ 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."); + this.out.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."); + //Changing in capital letter + for(int i = off; i < cbuf.length; i++){ + cbuf[i] = Character.toUpperCase(cbuf[i]); + } + this.out.write(cbuf, off, len); } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + + this.out.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 53b2328..651f3b2 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; /** @@ -22,8 +25,7 @@ public Writer decorateWithFilters(Writer writer) { * 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; + return new FileNumberingFilterWriter(new UpperCaseFilterWriter(writer)); } } From f1e0f1d2fa0272289e4a82cd66a4ed2b3eae3312 Mon Sep 17 00:00:00 2001 From: Grimlix Date: Wed, 4 Mar 2020 19:11:03 +0100 Subject: [PATCH 07/11] Passing DFSFileExplorerTest --- .../labio/impl/explorers/DFSFileExplorer.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 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 d347840..3112915 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,7 @@ import ch.heigvd.res.labio.interfaces.IFileExplorer; import ch.heigvd.res.labio.interfaces.IFileVisitor; + import java.io.File; /** @@ -9,22 +10,23 @@ * exploration of the file system and invokes the visitor for every encountered * node (file and directory). When the explorer reaches a directory, it visits all * files in the directory and then moves into the subdirectories. - * - * @author Olivier Liechti + * + * @author Olivier LiechtiA */ public class DFSFileExplorer implements IFileExplorer { - @Override - public void explore(File rootDirectory, IFileVisitor vistor) { - - - - - - vistor.visit(rootDirectory); - //throw new UnsupportedOperationException("The student has not implemented this method yet."); + @Override + public void explore(File rootDirectory, IFileVisitor vistor) { + vistor.visit(rootDirectory); + //if there is no directory in root, we just visit root. + if (rootDirectory.exists()) { + File[] files = rootDirectory.listFiles(); + for (File file : files) { + explore(file, vistor); + } + } + } - } } From 4371eccf655e82cec272cbf2dbbbb348e1fe924a Mon Sep 17 00:00:00 2001 From: Grimlix Date: Wed, 4 Mar 2020 19:58:01 +0100 Subject: [PATCH 08/11] Passing FileTransformerTest + change in DFS --- .../res/labio/impl/explorers/DFSFileExplorer.java | 5 ++++- .../labio/impl/transformers/FileTransformer.java | 14 ++++++++------ .../impl/transformers/NoOpFileTransformer.java | 9 +-------- 3 files changed, 13 insertions(+), 15 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 3112915..7370143 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 @@ -23,7 +23,10 @@ public void explore(File rootDirectory, IFileVisitor vistor) { if (rootDirectory.exists()) { File[] files = rootDirectory.listFiles(); for (File file : files) { - explore(file, vistor); + if(file.isDirectory()){ + explore(file, vistor); + } + } } } 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..0471898 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 @@ -53,12 +53,14 @@ public void visit(File file) { Writer writer = new OutputStreamWriter(new FileOutputStream(file.getPath()+ ".out"), "UTF-8"); // the bug fix by teacher writer = decorateWithFilters(writer); - /* - * There is a missing piece here: you have an input reader and an ouput writer (notice how the - * writer has been decorated by the concrete subclass!). You need to write a loop to read the - * characters and write them to the writer. - */ - + if(reader.ready()){ + char[] buf = new char[255]; + int length; + while((length = reader.read(buf)) != -1){ + writer.write(buf, 0, length); + } + } + reader.close(); writer.flush(); writer.close(); 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..880c065 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,7 @@ 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 83696e03d93c61a767daf2f4c0352c773fc0d8f6 Mon Sep 17 00:00:00 2001 From: Grimlix Date: Thu, 5 Mar 2020 14:38:59 +0100 Subject: [PATCH 09/11] Passing UtilsTest --- .../java/ch/heigvd/res/labio/impl/Utils.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 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 c8a3a5a..d28add1 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 @@ -12,15 +12,32 @@ public class Utils { /** * This method looks for the next new line separators (\r, \n, \r\n) to extract - * the next line in the string passed in arguments. - * + * the next line in the string passed in arguments. + * * @param lines a string that may contain 0, 1 or more lines * @return an array with 2 elements; the first element is the next line with * the line separator, the second element is the remaining text. If the argument does not * 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 = new String[2]; + String str = ""; + for (int i = 0; i < lines.length(); i++) { + str += lines.charAt(i); + LOG.info(str); + if (lines.charAt(i) == '\r' && ((i + 1) != lines.length()) && lines.charAt(i + 1) == '\n') { + continue; + } else if (lines.charAt(i) == '\r' || lines.charAt(i) == '\n') { + result[0] = str; + result[1] = lines.substring(i + 1); + break; + } else if (i == lines.length() - 1) { + result[0] = ""; + result[1] = str; + } + } + return result; + } } From 5daf216fd0a177d955edcd157288b6e89fcfaca5 Mon Sep 17 00:00:00 2001 From: Grimlix Date: Sun, 8 Mar 2020 17:28:15 +0100 Subject: [PATCH 10/11] Passing FileNumeringFilterWriterTest --- .../java/ch/heigvd/res/labio/impl/Utils.java | 1 - .../filters/FileNumberingFilterWriter.java | 59 +++++++++++++++++-- 2 files changed, 55 insertions(+), 5 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 d28add1..7ac0434 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 @@ -26,7 +26,6 @@ public static String[] getNextLine(String lines) { for (int i = 0; i < lines.length(); i++) { str += lines.charAt(i); - LOG.info(str); if (lines.charAt(i) == '\r' && ((i + 1) != lines.length()) && lines.charAt(i + 1) == '\n') { continue; } else if (lines.charAt(i) == '\r' || lines.charAt(i) == '\n') { 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..e67affe 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 @@ -5,37 +5,88 @@ import java.io.Writer; import java.util.logging.Logger; +import static ch.heigvd.res.labio.impl.Utils.getNextLine; + /** * This class transforms the streams of character sent to the decorated writer. * When filter encounters a line separator, it sends it to the decorated writer. * 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 */ public class FileNumberingFilterWriter extends FilterWriter { + private int lineNumber = 0; + private boolean isNewLine = true; + private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); public FileNumberingFilterWriter(Writer out) { super(out); } + private String numering(String str, int off, int len){ + StringBuilder tmp = new StringBuilder(); + + if(isNewLine){ + lineNumber++; + tmp.append(lineNumber + "\t"); + isNewLine = false; + } + + for(int i = off; i < off + len; i++){ + tmp.append(str.charAt(i)); + if(tmp.charAt(tmp.length() - 1) == '\n' || tmp.charAt(tmp.length() - 1) == '\r' && str.charAt(i + 1) != '\n'){ + lineNumber++; + tmp.append(lineNumber + "\t"); + } + } + + + return tmp.toString(); + } + + @Override public void write(String str, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + out.write(numering(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."); + String str = String.valueOf(cbuf); + out.write(numering(str, off, len)); } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + + StringBuilder tmp = new StringBuilder(); + + if (isNewLine){ + isNewLine = false; + lineNumber++; + tmp.append(lineNumber); + tmp.append('\t'); + } + + if ((char) c == '\n'){ + lineNumber++; + tmp.append((char)c); + tmp.append(lineNumber); + tmp.append('\t'); + + } else { + tmp.append((char)c); + } + out.write(tmp.toString()); + + + } + } From 47724efe10b1dbdbe44022de1e7190ff623f69b3 Mon Sep 17 00:00:00 2001 From: Grimlix Date: Sun, 8 Mar 2020 18:21:19 +0100 Subject: [PATCH 11/11] Passing all the tests, problem was in DFSFileExplorer --- .../ch/heigvd/res/labio/impl/Application.java | 22 +++++-------------- .../labio/impl/explorers/DFSFileExplorer.java | 13 +++++------ 2 files changed, 12 insertions(+), 23 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 27ca137..41b9dcc 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 @@ -120,23 +120,19 @@ void clearOutputDirectory() throws IOException { */ void storeQuote(Quote quote, String filename) throws IOException { - String path = WORKSPACE_DIRECTORY + File.separator; + String path = WORKSPACE_DIRECTORY + "/"; File actualDir = new File(path); int tagNum = quote.getTags().size(); if(tagNum != 0){ for(String tag : quote.getTags()){ - - //creating the new directory with the tag - path += tag + File.separator; + //creating the new path + path += tag + "/"; actualDir = new File(path); - - //if there is already this directory, it is not necessary to create a new one - if(actualDir.isDirectory() == false){ - actualDir.mkdirs(); - } } } + //creating the directory with the new path + actualDir.mkdirs(); //Adding the quote utf8 file File newFile = new File(actualDir, filename); @@ -157,14 +153,8 @@ 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(System.lineSeparator()); + writer.write(file.getPath() + '\n'); } catch (IOException e) { e.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 7370143..838a547 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,7 @@ 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 @@ -19,16 +20,14 @@ public class DFSFileExplorer implements IFileExplorer { public void explore(File rootDirectory, IFileVisitor vistor) { vistor.visit(rootDirectory); - //if there is no directory in root, we just visit root. - if (rootDirectory.exists()) { + if(rootDirectory.isDirectory()){ File[] files = rootDirectory.listFiles(); - for (File file : files) { - if(file.isDirectory()){ - explore(file, vistor); - } - + Arrays.sort(files); //otherwise theApplicationShouldGenerateTheCorrectNumberOfOutputFiles() does not pass + for(File file : files){ + explore(file,vistor); } } + }