From 19d5c7790d919f3c51c4bbe5f0ee1d32fd16fe74 Mon Sep 17 00:00:00 2001 From: Tchewi Atchoum Date: Sat, 7 Mar 2020 17:18:44 +0100 Subject: [PATCH 1/2] Majuscules partout ! --- .../labio/impl/filters/UpperCaseFilterWriter.java | 15 ++++++++++++--- .../transformers/CompleteFileTransformer.java | 2 +- 2 files changed, 13 insertions(+), 4 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..d49bab2 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,26 @@ 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."); + out.write(str.substring(off, off+len).toUpperCase()); + + //throw new UnsupportedOperationException("The student has not implemented this method yet."); } @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 = off; i < off+len; i++) { + out.write(Character.toUpperCase(cbuf[i])); + } + + + //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/impl/transformers/CompleteFileTransformer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformer.java index 4beca48..52a5d4b 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 @@ -25,7 +25,7 @@ public Writer decorateWithFilters(Writer writer) { * 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 writer; } } From 5d066ab803cc6ee739efea47d8583031ff8a2b28 Mon Sep 17 00:00:00 2001 From: Tchewi Atchoum Date: Sun, 8 Mar 2020 23:56:21 +0100 Subject: [PATCH 2/2] Try to do something in time (but fail) --- .../ch/heigvd/res/labio/impl/Application.java | 38 ++++++++++++++++--- .../java/ch/heigvd/res/labio/impl/Utils.java | 24 +++++++++++- .../impl/filters/UpperCaseFilterWriter.java | 7 ---- .../transformers/CompleteFileTransformer.java | 8 ++-- .../impl/transformers/FileTransformer.java | 7 ++++ .../res/labio/interfaces/IFileVisitor.java | 3 +- 6 files changed, 69 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 ad87a7d..9913c6a 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 @@ -14,6 +14,9 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.FileUtils; +import java.util.List; +import java.io.FileWriter; +import java.io.BufferedWriter; /** * @@ -84,9 +87,13 @@ 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! + storeQuote(quote, "quote-" + quote.getValue().getId() + ".utf8"); + + + + /* There is no 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 + * client to fetch quotes. We have not 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). */ @@ -123,7 +130,25 @@ 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."); + List tags = quote.getTags(); + + String directoryName = WORKSPACE_DIRECTORY; + + for (int i = 0; i < tags.size(); i++){ + directoryName += "\\" + tags.get(i); + } + + File directory = new File(directoryName); + directory.mkdirs(); + + File name = new File(directoryName + "\\" + filename); + name.createNewFile(); + + FileWriter fw = new FileWriter(name.getAbsoluteFile()); + BufferedWriter bw = new BufferedWriter(fw); + bw.write(quote.getQuote()); + bw.close(); + } /** @@ -134,7 +159,10 @@ void printFileNames(final Writer writer) { IFileExplorer explorer = new DFSFileExplorer(); explorer.explore(new File(WORKSPACE_DIRECTORY), new IFileVisitor() { @Override - public void visit(File file) { + public void visit(File file) throws IOException { + + writer.write(file.getPath()); + /* * 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 @@ -147,7 +175,7 @@ public void visit(File file) { @Override public void processQuoteFiles() throws IOException { IFileExplorer explorer = new DFSFileExplorer(); - explorer.explore(new File(WORKSPACE_DIRECTORY), new CompleteFileTransformer()); + explorer.explore(new File(WORKSPACE_DIRECTORY), new CompleteFileTransformer()); } } 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..d3e82e2 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,29 @@ 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[] arrayReturn = new String[2]; + int index = -2; + //Y'a certainement un meilleur moyen que cette vielle répétition de code. + index = lines.indexOf("\r\n"); + if (index > -1){ + arrayReturn[0] = lines.substring(0, index); + arrayReturn[1] = lines.substring(index); + } + + index = lines.indexOf('\r'); + if (index > -1){ + arrayReturn[0] = lines.substring(0, index); + arrayReturn[1] = lines.substring(index); + } + + index = lines.indexOf('\n'); + if (index > -1){ + arrayReturn[0] = lines.substring(0, index); + arrayReturn[1] = lines.substring(index); + } + + return arrayReturn; } } 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 d49bab2..0362148 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 @@ -17,8 +17,6 @@ public UpperCaseFilterWriter(Writer wrappedWriter) { @Override public void write(String str, int off, int len) throws IOException { out.write(str.substring(off, off+len).toUpperCase()); - - //throw new UnsupportedOperationException("The student has not implemented this method yet."); } @Override @@ -26,16 +24,11 @@ public void write(char[] cbuf, int off, int len) throws IOException { for (int i = off; i < off+len; i++) { out.write(Character.toUpperCase(cbuf[i])); } - - - //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 52a5d4b..22b108d 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,13 @@ 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 * 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..82eb509 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,6 +53,13 @@ public void visit(File file) { Writer writer = new OutputStreamWriter(new FileOutputStream(file.getPath()+ ".out"), "UTF-8"); // the bug fix by teacher writer = decorateWithFilters(writer); + //REpris du cours + int octet = reader.read(); + while (octet != -1){ + writer.write(octet); + octet = reader.read(); + } + /* * 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 diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileVisitor.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileVisitor.java index b0abe27..11ddfb2 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileVisitor.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileVisitor.java @@ -1,6 +1,7 @@ package ch.heigvd.res.labio.interfaces; import java.io.File; +import java.io.IOException; /** * This interface is used together with the IFileExplorer interface. It defines @@ -18,6 +19,6 @@ public interface IFileVisitor { * * @param file the current file or directory visited by the IFileExplorer instance */ - public void visit(File file); + public void visit(File file) throws IOException; }