From a5894d443f63dcc8ab8e189e4e7b9364e9d16888 Mon Sep 17 00:00:00 2001 From: Thierry Date: Fri, 5 Mar 2021 15:17:44 +0100 Subject: [PATCH 01/11] initial commit --- .../ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java | 2 ++ 1 file changed, 2 insertions(+) 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..1f8fc7eb 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 @@ -8,6 +8,8 @@ * * @author Olivier Liechti */ + +//initial commit public class UpperCaseFilterWriter extends FilterWriter { public UpperCaseFilterWriter(Writer wrappedWriter) { From 943f2f13f32ade8ed771b125a0d04bc2d5b4aa08 Mon Sep 17 00:00:00 2001 From: MichaelRuckstuhl Date: Fri, 12 Mar 2021 14:28:23 +0100 Subject: [PATCH 02/11] [WIP] to big push that contains modified dfs and FileNumber. Utils.java and UpperCase pass tests on intelj --- .../java/ch/heigvd/res/labio/impl/Utils.java | 30 ++++++++++++++++++- .../labio/impl/explorers/DFSFileExplorer.java | 21 ++++++++++++- .../filters/FileNumberingFilterWriter.java | 8 +++-- .../impl/filters/UpperCaseFilterWriter.java | 14 +++++++-- .../ch/heigvd/res/labio/quotes/Quote.java | 1 + 5 files changed, 67 insertions(+), 7 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..fec1be8f 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,35 @@ 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[] s = new String[2]; + s[0] = ""; + s[1] = ""; + for(int i = 0; i < lines.length(); ++i){ + s[0] += lines.substring(i, i+1); + if(lines.charAt(i) == '\r'){ + if((i + 1) < lines.length()){ + if(lines.charAt(i+1) == '\n'){ + ++i; + s[0] += lines.substring(i, i+1); + } + } + break; + } + if(lines.charAt(i) == '\n') + break; + } + if (s[0].length() < lines.length()) + s[1] = lines.substring(s[0].length()); + char c = s[0].charAt(s[0].length() - 1); + if (c != '\r' && c != '\n'){ + s[1] = s[0]; + s[0] = ""; + } + + + //throw new UnsupportedOperationException("The student has not implemented this method yet."); + return s; } } 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..72f13487 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 @@ -17,7 +17,26 @@ public class DFSFileExplorer implements IFileExplorer { @Override public void explore(File rootDirectory, IFileVisitor vistor) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + + //System.out.println("test"); + + File lFiles [] = rootDirectory.listFiles(); + + for (int i = 0; i < lFiles.length; ++i){ + //System.out.println(lFiles[i].getPath()); + //if (lFiles[i].isFile()){ + //System.out.println(lFiles[i].getPath()); + vistor.visit(lFiles[i]); + //} + + } + + for (int i = 0; i < lFiles.length; ++i){ + if(lFiles[i].isDirectory()) + explore(lFiles[i], vistor); + } + + //throw new UnsupportedOperationException("The student has not implemented this method yet."); } } 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..4f6fe1ef 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 @@ -19,13 +19,17 @@ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); + private int nbLines = 1; + private boolean lastWasRorN = true; + public FileNumberingFilterWriter(Writer out) { super(out); } @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."); } @Override @@ -35,7 +39,7 @@ public void write(char[] cbuf, int off, int len) throws IOException { @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."); } } 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..721d5a63 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,25 @@ 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."); + String newStr = str.substring(0, off); + newStr += str.substring(off, off + len).toUpperCase(); + newStr += str.substring(off + len); + System.out.println(newStr); + out.write(newStr, 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 < len; ++i) + cbuf[i + off] = Character.toUpperCase(cbuf[i + off]); + out.write(cbuf, off, len); + //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."); + out.write(Character.toUpperCase(c)); + //throw new UnsupportedOperationException("The student has not implemented this method yet."); } } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/quotes/Quote.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/quotes/Quote.java index d02b320e..0c2eee61 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/quotes/Quote.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/quotes/Quote.java @@ -1,5 +1,6 @@ package ch.heigvd.res.labio.quotes; + import lombok.Getter; import lombok.Setter; From f9aa3c4184cb53da30d48e2dff26dd243c8ccba2 Mon Sep 17 00:00:00 2001 From: MichaelRuckstuhl Date: Fri, 12 Mar 2021 15:56:53 +0100 Subject: [PATCH 03/11] [WIP] class FileNumberingFilterWriter, first write function seems to work --- .../filters/FileNumberingFilterWriter.java | 28 ++++++++++++++++--- 1 file changed, 24 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 4f6fe1ef..8d7a71db 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 @@ -19,8 +19,7 @@ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); - private int nbLines = 1; - private boolean lastWasRorN = true; + private int nbLines = 0; public FileNumberingFilterWriter(Writer out) { super(out); @@ -28,13 +27,34 @@ public FileNumberingFilterWriter(Writer out) { @Override public void write(String str, int off, int len) throws IOException { + String newStr = ""; + int newChar = 0; + if(nbLines == 0){ + newStr = "1\t"; + ++nbLines; + newChar += 2; + } + newStr += str; + for (int i = 0; i < newStr.length(); ++i){ + if(newStr.charAt(i) == '\r' || newStr.charAt(i) == '\n'){ + ++nbLines; + ++i; + if (i < newStr.length()){ + if(newStr.charAt(i) == '\n'){ + ++i; + } + } + newStr = newStr.substring(0, i) + Integer.toString(nbLines) + "\t" + newStr.substring(i); + newChar += 2; + } + } - //throw new UnsupportedOperationException("The student has not implemented this method yet."); + out.write(newStr, off, len + newChar); } @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."); } @Override From ea37e0590792813d2b0f7344dcfb5c8ce045b04e Mon Sep 17 00:00:00 2001 From: Thierry Date: Thu, 18 Mar 2021 11:37:48 +0100 Subject: [PATCH 04/11] =?UTF-8?q?impl=C3=A9mentation=20de=20FileTransforme?= =?UTF-8?q?r.java=20et=20DFSFileExplorer.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LabJavaIO/pom.xml | 4 +-- .../labio/impl/explorers/DFSFileExplorer.java | 28 ++++++++----------- .../impl/transformers/FileTransformer.java | 6 ++++ 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/LabJavaIO/pom.xml b/LabJavaIO/pom.xml index 6a5afb94..6c48572c 100644 --- a/LabJavaIO/pom.xml +++ b/LabJavaIO/pom.xml @@ -7,8 +7,8 @@ jar UTF-8 - 1.8 - 1.8 + 11 + 11 RES Lab Java IO 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 72f13487..01aeb6d5 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 @@ -17,23 +17,17 @@ public class DFSFileExplorer implements IFileExplorer { @Override public void explore(File rootDirectory, IFileVisitor vistor) { - - //System.out.println("test"); - - File lFiles [] = rootDirectory.listFiles(); - - for (int i = 0; i < lFiles.length; ++i){ - //System.out.println(lFiles[i].getPath()); - //if (lFiles[i].isFile()){ - //System.out.println(lFiles[i].getPath()); - vistor.visit(lFiles[i]); - //} - - } - - for (int i = 0; i < lFiles.length; ++i){ - if(lFiles[i].isDirectory()) - explore(lFiles[i], vistor); + vistor.visit(rootDirectory); + File[] lFiles = rootDirectory.listFiles(); + if (lFiles != null) { + for (File file : lFiles) { + if (file.isDirectory()) { + explore(file, vistor); + } + else { + vistor.visit(file); + } + } } //throw new UnsupportedOperationException("The student has not implemented this method yet."); 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..8adda7ef 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,12 @@ 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 i; + char c; + + while((i = reader.read()) != -1){ + writer.write((char) i); + } reader.close(); writer.flush(); From 9fe5bdf62c4ead45aab1ad54fad32cd407a5525b Mon Sep 17 00:00:00 2001 From: MichaelRuckstuhl Date: Thu, 18 Mar 2021 13:33:14 +0100 Subject: [PATCH 05/11] [WIP] all test pass exept application --- .../filters/FileNumberingFilterWriter.java | 29 +++++++++++++++---- .../transformers/CompleteFileTransformer.java | 11 ++++--- .../transformers/NoOpFileTransformer.java | 4 +-- 3 files changed, 33 insertions(+), 11 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 8d7a71db..9e052f7f 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 @@ -20,6 +20,8 @@ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); private int nbLines = 0; + boolean isLastIsN = false; + boolean isLastIsR = false; public FileNumberingFilterWriter(Writer out) { super(out); @@ -30,11 +32,11 @@ public void write(String str, int off, int len) throws IOException { String newStr = ""; int newChar = 0; if(nbLines == 0){ - newStr = "1\t"; + newStr = str.substring(0, off) + "1\t"; ++nbLines; newChar += 2; } - newStr += str; + newStr += str.substring(off); for (int i = 0; i < newStr.length(); ++i){ if(newStr.charAt(i) == '\r' || newStr.charAt(i) == '\n'){ ++nbLines; @@ -45,7 +47,8 @@ public void write(String str, int off, int len) throws IOException { } } newStr = newStr.substring(0, i) + Integer.toString(nbLines) + "\t" + newStr.substring(i); - newChar += 2; + newChar += 1; + newChar += Integer.toString(nbLines).length(); } } @@ -54,12 +57,28 @@ public void write(String str, int off, int len) throws IOException { @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); + write(str, off, len); } @Override public void write(int c) throws IOException { - //throw new UnsupportedOperationException("The student has not implemented this method yet."); + if(nbLines == 0){ + ++nbLines; + out.write('1'); + out.write('\t'); + } + if (isLastIsR || isLastIsN){ + if(c != '\n'){ + ++nbLines; + //out.write((char)(nbLines + '0')); + out.write(Integer.toString(nbLines)); + out.write('\t'); + } + } + isLastIsR = c == '\r'; + isLastIsN = c == '\n'; + out.write(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 4beca482..274d6e69 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) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); - } + //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 5971a302..b9513dd4 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 ce616cf07fd169f5b6c006cdfd6085e922effe8e Mon Sep 17 00:00:00 2001 From: MichaelRuckstuhl Date: Thu, 18 Mar 2021 20:20:22 +0100 Subject: [PATCH 06/11] [WIP] All tests pass, but need to increase quality code and write in quotes in files --- .../ch/heigvd/res/labio/impl/Application.java | 18 +++++++++++++++++- 1 file changed, 17 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 25e835c4..48f1b13e 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 @@ -102,6 +102,7 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException { for (String tag : quote.getTags()) { LOG.info("> " + tag); } + storeQuote(quote, "quote-" + Integer.toString(i) + ".utf8"); //added } } @@ -133,7 +134,17 @@ 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 str = WORKSPACE_DIRECTORY; + for (int i = 0; i < quote.getTags().size(); ++i){ + str += "/"; + str += quote.getTags().get(i); + } + new File(str).mkdirs(); + str += "/"; + str += filename; + new File(str).createNewFile(); + //File file = new File(str); + } /** @@ -150,6 +161,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 e) { + e.printStackTrace(); + } } }); } From 3f1a9ee3b8fe784574395c04423499cd2b752a8f Mon Sep 17 00:00:00 2001 From: MichaelRuckstuhl Date: Fri, 19 Mar 2021 15:26:50 +0100 Subject: [PATCH 07/11] [WIP] All tests pass, write in field, code cheked. Need to check if everything has been done for the lab --- .../ch/heigvd/res/labio/impl/Application.java | 29 +++- .../java/ch/heigvd/res/labio/impl/Utils.java | 85 +++++------ .../labio/impl/explorers/DFSFileExplorer.java | 2 - .../filters/FileNumberingFilterWriter.java | 132 ++++++++++-------- .../impl/filters/UpperCaseFilterWriter.java | 3 - .../transformers/CompleteFileTransformer.java | 3 + .../impl/transformers/FileTransformer.java | 2 + .../transformers/NoOpFileTransformer.java | 2 + 8 files changed, 150 insertions(+), 108 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 48f1b13e..d2c711f6 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,10 +9,7 @@ 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.util.logging.Level; import java.util.logging.Logger; @@ -97,12 +94,15 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException { * 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). + * + * => Done */ LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); } - storeQuote(quote, "quote-" + Integer.toString(i) + ".utf8"); //added + // Call storeQuote. With quote and the filename = "quote-i.utf8 + storeQuote(quote, "quote-" + Integer.toString(i) + ".utf8"); } } @@ -134,17 +134,30 @@ void clearOutputDirectory() throws IOException { * @throws IOException */ void storeQuote(Quote quote, String filename) throws IOException { + + //Create directories with with the tags + String str = WORKSPACE_DIRECTORY; for (int i = 0; i < quote.getTags().size(); ++i){ str += "/"; str += quote.getTags().get(i); } new File(str).mkdirs(); + + + //Create file and write quote in + str += "/"; str += filename; - new File(str).createNewFile(); - //File file = new File(str); + try { + FileWriter myWriter = new FileWriter(str); + myWriter.write(quote.getQuote()); + myWriter.close(); + } catch (IOException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + } } /** @@ -160,6 +173,8 @@ 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). + * + * => Done */ try { writer.write(file.getPath() + "\n"); 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 fec1be8f..22ae3b1d 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 @@ -3,52 +3,57 @@ import java.util.logging.Logger; /** - * * @author Olivier Liechti */ public class Utils { - private static final Logger LOG = Logger.getLogger(Utils.class.getName()); - - /** - * This method looks for the next new line separators (\r, \n, \r\n) to extract - * 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) { - - String[] s = new String[2]; - s[0] = ""; - s[1] = ""; - for(int i = 0; i < lines.length(); ++i){ - s[0] += lines.substring(i, i+1); - if(lines.charAt(i) == '\r'){ - if((i + 1) < lines.length()){ - if(lines.charAt(i+1) == '\n'){ - ++i; - s[0] += lines.substring(i, i+1); - } + private static final Logger LOG = Logger.getLogger(Utils.class.getName()); + + /** + * This method looks for the next new line separators (\r, \n, \r\n) to extract + * 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) { + + // String array of size 2 + String[] s = new String[2]; + s[0] = ""; + s[1] = ""; + + // Write char in the first cell of the String array until end or line separators. + for (int i = 0; i < lines.length(); ++i) { + s[0] += lines.substring(i, i + 1); + if (lines.charAt(i) == '\r') { + // Check if there is \n after \r for windows conditions. + if ((i + 1) < lines.length()) { + if (lines.charAt(i + 1) == '\n') { + ++i; + s[0] += lines.substring(i, i + 1); + } + } + break; + } + if (lines.charAt(i) == '\n') + break; } - break; - } - if(lines.charAt(i) == '\n') - break; - } - if (s[0].length() < lines.length()) - s[1] = lines.substring(s[0].length()); - char c = s[0].charAt(s[0].length() - 1); - if (c != '\r' && c != '\n'){ - s[1] = s[0]; - s[0] = ""; - } + // Write what remains in lines in the 2nd cell + if (s[0].length() < lines.length()) + s[1] = lines.substring(s[0].length()); + char c = s[0].charAt(s[0].length() - 1); - //throw new UnsupportedOperationException("The student has not implemented this method yet."); - return s; - } + // If there is only one line, first cell is empty, 2nd has the line. + if (c != '\r' && c != '\n') { + s[1] = s[0]; + s[0] = ""; + } + + return s; + } } 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 01aeb6d5..59848672 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 @@ -29,8 +29,6 @@ public void explore(File rootDirectory, IFileVisitor vistor) { } } } - - //throw new UnsupportedOperationException("The student has not implemented this method yet."); } } 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 9e052f7f..c638ca44 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 @@ -10,75 +10,95 @@ * 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 * * @author Olivier Liechti */ public class FileNumberingFilterWriter extends FilterWriter { - private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); - - private int nbLines = 0; - boolean isLastIsN = false; - boolean isLastIsR = false; + private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); - public FileNumberingFilterWriter(Writer out) { - super(out); - } + private int nbLines = 0; + boolean isLastIsN = false; + boolean isLastIsR = false; - @Override - public void write(String str, int off, int len) throws IOException { - String newStr = ""; - int newChar = 0; - if(nbLines == 0){ - newStr = str.substring(0, off) + "1\t"; - ++nbLines; - newChar += 2; + public FileNumberingFilterWriter(Writer out) { + super(out); } - newStr += str.substring(off); - for (int i = 0; i < newStr.length(); ++i){ - if(newStr.charAt(i) == '\r' || newStr.charAt(i) == '\n'){ - ++nbLines; - ++i; - if (i < newStr.length()){ - if(newStr.charAt(i) == '\n'){ - ++i; - } + + @Override + public void write(String str, int off, int len) throws IOException { + + // Creation of the new String to write + String newStr = ""; + + // Keep trace of new char + int newChar = 0; + + // Condition for first line + if (nbLines == 0) { + newStr = str.substring(0, off) + "1\t"; + ++nbLines; + newChar += 2; + } + newStr += str.substring(off); + + // Add nbLines + \t when there is a line separator + for (int i = 0; i < newStr.length(); ++i) { + if (newStr.charAt(i) == '\r' || newStr.charAt(i) == '\n') { + ++nbLines; + ++i; + if (i < newStr.length()) { + if (newStr.charAt(i) == '\n' && newStr.charAt(i-1) == '\r') { + ++i; + } + } + newStr = newStr.substring(0, i) + Integer.toString(nbLines) + "\t" + newStr.substring(i); + newChar += 1; + newChar += Integer.toString(nbLines).length(); + } } - newStr = newStr.substring(0, i) + Integer.toString(nbLines) + "\t" + newStr.substring(i); - newChar += 1; - newChar += Integer.toString(nbLines).length(); - } + + // Keep trace for write(int c) + isLastIsN = newStr.charAt(newStr.length() - 1) == '\n'; + isLastIsR = newStr.charAt(newStr.length() - 1) == '\r'; + + // Write + out.write(newStr, off, len + newChar); } - out.write(newStr, off, len + newChar); - } - - @Override - public void write(char[] cbuf, int off, int len) throws IOException { - String str = new String(cbuf); - write(str, off, len); - } - - @Override - public void write(int c) throws IOException { - if(nbLines == 0){ - ++nbLines; - out.write('1'); - out.write('\t'); + @Override + public void write(char[] cbuf, int off, int len) throws IOException { + // Simply send it to String version. + String str = new String(cbuf); + write(str, off, len); } - if (isLastIsR || isLastIsN){ - if(c != '\n'){ - ++nbLines; - //out.write((char)(nbLines + '0')); - out.write(Integer.toString(nbLines)); - out.write('\t'); - } + + @Override + public void write(int c) throws IOException { + + // First line conditions + if (nbLines == 0) { + ++nbLines; + out.write('1'); + out.write('\t'); + } + + // If last is a separator line, then write new line + // Except if next char is the 2nd is the separator line for windows condition + if (isLastIsR || isLastIsN) { + if (c != '\n') { + ++nbLines; + out.write(Integer.toString(nbLines)); + out.write('\t'); + } + } + + // Keep trace and write current char + isLastIsR = c == '\r'; + isLastIsN = c == '\n'; + out.write(c); } - isLastIsR = c == '\r'; - isLastIsN = c == '\n'; - out.write(c); - } } 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 f60b8021..73365480 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 @@ -21,7 +21,6 @@ public void write(String str, int off, int len) throws IOException { String newStr = str.substring(0, off); newStr += str.substring(off, off + len).toUpperCase(); newStr += str.substring(off + len); - System.out.println(newStr); out.write(newStr, off, len); } @@ -30,13 +29,11 @@ public void write(char[] cbuf, int off, int len) throws IOException { for (int i = 0; i < len; ++i) cbuf[i + off] = Character.toUpperCase(cbuf[i + off]); out.write(cbuf, off, len); - //throw new UnsupportedOperationException("The student has not implemented this method yet."); } @Override public void write(int c) throws IOException { out.write(Character.toUpperCase(c)); - //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 274d6e69..1656db52 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 @@ -21,11 +21,14 @@ 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 + * + * => Done */ 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 8adda7ef..da6500cf 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 @@ -51,6 +51,8 @@ public void visit(File file) { * 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. + * + * => Done */ int i; char c; 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 b9513dd4..4d460c9d 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 @@ -19,6 +19,8 @@ public Writer decorateWithFilters(Writer writer) { * (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. + * + * => Done */ return writer; } From 98af36102ec0bae17abe09aab2acdf4a19c4bea8 Mon Sep 17 00:00:00 2001 From: MichaelRuckstuhl Date: Fri, 19 Mar 2021 16:03:55 +0100 Subject: [PATCH 08/11] [WIP] new version for dfs --- .../ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java | 6 +++++- 1 file changed, 5 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 59848672..b2e8d427 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 @@ -24,7 +24,11 @@ public void explore(File rootDirectory, IFileVisitor vistor) { if (file.isDirectory()) { explore(file, vistor); } - else { + } + } + if (lFiles != null) { + for (File file : lFiles) { + if (!file.isDirectory()) { vistor.visit(file); } } From 02edf0b9a7180097d55a217aa620cbe7beb87e09 Mon Sep 17 00:00:00 2001 From: MichaelRuckstuhl Date: Mon, 22 Mar 2021 10:56:10 +0100 Subject: [PATCH 09/11] [WIP] hop with ordered lfile, dfs pass --- .../ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java | 3 +++ 1 file changed, 3 insertions(+) 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 b2e8d427..b9aa5c47 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,6 +20,8 @@ public class DFSFileExplorer implements IFileExplorer { public void explore(File rootDirectory, IFileVisitor vistor) { vistor.visit(rootDirectory); File[] lFiles = rootDirectory.listFiles(); + if (lFiles != null) + Arrays.stream(lFiles).sorted(); if (lFiles != null) { for (File file : lFiles) { if (file.isDirectory()) { From 62f02f77b27d6732ca0c14b1dce60eddba72756f Mon Sep 17 00:00:00 2001 From: MichaelRuckstuhl Date: Mon, 22 Mar 2021 11:01:53 +0100 Subject: [PATCH 10/11] [WIP] here we try again --- .../ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java | 2 +- 1 file changed, 1 insertion(+), 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 b9aa5c47..2f73a7bf 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 @@ -21,7 +21,7 @@ public void explore(File rootDirectory, IFileVisitor vistor) { vistor.visit(rootDirectory); File[] lFiles = rootDirectory.listFiles(); if (lFiles != null) - Arrays.stream(lFiles).sorted(); + Arrays.sort(lFiles); if (lFiles != null) { for (File file : lFiles) { if (file.isDirectory()) { From c03bb0f0c3afe329acf45f773f74bf4e12adebfc Mon Sep 17 00:00:00 2001 From: Thierry Date: Tue, 23 Mar 2021 19:48:09 +0100 Subject: [PATCH 11/11] removed useless variable --- .../ch/heigvd/res/labio/impl/transformers/FileTransformer.java | 1 - 1 file changed, 1 deletion(-) 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 da6500cf..9af4eeb0 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 @@ -55,7 +55,6 @@ public void visit(File file) { * => Done */ int i; - char c; while((i = reader.read()) != -1){ writer.write((char) i);