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

[WIP] Labo2-Java-IO #7

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
19 changes: 17 additions & 2 deletions 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,22 @@ 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[] arrOfStr = new String[2];
int i = 0;

if(lines.contains("\r") || lines.contains("\n")) {
while(lines.charAt(i) != '\r' && lines.charAt(i) != '\n'){i++;}
i++;
if(lines.length() >= i+1) {
if (lines.substring(i-1, i + 1).contains("\r\n")) ++i;
}
arrOfStr[0] = lines.substring(0, i);
arrOfStr[1] = lines.substring(i, lines.length());
}
else{
arrOfStr[0] = "";
arrOfStr[1] = lines;
}
return arrOfStr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@
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) {
if(rootDirectory != null) {
File[] listOfFilesAndDirectory = rootDirectory.listFiles();
visitor.visit(rootDirectory);
if (listOfFilesAndDirectory != null) {
for (File file : listOfFilesAndDirectory) {
if (file.isDirectory()) {
explore(file, visitor);
}
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.heigvd.res.labio.impl.filters;

import ch.heigvd.res.labio.impl.Utils;

import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;
Expand All @@ -11,31 +13,64 @@
* It then sends the line number and a tab character, before resuming the write
* process.
*
* Hello\n\World -> 1\Hello\n2\tWorld
* Hello\n\World -> 1\tHello\n2\tWorld
*
* @author Olivier Liechti
*/

public class FileNumberingFilterWriter extends FilterWriter {

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


private boolean begin = true;
private boolean wasEscape = false;
private int compteur = 0;

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

@Override
public void write(String str, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
String[] array = new String[]{new String("EMPTY"), new String("EMPTY")};
String temp = new String();
array[1] = str.substring(off, off+len);
int i = 0;

if(begin){
begin = false;
temp += ++compteur + "\t";
}

if(!array[1].contains("\n") && !array[1].contains("\r")){
temp += array[1];
}

while(!array[0].equals("")) {
array = Utils.getNextLine(array[1]);
if(array[0].equals("") && i > 0){
temp += array[1];
}
temp += array[0];
wasEscape = temp.equals("\r") ? true : false;
if ((array[0].contains("\n") || array[0].contains("\r")) && !wasEscape) {
temp += ++compteur + "\t";
}
i++;
}
super.write(temp, 0, temp.length());
}

@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.");
char alpha = (char)c;
write("" + alpha);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@
* @author Olivier Liechti
*/
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.");
super.write(str.toUpperCase(),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.");
super.write(new String(cbuf).toUpperCase(), off, len);
}

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

}
Original file line number Diff line number Diff line change
@@ -1,6 +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
Expand All @@ -15,16 +16,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 @@ -54,11 +54,19 @@ public void visit(File file) {
writer = decorateWithFilters(writer);

/*
* There is a missing piece here: you have an input reader and an ouput writer (notice how the
* 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
* characters and write them to the writer.
*/

int value;
try {
while ((value = reader.read()) != -1)
writer.write(value);
}
catch(Exception e )
{
System.out.println("Error with the read");
}
reader.close();
writer.flush();
writer.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ public class NoOpFileTransformer extends FileTransformer {

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

}