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] All tests pass #48

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
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/Teaching-HEIGVD-RES-2020-Labo-Java-IO.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions LabJavaIO/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
Expand Down
48 changes: 36 additions & 12 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,11 @@
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.nio.file.*;
import java.util.List;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
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).
*/
this.storeQuote(quote,("quote-"+Integer.toString(i+1)));
LOG.info("Received a new joke with " + quote.getTags().size() + " tags.");
for (String tag : quote.getTags()) {
LOG.info("> " + tag);
Expand All @@ -104,7 +106,7 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException {
* @throws IOException
*/
void clearOutputDirectory() throws IOException {
FileUtils.deleteDirectory(new File(WORKSPACE_DIRECTORY));
FileUtils.deleteDirectory(new File(WORKSPACE_DIRECTORY));
}

/**
Expand All @@ -122,8 +124,26 @@ void clearOutputDirectory() throws IOException {
* @param filename the name of the file to create and where to store the quote text
* @throws IOException
*/
void storeQuote(Quote quote, String filename) throws IOException {
throw new UnsupportedOperationException("The student has not implemented this method yet.");
void storeQuote(Quote quote, String filename) throws IOException
{
List<String> tags=quote.getTags();
String path=(WORKSPACE_DIRECTORY+"/");
for(String tag:tags)
{
path+=(tag+"/");
}
File file=new File(path);
if ((!file.exists()) && !file.mkdirs())
{
throw new UnsupportedOperationException("Directories not created");
}

file=new File(path+filename+".utf8");
file.createNewFile();
FileWriter writer=new FileWriter(file);
writer.write(quote.getQuote());
writer.flush();
writer.close();
}

/**
Expand All @@ -135,11 +155,15 @@ void printFileNames(final Writer writer) {
explorer.explore(new File(WORKSPACE_DIRECTORY), new IFileVisitor() {
@Override
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).
*/
try
{
writer.write(file.getPath());
writer.write("\n");
}
catch (IOException except)
{
//make Java happy
}
}
});
}
Expand Down
37 changes: 34 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 @@ -9,7 +9,7 @@
public class Utils {

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

private static final int NB_STRINGS_RESULT=2;
/**
* This method looks for the next new line separators (\r, \n, \r\n) to extract
* the next line in the string passed in arguments.
Expand All @@ -19,8 +19,39 @@ public class Utils {
* 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.");
public static String[] getNextLine(String lines)
{
String[] result=new String[NB_STRINGS_RESULT];

for(int x=0;x<lines.length();++x)
{
char c=lines.charAt(x);

if(c=='\r' && x<=(lines.length()-2) && lines.charAt(x+1)=='\n')
{
result=lines.split("\r\n", NB_STRINGS_RESULT);
result[0]+=("\r\n");
break;
}
else if(c=='\r')
{
result=lines.split(String.valueOf(c), NB_STRINGS_RESULT);
result[0]+=c;
break;
}
else if(c=='\n')
{
result=lines.split(String.valueOf(c), NB_STRINGS_RESULT);
result[0]+=c;
break;
}
else
{
result=new String[] {"", lines};
}
}

return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.heigvd.res.labio.interfaces.IFileExplorer;
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 @@ -15,8 +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.");
}
public void explore(File rootDirectory, IFileVisitor vistor)
{
vistor.visit(rootDirectory);

if (rootDirectory.isDirectory())
{
File[] allFiles = rootDirectory.listFiles();
Arrays.sort(allFiles);

for (File f:allFiles)
{
explore(f, vistor);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,56 @@ public class FileNumberingFilterWriter extends FilterWriter {

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

private boolean firstLine;
private int nbLines;
private int previousChar;

public FileNumberingFilterWriter(Writer out) {
super(out);
firstLine=true;
nbLines=0;
}

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

@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("In weird method");
}

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

if(previousChar=='\r' && c !='\n')
{
out.write(writeLineNum());
}

out.write(c);

if(c=='\n')
{
out.write(writeLineNum());
}

previousChar=c;
}

private String writeLineNum() // throws IOException
{
++nbLines;
return(String.valueOf(nbLines)+"\t");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,46 @@
* @author Olivier Liechti
*/
public class UpperCaseFilterWriter extends FilterWriter {


private final int LOWER_MIN =97;
private final int LOWER_MAX = 122;
private final int LOW_UP_GAP=32;

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.");
for(int x=off;x<(off+len);++x)
{
out.write(convertChar(str.charAt(x)));
}
}

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

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

private char convertChar(char c)
{
if(c>=LOWER_MIN && c<=LOWER_MAX)
{
return ((char)(c-=LOW_UP_GAP));
}
else
{
return 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,13 @@ 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 @@ -53,11 +53,18 @@ public void visit(File file) {
Writer writer = new OutputStreamWriter(new FileOutputStream(file.getPath()+ ".out"), "UTF-8"); // the bug fix by teacher
writer = decorateWithFilters(writer);

/*
* 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.
*/
while(true)
{
int c=reader.read();
if(c>(-1))
{
writer.write(c);
}
else
{
break;
}
}

reader.close();
writer.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ 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;
}

}
Loading