package org.softlang.company.features;
import static org.softlang.company.features.recognizer.Token.*;
import org.softlang.company.features.recognizer.Token;
import org.softlang.company.features.recognizer.Recognizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.io.IOException;
public class Cut {
private Recognizer recognizer;
private Writer writer;
int indent = 0;
// Local write that swallows checked exception, too
private void write(String s) {
try {
writer.write(s);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// Convenient macros for pretty printing
private void space() { write(" "); }
private void nl() { write("\n"); }
private void right() { indent += 3; }
private void left() { indent -= 3; }
private void indent() { for(int i=0; i<indent; i++) space(); }
public void cut(String in, String out) throws IOException {
recognizer = new Recognizer(in);
writer = new OutputStreamWriter(new FileOutputStream(out));
Token current = null;
Token previous = null;
String lexeme = null;
while (recognizer.hasNext()) {
current = recognizer.next();
lexeme = recognizer.getLexeme();
// Cut salary in half
if (current==FLOAT && previous==SALARY)
lexeme = Double.toString(
(Double.parseDouble(recognizer.getLexeme())
/ 2.0d));
// Adjust indentation
if (current==OPEN) right();
if (current==CLOSE) left();
// Add linebreaks
if (current==DEPARTMENT
|| current==MANAGER
|| current==EMPLOYEE
|| current==ADDRESS
|| current==SALARY
|| current==CLOSE) {
nl();
indent();
}
// Copy possibly modified lexeme
write(lexeme + " ");
previous = current;
}
writer.close();
}
}