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..6742067f 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 @@ -9,17 +9,16 @@ 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.io.*; import java.net.URISyntaxException; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Olivier Liechti + * Modifed by Blanc Jean-Luc */ public class Application implements IApplication { @@ -98,6 +97,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). */ + storeQuote(quote, "quote-" + i + ".utf8"); LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); @@ -133,7 +133,22 @@ 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."); + //throw new UnsupportedOperationException("The student has not implemented this method yet."); + + List tags = quote.getTags(); + String quoteText = quote.getQuote(); + + String pathToRep = WORKSPACE_DIRECTORY; + for(String tag : tags){ + pathToRep += "/" + tag; + } + File fileDir = new File(pathToRep); + fileDir.mkdirs(); + + pathToRep += "/" + filename; + FileWriter fileWriter = new FileWriter(pathToRep); + fileWriter.write(quoteText); + fileWriter.close(); } /** @@ -150,6 +165,12 @@ public void visit(File file) { * 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(); + } } }); } 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..81cdbc6e 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,8 @@ /** * * @author Olivier Liechti + * + * Modified by Blanc Jean-Luc */ public class Utils { @@ -20,7 +22,28 @@ 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."); + + //First we want to know the index of the line separators + int indexR = lines.indexOf("\r");//MAC OS + int indexN = lines.indexOf("\n");//UNIX + int indexRN = lines.indexOf("\r\n");//WINDOWS + + //Then we want to check if those indexes exist or not, + // if the index = -1 then the line separator is not in the "lines" string + // Once we know if they exist, we want to start forming our return element String[] + // and actually return it if the line separator exists + // we return something too if we don't find any line separators + //here we add 2 to the index because the line separator is 2 characters + if(indexRN != -1) + return new String[]{lines.substring(0, indexRN + 2), lines.substring(indexRN + 2)}; + + if(indexR != -1) + return new String[]{lines.substring(0, indexR + 1), lines.substring(indexR + 1)}; + + if(indexN != -1) + return new String[]{lines.substring(0, indexN + 1), lines.substring(indexN + 1)}; + + return new String[]{"", lines}; } } 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..03f72341 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,6 +4,7 @@ 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 @@ -12,12 +13,32 @@ * files in the directory and then moves into the subdirectories. * * @author Olivier Liechti + * + * Modified by Blanc Jean-Luc */ 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) { + + //Checking that rootDirectory does exist before going further + if(rootDirectory == null){ + return; + } + + //if it exists, then we can visit it + visitor.visit(rootDirectory); + + //if the file is actually a directory, then we need to go further in + if(rootDirectory.isDirectory()){ + //we create an array that contains the directory content, we then sort this content in order to display it + File[] folderContent = rootDirectory.listFiles(); + Arrays.sort(folderContent); + //adding recursive here + for(File file : folderContent){ + explore(file, 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..72ceb37f 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 @@ -11,13 +11,16 @@ * It then sends the line number and a tab character, before resuming the write * process. * - * Hello\n\World -> 1\Hello\n2\tWorld + * Hello\n\World -> 1\tHello\n2\tWorld * * @author Olivier Liechti + * Modified by Blanc Jean-Luc */ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); + private int counter = 0; + private int lastC = '\0'; public FileNumberingFilterWriter(Writer out) { super(out); @@ -25,17 +28,42 @@ public FileNumberingFilterWriter(Writer 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 i = off; i < len+off; i++){ + write(str.charAt(i)); + } } @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 < len+off; i++){ + write(cbuf[i]); + } } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + //here we want to check if we are on the first line, if that's not the case then we want to check + // the various "c" cases regarding the OS + // UNIX = \n MAC OS = \r Windows = \r\n + // after that we write down the counter, insert a tab + // then we write down the char (while not forgetting to save the last char + if(counter == 0){ + out.write(++counter + "\t"); + } + else if(lastC == '\r' && c != '\n'){ + out.write(++counter + "\t"); + } + + out.write(c); + + if(c == '\n' && lastC == '\r'){ + out.write(++counter + "\t"); + } + else if(c == '\n' && lastC != '\r'){ + out.write(++counter + "\t"); + } + + lastC = c; } } 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..5b9ecbc0 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 @@ -3,11 +3,16 @@ import java.io.FilterWriter; import java.io.IOException; import java.io.Writer; +import java.util.Locale; /** * * @author Olivier Liechti + * + * Modified by Blanc Jean-Luc */ + +//basically here we only have to implement last class methods using "super" while making sure we put everything to upper case public class UpperCaseFilterWriter extends FilterWriter { public UpperCaseFilterWriter(Writer wrappedWriter) { @@ -16,17 +21,20 @@ 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."); + for(int i = 0; i < cbuf.length; i++){ + cbuf[i] = Character.toUpperCase(cbuf[i]); + } + super.write(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)); } } 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..d74c04e0 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 @@ -10,21 +12,20 @@ * beginning of each line. * * @author Olivier Liechti + * + * Modified by Blanc Jean-Luc */ 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 bde833e8..49b35a51 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 @@ -18,6 +18,8 @@ * a list of filters and decorates the output writer with them. * * @author Olivier Liechti + * + * Modified by Blanc Jean-Luc */ public abstract class FileTransformer implements IFileVisitor { @@ -47,11 +49,11 @@ 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 c; //character we want to read and write + while((c = reader.read()) != -1){ //-1 here means end of flux + writer.write(c); + } 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 5971a302..9ffebb56 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 @@ -8,19 +8,14 @@ * the content of the input file into the output file. * * @author Olivier Liechti + * + * Modified by Blanc Jean-Luc */ 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; } }