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

Rendu labo01 #105

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties

*.DS_Store
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@
import ch.heigvd.res.lab01.interfaces.IApplication;
import ch.heigvd.res.lab01.interfaces.IFileExplorer;
import ch.heigvd.res.lab01.interfaces.IFileVisitor;
import ch.heigvd.res.lab01.quotes.QuoteClient;
import ch.heigvd.res.lab01.quotes.Quote;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import ch.heigvd.res.lab01.quotes.QuoteClient;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Olivier Liechti
* @author Damien Carnal
*/
public class Application implements IApplication {

Expand All @@ -30,6 +27,8 @@ public class Application implements IApplication {
public static String WORKSPACE_DIRECTORY = "./workspace/quotes";

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

private int quoteNumber = 1;

public static void main(String[] args) {

Expand Down Expand Up @@ -86,12 +85,7 @@ 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).
*/
storeQuote(quote, WORKSPACE_DIRECTORY);
LOG.info("Received a new joke with " + quote.getTags().size() + " tags.");
for (String tag : quote.getTags()) {
LOG.info("> " + tag);
Expand Down Expand Up @@ -125,7 +119,28 @@ 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 path = filename;
List<String> listTags = quote.getTags();
String q = quote.getQuote();

new File(path).mkdir();
path += '/';

for (int i = 0; i < listTags.size(); i++){
path += listTags.get(i) + '/';
new File(path).mkdirs();
}

path += "quote-" + quoteNumber + ".utf8";

new File(path).createNewFile();

try (Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(path), "utf-8")))
{
writer.write(q);
}
quoteNumber++;
}

/**
Expand All @@ -137,18 +152,20 @@ 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());
writer.write('\n');
} catch (IOException e){
e.printStackTrace();
}

}
});
}

@Override
public String getAuthorEmail() {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
return "[email protected]";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/**
*
* @author Olivier Liechti
* @author Damien Carnal
*/
public class Utils {

Expand All @@ -20,7 +21,23 @@ 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[] result = new String[2];

// si on a pas de nouvelle ligne on retourne la ligne actuel et un string vide
if (lines.indexOf('\n') == -1 && lines.indexOf('\r') == -1){
result[0] = new String("");
result[1] = lines;
}
//si il y a un caractère de saut de ligne on parse
else if (lines.contains("\n")){
result[0] = lines.substring(0, lines.indexOf('\n') + 1);
result[1] = lines.substring(lines.indexOf('\n') + 1);
}else{
result[0] = lines.substring(0, lines.indexOf('\r') + 1);
result[1] = lines.substring(lines.indexOf('\r') + 1);
}

return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.heigvd.res.lab01.interfaces.IFileExplorer;
import ch.heigvd.res.lab01.interfaces.IFileVisitor;
import java.io.File;
import java.util.Arrays;

/**
* This implementation of the IFileExplorer interface performs a depth-first
Expand All @@ -11,12 +12,35 @@
* files in the directory and then moves into the subdirectories.
*
* @author Olivier Liechti
* @author Damien Carnal
*/
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);

if (rootDirectory.isDirectory()){
// Creation d'un tableau avec le contenu du fichier
File[] files = rootDirectory.listFiles();
Arrays.sort(files);

// On visite d'abord les fichier
for (File file : files){
if (file.isFile()){
vistor.visit(file);
}
}

// ensuite on explore en profondeur
for (File file : files){
if (file.isDirectory()){
explore(file, vistor);
}
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,78 @@
* Hello\n\World -> 1\Hello\n2\tWorld
*
* @author Olivier Liechti
* @author Damien Carnal
*/
public class FileNumberingFilterWriter extends FilterWriter {

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


// Numéro de linge
private int counter;
private boolean firstCall;
private boolean previousWasCR;

public FileNumberingFilterWriter(Writer out) {
super(out);
}
public FileNumberingFilterWriter(Writer out) {
super(out);
counter = 1;
firstCall = true;
previousWasCR = false;
}

@Override
public void write(String str, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
}
public void write(String str, int off, int len) throws IOException {
if (str == null){
return;
}
// regard si l'oofset sort du string
if (off + len > str.length()){
return;
}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
}
String string = str.substring(off, off + len);
char[] cbuf = string.toCharArray();
write(cbuf, 0, cbuf.length);
}

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
}
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
if (cbuf == null){
return;
}
// Envoie des caractères individuellement
for (char c : cbuf){
write((int) c);
}
}

@Override
public void write(int c) throws IOException {
String str;
if (firstCall){
// ajoute le numéro et le tab en début
str = counter++ + "\t";
super.write(str, 0, str.length());
firstCall = false;
}

if (c == '\n') {
str = "\n" + counter++ + "\t";
previousWasCR = false;
super.write(str, 0, str.length());
} else if (c == '\r') {
str = "\r";
previousWasCR = true;
super.write(str, 0, str.length());
} else {
// si c'est un \r on dois écrire le no de linge et le tab
if (previousWasCR){
str = counter++ + "\t";
super.write(str, 0, str.length());
}
super.write(c);
previousWasCR = false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,51 @@
/**
*
* @author Olivier Liechti
* @author Damien Carnal
*/
public class UpperCaseFilterWriter extends FilterWriter {

public UpperCaseFilterWriter(Writer wrappedWriter) {
super(wrappedWriter);
}

@Override
public void write(String str, int off, int len) throws IOException {
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.");
}

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
}
public UpperCaseFilterWriter(Writer wrappedWriter) {
super(wrappedWriter);
}

@Override
public void write(String str, int off, int len) throws IOException {

if (str == null){
return;
}
if (off + len > str.length()){
return;
}

String substring = str.substring(off, off + len);
substring = substring.toUpperCase();
super.write(substring, 0, substring.length());
}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
if (cbuf == null){
return;
}
if (off + len > cbuf.length){
return;
}

for (int i = off; i < off + len; i++){
char c = cbuf[i];
write(c);
}
}

@Override
public void write(int c) throws IOException {
// conversion en majuscule
if (Character.isLowerCase(c)){
c = Character.toUpperCase(c);
}
super.write(c);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ch.heigvd.res.lab01.impl.transformers;

import ch.heigvd.res.lab01.impl.filters.FileNumberingFilterWriter;
import ch.heigvd.res.lab01.impl.filters.UpperCaseFilterWriter;

import java.io.Writer;

/**
Expand All @@ -13,19 +16,10 @@
*/
public class CompleteFileTransformer extends FileTransformer {

@Override
public Writer decorateWithFilters(Writer writer) {
if (true) {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
@Override
public Writer decorateWithFilters(Writer writer) {
writer = new UpperCaseFilterWriter(new FileNumberingFilterWriter(writer));
return writer;
}
/*
* 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;
}

}
Loading