* 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); + } } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java index 0f41a5dd..73365480 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java @@ -8,6 +8,8 @@ * * @author Olivier Liechti */ + +//initial commit public class UpperCaseFilterWriter extends FilterWriter { public UpperCaseFilterWriter(Writer wrappedWriter) { @@ -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)); } } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformer.java index 4beca482..1656db52 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformer.java @@ -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; /** @@ -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; } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/FileTransformer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/FileTransformer.java index bde833e8..9af4eeb0 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/FileTransformer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/FileTransformer.java @@ -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(); diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java index 5971a302..4d460c9d 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java @@ -13,14 +13,16 @@ 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. + * + * => Done */ - //return writer; + return writer; } } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/quotes/Quote.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/quotes/Quote.java index d02b320e..0c2eee61 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/quotes/Quote.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/quotes/Quote.java @@ -1,5 +1,6 @@ package ch.heigvd.res.labio.quotes; + import lombok.Getter; import lombok.Setter;