Formatter { {{ private BufferedWriter file; private int currentLevel = 0; private int CDLeftMargin = 0; private boolean errorCondition = true; private String errorText = "Writer not initialized"; private static String METHOD_START = "", METHOD_END = "", VAR_START = "", VAR_END = ""; }} public void open() {{ // Open the output file for writing try { file = new BufferedWriter(new FileWriter(filename)); errorCondition = false; currentLevel = 0; CDLeftMargin = 16; if (behCodeType.equals("DEMETERJ")) { METHOD_START = "{" + "{"; METHOD_END = "}" + "}"; VAR_START = "{" + "{"; VAR_END = "}" + "}"; } else if (behCodeType.equals("DJ")) { METHOD_START = "{"; METHOD_END = "}"; VAR_START = ""; VAR_END = ""; } else if (behCodeType.equals("DJ")) { errorCondition = true; errorText = "Unknown behavior code type"; } } catch (IOException e) { errorCondition = true; errorText = "Error in opening file '" + filename + "' for writing"; } }} public void close() {{ if (errorCondition) return; // Close the output file try { file.close(); } catch (IOException e) { errorCondition = true; errorText = "Error in closing file '" + filename + "'"; } }} public void skipLine() {{ writeln(""); }} public void startClass(String classname) {{ if (errorCondition) return; if (behCodeType.equals("DEMETERJ")) writeln(classname + " {"); else if (behCodeType.equals("DJ")) writeln("public class " + classname + " {"); changeIndent('+'); }} public void endClass(String classname) {{ if (errorCondition) return; changeIndent('-'); writeln(""); writeln("} // end of class " + classname); }} public void declareVariable(String declaration) {{ writeln(indent() + VAR_START); writeln(indent() + declaration); writeln(indent() + VAR_END); }} public void startMethod(String signature) {{ if (errorCondition) return; writeln(""); writeln(indent() + signature + " " + METHOD_START); changeIndent('+'); }} public void endMethod() {{ if (errorCondition) return; changeIndent('-'); writeln(indent() + METHOD_END); }} public void startVisitorMethod(String methodName, String className) {{ if (errorCondition) return; String signature = ""; if (behCodeType.equals("DEMETERJ")) signature = methodName + " " + className; else // behCodeType.equals("DJ") signature = "public void " + methodName + "(" + className + " host)"; startMethod(signature); }} public void declareAdaptiveMethod(String signature, String strategy, String visitors[]) {{ if (errorCondition) return; writeln(""); if (behCodeType.equals("DEMETERJ")) { String visitorNames = ""; for (int i = 0; i < visitors.length; i++) { if (!visitorNames.equals("")) visitorNames = visitorNames + ", "; visitorNames = visitorNames + visitors[i]; } writeTextLine(signature + " " + strategy + " (" + visitorNames + ");", ' '); } else if (behCodeType.equals("DJ")) { startMethod(signature); String vList = ""; for (int i = 0; i < visitors.length; i++) { String vVar = "v" + (i+1); writeTextLine(visitors[i] + " " + vVar + " = new " + visitors[i] + "();", ' '); if (!vList.equals("")) vList = vList + ", "; vList = vList + vVar; } writeTextLine("Visitor[] v = new Visitor[] {" + vList + "};", ' '); writeTextLine("ClassGraph cg = new ClassGraph(true, false);", ' '); writeTextLine("TraversalGraph tg = new TraversalGraph(\"" + strategy + "\", cg);", ' '); writeTextLine("return tg.traverse(this, v);", ' '); endMethod(); } }} public void writeTextLine(String text) {{ writeTextLine(text, ' '); }} public void writeTextLine(String text, char indentChange) {{ if (errorCondition) return; if (CD_Beh_flag.equals("CD")) writeln(text); else { writeln(indent() + text); changeIndent(indentChange); } }} public void writeText(String text) {{ if (errorCondition) return; if (CD_Beh_flag.equals("CD")) write(text); else write(indent() + text); }} public void writeWithNoIndent(String text) {{ if (errorCondition) return; write(text); }} public void writeCDClassName(String className) {{ if (errorCondition) return; String text = className; int noOfTabs = new Double(Math.ceil( (CDLeftMargin - className.length()) / 8.0)).intValue(); for (int i = 0; i < noOfTabs; i++) text = text + "\t"; if (noOfTabs <= 0) text = text + " "; write(text); }} public String indent() {{ String text = ""; if (CD_Beh_flag.equals("CD")) { for (int i = 0; i < (CDLeftMargin/8); i++) text = text + "\t"; text = text + " "; } else { for (int i = 0; i < currentLevel; i++) text = text + " "; } return text; }} public void changeIndent(char c) {{ if (c == '+') currentLevel++; else if (c == '-') currentLevel--; }} public String getErrors() {{ if (!errorCondition) return ""; else return errorText; }} private void write(String text) {{ if (errorCondition) return; try { file.write(text); } catch (IOException e) { errorCondition = true; errorText = "Error in writing to file '" + filename + "'"; } }} private void writeln(String text) {{ if (errorCondition) return; write(text + "\n"); }} }