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..c420dc2b 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 @@ -28,19 +28,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 +48,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,6 +92,7 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException { e.printStackTrace(); } if (quote != null) { + storeQuote(quote, "quote-" + i + ".utf8"); /* 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 @@ -100,42 +101,60 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException { */ LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { - LOG.info("> " + tag); + LOG.info("> " + tag ); } } + //LOG.info("" + quote.getQuote()); + } } - + /** * 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."); + void storeQuote(Quote quote, String filename) { + String content = WORKSPACE_DIRECTORY; + + //ajout des sous dossier + for (String tag : quote.getTags()) { + content += "/" + tag; + } + //ajout du nom du fichier + content += "/" + filename; + + try{ + FileUtils.writeStringToFile(new File(content),quote.getQuote()); // Je ne comprends par le flag "deprecated" + } catch (IOException e) { + e.printStackTrace(); } - + + } + /** * This method uses a IFileExplorer to explore the file system and prints the name of each * encountered file and directory. @@ -145,6 +164,12 @@ void printFileNames(final Writer writer) { explorer.explore(new File(WORKSPACE_DIRECTORY), new IFileVisitor() { @Override public void visit(File file) { + try { + writer.write(file.getPath()); + writer.write("\n"); + } catch (IOException e) { + e.printStackTrace(); + } /* * 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 @@ -154,10 +179,11 @@ 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..4c4b23d2 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 + * modifié par Laurent Tailhades */ public class Utils { @@ -19,8 +20,43 @@ public class Utils { * the line separator, the second element is the remaining text. If the argument does not * 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\n")) { + return splitLine(lines, "\r\n"); + } + + else if (lines.contains("\r")) { + return splitLine(lines, "\r"); + } + + + else if (lines.contains("\n")) { + return splitLine(lines, "\n"); + } + + else { + return new String[]{"", lines}; + } } + /** + * Cette méthode transforme la ligne en argument selon le séparateur (/n, /r, /r/n) + * + * + * @param lines un string qui contient une ou plusieurs ligne + * @param separator le type de séparateur + * @return un array avec 2 éléments, le séparateur adéquat est ajouté à la première ligne + */ + private static String[] splitLine(String lines, String separator) { + String[] parts = lines.split(separator,2); + return new String[]{parts[0] + separator, parts[1]}; + } + + } + + 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..f7c15e24 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,40 @@ 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 + * + * modifié par Laurent Tailhades */ public class DFSFileExplorer implements IFileExplorer { + @Override public void explore(File rootDirectory, IFileVisitor vistor) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); - } + + File listOfFiles[] = rootDirectory.listFiles(); + + //trie de la liste, apparement pour corriger les test en ligne + if(listOfFiles != null){ + Arrays.sort(listOfFiles); + } + + vistor.visit(rootDirectory); + + if (listOfFiles != null) { + + //recursion sur le prochain dossier + for (File file : listOfFiles) { + explore(file, vistor); + } + } + } } 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..f954e115 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 @@ -14,28 +14,58 @@ * Hello\n\World -> 1\Hello\n2\tWorld * * @author Olivier Liechti + * + * Modifié par Laurent Tailhades */ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); - public FileNumberingFilterWriter(Writer out) { - super(out); - } + public FileNumberingFilterWriter(Writer out) { super(out); } + + private int lineNumber = 1; + private boolean nextLine = true; @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 < off + len; i++){ + char a = str.charAt(i); + this.write(a); + } } @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++){ + this.write(cbuf[i]); + } } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); - } + //check retour à la ligne MACOS + if (c == '\r') { + nextLine = true; + super.write(c); + } + + //check retour à la ligne LINUX et PC + else if (c == '\n') { + nextLine = false; + super.write(c); + super.write(String.valueOf(lineNumber++)); + super.write('\t'); + + }else { + // vérifie si c'est la première ligne + if (nextLine) { + nextLine = false; + super.write(String.valueOf(lineNumber++)); + super.write('\t'); + } + + super.write(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..5128baf7 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,8 @@ /** * * @author Olivier Liechti + * + * Modifié par Laurent Tailhades */ public class UpperCaseFilterWriter extends FilterWriter { @@ -16,17 +18,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."); + 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."); + char tempBuf[]; + tempBuf = new char[off + len]; + int j = 0; + for(int i = 0; i < off + len; i++){ + tempBuf[j++] = Character.toUpperCase(cbuf[i]); + } + super.write(tempBuf,off,len); + } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + char a = (char)c; + a = Character.toUpperCase(a); + super.write(a); } } 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..a16c1238 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; /** @@ -10,21 +13,14 @@ * beginning of each line. * * @author Olivier Liechti + * + * Modifié par Laurent Tailhades */ 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..33a0320d 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 + * + * Modifié par Laurent Tailhades */ public abstract class FileTransformer implements IFileVisitor { @@ -47,12 +49,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 c = reader.read(); //read character + while (c != -1) { + writer.write(c); + c = 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..41027d05 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 + * + * Modifié par Laurent Tailhades */ 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; } }