From 875e4d1012a24a0a10c683077ec78695279735df Mon Sep 17 00:00:00 2001 From: psrochat Date: Sun, 12 Mar 2017 15:43:12 +0100 Subject: [PATCH 1/2] Lab01 - psrochat --- .../ch/heigvd/res/lab01/impl/Application.java | 32 ++++++++++++++++-- .../java/ch/heigvd/res/lab01/impl/Utils.java | 31 +++++++++++++++-- .../lab01/impl/explorers/DFSFileExplorer.java | 26 ++++++++++++++- .../filters/FileNumberingFilterWriter.java | 33 ++++++++++++++++--- .../impl/filters/UpperCaseFilterWriter.java | 8 +++-- .../transformers/CompleteFileTransformer.java | 6 ++-- 6 files changed, 119 insertions(+), 17 deletions(-) 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..097e0c8 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 @@ -16,6 +16,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.FileUtils; +import java.nio.charset.Charset; +import java.util.List; /** * @@ -92,6 +94,7 @@ 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). */ + storeQuote(quote, "quote-" + quote.getValue().getId() + ".utf8"); LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); for (String tag : quote.getTags()) { LOG.info("> " + tag); @@ -106,7 +109,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)); } /** @@ -125,7 +128,25 @@ 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."); + String separator = File.separator; + String path = WORKSPACE_DIRECTORY; + List tags = quote.getTags(); + + for(String tag : tags) { + path += separator + tag; + } + + path += separator + filename; + + //Create directories + File file = new File(path); + file.getParentFile().mkdirs(); + + //Create outputStream to write the quotes in the files + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(path), Charset.forName("UTF-8")); + os.write(quote.getQuote()); + os.close(); + } /** @@ -142,13 +163,18 @@ 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 String getAuthorEmail() { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + return "pierre-samuel.rochat@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..aa3b91e 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,34 @@ 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[] linesTab = new String[2]; + boolean newLineFound = false; + int newLineIndex = 0; + + for (int i = 0; i < lines.length(); i++) { + if (lines.charAt(i) == '\r') { + newLineFound = true; + if ((i + 2) <= lines.length() && lines.charAt(i + 1) == '\n') { + newLineIndex = i + 2; + } else { + newLineIndex = i + 1; + } + break; + } else if (lines.charAt(i) == '\n') { + newLineFound = true; + newLineIndex = i + 1; + break; + } + } + if (newLineFound) { + linesTab[0] = lines.substring(0, newLineIndex); + linesTab[1] = lines.substring(newLineIndex); + } else { + linesTab[0] = ""; + linesTab[1] = lines; + } + + return linesTab; + } } 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..970cf2e 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,9 @@ 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 @@ -16,7 +19,28 @@ public class DFSFileExplorer implements IFileExplorer { @Override public void explore(File rootDirectory, IFileVisitor vistor) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + + vistor.visit(rootDirectory); + + File[] list = rootDirectory.listFiles(); + ArrayList dir = new ArrayList(); + + if (list == null) return; + + Arrays.sort(list); + + for ( File f : list ) { + if ( f.isDirectory() ) { + dir.add(f); + } + else { + explore(f, vistor); + } + } + + for(File f : dir) { + explore(f, 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..a026f9a 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 @@ -18,24 +18,49 @@ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); + private int lineNumber; + private Boolean lineSeparator; public FileNumberingFilterWriter(Writer out) { + super(out); + lineNumber = 1; + lineSeparator = false; + try{ + super.write(lineNumber + "\t"); + }catch(IOException e){ + System.err.println(e); + } } @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 < off + len; i++) { + write(str.charAt(i)); + } } @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."); - } + char charC = (char) c; + + if (lineSeparator && charC != '\n') { + out.write(++lineNumber + "\t"); + } + lineSeparator = false; + + out.write(charC); + + if(charC == '\n') { + out.write(++lineNumber + "\t"); + } else if(charC == '\r') { + lineSeparator = true; + } + } } 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..acad5d0 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,19 @@ 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."); + for(int i = off; i < off + len; i++) { + write(str.charAt(i)); + } } @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."); + super.write(Character.toUpperCase(c)); } } 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..0c0d12e 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,6 +1,7 @@ package ch.heigvd.res.lab01.impl.transformers; import java.io.Writer; +import ch.heigvd.res.lab01.impl.filters.*; /** * This class returns a writer decorated with two filters: an instance of @@ -15,16 +16,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; } From 980ce31b285298e1bb5c6eb94a53f2636af85ee4 Mon Sep 17 00:00:00 2001 From: psrochat Date: Sun, 12 Mar 2017 17:12:37 +0100 Subject: [PATCH 2/2] Lab01 - psrochat - Transformation correction --- .../res/lab01/impl/transformers/FileTransformer.java | 7 +++++++ .../res/lab01/impl/transformers/NoOpFileTransformer.java | 3 +-- 2 files changed, 8 insertions(+), 2 deletions(-) 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..77db85d 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,13 @@ 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/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..78412bc 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,13 @@ 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; } }