From 0cc0d1714f315fc304e2b42731c71af4c3d715bd Mon Sep 17 00:00:00 2001 From: Nicolo Buzzi Date: Sun, 8 Mar 2020 21:43:59 +0100 Subject: [PATCH 1/6] UtilsTest Passed! --- .idea/.gitignore | 2 + .../Teaching-HEIGVD-RES-2020-Labo-Java-IO.iml | 9 ++++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ LabJavaIO/.classpath | 44 +++++++++++++++++++ LabJavaIO/.factorypath | 27 ++++++++++++ LabJavaIO/.project | 23 ++++++++++ .../org.eclipse.core.resources.prefs | 4 ++ .../.settings/org.eclipse.jdt.apt.core.prefs | 4 ++ .../.settings/org.eclipse.jdt.core.prefs | 9 ++++ .../.settings/org.eclipse.m2e.core.prefs | 4 ++ .../java/ch/heigvd/res/labio/impl/Utils.java | 15 ++++++- 13 files changed, 160 insertions(+), 1 deletion(-) 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 create mode 100644 LabJavaIO/.classpath create mode 100644 LabJavaIO/.factorypath create mode 100644 LabJavaIO/.project create mode 100644 LabJavaIO/.settings/org.eclipse.core.resources.prefs create mode 100644 LabJavaIO/.settings/org.eclipse.jdt.apt.core.prefs create mode 100644 LabJavaIO/.settings/org.eclipse.jdt.core.prefs create mode 100644 LabJavaIO/.settings/org.eclipse.m2e.core.prefs diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..e7e9d11 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml 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..28a804d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ 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..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LabJavaIO/.classpath b/LabJavaIO/.classpath new file mode 100644 index 0000000..f0257c5 --- /dev/null +++ b/LabJavaIO/.classpath @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/LabJavaIO/.factorypath b/LabJavaIO/.factorypath new file mode 100644 index 0000000..bf45bee --- /dev/null +++ b/LabJavaIO/.factorypath @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/LabJavaIO/.project b/LabJavaIO/.project new file mode 100644 index 0000000..3ed70f6 --- /dev/null +++ b/LabJavaIO/.project @@ -0,0 +1,23 @@ + + + lab-java-io + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/LabJavaIO/.settings/org.eclipse.core.resources.prefs b/LabJavaIO/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..f9fe345 --- /dev/null +++ b/LabJavaIO/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/LabJavaIO/.settings/org.eclipse.jdt.apt.core.prefs b/LabJavaIO/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 0000000..687ad91 --- /dev/null +++ b/LabJavaIO/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=true +org.eclipse.jdt.apt.genSrcDir=target\\generated-sources\\annotations +org.eclipse.jdt.apt.genTestSrcDir=target\\generated-test-sources\\test-annotations diff --git a/LabJavaIO/.settings/org.eclipse.jdt.core.prefs b/LabJavaIO/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..90f7a5b --- /dev/null +++ b/LabJavaIO/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,9 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.processAnnotations=enabled +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/LabJavaIO/.settings/org.eclipse.m2e.core.prefs b/LabJavaIO/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/LabJavaIO/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 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..92d5267 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,20 @@ 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."); + + if (lines.contains("\r") && lines.contains("\n") ) { + return new String[] { lines.substring(0, lines.indexOf("\r\n") + 2), lines.substring(lines.indexOf("\r\n") + 2) }; + } + + if (lines.contains("\n")) { + return new String[] { lines.substring(0, lines.indexOf("\n") + 1), lines.substring(lines.indexOf("\n") + 1) }; + } + + if (lines.contains("\r")) { + return new String[] { lines.substring(0, lines.indexOf("\r") + 1), lines.substring(lines.indexOf("\r") + 1) }; + + } + return new String[] { "", lines }; } } From ce5b1f9d206b7d7c929c000c16b11f2923c41cca Mon Sep 17 00:00:00 2001 From: Nicolo Buzzi Date: Sun, 8 Mar 2020 22:36:11 +0100 Subject: [PATCH 2/6] UpperCaseFilterWriterTest Done! --- .../res/labio/impl/filters/UpperCaseFilterWriter.java | 6 +++--- 1 file changed, 3 insertions(+), 3 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..6b4f251 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,17 @@ 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."); + 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().toCharArray(), 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 03c46d5f68fb7a7b03774ec60ff3d34180171dac Mon Sep 17 00:00:00 2001 From: Nicolo Buzzi Date: Sun, 8 Mar 2020 23:44:47 +0100 Subject: [PATCH 3/6] FileNumberingFilterWriterTest done! --- .../filters/FileNumberingFilterWriter.java | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 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..c3150d6 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 @@ -18,24 +18,50 @@ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); + private Integer lineC = 0; + private boolean endWordR = false; public FileNumberingFilterWriter(Writer out) { super(out); } + private void addLine() throws IOException + { + String toSend = String.valueOf(++lineC) + '\t'; + super.write(toSend, 0, toSend.length()); + } + @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 pos = off; pos < off + len;pos++) { + this.write(str.charAt(pos)); + } } @Override public void write(char[] cbuf, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + this.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."); + if (lineC == 0 || lineC == null) { + this.addLine(); + } + + if (c == '\n') { + super.write(c); + this.addLine(); + } else { + if (endWordR) + this.addLine(); + super.write(c); + } + if (c == '\r') { + endWordR = true; + }else { + endWordR = false; + } } } From 55d68b5bbaea2be5413af8ed4772faf87ff9e52e Mon Sep 17 00:00:00 2001 From: Nicolo Buzzi Date: Sun, 8 Mar 2020 23:56:44 +0100 Subject: [PATCH 4/6] CompleteFileTransformerTest done! --- .../labio/impl/transformers/CompleteFileTransformer.java | 8 +++++--- .../res/labio/impl/transformers/FileTransformer.java | 5 ++++- 2 files changed, 9 insertions(+), 4 deletions(-) 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..5168ae1 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,6 @@ 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 +16,17 @@ 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..eb05e83 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 @@ -58,7 +58,10 @@ 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 c; + while ((c = reader.read()) != -1) { + writer.write(c); + } reader.close(); writer.flush(); writer.close(); From a28782cd0e1da88f174de85fb8e70c0805d4e797 Mon Sep 17 00:00:00 2001 From: Nicolo Buzzi Date: Mon, 9 Mar 2020 00:07:29 +0100 Subject: [PATCH 5/6] FileTransformerTest done! --- .../res/labio/impl/transformers/NoOpFileTransformer.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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..e0651e6 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; } } From fe388c525b0821378dfa1202bc952185e59d3bfd Mon Sep 17 00:00:00 2001 From: Nicolo Buzzi Date: Mon, 9 Mar 2020 00:45:57 +0100 Subject: [PATCH 6/6] DFS & Application done! --- .../ch/heigvd/res/labio/impl/Application.java | 34 +++++++++++++------ .../labio/impl/explorers/DFSFileExplorer.java | 24 ++++++++++--- 2 files changed, 43 insertions(+), 15 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 ad87a7d..6be7def 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,7 @@ 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.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.FileUtils; @@ -90,6 +87,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, String.format("quote-%d", i)); + LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); @@ -123,7 +122,19 @@ 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."); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(WORKSPACE_DIRECTORY).append(File.separator); + + for (String tag : quote.getTags()) { + stringBuilder.append(tag).append(File.separator); + } + new File(stringBuilder.toString()).mkdirs(); + + OutputStreamWriter oswriter = new OutputStreamWriter(new FileOutputStream(stringBuilder.toString() + filename + ".utf8"), "UTF-8"); + + oswriter.write(quote.getQuote()); + oswriter.flush(); + oswriter.close(); } /** @@ -135,11 +146,14 @@ 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.toString() + '\n'); + } + catch (java.io.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 83f8e61..ab38069 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 @@ -13,10 +14,23 @@ * @author Olivier Liechti */ public class DFSFileExplorer implements IFileExplorer { + @Override + public void explore(File rootDirectory, IFileVisitor vistor) { + if (rootDirectory != null) { - @Override - public void explore(File rootDirectory, IFileVisitor vistor) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); - } - + vistor.visit(rootDirectory); + File[] childFiles = rootDirectory.listFiles(); + if (childFiles != null){ + Arrays.sort(childFiles); + for (File f : childFiles) { + if (f.isDirectory()) { + explore(f, vistor); + } else + vistor.visit(f); + } + } else { return; } + } else { + return; + } + } }