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] Labo java I/O in progresss #50

Open
wants to merge 2 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
38 changes: 33 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 @@ -14,6 +14,9 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
import java.util.List;
import java.io.FileWriter;
import java.io.BufferedWriter;

/**
*
Expand Down Expand Up @@ -84,9 +87,13 @@ 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!
storeQuote(quote, "quote-" + quote.getValue().getId() + ".utf8");



/* There is no 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
* client to fetch quotes. We have not 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).
*/
Expand Down Expand Up @@ -123,7 +130,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.");
List<String> tags = quote.getTags();

String directoryName = WORKSPACE_DIRECTORY;

for (int i = 0; i < tags.size(); i++){
directoryName += "\\" + tags.get(i);
}

File directory = new File(directoryName);
directory.mkdirs();

File name = new File(directoryName + "\\" + filename);
name.createNewFile();

FileWriter fw = new FileWriter(name.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(quote.getQuote());
bw.close();

}

/**
Expand All @@ -134,7 +159,10 @@ void printFileNames(final Writer writer) {
IFileExplorer explorer = new DFSFileExplorer();
explorer.explore(new File(WORKSPACE_DIRECTORY), new IFileVisitor() {
@Override
public void visit(File file) {
public void visit(File file) throws IOException {

writer.write(file.getPath());

/*
* 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
Expand All @@ -147,7 +175,7 @@ public void visit(File file) {
@Override
public void processQuoteFiles() throws IOException {
IFileExplorer explorer = new DFSFileExplorer();
explorer.explore(new File(WORKSPACE_DIRECTORY), new CompleteFileTransformer());
explorer.explore(new File(WORKSPACE_DIRECTORY), new CompleteFileTransformer());
}

}
24 changes: 23 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 @@ -20,7 +20,29 @@ 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[] arrayReturn = new String[2];
int index = -2;
//Y'a certainement un meilleur moyen que cette vielle répétition de code.
index = lines.indexOf("\r\n");
if (index > -1){
arrayReturn[0] = lines.substring(0, index);
arrayReturn[1] = lines.substring(index);
}

index = lines.indexOf('\r');
if (index > -1){
arrayReturn[0] = lines.substring(0, index);
arrayReturn[1] = lines.substring(index);
}

index = lines.indexOf('\n');
if (index > -1){
arrayReturn[0] = lines.substring(0, index);
arrayReturn[1] = lines.substring(index);
}

return arrayReturn;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
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.");
for (int i = off; i < off+len; i++) {
out.write(Character.toUpperCase(cbuf[i]));
}
}

@Override
public void write(int c) throws IOException {
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,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;
writer = new FileNumberingFilterWriter(new UpperCaseFilterWriter(writer));
return writer;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public void visit(File file) {
Writer writer = new OutputStreamWriter(new FileOutputStream(file.getPath()+ ".out"), "UTF-8"); // the bug fix by teacher
writer = decorateWithFilters(writer);

//REpris du cours
int octet = reader.read();
while (octet != -1){
writer.write(octet);
octet = reader.read();
}

/*
* 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.heigvd.res.labio.interfaces;

import java.io.File;
import java.io.IOException;

/**
* This interface is used together with the IFileExplorer interface. It defines
Expand All @@ -18,6 +19,6 @@ public interface IFileVisitor {
*
* @param file the current file or directory visited by the IFileExplorer instance
*/
public void visit(File file);
public void visit(File file) throws IOException;

}