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..55f2970a 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 @@ -9,11 +9,9 @@ import ch.heigvd.res.labio.quotes.QuoteClient; import org.apache.commons.io.FileUtils; -import java.io.File; -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; +import java.io.*; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import java.util.logging.Level; import java.util.logging.Logger; @@ -102,6 +100,7 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException { for (String tag : quote.getTags()) { LOG.info("> " + tag); } + storeQuote(quote, "quote-" + (i + 1) + ".utf8"); } } @@ -114,7 +113,7 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException { * @throws IOException */ void clearOutputDirectory() throws IOException { - FileUtils.deleteDirectory(new File(WORKSPACE_DIRECTORY)); + FileUtils.deleteDirectory(new File(WORKSPACE_DIRECTORY)); } /** @@ -133,7 +132,17 @@ 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."); + StringBuilder filepath = new StringBuilder(WORKSPACE_DIRECTORY); + + for (String tag : quote.getTags()){ + filepath.append(File.separator).append(tag); + } + new File(filepath.toString()).mkdirs(); + + filepath.append(File.separator).append(filename); + Writer writer = new OutputStreamWriter(new FileOutputStream(filepath.toString()), StandardCharsets.UTF_8); + writer.write(quote.getQuote()); + writer.close(); } /** @@ -150,6 +159,13 @@ 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(); + } + + } }); } 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..2a3ceafb 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,19 @@ 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[] res = new String[2]; + int idx = -1; + int idxUnix = lines.indexOf('\n'); + int idxMac = lines.indexOf('\r'); + if (idxUnix >= 0 ) { + idx = idxUnix; + } else if (idxMac >= 0){ + idx = idxMac; + } + ++idx; + res[0] = lines.substring(0, idx); + res[1] = lines.substring(idx, lines.length()); + return res; } } 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..87aa6c53 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,6 +4,8 @@ import ch.heigvd.res.labio.interfaces.IFileVisitor; import java.io.File; +import java.lang.reflect.Array; +import java.util.Arrays; /** * This implementation of the IFileExplorer interface performs a depth-first @@ -16,8 +18,17 @@ 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); + if (rootDirectory.isDirectory()){ + File[] children = rootDirectory.listFiles(); + Arrays.sort(children); + for (File file : children){ + explore(file, visitor); + } + } + + } } 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..5340777b 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,68 @@ public FileNumberingFilterWriter(Writer out) { super(out); } + private int idLine = 1; + private boolean init = false; + private int previousChar; + @Override public void write(String str, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + String s = str.substring(off, off + len); + String[] line = new String[2]; + StringBuilder res = new StringBuilder(); + do { + line = Utils.getNextLine(s); + if (!init){ + init = true; + res.append(idLine).append("\t"); + } + res.append(line[0]); + if (line[0].length() != 0){ + ++idLine; + res.append(idLine).append("\t"); + } + s = line[1]; + + }while (line[0].length() != 0); + res.append(line[1]); + out.write(res.toString()); } @Override public void write(char[] cbuf, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + write(new String(cbuf, off,len)); } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + + String res = ""; + if (!init){ + init = true; + String noLine = String.valueOf(idLine); + for (int i=0; i= 'a' && c <= 'z'){ + c += 'A' - 'a'; + } + + out.write(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 4beca482..319f0ac8 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,16 +18,13 @@ 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..f8a14966 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 @@ -52,7 +52,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 = reader.read(); + 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..71477a0e 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; } }