From 68e44a28475cfd30e755fc0dd8dad3d834210a8f Mon Sep 17 00:00:00 2001 From: GTZL1 Date: Fri, 6 Mar 2020 22:00:04 +0100 Subject: [PATCH 1/6] all tests of Application except last one --- .idea/.gitignore | 2 + .../Teaching-HEIGVD-RES-2020-Labo-Java-IO.iml | 9 +++++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ LabJavaIO/pom.xml | 8 ++++ .../ch/heigvd/res/labio/impl/Application.java | 40 +++++++++++++++---- .../labio/impl/explorers/DFSFileExplorer.java | 5 ++- 8 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/Teaching-HEIGVD-RES-2020-Labo-Java-IO.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/Teaching-HEIGVD-RES-2020-Labo-Java-IO.iml b/.idea/Teaching-HEIGVD-RES-2020-Labo-Java-IO.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/Teaching-HEIGVD-RES-2020-Labo-Java-IO.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a675ee7 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e611cf4 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LabJavaIO/pom.xml b/LabJavaIO/pom.xml index 6224422..a34d7d5 100644 --- a/LabJavaIO/pom.xml +++ b/LabJavaIO/pom.xml @@ -39,6 +39,14 @@ + + org.apache.maven.plugins + maven-compiler-plugin + + 11 + 11 + + diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java index ad87a7d..0216b44 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,10 +7,11 @@ 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.io.*; +import java.nio.file.*; +import java.util.List; +import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.FileUtils; @@ -90,6 +91,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). */ + this.storeQuote(quote,("quote-"+Integer.toString(i+1))); LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); @@ -104,7 +106,7 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException { * @throws IOException */ void clearOutputDirectory() throws IOException { - FileUtils.deleteDirectory(new File(WORKSPACE_DIRECTORY)); + FileUtils.deleteDirectory(new File(WORKSPACE_DIRECTORY)); } /** @@ -122,8 +124,32 @@ void clearOutputDirectory() throws IOException { * @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."); + void storeQuote(Quote quote, String filename) throws IOException + { + List tags=quote.getTags(); + String path=(WORKSPACE_DIRECTORY+"/"); + for(String tag:tags) + { + path+=(tag+"/"); + } + File file=new File(path); + if ((!file.exists()) && !file.mkdirs()) + { + throw new UnsupportedOperationException("Directories not created"); + } + + file=new File(path+filename+".utf8"); + file.createNewFile(); + FileWriter writer=new FileWriter(file); + writer.write(quote.getQuote()); + + file=new File(path+filename+".utf8.out"); + file.createNewFile(); + writer=new FileWriter(file); + writer.write(quote.getQuote()); + + writer.flush(); + writer.close(); } /** 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..91c8afd 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,9 @@ 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 vistor) + { + //for(File f: rootDirectory) } } From ab2c43b3d9ca02fc1ecf1e8cf53299dbbd62703a Mon Sep 17 00:00:00 2001 From: GTZL1 Date: Sat, 7 Mar 2020 21:12:03 +0100 Subject: [PATCH 2/6] ApplicationTest works --- .../ch/heigvd/res/labio/impl/Application.java | 20 +++++++++---------- .../labio/impl/explorers/DFSFileExplorer.java | 15 ++++++++++++-- .../transformers/CompleteFileTransformer.java | 3 --- .../res/labio/impl/ApplicationTest.java | 3 +-- 4 files changed, 23 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 0216b44..07fd8dc 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,12 +142,6 @@ void storeQuote(Quote quote, String filename) throws IOException file.createNewFile(); FileWriter writer=new FileWriter(file); writer.write(quote.getQuote()); - - file=new File(path+filename+".utf8.out"); - file.createNewFile(); - writer=new FileWriter(file); - writer.write(quote.getQuote()); - writer.flush(); writer.close(); } @@ -161,11 +155,15 @@ 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 except) + { + //make Java happy + } } }); } 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 91c8afd..4e01d22 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 @@ -3,6 +3,7 @@ import ch.heigvd.res.labio.interfaces.IFileExplorer; 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 @@ -17,7 +18,17 @@ public class DFSFileExplorer implements IFileExplorer { @Override public void explore(File rootDirectory, IFileVisitor vistor) { - //for(File f: rootDirectory) - } + vistor.visit(rootDirectory); + + if (rootDirectory.isDirectory()) + { + File[] allFiles = rootDirectory.listFiles(); + Arrays.sort(allFiles); + for (File f:allFiles) + { + explore(f, vistor); + } + } + } } 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..bae5781 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,6 @@ 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 diff --git a/LabJavaIO/src/test/java/ch/heigvd/res/labio/impl/ApplicationTest.java b/LabJavaIO/src/test/java/ch/heigvd/res/labio/impl/ApplicationTest.java index 354a666..3423f3e 100644 --- a/LabJavaIO/src/test/java/ch/heigvd/res/labio/impl/ApplicationTest.java +++ b/LabJavaIO/src/test/java/ch/heigvd/res/labio/impl/ApplicationTest.java @@ -146,5 +146,4 @@ public void theApplicationShouldBeAbleToGenerateTheListOfFileNames() throws IOEx assertTrue(applicationReturnsValidFilePaths); } - -} +} \ No newline at end of file From 02c66307489815c256709da3158ab8d8b666c47a Mon Sep 17 00:00:00 2001 From: GTZL1 Date: Sun, 8 Mar 2020 14:37:25 +0100 Subject: [PATCH 3/6] All tests work --- .../java/ch/heigvd/res/labio/impl/Utils.java | 37 +++++++++++++++++-- .../transformers/CompleteFileTransformer.java | 5 ++- 2 files changed, 38 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..8f82d5a 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 @@ -9,7 +9,7 @@ public class Utils { private static final Logger LOG = Logger.getLogger(Utils.class.getName()); - + private static final int NB_STRINGS_RESULT=2; /** * This method looks for the next new line separators (\r, \n, \r\n) to extract * the next line in the string passed in arguments. @@ -19,8 +19,39 @@ public class Utils { * 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."); + public static String[] getNextLine(String lines) + { + String[] result=new String[NB_STRINGS_RESULT]; + + for(int x=0;x Date: Sun, 8 Mar 2020 19:51:15 +0100 Subject: [PATCH 4/6] FileNumTrucTest pass --- .../filters/FileNumberingFilterWriter.java | 41 +++++++++++++++++-- 1 file changed, 37 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..4ed11f3 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,23 +19,56 @@ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); + private boolean firstLine; + private int nbLines; + private int previousChar; + public FileNumberingFilterWriter(Writer out) { super(out); + firstLine=true; + nbLines=0; } @Override public void write(String str, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + for(int x=off;x<(off+len);++x) + { + write(str.charAt(x)); + } } @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("In weird method"); } @Override - public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + public void write(int c) throws IOException + { + if(firstLine) + { + out.write(writeLineNum()); + firstLine=false; + } + + if(previousChar=='\r' && c !='\n') + { + out.write(writeLineNum()); + } + + out.write(c); + + if(c=='\n') + { + out.write(writeLineNum()); + } + + previousChar=c; } + private String writeLineNum() // throws IOException + { + ++nbLines; + return(String.valueOf(nbLines)+"\t"); + } } From a859a9c7608d34b977e9ceacdeab6239b8aa46b3 Mon Sep 17 00:00:00 2001 From: GTZL1 Date: Sun, 8 Mar 2020 20:09:16 +0100 Subject: [PATCH 5/6] upper transformation tests work --- .../impl/filters/UpperCaseFilterWriter.java | 32 ++++++++++++++++--- 1 file changed, 27 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..de78041 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,46 @@ * @author Olivier Liechti */ public class UpperCaseFilterWriter extends FilterWriter { - + + private final int LOWER_MIN =97; + private final int LOWER_MAX = 122; + private final int LOW_UP_GAP=32; + 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."); + for(int x=off;x<(off+len);++x) + { + out.write(convertChar(str.charAt(x))); + } } @Override - public void write(char[] cbuf, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + public void write(char[] cbuf, int off, int len) throws IOException + { + for(int x=off;x<(off+len);++x) + { + out.write(convertChar(cbuf[x])); + } } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + out.write(convertChar((char)c)); } + private char convertChar(char c) + { + if(c>=LOWER_MIN && c<=LOWER_MAX) + { + return ((char)(c-=LOW_UP_GAP)); + } + else + { + return c; + } + } } From 3c4bd65d8874a4b10dd6f5457a405255df89e79e Mon Sep 17 00:00:00 2001 From: GTZL1 Date: Sun, 8 Mar 2020 20:54:16 +0100 Subject: [PATCH 6/6] All tests work. Serious that time --- .../impl/transformers/FileTransformer.java | 17 ++++++++++++----- .../impl/transformers/NoOpFileTransformer.java | 10 ++-------- .../CompleteFileTransformerTest.java | 1 - 3 files changed, 14 insertions(+), 14 deletions(-) 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..866561d 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,11 +53,18 @@ 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. - */ + while(true) + { + int c=reader.read(); + if(c>(-1)) + { + writer.write(c); + } + else + { + break; + } + } reader.close(); writer.flush(); diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java index 5971a30..83670d4 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,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; + + return writer; } } diff --git a/LabJavaIO/src/test/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformerTest.java b/LabJavaIO/src/test/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformerTest.java index 84d7198..2aa6d86 100644 --- a/LabJavaIO/src/test/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformerTest.java +++ b/LabJavaIO/src/test/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformerTest.java @@ -38,5 +38,4 @@ public void itShouldApplyTwoTransformationsOnAFile() throws IOException { assertTrue( FileUtils.contentEquals(expectedFile, outputFile) ); FileUtils.deleteDirectory(new File("./tmp")); } - }