From dca42d30fd33ed602d9a569130d3346b4c88c83d Mon Sep 17 00:00:00 2001 From: Thierry OTTO Date: Wed, 13 Mar 2019 19:35:16 +0100 Subject: [PATCH 1/7] =?UTF-8?q?Premier=20commit=20de=20la=20branche=20dev?= =?UTF-8?q?=20avec=20modification=20du=20fichier=20qui=20ne=20fait=20aucun?= =?UTF-8?q?e=20op=C3=A9ration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../res/labio/impl/transformers/NoOpFileTransformer.java | 9 +-------- 1 file changed, 1 insertion(+), 8 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..880c065 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,7 @@ 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 f7cb5e7917615d23d58d2ce4b00ee6c574e7c41e Mon Sep 17 00:00:00 2001 From: Thierry OTTO Date: Sun, 24 Mar 2019 11:04:35 +0100 Subject: [PATCH 2/7] Modify the DFSFileExplorer --- .../labio/impl/explorers/DFSFileExplorer.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 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..24af80e 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,20 @@ 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[] files = rootDirectory.listFiles(); + visitor.visit(rootDirectory); // visits all files in the directory + + // Move into the subdirectories + if (files != null) { + for(int i = 0; i < files.length; i++) { + if (files[i].isDirectory()) + explore(files[i], visitor); //Recursion + } + } + } } } From 4e983e7d10bd31cdc56a5da0feb51dcaf0262d01 Mon Sep 17 00:00:00 2001 From: Thierry OTTO Date: Sun, 24 Mar 2019 11:21:15 +0100 Subject: [PATCH 3/7] Modify file filters --- .../filters/FileNumberingFilterWriter.java | 36 +++++++++++++++++-- .../impl/filters/UpperCaseFilterWriter.java | 6 ++-- 2 files changed, 36 insertions(+), 6 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..241d2ef 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,53 @@ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); + private int nbLine = 1; + private boolean newLine = 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."); + for (int index = off; index < (off + len); index++) + this.write(str.charAt(index)); } @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 index = off; index < (off + len); index++) + this.write(cbuf[index]); } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + + // New line + if (c == '\n') { + super.write(c); + writeLineNbr(); + newLine = false; + + } else if (c == '\r') { // Carriage return + newLine = true; + super.write(c); + + } else { // Not an escape char + if (newLine) { + writeLineNbr(); + newLine = false; + } + + super.write(c); + } + } + + private void writeLineNbr() throws IOException { + + int len = String.valueOf(nbLine).length(); + super.write(Integer.toString(nbLine++), 0, len); + super.write('\t'); } } 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..cf816c0 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."); + 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."); + super.write(Character.toUpperCase(c)); } } From 1ceca45dfe6da6ad14fb53c4ec1d7db34429ac17 Mon Sep 17 00:00:00 2001 From: Thierry OTTO Date: Sun, 24 Mar 2019 11:27:47 +0100 Subject: [PATCH 4/7] Modify file transformer --- .../impl/transformers/CompleteFileTransformer.java | 13 +++---------- .../labio/impl/transformers/FileTransformer.java | 7 ++++--- 2 files changed, 7 insertions(+), 13 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..cb328b0 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,8 @@ package ch.heigvd.res.labio.impl.transformers; import java.io.Writer; +import ch.heigvd.res.labio.impl.filters.FileNumberingFilterWriter; +import ch.heigvd.res.labio.impl.filters.UpperCaseFilterWriter; /** * This class returns a writer decorated with two filters: an instance of @@ -15,16 +17,7 @@ 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..d7a2e12 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,10 +54,11 @@ 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 - * writer has been decorated by the concrete subclass!). You need to write a loop to read the - * characters and write them to the writer. + * loop to read the characters and write them to the writer. */ + int character; + while ((character = reader.read()) != -1) + writer.write(character); reader.close(); writer.flush(); From 0d157778d9f320864dacf5ac1d14115e6e89fdd0 Mon Sep 17 00:00:00 2001 From: Thierry OTTO Date: Sun, 24 Mar 2019 12:41:09 +0100 Subject: [PATCH 5/7] Modified Application and utils --- .../ch/heigvd/res/labio/impl/Application.java | 74 ++++++++++--------- .../java/ch/heigvd/res/labio/impl/Utils.java | 15 +++- 2 files changed, 55 insertions(+), 34 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..4be52a9 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 @@ -26,19 +26,19 @@ public class Application implements IApplication { * to where the Java application is invoked. */ public static String WORKSPACE_DIRECTORY = "./workspace/quotes"; - + private static final Logger LOG = Logger.getLogger(Application.class.getName()); - + public static void main(String[] args) { - + /* * I prefer to have LOG output on a single line, it's easier to read. Being able * to change the formatting of console outputs is one of the reasons why it is * better to use a Logger rather than using System.out.println */ System.setProperty("java.util.logging.SimpleFormatter.format", "%4$s: %5$s%6$s%n"); - - + + int numberOfQuotes = 0; try { numberOfQuotes = Integer.parseInt(args[0]); @@ -46,32 +46,32 @@ public static void main(String[] args) { System.err.println("The command accepts a single numeric argument (number of quotes to fetch)"); System.exit(-1); } - + Application app = new Application(); try { /* * Step 1 : clear the output directory */ app.clearOutputDirectory(); - + /* * Step 2 : use the QuotesClient to fetch quotes; store each quote in a file */ app.fetchAndStoreQuotes(numberOfQuotes); - + /* * Step 3 : use a file explorer to traverse the file system; print the name of each directory and file */ Writer writer = new StringWriter(); // we create a special writer that will send characters into a string (memory) app.printFileNames(writer); // we hand over this writer to the printFileNames method LOG.info(writer.toString()); // we dump the whole result on the console - + /* * Step 4 : process the quote files, by applying 2 transformations to their content * (convert to uppercase and add line numbers) */ app.processQuoteFiles(); - + } catch (IOException ex) { LOG.log(Level.SEVERE, "Could not fetch quotes. {0}", ex.getMessage()); ex.printStackTrace(); @@ -84,48 +84,56 @@ 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! - * 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 - * 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, "quote-" + i + ".utf8"); LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); } } } - + /** * This method deletes the WORKSPACE_DIRECTORY and its content. It uses the * apache commons-io library. You should call this method in the main method. - * - * @throws IOException + * + * @throws IOException */ void clearOutputDirectory() throws IOException { - FileUtils.deleteDirectory(new File(WORKSPACE_DIRECTORY)); + FileUtils.deleteDirectory(new File(WORKSPACE_DIRECTORY)); } /** * This method stores the content of a quote in the local file system. It has - * 2 responsibilities: - * + * 2 responsibilities: + * * - with quote.getTags(), it gets a list of tags and uses * it to create sub-folders (for instance, if a quote has three tags "A", "B" and * "C", it will be stored in /quotes/A/B/C/quotes-n.utf8. - * + * * - with quote.getQuote(), it has access to the text of the quote. It stores * this text in UTF-8 file. - * + * * @param quote the quote object, with tags and text * @param filename the name of the file to create and where to store the quote text - * @throws IOException + * @throws IOException */ void storeQuote(Quote quote, String filename) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + StringBuilder hierarchy = new StringBuilder(WORKSPACE_DIRECTORY); + + // Create directory hierarchy + for (String tag : quote.getTags()) + hierarchy.append("/").append(tag); + + // Append file + hierarchy.append("/").append(filename); + + // Create file with filename and hierarchy + File file = new File(hierarchy.toString()); + + // Write quote in file + FileUtils.write(file, quote.getQuote()); } - + /** * This method uses a IFileExplorer to explore the file system and prints the name of each * encountered file and directory. @@ -135,11 +143,11 @@ 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() + "\n"); + } catch (IOException e) { + e.printStackTrace(); + } } }); } @@ -147,7 +155,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..a36b375 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."); + + // New line & Carriage return + if (lines.contains("\r\n")) + return lines.split("(?<=\r\n)", 2); + + else if (lines.contains("\n")) // New line + return lines.split("(?<=\n)", 2); + + else if (lines.contains("\r")) // Carriage return + return lines.split("(?<=\r)", 2); + + else // Empty + return new String[]{"", lines}; + } } From 9ceaa89682f3ed30175a3882bfb86ba683734521 Mon Sep 17 00:00:00 2001 From: Thierry OTTO Date: Sun, 24 Mar 2019 13:00:05 +0100 Subject: [PATCH 6/7] Cleaned files and all tests are successfull --- .../ch/heigvd/res/labio/impl/Application.java | 21 +++++++------- .../java/ch/heigvd/res/labio/impl/Utils.java | 11 ++++---- .../labio/impl/explorers/DFSFileExplorer.java | 24 ++++++++-------- .../filters/FileNumberingFilterWriter.java | 13 ++++----- .../impl/filters/UpperCaseFilterWriter.java | 4 +-- .../transformers/CompleteFileTransformer.java | 9 +++--- .../impl/transformers/FileTransformer.java | 28 ++++++------------- .../transformers/NoOpFileTransformer.java | 2 +- 8 files changed, 51 insertions(+), 61 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 4be52a9..a10e219 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 @@ -5,30 +5,29 @@ import ch.heigvd.res.labio.interfaces.IApplication; import ch.heigvd.res.labio.interfaces.IFileExplorer; import ch.heigvd.res.labio.interfaces.IFileVisitor; -import ch.heigvd.res.labio.quotes.QuoteClient; import ch.heigvd.res.labio.quotes.Quote; +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.util.logging.Level; import java.util.logging.Logger; -import org.apache.commons.io.FileUtils; /** - * * @author Olivier Liechti */ public class Application implements IApplication { + private static final Logger LOG = Logger.getLogger(Application.class.getName()); /** * This constant defines where the quotes will be stored. The path is relative * to where the Java application is invoked. */ public static String WORKSPACE_DIRECTORY = "./workspace/quotes"; - private static final Logger LOG = Logger.getLogger(Application.class.getName()); - public static void main(String[] args) { /* @@ -105,15 +104,15 @@ void clearOutputDirectory() throws IOException { /** * This method stores the content of a quote in the local file system. It has * 2 responsibilities: - * + *

* - with quote.getTags(), it gets a list of tags and uses - * it to create sub-folders (for instance, if a quote has three tags "A", "B" and - * "C", it will be stored in /quotes/A/B/C/quotes-n.utf8. - * + * it to create sub-folders (for instance, if a quote has three tags "A", "B" and + * "C", it will be stored in /quotes/A/B/C/quotes-n.utf8. + *

* - with quote.getQuote(), it has access to the text of the quote. It stores - * this text in UTF-8 file. + * this text in UTF-8 file. * - * @param quote the quote object, with tags and text + * @param quote the quote object, with tags and text * @param filename the name of the file to create and where to store the quote text * @throws IOException */ 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 a36b375..76a740c 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,7 +3,6 @@ import java.util.logging.Logger; /** - * * @author Olivier Liechti */ public class Utils { @@ -12,8 +11,8 @@ public class Utils { /** * This method looks for the next new line separators (\r, \n, \r\n) to extract - * the next line in the string passed in arguments. - * + * 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 @@ -25,10 +24,12 @@ public static String[] getNextLine(String lines) { if (lines.contains("\r\n")) return lines.split("(?<=\r\n)", 2); - else if (lines.contains("\n")) // New line + // New line + else if (lines.contains("\n")) return lines.split("(?<=\n)", 2); - else if (lines.contains("\r")) // Carriage return + // Carriage return + else if (lines.contains("\r")) return lines.split("(?<=\r)", 2); else // Empty 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 24af80e..24a7f3f 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 @@ -2,14 +2,16 @@ 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 * exploration of the file system and invokes the visitor for every encountered * node (file and directory). When the explorer reaches a directory, it visits all * files in the directory and then moves into the subdirectories. - * + * * @author Olivier Liechti */ public class DFSFileExplorer implements IFileExplorer { @@ -17,18 +19,16 @@ public class DFSFileExplorer implements IFileExplorer { @Override public void explore(File rootDirectory, IFileVisitor visitor) { - if (rootDirectory != null) { - File[] files = rootDirectory.listFiles(); - visitor.visit(rootDirectory); // visits all files in the directory - - // Move into the subdirectories - if (files != null) { - for(int i = 0; i < files.length; i++) { - if (files[i].isDirectory()) - explore(files[i], visitor); //Recursion - } - } + // Visit files in the current directory + visitor.visit(rootDirectory); + + File[] files = rootDirectory.listFiles(); + if (files != null) { + for (File file : files) + explore(file, visitor); // Recursion } + } } + 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 241d2ef..8126fb0 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,7 +10,7 @@ * 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 @@ -22,9 +22,7 @@ public class FileNumberingFilterWriter extends FilterWriter { private int nbLine = 1; private boolean newLine = true; - public FileNumberingFilterWriter(Writer out) { - super(out); - } + public FileNumberingFilterWriter(Writer out) { super(out); } @Override public void write(String str, int off, int len) throws IOException { @@ -43,22 +41,24 @@ public void write(int c) throws IOException { // New line if (c == '\n') { + newLine = false; super.write(c); writeLineNbr(); - newLine = false; } else if (c == '\r') { // Carriage return newLine = true; super.write(c); } else { // Not an escape char + if (newLine) { - writeLineNbr(); newLine = false; + writeLineNbr(); } super.write(c); } + } private void writeLineNbr() throws IOException { @@ -67,5 +67,4 @@ private void writeLineNbr() throws IOException { super.write(Integer.toString(nbLine++), 0, len); super.write('\t'); } - } 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 cf816c0..95f244a 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,7 +9,7 @@ * @author Olivier Liechti */ public class UpperCaseFilterWriter extends FilterWriter { - + public UpperCaseFilterWriter(Writer wrappedWriter) { super(wrappedWriter); } @@ -29,4 +29,4 @@ public void write(int c) throws IOException { super.write(Character.toUpperCase(c)); } -} +} \ No newline at end of file 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 cb328b0..aee3f65 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,16 +1,17 @@ package ch.heigvd.res.labio.impl.transformers; -import java.io.Writer; import ch.heigvd.res.labio.impl.filters.FileNumberingFilterWriter; import ch.heigvd.res.labio.impl.filters.UpperCaseFilterWriter; +import java.io.Writer; + /** * This class returns a writer decorated with two filters: an instance of * the UpperCaseFilterWriter and an instance of the FileNumberingFilterWriter. * When an instance of this class is passed to a file system explorer, it will * generate an output file with 1) uppercase letters and 2) line numbers at the * beginning of each line. - * + * * @author Olivier Liechti */ public class CompleteFileTransformer extends FileTransformer { @@ -18,7 +19,7 @@ public class CompleteFileTransformer extends FileTransformer { @Override public Writer decorateWithFilters(Writer writer) { writer = new FileNumberingFilterWriter(new UpperCaseFilterWriter(writer)); - return writer; + return writer; } -} +} \ No newline at end of file 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 d7a2e12..7a8db49 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 @@ -1,15 +1,8 @@ package ch.heigvd.res.labio.impl.transformers; import ch.heigvd.res.labio.interfaces.IFileVisitor; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FilterWriter; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.Reader; -import java.io.Writer; + +import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; @@ -19,25 +12,25 @@ * This abstract class implements the IFileVisitor interface and has the responsibility * to open an input text file, to read its content, to apply a number of transformations * (via filters) and to write the result in an output text file. - * + *

* The subclasses have to implement the decorateWithFilters method, which instantiates * a list of filters and decorates the output writer with them. - * + * * @author Olivier Liechti */ public abstract class FileTransformer implements IFileVisitor { private static final Logger LOG = Logger.getLogger(FileTransformer.class.getName()); private final List filters = new ArrayList<>(); - + /** * The subclasses implement this method to define what transformation(s) are * applied when writing characters to the output writer. The visit(File file) * method creates an output file and creates a corresponding writer. It then * calls decorateWithFilters and passes the writer as argument. The method - * wraps 0, 1 or more filter writers around the original writer and returns + * wraps 0, 1 or more filter writers around the original writer and returns * the result. - * + * * @param writer the writer connected to the output file * @return the writer decorated by 0, 1 or more filter writers */ @@ -50,16 +43,13 @@ public void visit(File file) { } try { Reader reader = new InputStreamReader(new FileInputStream(file), "UTF-8"); - Writer writer = new OutputStreamWriter(new FileOutputStream(file.getPath()+ ".out"), "UTF-8"); // the bug fix by teacher + Writer writer = new OutputStreamWriter(new FileOutputStream(file.getPath() + ".out"), "UTF-8"); // the bug fix by teacher writer = decorateWithFilters(writer); - /* - * loop to read the characters and write them to the writer. - */ int character; while ((character = reader.read()) != -1) writer.write(character); - + 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 880c065..8c20e5b 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 @@ -6,7 +6,7 @@ * This class returns a writer without any decorator. When an instance of * this class is passed to a file system explorer, it will simply duplicate * the content of the input file into the output file. - * + * * @author Olivier Liechti */ public class NoOpFileTransformer extends FileTransformer { From c6b84164af4b021818c98a463fdfb394ccff181d Mon Sep 17 00:00:00 2001 From: Thierry OTTO Date: Sun, 24 Mar 2019 13:06:03 +0100 Subject: [PATCH 7/7] new build test with Travis --- .../src/main/java/ch/heigvd/res/labio/impl/Application.java | 3 +-- 1 file changed, 1 insertion(+), 2 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 a10e219..9f62ff2 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 @@ -156,5 +156,4 @@ public void processQuoteFiles() throws IOException { IFileExplorer explorer = new DFSFileExplorer(); explorer.explore(new File(WORKSPACE_DIRECTORY), new CompleteFileTransformer()); } - -} +} \ No newline at end of file