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..41b9dcc 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,12 +7,11 @@ 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; /** @@ -84,12 +83,8 @@ 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). - */ + String filename = "/quote-" + i + ".utf8"; + storeQuote(quote, filename); LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); @@ -118,12 +113,35 @@ void clearOutputDirectory() throws IOException { * - 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 quote the quote object, with tags a + * nd text * @param filename the name of the file to create and where to store the quote text * @throws IOException */ void storeQuote(Quote quote, String filename) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + + String path = WORKSPACE_DIRECTORY + "/"; + File actualDir = new File(path); + + int tagNum = quote.getTags().size(); + if(tagNum != 0){ + for(String tag : quote.getTags()){ + //creating the new path + path += tag + "/"; + actualDir = new File(path); + } + } + //creating the directory with the new path + actualDir.mkdirs(); + + //Adding the quote utf8 file + File newFile = new File(actualDir, filename); + FileWriter fileWriter = new FileWriter(newFile); + fileWriter.write(quote.getQuote()); + + fileWriter.flush(); + fileWriter.close(); + } /** @@ -135,11 +153,11 @@ 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() + '\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 c8a3a5a..7ac0434 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,31 @@ 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."); - } + //throw new UnsupportedOperationException("The student has not implemented this method yet."); + String[] result = new String[2]; + String str = ""; + for (int i = 0; i < lines.length(); i++) { + str += lines.charAt(i); + if (lines.charAt(i) == '\r' && ((i + 1) != lines.length()) && lines.charAt(i + 1) == '\n') { + continue; + } else if (lines.charAt(i) == '\r' || lines.charAt(i) == '\n') { + result[0] = str; + result[1] = lines.substring(i + 1); + break; + } else if (i == lines.length() - 1) { + result[0] = ""; + result[1] = str; + } + } + 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..838a547 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 @@ -2,21 +2,33 @@ import ch.heigvd.res.labio.interfaces.IFileExplorer; 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 + * + * @author Olivier LiechtiA */ 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) { + + vistor.visit(rootDirectory); + if(rootDirectory.isDirectory()){ + File[] files = rootDirectory.listFiles(); + Arrays.sort(files); //otherwise theApplicationShouldGenerateTheCorrectNumberOfOutputFiles() does not pass + for(File file : files){ + 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 976c946..e67affe 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 @@ -5,37 +5,88 @@ import java.io.Writer; import java.util.logging.Logger; +import static ch.heigvd.res.labio.impl.Utils.getNextLine; + /** * This class transforms the streams of character sent to the decorated writer. * When filter encounters a line separator, it sends it to the decorated writer. * 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 */ public class FileNumberingFilterWriter extends FilterWriter { + private int lineNumber = 0; + private boolean isNewLine = true; + private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); public FileNumberingFilterWriter(Writer out) { super(out); } + private String numering(String str, int off, int len){ + StringBuilder tmp = new StringBuilder(); + + if(isNewLine){ + lineNumber++; + tmp.append(lineNumber + "\t"); + isNewLine = false; + } + + for(int i = off; i < off + len; i++){ + tmp.append(str.charAt(i)); + if(tmp.charAt(tmp.length() - 1) == '\n' || tmp.charAt(tmp.length() - 1) == '\r' && str.charAt(i + 1) != '\n'){ + lineNumber++; + tmp.append(lineNumber + "\t"); + } + } + + + return tmp.toString(); + } + + @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(numering(str, 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."); + String str = String.valueOf(cbuf); + out.write(numering(str, off, len)); } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + + StringBuilder tmp = new StringBuilder(); + + if (isNewLine){ + isNewLine = false; + lineNumber++; + tmp.append(lineNumber); + tmp.append('\t'); + } + + if ((char) c == '\n'){ + lineNumber++; + tmp.append((char)c); + tmp.append(lineNumber); + tmp.append('\t'); + + } else { + tmp.append((char)c); + } + out.write(tmp.toString()); + + + } + } 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..72b16b7 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,24 @@ 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."); + this.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."); + //Changing in capital letter + for(int i = off; i < cbuf.length; i++){ + cbuf[i] = Character.toUpperCase(cbuf[i]); + } + this.out.write(cbuf, off, len); } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + + this.out.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 4beca48..651f3b2 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; /** @@ -15,17 +18,14 @@ 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)); - return writer; + return new FileNumberingFilterWriter(new UpperCaseFilterWriter(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..0471898 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 @@ -53,12 +53,14 @@ public void visit(File file) { Writer writer = new OutputStreamWriter(new FileOutputStream(file.getPath()+ ".out"), "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. - */ - + if(reader.ready()){ + char[] buf = new char[255]; + int length; + while((length = reader.read(buf)) != -1){ + writer.write(buf, 0, length); + } + } + 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 5971a30..880c065 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,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; } }