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] RuckStuhl_Peguiron #48

Open
wants to merge 13 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
4 changes: 2 additions & 2 deletions LabJavaIO/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<name>RES Lab Java IO</name>
<build>
Expand Down
41 changes: 36 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,10 +9,7 @@
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.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -97,11 +94,15 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException {
* 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).
*
* => Done
*/
LOG.info("Received a new joke with " + quote.getTags().size() + " tags.");
for (String tag : quote.getTags()) {
LOG.info("> " + tag);
}
// Call storeQuote. With quote and the filename = "quote-i.utf8
storeQuote(quote, "quote-" + Integer.toString(i) + ".utf8");
}

}
Expand Down Expand Up @@ -133,7 +134,30 @@ 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.");

//Create directories with with the tags

String str = WORKSPACE_DIRECTORY;
for (int i = 0; i < quote.getTags().size(); ++i){
str += "/";
str += quote.getTags().get(i);
}
new File(str).mkdirs();


//Create file and write quote in

str += "/";
str += filename;

try {
FileWriter myWriter = new FileWriter(str);
myWriter.write(quote.getQuote());
myWriter.close();
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}

/**
Expand All @@ -149,7 +173,14 @@ 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).
*
* => Done
*/
try {
writer.write(file.getPath() + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
Expand Down
63 changes: 48 additions & 15 deletions LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,57 @@
import java.util.logging.Logger;

/**
*
* @author Olivier Liechti
*/
public class Utils {

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

/**
* This method looks for the next new line separators (\r, \n, \r\n) to extract
* the next line in the string passed in arguments.
*
* @param lines a string that may contain 0, 1 or more lines
* @return an array with 2 elements; the first element is the next line with
* the line separator, the second element is the remaining text. If the argument does not
* 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.");
}
private static final Logger LOG = Logger.getLogger(Utils.class.getName());

/**
* This method looks for the next new line separators (\r, \n, \r\n) to extract
* the next line in the string passed in arguments.
*
* @param lines a string that may contain 0, 1 or more lines
* @return an array with 2 elements; the first element is the next line with
* the line separator, the second element is the remaining text. If the argument does not
* contain any line separator, then the first element is an empty string.
*/
public static String[] getNextLine(String lines) {

// String array of size 2
String[] s = new String[2];
s[0] = "";
s[1] = "";

// Write char in the first cell of the String array until end or line separators.
for (int i = 0; i < lines.length(); ++i) {
s[0] += lines.substring(i, i + 1);
if (lines.charAt(i) == '\r') {
// Check if there is \n after \r for windows conditions.
if ((i + 1) < lines.length()) {
if (lines.charAt(i + 1) == '\n') {
++i;
s[0] += lines.substring(i, i + 1);
}
}
break;
}
if (lines.charAt(i) == '\n')
break;
}

// Write what remains in lines in the 2nd cell
if (s[0].length() < lines.length())
s[1] = lines.substring(s[0].length());
char c = s[0].charAt(s[0].length() - 1);

// If there is only one line, first cell is empty, 2nd has the line.
if (c != '\r' && c != '\n') {
s[1] = s[0];
s[0] = "";
}

return s;
}

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

import java.io.File;
import java.util.Arrays;

/**
* This implementation of the IFileExplorer interface performs a depth-first
Expand All @@ -17,7 +18,24 @@ 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);
File[] lFiles = rootDirectory.listFiles();
if (lFiles != null)
Arrays.sort(lFiles);
if (lFiles != null) {
for (File file : lFiles) {
if (file.isDirectory()) {
explore(file, vistor);
}
}
}
if (lFiles != null) {
for (File file : lFiles) {
if (!file.isDirectory()) {
vistor.visit(file);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,95 @@
* When filter encounters a line separator, it sends it to the decorated writer.
* It then sends the line number and a tab character, before resuming the write
* process.
*
* <p>
* Hello\n\World -> 1\Hello\n2\tWorld
*
* @author Olivier Liechti
*/
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());

private int nbLines = 0;
boolean isLastIsN = false;
boolean isLastIsR = false;

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

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

// Creation of the new String to write
String newStr = "";

// Keep trace of new char
int newChar = 0;

// Condition for first line
if (nbLines == 0) {
newStr = str.substring(0, off) + "1\t";
++nbLines;
newChar += 2;
}
newStr += str.substring(off);

// Add nbLines + \t when there is a line separator
for (int i = 0; i < newStr.length(); ++i) {
if (newStr.charAt(i) == '\r' || newStr.charAt(i) == '\n') {
++nbLines;
++i;
if (i < newStr.length()) {
if (newStr.charAt(i) == '\n' && newStr.charAt(i-1) == '\r') {
++i;
}
}
newStr = newStr.substring(0, i) + Integer.toString(nbLines) + "\t" + newStr.substring(i);
newChar += 1;
newChar += Integer.toString(nbLines).length();
}
}

// Keep trace for write(int c)
isLastIsN = newStr.charAt(newStr.length() - 1) == '\n';
isLastIsR = newStr.charAt(newStr.length() - 1) == '\r';

// Write
out.write(newStr, off, len + newChar);
}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
// Simply send it to String version.
String str = new String(cbuf);
write(str, off, len);
}

public FileNumberingFilterWriter(Writer out) {
super(out);
}
@Override
public void write(int c) throws IOException {

@Override
public void write(String str, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
}
// First line conditions
if (nbLines == 0) {
++nbLines;
out.write('1');
out.write('\t');
}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
}
// If last is a separator line, then write new line
// Except if next char is the 2nd is the separator line for windows condition
if (isLastIsR || isLastIsN) {
if (c != '\n') {
++nbLines;
out.write(Integer.toString(nbLines));
out.write('\t');
}
}

@Override
public void write(int c) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
}
// Keep trace and write current char
isLastIsR = c == '\r';
isLastIsN = c == '\n';
out.write(c);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*
* @author Olivier Liechti
*/

//initial commit
public class UpperCaseFilterWriter extends FilterWriter {

public UpperCaseFilterWriter(Writer wrappedWriter) {
Expand All @@ -16,17 +18,22 @@ 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.");
String newStr = str.substring(0, off);
newStr += str.substring(off, off + len).toUpperCase();
newStr += str.substring(off + len);
out.write(newStr, 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)
cbuf[i + off] = Character.toUpperCase(cbuf[i + off]);
out.write(cbuf, off, len);
}

@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,16 +18,19 @@ 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 (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
*
* => Done
*/
//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 @@ -51,7 +51,14 @@ public void visit(File file) {
* 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.
*
* => Done
*/
int i;

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

reader.close();
writer.flush();
Expand Down
Loading