diff --git a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Application.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Application.java index b33a15c..287f601 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Application.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Application.java @@ -7,6 +7,7 @@ import ch.heigvd.res.lab01.interfaces.IFileVisitor; import ch.heigvd.res.lab01.quotes.QuoteClient; import ch.heigvd.res.lab01.quotes.Quote; +import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -92,7 +93,9 @@ 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). */ + LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); + storeQuote(quote, "quote-" + i + ".utf8"); for (String tag : quote.getTags()) { LOG.info("> " + tag); } @@ -125,7 +128,20 @@ 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."); + String path = WORKSPACE_DIRECTORY + File.separator; + // recover a tag + for (String tag : quote.getTags()){ + path = path + tag + File.separator; + } + + path = path + "/" + filename; // add filename + File files = new File(path); // we create file + files.getParentFile().mkdirs(); //we create parent repertory + BufferedWriter BufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8")); + BufferedWriter.write(quote.getQuote()); + BufferedWriter.close(); + } /** @@ -142,13 +158,20 @@ 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 ex) { + LOG.log(Level.SEVERE, "Could not write quotes.", ex.getMessage()); + ex.printStackTrace(); + } } }); } @Override public String getAuthorEmail() { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + return "zacharie.nguefack@heig-vd.ch"; } @Override diff --git a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Utils.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Utils.java index ebededd..73de0ac 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Utils.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/Utils.java @@ -20,7 +20,23 @@ 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."); + + String[] separators = new String[2]; + int n; + + if(lines.indexOf('\n') == -1){ + n = lines.indexOf('\r'); + } + else{ + n= lines.indexOf('\n'); + } + separators[0] = lines.substring(0,n+1); + separators[1] = lines.substring(n+1); + return separators; + + + //throw new UnsupportedOperationException("The student has not implemented this method yet."); } + } diff --git a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/explorers/DFSFileExplorer.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/explorers/DFSFileExplorer.java index 0322ffc..d4491ea 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/explorers/DFSFileExplorer.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/explorers/DFSFileExplorer.java @@ -3,6 +3,8 @@ import ch.heigvd.res.lab01.interfaces.IFileExplorer; import ch.heigvd.res.lab01.interfaces.IFileVisitor; import java.io.File; +import java.util.Arrays; +import java.util.ArrayList; /** * This implementation of the IFileExplorer interface performs a depth-first @@ -14,9 +16,26 @@ */ public class DFSFileExplorer implements IFileExplorer { - @Override - public void explore(File rootDirectory, IFileVisitor vistor) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); - } + @Override + public void explore(File rootDirectory, IFileVisitor vistor) { + + if (rootDirectory != null) { + vistor.visit(rootDirectory); + if (rootDirectory.isDirectory()) { + File[] fileList = rootDirectory.listFiles(); + for (File file : fileList) { + if (file.isFile()) { + vistor.visit(file); + } + } + for (File file : fileList) { + if (file.isDirectory()) { + explore(file, vistor); + } + } + + } + } + } } diff --git a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/FileNumberingFilterWriter.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/FileNumberingFilterWriter.java index 509843d..3873070 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/FileNumberingFilterWriter.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/FileNumberingFilterWriter.java @@ -16,26 +16,53 @@ * @author Olivier Liechti */ public class FileNumberingFilterWriter extends FilterWriter { + private int nbLines = 1; + private int charac; private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); - + 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."); + //throw new UnsupportedOperationException("The student has not implemented this method yet."); + write(str.toCharArray(), 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."); + //throw new UnsupportedOperationException("The student has not implemented this method yet."); + int i = off; + while(i < off + len){ + write(cbuf[i]); + i++; + } } - + @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + if (nbLines ==1){ + writeHeaderLine(); } - + + if ((this.charac == '\r' && c != '\n')) { + writeHeaderLine(); + } + + out.write(c); + + if (c == '\n') { + writeHeaderLine(); + } + + this.charac = c; + } + + private void writeHeaderLine() throws IOException { + out.write(nbLines + "\t"); + nbLines++; + } } + diff --git a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/UpperCaseFilterWriter.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/UpperCaseFilterWriter.java index 1be272e..e33760f 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/UpperCaseFilterWriter.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/filters/UpperCaseFilterWriter.java @@ -16,17 +16,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); + //throw new UnsupportedOperationException("The student has not implemented this method yet."); } @Override public void write(char[] cbuf, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + super.write(new String(cbuf).toUpperCase(),off, len); + //throw new UnsupportedOperationException("The student has not implemented this method yet."); } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + super.write(Character.toUpperCase(c)); + //throw new UnsupportedOperationException("The student has not implemented this method yet."); } } diff --git a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/CompleteFileTransformer.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/CompleteFileTransformer.java index 86fff79..a1ec468 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/CompleteFileTransformer.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/CompleteFileTransformer.java @@ -1,5 +1,6 @@ package ch.heigvd.res.lab01.impl.transformers; - +import ch.heigvd.res.lab01.impl.filters.FileNumberingFilterWriter; +import ch.heigvd.res.lab01.impl.filters.UpperCaseFilterWriter; import java.io.Writer; /** @@ -15,16 +16,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/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/FileTransformer.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/FileTransformer.java index 5eec488..683ff1e 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/FileTransformer.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/FileTransformer.java @@ -58,6 +58,12 @@ public void visit(File file) { * 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; + while((c = reader.read()) != -1) + writer.write(c); + + + reader.close(); writer.flush(); diff --git a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/NoOpFileTransformer.java b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/NoOpFileTransformer.java index 41670aa..0ea94de 100644 --- a/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/impl/transformers/NoOpFileTransformer.java +++ b/Lab01App-build/Lab01App-code/src/main/java/ch/heigvd/res/lab01/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; } }