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..8d472f0 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,8 @@ 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.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.FileUtils; @@ -80,16 +78,21 @@ public static void main(String[] args) { @Override public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException { - clearOutputDirectory(); + + this.clearOutputDirectory(); + QuoteClient client = new QuoteClient(); for (int i = 0; i < numberOfQuotes; i++) { + Quote quote = client.fetchQuote(); + storeQuote(quote, "quote-"+(i + 1)+".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 * 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."); for (String tag : quote.getTags()) { LOG.info("> " + tag); @@ -123,16 +126,29 @@ 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 tagsPath = String.join("/", quote.getTags()); + + File file = new File(WORKSPACE_DIRECTORY+"/"+tagsPath+"/"+filename); + file.getParentFile().mkdirs(); + file.createNewFile(); + + FileWriter quoteWriter = new FileWriter(WORKSPACE_DIRECTORY+"/"+tagsPath+"/"+filename); + quoteWriter.write(quote.getQuote()); + quoteWriter.close(); } /** * This method uses a IFileExplorer to explore the file system and prints the name of each * encountered file and directory. */ - void printFileNames(final Writer writer) { + void printFileNames(final Writer writer) throws IOException { + IFileExplorer explorer = new DFSFileExplorer(); + explorer.explore(new File(WORKSPACE_DIRECTORY), new IFileVisitor() { + @Override public void visit(File file) { /* @@ -140,12 +156,21 @@ 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(); + } + } }); } @Override public void processQuoteFiles() throws IOException { + IFileExplorer explorer = new DFSFileExplorer(); 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 c8a3a5a..c039db5 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 @@ -20,7 +20,50 @@ 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[] ret = new String[2]; + ret[1] = ""; + + int posWindowsEndLine = lines.indexOf("\r\n"); + int posLinuxEndLine = lines.indexOf("\n"); + int posMacEndLine = lines.indexOf("\r"); + + + if(posWindowsEndLine >= 0){ + + ret[0] = lines.substring(0, posWindowsEndLine + 2); + + if(lines.length() > posWindowsEndLine + 2) { + + ret[1] = lines.substring(posWindowsEndLine + 2); + } + + } else if(posLinuxEndLine >= 0) { + + ret[0] = lines.substring(0, posLinuxEndLine + 1); + + if(lines.length() > posLinuxEndLine + 1) { + + ret[1] = lines.substring(posLinuxEndLine + 1); + } + + } else if(posMacEndLine >= 0) { + + ret[0] = lines.substring(0, posMacEndLine + 1); + + if(lines.length() > posMacEndLine + 1) { + + ret[1] = lines.substring(posMacEndLine + 1); + } + } else { + + ret[0] = ""; + ret[1] = lines; + } + + return ret; +// throw new UnsupportedOperationException("The student has not implemented this method yet."); } } 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..a611208 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 @@ -3,6 +3,9 @@ import ch.heigvd.res.labio.interfaces.IFileExplorer; import ch.heigvd.res.labio.interfaces.IFileVisitor; import java.io.File; +import java.io.IOException; +import java.lang.reflect.Array; +import java.util.Arrays; /** * This implementation of the IFileExplorer interface performs a depth-first @@ -15,8 +18,32 @@ 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 vistor) throws IOException { + + vistor.visit(rootDirectory); + + if(!rootDirectory.isDirectory()){ + + return; + } + + File[] fileList = rootDirectory.listFiles(); + + Arrays.sort(fileList); + + for(File f : fileList){ + + if(f.isDirectory()){ + + this.explore(f, vistor); + + } else { + + vistor.visit(f); + } + } + +// throw new UnsupportedOperationException("The student has not implemented this method yet."); } } 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..34a0afb 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 @@ -18,6 +18,8 @@ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); + private int linecpt = 0; + private int lastChar = -1; public FileNumberingFilterWriter(Writer out) { super(out); @@ -25,17 +27,50 @@ 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++) { + + this.write(str.charAt(i)); + } + +// 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."); + + for(int i = off; i < len + off; i++) { + this.write(cbuf[i]); + } + +// 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."); + + + if(linecpt == 0){ + out.write(Integer.toString(++linecpt) + '\t'); + } + + + if(lastChar == '\r' && c != '\n') { + + out.write(Integer.toString(++linecpt) + '\t'); + + } + + out.write(c); + + if(c == '\n') { + + out.write(Integer.toString(++linecpt) + '\t'); + } + + this.lastChar = c; + +// throw new UnsupportedOperationException("The student has not implemented this method yet."); } } 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..c94a28c 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,31 @@ 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); + +// 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."); + + + for(int i = off; i < len + off; i++) { + cbuf[i] = Character.toUpperCase(cbuf[i]); + } + + out.write(cbuf, 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."); + + out.write(Character.toUpperCase(c)); + +// throw new UnsupportedOperationException("The student has not implemented this method yet."); } } 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 4beca48..cf481af 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 @@ -15,16 +17,17 @@ 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 (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 18e3f14..b5002f7 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 @@ -58,6 +58,14 @@ 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 = reader.read(); + + while(c != -1) { + + writer.write(c); + c = reader.read(); + } 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 5971a30..90031de 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 @@ -13,14 +13,14 @@ public class NoOpFileTransformer extends FileTransformer { @Override public Writer decorateWithFilters(Writer writer) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); +// 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/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileExplorer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileExplorer.java index 7d387e7..1dc83f3 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileExplorer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileExplorer.java @@ -1,6 +1,7 @@ package ch.heigvd.res.labio.interfaces; import java.io.File; +import java.io.IOException; /** * This interface is used to perform one operation on each element (file and @@ -23,6 +24,6 @@ public interface IFileExplorer { * @param rootDirectory the directory where to start the traversal * @param vistor defines the operation to be performed on each file */ - public void explore(File rootDirectory, IFileVisitor vistor); + public void explore(File rootDirectory, IFileVisitor vistor) throws IOException; } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileVisitor.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileVisitor.java index b0abe27..11ddfb2 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileVisitor.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/interfaces/IFileVisitor.java @@ -1,6 +1,7 @@ package ch.heigvd.res.labio.interfaces; import java.io.File; +import java.io.IOException; /** * This interface is used together with the IFileExplorer interface. It defines @@ -18,6 +19,6 @@ public interface IFileVisitor { * * @param file the current file or directory visited by the IFileExplorer instance */ - public void visit(File file); + public void visit(File file) throws IOException; } diff --git a/LabJavaIO/src/test/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorerTest.java b/LabJavaIO/src/test/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorerTest.java index a62cc47..708d26d 100644 --- a/LabJavaIO/src/test/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorerTest.java +++ b/LabJavaIO/src/test/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorerTest.java @@ -22,7 +22,7 @@ public class DFSFileExplorerTest { private static final Logger LOG = Logger.getLogger(DFSFileExplorerTest.class.getName()); @Test - public void dfsExplorerShouldWork() { + public void dfsExplorerShouldWork() throws IOException { List dfsNodes = generateTestTree(5, 5, 5); final List directories = new ArrayList<>(); @@ -38,7 +38,7 @@ public void visit(File file) { } @Test - public void dfsExplorerShouldWorkWhenThereIsNoFile() { + public void dfsExplorerShouldWorkWhenThereIsNoFile() throws IOException { List dfsNodes = generateTestTree(0, 0, 0); final List directories = new ArrayList<>();