Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TOREVIEW] Quillasp_DavPel #25

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LabJavaIO/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>11</source>
<target>11</target>
Expand Down
36 changes: 31 additions & 5 deletions LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -98,6 +96,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, String.format("quote-%d", i));
LOG.info("Received a new joke with " + quote.getTags().size() + " tags.");
for (String tag : quote.getTags()) {
LOG.info("> " + tag);
Expand Down Expand Up @@ -133,7 +132,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.");
// throw new UnsupportedOperationException("The student has not implemented this method yet.");
StringBuilder sbpath = new StringBuilder();
sbpath.append(WORKSPACE_DIRECTORY).append("/");

for (String tag: quote.getTags())
sbpath.append(tag).append("/");

String path = sbpath.toString();

File f = new File(path);
f.mkdirs();

OutputStreamWriter oswriter = new OutputStreamWriter(
new FileOutputStream(path + filename + ".utf8"), StandardCharsets.UTF_8
);

oswriter.write(quote.getQuote());
oswriter.flush();
oswriter.close();
}

/**
Expand All @@ -150,6 +167,15 @@ 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).
*/
if (file == null)
return;

try {
writer.write(file.getPath());
writer.write(System.lineSeparator());
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
});
}
Expand Down
26 changes: 25 additions & 1 deletion LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ public class Utils {

private static final Logger LOG = Logger.getLogger(Utils.class.getName());

/**
* découverte des lookahead et lookbehind grâce à :
* - https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
* pour ce qui est de la structure d'une regex
* - https://www.regular-expressions.info/lookaround.html
* pour l'utilisation des lookahead et lookbehind
* - (?<=X) : si X se trouve bien avant le caractère
* - (?!X) : si X ne se trouve pas après le caractère
* - (?<=X(?!Y)) : si X se trouve avant le caractère mais qu'il n'est pas
* suivi par Y
* - lookahead et lookbehind permettent de faire des comparaisons sur les
* String sans que ceux-ci ne soient consommés : ils permettent de dire
* si le String match ou non la regexe
*/
private static final String REGEX = "(?<=(\r\n))|(?<=\n)|(?<=\r(?!\n))";


/**
* This method looks for the next new line separators (\r, \n, \r\n) to extract
* the next line in the string passed in arguments.
Expand All @@ -20,7 +37,14 @@ 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.");
// throw new UnsupportedOperationException("The student has not implemented this method yet.");
String[] result = lines.split(REGEX, 2);

if (result.length == 2)
return result;
else
return new String[]{"", result[0]};

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import ch.heigvd.res.labio.interfaces.IFileExplorer;
import ch.heigvd.res.labio.interfaces.IFileVisitor;
import java.util.ArrayList;
import java.util.Arrays;


import java.io.File;

Expand All @@ -17,7 +20,25 @@ public class DFSFileExplorer implements IFileExplorer {

@Override
public void explore(File rootDirectory, IFileVisitor vistor) {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
// throw new UnsupportedOperationException("The student has not implemented this method yet.");
if (rootDirectory == null)
return;

vistor.visit(rootDirectory);

File[] childFiles = rootDirectory.listFiles();

if(childFiles == null)
return;

Arrays.sort(childFiles);

for (File f : childFiles) {
if (f.isDirectory())
explore(f, vistor);
else
vistor.visit(f);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,64 @@
public class FileNumberingFilterWriter extends FilterWriter {

private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName());
private int lineNumber;
private boolean beginning;
private boolean newline;

public FileNumberingFilterWriter(Writer out) {
super(out);
lineNumber = 0;
beginning = true;
newline = false;
}

@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.");
for (int i = 0; i < len; ++i)
write(cbuf[off + i]);

// ajout de la dernière ligne (assez artificiel)
write(0);
}

/**
* Écrit un numéro de ligne suivi d'un tab à chaque nouvelle ligne
* @throws IOException si erreur à l'écriture sur le flux
*/
public void newline() throws IOException {
out.write(++lineNumber + "\t");
}

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
// throw new UnsupportedOperationException("The student has not implemented this method yet.");
if (beginning) {
beginning = false;
newline();
out.write(c);
} else {
if (c == '\r' || c == '\n') {
if (!newline)
newline = true;

out.write(c);
} else {
if (newline)
newline();

newline = false;

if (c != 0)
out.write(c);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ 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.");
// throw new UnsupportedOperationException("The student has not implemented this method yet.");
out.write(str.substring(off, off + len).toUpperCase());
}

@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.");
for (int i = 0; i < len; ++i) {
out.write(Character.toUpperCase(cbuf[off + i]));
}
}

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
// throw new UnsupportedOperationException("The student has not implemented this method yet.");
out.write(Character.toUpperCase(c));
}

}
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -15,16 +18,16 @@ public class CompleteFileTransformer extends FileTransformer {

@Override
public Writer decorateWithFilters(Writer writer) {
if (true) {
/*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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}