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

First release #14

Open
wants to merge 8 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
14 changes: 14 additions & 0 deletions LabJavaIO/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
<name>RES Lab Java IO</name>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<!-- <addClasspath>true</addClasspath> -->
<mainClass>ch.heigvd.res.labio.impl.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
Expand Down
42 changes: 37 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 @@ -7,10 +7,8 @@
import ch.heigvd.res.labio.interfaces.IFileVisitor;
import ch.heigvd.res.labio.quotes.QuoteClient;
import ch.heigvd.res.labio.quotes.Quote;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -90,6 +88,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).
*/
this.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 +122,35 @@ 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.");
//throw new UnsupportedOperationException("The student has not implemented this method yet.");

File dir = new File(WORKSPACE_DIRECTORY );
if (! dir.exists()){
dir.mkdir();
}

if(quote.getTags().isEmpty()){
BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath()+ "/" + filename));
writer.write(quote.getQuote());

writer.close();
}

String folderToSave = WORKSPACE_DIRECTORY;

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

folderToSave += "/" + tag;
dir = new File(folderToSave);
if (! dir.exists()){
dir.mkdirs();
}
}

BufferedWriter writer = new BufferedWriter(new FileWriter(folderToSave + "/" + filename));
writer.write(quote.getQuote());

writer.close();
}

/**
Expand All @@ -140,6 +167,11 @@ 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");
} catch(java.io.IOException e){

}
}
});
}
Expand Down
36 changes: 35 additions & 1 deletion LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ch.heigvd.res.labio.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

/**
Expand All @@ -20,7 +23,38 @@ 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.");
String[] result = new String[2];
result[0] = new String("");
boolean containsLineSep = true;
List<String> t;
String separator ="";


if(lines.contains("\n\r")){
separator = "\n\r";
} else if(lines.contains("\n")){
separator = "\n";
} else if(lines.contains("\r")){
separator = "\r";
} else {
containsLineSep = false;
}

t = new ArrayList<>(Arrays.asList(lines.split(separator)));

if(containsLineSep) {
result[1] = new String("");
result[0] = (t.get(0)+separator);
t.remove(0);
for(String s : t){
result[1] = result[1].concat(s+separator);
}
}
else
result[1] = lines;

return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ public class DFSFileExplorer implements IFileExplorer {

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

for (File f : rootDirectory.listFiles()) {

if (f.isDirectory())
explore(f, vistor);
else
vistor.visit(f);

}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
* @author Olivier Liechti
*/
public class FileNumberingFilterWriter extends FilterWriter {

int cpt = 1;
String strLineNr = "";
boolean firstLine = true;
boolean lastwasr = false;
private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName());

public FileNumberingFilterWriter(Writer out) {
Expand All @@ -25,17 +28,49 @@ public FileNumberingFilterWriter(Writer out) {

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

if(firstLine) {
strLineNr = String.valueOf(cpt++);
super.out.write(strLineNr, 0, strLineNr.length());
super.out.write('\t');
firstLine = false;
}

for (int i = off; i < off + len; i++){
if(cbuf[i] == '\n' || (cbuf[i] == '\r')){
super.out.write(cbuf[i]);
if(!(i+1 < len && cbuf[i+1]=='\n')) {
strLineNr = String.valueOf(cpt++);
super.out.write(strLineNr, 0, strLineNr.length());
super.out.write('\t');
}
} else {
super.out.write(cbuf[i]);
}
lastwasr = (cbuf[i] == '\r');
}
}

@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.");
if(firstLine) {
strLineNr = String.valueOf(cpt++);
super.out.write(strLineNr, 0, strLineNr.length());
super.out.write('\t');
firstLine = false;
}
super.out.write(c);
if(c == '\n'){
firstLine = true;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
//throw new UnsupportedOperationException("The student has not implemented this method yet.");
for(int i = off; i < off+len; i++)
this.write(cbuf[i]);
}

@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.");
if(c >= 97 && c <= 122)
super.out.write(c-32);
else
super.out.write(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 @@ -16,15 +19,15 @@ public class CompleteFileTransformer extends FileTransformer {
@Override
public Writer decorateWithFilters(Writer writer) {
if (true) {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
//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
@@ -1,6 +1,8 @@
package ch.heigvd.res.labio.impl.transformers;

import ch.heigvd.res.labio.interfaces.IFileVisitor;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -58,6 +60,7 @@ 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.
*/
IOUtils.copy(reader,writer);

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

}