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] LabIO - Fixing code to pass all tests #57

Open
wants to merge 9 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
22 changes: 20 additions & 2 deletions LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;


/**
*
* @author Olivier Liechti
* @author Olivier Liechti, Florian Mulhauser
*/
public class Application implements IApplication {

Expand Down Expand Up @@ -90,6 +91,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 + ".utf8" );
LOG.info("Received a new joke with " + quote.getTags().size() + " tags.");
for (String tag : quote.getTags()) {
LOG.info("> " + tag);
Expand Down Expand Up @@ -123,7 +125,17 @@ 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.");
StringBuilder sb_path = new StringBuilder(WORKSPACE_DIRECTORY);

for (String tag: quote.getTags()){

sb_path.append(File.separator).append(tag);
}

sb_path.append(File.separator).append(filename);
File f = new File(sb_path.toString());
FileUtils.writeStringToFile(f, quote.getQuote());
//throw new UnsupportedOperationException("The student has not implemented this method yet.");
}

/**
Expand All @@ -140,6 +152,12 @@ 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.append(file.toString()).append("\n");
} catch (IOException ex){
ex.printStackTrace();

}
}
});
}
Expand Down
44 changes: 41 additions & 3 deletions LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
*
* @author Olivier Liechti
* @author Olivier Liechti, Florian Mülhauser
*/
public class Utils {

Expand All @@ -20,7 +20,45 @@ 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.");
int posSeparatorL = lines.indexOf('\n'); // linux
int posSeparatorM = lines.indexOf('\r'); // linux
int posSeparatorW = lines.indexOf("\r\n"); // linux

int len = lines.length();

String[] ret = new String[2];
if (posSeparatorL != -1) {
ret[0] = lines.substring(0, posSeparatorL + 1);

if (posSeparatorL + 1 < len) {
ret[1] = lines.substring(posSeparatorL + 1);
} else {
ret[1] = "";
}
} else if (posSeparatorM != -1) {

ret[0] = lines.substring(0, posSeparatorM + 1);
if (posSeparatorM + 1 < len) {
ret[1] = lines.substring(posSeparatorM + 1);
} else {
ret[1] = "";
}

} else if (posSeparatorW != -1) {

ret[0] = lines.substring(0, posSeparatorW + 1);
if (posSeparatorW + 1 < len) {
ret[1] = lines.substring(posSeparatorW + 1);
} else {
ret[1] = "";
}

} else { //default
ret[0] = "";
ret[1] = lines;
}

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

import ch.heigvd.res.labio.interfaces.IFileExplorer;
import ch.heigvd.res.labio.interfaces.IFileVisitor;
import java.util.*;
import java.io.File;

/**
Expand All @@ -10,13 +11,41 @@
* node (file and directory). When the explorer reaches a directory, it visits all
* files in the directory and then moves into the subdirectories.
*
* @author Olivier Liechti
* @author Olivier Liechti, Florian Mulhauser
*/
public class DFSFileExplorer implements IFileExplorer {

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

// first visit
visitor.visit(rootDirectory);
// then solve recursively if it's a directory
if (rootDirectory.isDirectory()) exploreDFS(rootDirectory,visitor);

}

public void exploreDFS(File rD, IFileVisitor visitor){
File[] files = rD.listFiles();

if(files == null){
return;
} else {
Arrays.sort(files);
for (File f:
files) {
visitor.visit(f);
if(f.isDirectory()){ //if it's a directory, go recursively

exploreDFS(f, visitor); // recursive call
}



}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.io.IOException;
import java.io.Writer;
import java.util.logging.Logger;

import ch.heigvd.res.labio.impl.Utils;
/**
* This class transforms the streams of character sent to the decorated writer.
* When filter encounters a line separator, it sends it to the decorated writer.
Expand All @@ -13,29 +13,79 @@
*
* Hello\n\World -> 1\Hello\n2\tWorld
*
* @author Olivier Liechti
* @author Olivier Liechti, Florian Mülhauser
*/
public class FileNumberingFilterWriter extends FilterWriter {

private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName());
private boolean isFirst = true;
private int lCounter = 0;
//private static int cCounter = 0;
private int lastC = -1;



public FileNumberingFilterWriter(Writer out) {
super(out);
}

public void writeLnTab() throws IOException{
String str = Integer.toString(++lCounter);
super.write(str, 0 , str.length());
super.write('\t');

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


if(isFirst) {
writeLnTab();
isFirst = false;
}

str = str.substring(off,len + off);
String[] lines;

while(!(lines = Utils.getNextLine(str))[0].isEmpty()){
super.write(lines[0],0,lines[0].length());
writeLnTab();

str = lines[1];
}

super.write(str, 0 , str.length());

}


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


String str = new String(cbuf,off,len);
this.write(str,0,len);

}

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

if (isFirst) {
writeLnTab();
isFirst = false;
}

if (c != '\n' && lastC == '\r') {
writeLnTab();
}

super.write(c);

if (c == '\n') {
writeLnTab();
}

lastC = c;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
*
* @author Olivier Liechti
* @author Olivier Liechti, Florian Mülhauser
*/
public class UpperCaseFilterWriter extends FilterWriter {

Expand All @@ -16,17 +16,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.");
//throw new UnsupportedOperationException("The student has not implemented this method yet.");

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

}
//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.");
//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,7 +1,7 @@
package ch.heigvd.res.labio.impl.transformers;

import java.io.Writer;

import ch.heigvd.res.labio.impl.filters.*;
/**
* This class returns a writer decorated with two filters: an instance of
* the UpperCaseFilterWriter and an instance of the FileNumberingFilterWriter.
Expand All @@ -15,16 +15,17 @@ 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* The subclasses have to implement the decorateWithFilters method, which instantiates
* a list of filters and decorates the output writer with them.
*
* @author Olivier Liechti
* @author Olivier Liechti, Florian Mülhauser
*/
public abstract class FileTransformer implements IFileVisitor {

Expand Down Expand Up @@ -58,7 +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 val = reader.read();

while((val != -1)){
writer.write(val);
val = reader.read();
}

reader.close();
writer.flush();
writer.close();
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;
}

}