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 25e835c4..23e9f174 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,9 +14,14 @@ import java.io.StringWriter; import java.io.Writer; import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; + /** * * @author Olivier Liechti @@ -28,19 +33,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]); @@ -48,32 +53,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(); @@ -92,12 +97,7 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException { e.printStackTrace(); } if (quote != null) { - /* 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); @@ -106,36 +106,45 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException { } } - + /** * 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."); + List tags = quote.getTags(); + String folder_name = "/"; + for (String tag : tags) { + folder_name += tag + "/"; + } + + String quote_text = quote.getQuote(); + File my_quote = new File("./workspace/quotes/"+folder_name + filename); + FileUtils.writeStringToFile(my_quote, quote_text, "UTF-8"); + } - + /** * This method uses a IFileExplorer to explore the file system and prints the name of each * encountered file and directory. @@ -144,12 +153,14 @@ void printFileNames(final Writer writer) { IFileExplorer explorer = new DFSFileExplorer(); 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). - */ + public void visit(File file) { + + try { + writer.write(file.getPath()+System.lineSeparator()); + } + catch(IOException e) { + System.err.println("Invalid File"); + } } }); } @@ -157,7 +168,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 c8a3a5ad..6a1803d5 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 @@ -5,6 +5,7 @@ /** * * @author Olivier Liechti + * Modified by Joan Maillard and Mathias Maillard */ public class Utils { @@ -20,7 +21,32 @@ 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."); + char lsMac = '\r'; + char lsUnix = '\n'; + String lsWindows = "\r\n"; + String[] result = new String[2]; + int index; + //Check for \r\n + if ( (index = lines.indexOf(lsWindows)) != -1) { + result[0] = lines.substring(0, index+2); + result[1] = lines.substring(index+2); + } + //check for \r + else if ((index = lines.indexOf(lsMac)) != -1) { + result[0] = lines.substring(0, index+1); + result[1] = lines.substring(index+1); + } + //check for \n + else if ((index = lines.indexOf(lsUnix)) != -1) { + result[0] = lines.substring(0, index+1); + result[1] = lines.substring(index+1); + } + //No line separators + else { + result[0] = ""; + result[1] = lines; + } + return result; } } 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 b97c4a72..fe806c97 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 @@ -4,20 +4,36 @@ 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 + * + * Modified by Joan Maillard and Mathias Maillard */ 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) { + // Avoid errors if file is null + if(rootDirectory == null){ + return; + } + // Visit file + visitor.visit(rootDirectory); + // Check if file is dir + if (rootDirectory.isDirectory()) { + // if so is the case, list all files inside and explore within + File[] dirContentFiles = rootDirectory.listFiles(); + Arrays.sort(dirContentFiles); + for (File dirFile : dirContentFiles) { + explore(dirFile, visitor); + } + } } - } 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 976c9462..d4542e37 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,8 @@ package ch.heigvd.res.labio.impl.filters; +import ch.heigvd.res.labio.impl.Utils; +import jdk.jshell.execution.Util; + import java.io.FilterWriter; import java.io.IOException; import java.io.Writer; @@ -14,28 +17,69 @@ * Hello\n\World -> 1\Hello\n2\tWorld * * @author Olivier Liechti + * + * Modified by Joan Maillard and Mathias Maillard */ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); + private int count = 1; + private boolean hadAnR; + private boolean newLine; public FileNumberingFilterWriter(Writer out) { super(out); + hadAnR = true; + newLine = true; + } + + private void writeLineNumToken() throws IOException { + if(newLine) { + newLine = false; + out.write(count + "\t"); + count++; + } } @Override public void write(String str, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + String lines[] = Utils.getNextLine(str.substring(off, off+len)); + while (!lines[0].equals("")) { + writeLineNumToken(); + out.write(lines[0]); + newLine = true; + lines = Utils.getNextLine(lines[1]); + } + writeLineNumToken(); + out.write(lines[1]); } @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."); + if (c != '\n' && hadAnR) { + writeLineNumToken(); + } + out.write(c); + if (c == '\n') { + if (!hadAnR) { + newLine = true; + writeLineNumToken(); + } + else { + writeLineNumToken(); + } + } + if (c == '\r') { + hadAnR = true; + newLine = true; + } + else { + hadAnR = false; + } } - } 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 0f41a5dd..48064c13 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 @@ -7,6 +7,7 @@ /** * * @author Olivier Liechti + * Modified by Joan Maillard and Mathias Maillard */ public class UpperCaseFilterWriter extends FilterWriter { @@ -16,17 +17,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."); + out.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."); + out.write(String.copyValueOf(cbuf).toUpperCase(), off, len); } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + out.write(Character.toUpperCase((char) c)); } } 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 4beca482..40df932e 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 @@ -8,24 +9,16 @@ * 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 + * Modified by Joan Maillard and Mathias Maillard */ 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)); - return 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 bde833e8..d7a1324f 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 @@ -13,25 +13,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 */ @@ -47,12 +47,13 @@ public void visit(File file) { Writer writer = new OutputStreamWriter(new FileOutputStream(file.getPath()+ ".out"), StandardCharsets.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. - */ - + int charId; + charId = reader.read(); + while (charId != -1) { + writer.write(charId); + charId = reader.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 5971a302..6f1a066d 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,21 +6,16 @@ * 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 + * Modified by Joan Maillard and Mathias Maillard */ 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/README.md b/README.md index 3aace291..fe2d01e9 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ### About the 2021 version of this lab -We did not change the 2019 version. However, read the paragraph below to be aware of some differences between the project structure in the webcasts and what you will see this year in your IDE. +We did not change the 2019 version. However, read the paragraph below to be aware of some differences between the project structure in the webcasts and whatt you will see this year in your IDE. ### About the 2019 version of this lab @@ -15,7 +15,7 @@ We changed the project structure. In previous editions, there was a "test" proje For this lab, we will try the following workflow: * create a branch for the entire lab (you call it "dev", "lab" or whatever you want) * commit code as soon as possible and push the branch to your fork -* open a PR. In the name of the PR, start with `[WIP] `. This makes it easy for us to see that you are still working on the lab (Work In Progress) +* open a PR. In the name of the PR, start with `[WIP] `. This makes it easy for us to see that you are still working on the lab (Work In Progress) * even if tests are still red, we have a communication space while you work on the lab * whenever you push a new commit, we will be able to see it (and possibly to comment it) * at some point, all your tests will be green: you can then signal that you are done; for that, change the name of your PR. Replace `[WIP] ` with `[TOREVIEW] `. @@ -57,7 +57,7 @@ If your application is fully implemented you should have the following result on ``` [INFO] Scanning for projects... -[INFO] +[INFO] [INFO] --------------------< ch.heigvd.res.io:lab-java-io >-------------------- [INFO] Building RES Lab Java IO 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- @@ -66,9 +66,9 @@ If your application is fully implemented you should have the following result on [INFO] Running ch.heigvd.res.labio.impl.explorers.DFSFileExplorerTest [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.15 s - in ch.heigvd.res.labio.impl.explorers.DFSFileExplorerTest -[INFO] +[INFO] [INFO] Results: -[INFO] +[INFO] [INFO] Tests run: 27, Failures: 0, Errors: 0, Skipped: 0 ... (skipping some of the log output)