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..7726e54 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 @@ -7,10 +7,9 @@ import ch.heigvd.res.labio.interfaces.IFileVisitor; import ch.heigvd.res.labio.quotes.QuoteClient; import ch.heigvd.res.labio.quotes.Quote; -import java.io.File; -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; + +import java.io.*; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.FileUtils; @@ -84,12 +83,7 @@ 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, WORKSPACE_DIRECTORY); LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); @@ -123,7 +117,24 @@ 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 text = quote.getQuote(); + String name = filename; + name += "/quotes"; + for (String tag: tags) { + name+= '/'+ tag; + } + name+= "/quote-" + quote.getValue().getId() + ".utf8"; + File f = new File(name); + if (!f.getParentFile().exists()) { + f.getParentFile().mkdirs(); + } + if (!f.exists()) { + f.createNewFile(); + } + PrintWriter writer = new PrintWriter( name, "UTF-8"); + writer.println(text); + writer.close(); } /** @@ -135,11 +146,12 @@ 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()); + } 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 c8a3a5a..99194d6 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 @@ -12,15 +12,35 @@ 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 * 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[] result = new String[2]; + int i1 = lines.indexOf("\n"); + int i2 = lines.indexOf("\r"); + int i3 = lines.indexOf("\r\n"); + if (i1 == -1 && i2==-1 && i3==-1) { //Aucune occurence + result[0] = ""; + result[1] = lines; + } + else if(i3>i1 && i3 >=i2) { // pour \r\n + result[0] = lines.substring(0, i3 + 3); + result[1] = lines.substring(i3 + 3); + } + else if (i2>i1) { // pour \r + result[0] = lines.substring(0, i2+1); + result[1] = lines.substring(i2+1); + } else { // pour \n + result[0] = lines.substring(0, i1+1); + result[1] = lines.substring(i1+1); + } + 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 83f8e61..820d224 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,18 @@ 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) { + visitor.visit(rootDirectory); + File[] listOfFiles = rootDirectory.listFiles(); + if (listOfFiles != null) { //Si c'est un directory + for (File file : listOfFiles) { + if (file.isDirectory()) { + explore(file, visitor); + } else { + visitor.visit(file); + } + } + } } } 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..8375035 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,7 @@ package ch.heigvd.res.labio.impl.filters; +import ch.heigvd.res.labio.impl.Utils; + import java.io.FilterWriter; import java.io.IOException; import java.io.Writer; @@ -23,19 +25,42 @@ public FileNumberingFilterWriter(Writer out) { super(out); } + /* Méthodes non fonctionelles */ @Override public void write(String str, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + String str_f = str.substring(off, off+len); + String[] next; + String result = "1\t"; + next = Utils.getNextLine(str_f); + result += next[0]; + int i =2; + while (next[0].indexOf("\r") != -1 || next[0].indexOf("\n") != -1) { + next = Utils.getNextLine(next[1]); + result += (i+"\t"); + result += next[0]; + } + super.out.write(result); } @Override public void write(char[] cbuf, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + String str_f = cbuf.toString().substring(off, off+len); + String[] next; + String result = "1\t"; + next = Utils.getNextLine(str_f); + result += next[0]; + int i =2; + while (next[0].indexOf("\r") != -1 || next[0].indexOf("\n") != -1) { + next = Utils.getNextLine(next[1]); + result += (i+"\t"); + result += next[0]; + } + super.out.write(result); } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + super.out.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 0f41a5d..f336c52 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,21 @@ 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.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."); + char[] bufUpper = new char[cbuf.length]; + for (int i=0; i