From 372255adb8e0b8d71a2a65e155aa612a880b93de Mon Sep 17 00:00:00 2001 From: HayPimp Date: Fri, 15 Mar 2019 15:46:30 +0100 Subject: [PATCH 1/6] Version fonctionnelle Utilis.java --- .../java/ch/heigvd/res/labio/impl/Utils.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java index c8a3a5a..4557410 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,19 @@ 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[] arrOfStr = new String[2]; + int i = 0; + if(lines.contains("\r") || lines.contains("\n")) { + while(lines.charAt(i) != '\r' && lines.charAt(i) != '\n'){i++;}i++; + if(lines.contains("\r\n"))++i; + arrOfStr[0] = lines.substring(0, i); + arrOfStr[1] = lines.substring(i, lines.length()); + } + else{ + arrOfStr[0] = ""; + arrOfStr[1] = lines; + } + return arrOfStr; + } } From 671735303108b280a380c6ad5b0a5031e8319579 Mon Sep 17 00:00:00 2001 From: HayPimp Date: Fri, 15 Mar 2019 15:49:15 +0100 Subject: [PATCH 2/6] Version fonctionnelle Utilis.java --- LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 4557410..cdd6b49 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 @@ -24,7 +24,8 @@ public static String[] getNextLine(String lines) { int i = 0; if(lines.contains("\r") || lines.contains("\n")) { - while(lines.charAt(i) != '\r' && lines.charAt(i) != '\n'){i++;}i++; + while(lines.charAt(i) != '\r' && lines.charAt(i) != '\n'){i++;} + i++; if(lines.contains("\r\n"))++i; arrOfStr[0] = lines.substring(0, i); arrOfStr[1] = lines.substring(i, lines.length()); From cf7cce8aba007f24a4b3a5dc8d456b5ae2f5b37e Mon Sep 17 00:00:00 2001 From: HayPimp Date: Sun, 24 Mar 2019 16:10:15 +0100 Subject: [PATCH 3/6] UpperCase fonctionnel --- .../res/labio/impl/filters/UpperCaseFilterWriter.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 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 0f41a5d..4257853 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 @@ -9,24 +9,23 @@ * @author Olivier Liechti */ public class UpperCaseFilterWriter extends FilterWriter { - + public UpperCaseFilterWriter(Writer wrappedWriter) { super(wrappedWriter); } @Override public void write(String str, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + super.write(str.toUpperCase(),off, len); } @Override public void write(char[] cbuf, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + super.write(new String(cbuf).toUpperCase(), off, len); } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + super.write(Character.toUpperCase(c)); } - } From 2018dff5b9ed8f5400e91941fbb7b7cfd0b7dd0d Mon Sep 17 00:00:00 2001 From: HayPimp Date: Sun, 24 Mar 2019 18:24:16 +0100 Subject: [PATCH 4/6] FileNumberingFilterWriter is complete --- .../filters/FileNumberingFilterWriter.java | 43 +++++++++++++++++-- 1 file changed, 39 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 976c946..191aadb 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 @@ -1,5 +1,7 @@ package ch.heigvd.res.labio.impl.filters; +import ch.heigvd.res.labio.impl.Utils; + import java.io.FilterWriter; import java.io.IOException; import java.io.Writer; @@ -11,31 +13,64 @@ * 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 static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); + + private boolean begin = true; + private boolean wasEscape = false; + private int compteur = 0; + 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."); + String[] array = new String[]{new String("EMPTY"), new String("EMPTY")}; + String temp = new String(); + array[1] = str.substring(off, off+len); + int i = 0; + + if(begin){ + begin = false; + temp += ++compteur + "\t"; + } + + if(!array[1].contains("\n") && !array[1].contains("\r")){ + temp += array[1]; + } + + while(!array[0].equals("")) { + array = Utils.getNextLine(array[1]); + if(array[0].equals("") && i > 0){ + temp += array[1]; + } + temp += array[0]; + wasEscape = temp.equals("\r") ? true : false; + if ((array[0].contains("\n") || array[0].contains("\r")) && !wasEscape) { + temp += ++compteur + "\t"; + } + i++; + } + super.write(temp, 0, temp.length()); } @Override public void write(char[] cbuf, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + write(new String(cbuf), off, len); } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + char alpha = (char)c; + write("" + alpha); } } From 6ebcd03bfd5aaf9a923b05ecbdbb06fd3b9c4950 Mon Sep 17 00:00:00 2001 From: HayPimp Date: Sun, 24 Mar 2019 18:49:50 +0100 Subject: [PATCH 5/6] DFSEXPLORER is complete --- .../res/labio/impl/explorers/DFSFileExplorer.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 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 83f8e61..208f22c 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 @@ -15,8 +15,17 @@ public class DFSFileExplorer implements IFileExplorer { @Override - public void explore(File rootDirectory, IFileVisitor vistor) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + public void explore(File rootDirectory, IFileVisitor visitor) { + if(rootDirectory != null) { + File[] listOfFilesAndDirectory = rootDirectory.listFiles(); + visitor.visit(rootDirectory); + if (listOfFilesAndDirectory != null) { + for (File file : listOfFilesAndDirectory) { + if (file.isDirectory()) { + explore(file, visitor); + } + } + } + } } - } From aa6efd3ad69f67b4f6c35f6a05bd3b47c2f7ed13 Mon Sep 17 00:00:00 2001 From: HayPimp Date: Sun, 24 Mar 2019 19:23:11 +0100 Subject: [PATCH 6/6] Labo complete with all tests passed --- .../main/java/ch/heigvd/res/labio/impl/Utils.java | 4 +++- .../impl/transformers/CompleteFileTransformer.java | 7 ++++--- .../res/labio/impl/transformers/FileTransformer.java | 12 ++++++++++-- .../labio/impl/transformers/NoOpFileTransformer.java | 5 ++--- 4 files changed, 19 insertions(+), 9 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 cdd6b49..6fe234a 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,9 @@ public static String[] getNextLine(String lines) { if(lines.contains("\r") || lines.contains("\n")) { while(lines.charAt(i) != '\r' && lines.charAt(i) != '\n'){i++;} i++; - if(lines.contains("\r\n"))++i; + if(lines.length() >= i+1) { + if (lines.substring(i-1, i + 1).contains("\r\n")) ++i; + } arrOfStr[0] = lines.substring(0, i); arrOfStr[1] = lines.substring(i, lines.length()); } 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..ee33980 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,6 +1,7 @@ package ch.heigvd.res.labio.impl.transformers; import java.io.Writer; +import ch.heigvd.res.labio.impl.filters.*; /** * This class returns a writer decorated with two filters: an instance of @@ -15,16 +16,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 18e3f14..a9b536b 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 @@ -54,11 +54,19 @@ public void visit(File file) { writer = decorateWithFilters(writer); /* - * There is a missing piece here: you have an input reader and an ouput writer (notice how the + * 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. */ - + int value; + try { + while ((value = reader.read()) != -1) + writer.write(value); + } + catch(Exception e ) + { + System.out.println("Error with the read"); + } 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..de10e5b 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java @@ -13,14 +13,13 @@ public class NoOpFileTransformer extends FileTransformer { @Override public Writer decorateWithFilters(Writer writer) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); - /* + /* * The NoOpFileTransformer does not apply any transformation of the character stream * (no uppercase, no line number, etc.). So, we don't need to decorate the writer connected to * the output file at all. Just uncomment the following line and get rid of the UnsupportedOperationException and * you will be all set. */ - //return writer; + return writer; } }