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

[TOREWIEW] projet java IO #45

Open
wants to merge 14 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
31 changes: 26 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, "quote-" + 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,20 @@ 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 subPath = "";
String extension = ".utf8";
for (String tag : quote.getTags()) {
subPath += "/" + tag;
}
File f = new File(WORKSPACE_DIRECTORY + subPath + "/" + filename + extension);
f.getParentFile().mkdirs();

Writer fileWriter = new OutputStreamWriter(new FileOutputStream(f), StandardCharsets.UTF_8);
//FileWriter fileWriter = new FileWriter(f);

fileWriter.write(quote.getQuote());
fileWriter.close();

}

/**
Expand All @@ -150,6 +162,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).
*/
try{
writer.write(file.getPath() + "\n");
writer.flush();
writer.close();
}catch (Exception ex)
{
LOG.log(Level.SEVERE, null, ex);
}

}
});
}
Expand Down
18 changes: 17 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,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 [] strings = new String[2];

if(lines.indexOf("\r\n") != -1)
{
strings[0] = lines.substring(0,lines.indexOf("\r\n")+2);
strings[1] = lines.substring(lines.indexOf("\r\n")+2,lines.length());
}else if(lines.indexOf("\n") != -1){
strings[0] = lines.substring(0,lines.indexOf("\n")+1);
strings[1] = lines.substring(lines.indexOf("\n")+1,lines.length());
}else if(lines.indexOf("\r") != -1){
strings[0] = lines.substring(0,lines.indexOf("\r")+1);
strings[1] = lines.substring(lines.indexOf("\r")+1,lines.length());
}else{
strings[0] = "";
strings[1] = lines;
}
return strings;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import ch.heigvd.res.labio.interfaces.IFileVisitor;

import java.io.File;
import java.io.FilenameFilter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;

/**
* This implementation of the IFileExplorer interface performs a depth-first
Expand All @@ -17,7 +23,33 @@ 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.list() != null) {
Rexplore(rootDirectory, vistor);
}
}

private void Rexplore(File rootDirectory, IFileVisitor vistor){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I detect that this code is problematic. According to the Bad practice (BAD_PRACTICE), Nm: Method names should start with a lower case letter (NM_METHOD_NAMING_CONVENTION).
Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.


File [] subFolder = rootDirectory.listFiles();

Arrays.sort(subFolder, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
return o1.getName().compareTo(o2.getName());
}
});

for (File f : subFolder ) {
if(f.isDirectory()) {
vistor.visit(f);
Rexplore(f, vistor);
}
if (f.isFile()) {
vistor.visit(f);
}
}
return;
}

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

import java.io.FilterWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.logging.Logger;

Expand All @@ -21,21 +22,72 @@ public class FileNumberingFilterWriter extends FilterWriter {

public FileNumberingFilterWriter(Writer out) {
super(out);
nbLine = 1;
newLine = true;
}

private static int nbLine;
private static boolean newLine;

@Override
public void write(String str, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");

int endIndex = off + len;
String tempStr = str.substring(off, endIndex);
String newStr = "";



for (int i=0; i < tempStr.length(); i++) {
char c = tempStr.charAt(i);

if(c == '\n')
{
newStr += c;
out.write(newStr);
if(i+1 >= len || tempStr.charAt(i+1) != '\n')
out.write(nbLine++ + "\t");
newStr = "";
newLine = false;
}else{
if(newLine) {
out.write(newStr);
out.write(nbLine++ + "\t");
newLine = false;
newStr = "";
}
newStr += c;
}

if(c == '\r')
newLine = true;
}

out.write(newStr);
}

@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.");

if((char) c == '\n') {
out.write((char) c);
out.write(nbLine++ + "\t");
newLine = false;
}else {
if(newLine) {
out.write(nbLine++ + "\t");
newLine = false;
}
out.write((char) c);
}

if((char) c == '\r')
newLine = true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.logging.Level;

/**
*
Expand All @@ -16,17 +17,24 @@ 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.");
int endIndex = off + len;
String upStr = str.substring(off, endIndex);
out.write(upStr.toUpperCase());
}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
char[] upCbuf = new char[len];
for (int i = 0; i < len ; ++i) {
upCbuf[i] = Character.toUpperCase(cbuf[off + i]);
}
out.write(upCbuf);
}

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

}
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,16 @@ 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.
*/

char [] charBuf = new char[255];

int numberOfNewBytes = reader.read(charBuf);

while(numberOfNewBytes != -1){

writer.write(charBuf,0,numberOfNewBytes);
numberOfNewBytes = reader.read(charBuf);
}

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;
}

}