GenerateBasicMethods { public static String generateXMLDocCode(String filename, String behCodeType) throws Exception {{ Formatter f = new Formatter(filename, "Beh", behCodeType); f.open(); f.startClass("XMLDoc"); f.startMethod("public static XMLDoc XMLparse(String in)" + " throws Exception"); f.writeTextLine("String errors;"); f.skipLine(); f.writeTextLine("XMLDoc doc = parse(Preprocessor.process(in));"); f.skipLine(); f.writeTextLine("errors = doc.init();"); f.writeTextLine("if (!errors.equals(\"\"))", '+'); f.writeTextLine("throw new ParseException(errors);", '-'); f.skipLine(); f.writeTextLine("errors = doc.validate();"); f.writeTextLine("if (!errors.equals(\"\"))", '+'); f.writeTextLine("throw new ParseException(errors);", '-'); f.skipLine(); f.writeTextLine("return doc;"); f.endMethod(); f.startMethod("public static XMLDoc XMLparse(InputStream in)" + " throws Exception"); f.writeTextLine("return XMLparse(Preprocessor.process(readContent(in)));"); f.endMethod(); f.startMethod("public static XMLDoc XMLparse(Reader in)" + " throws Exception"); f.writeTextLine("return XMLparse(Preprocessor.process(readContent(in)));"); f.endMethod(); f.startMethod("private static String readContent(InputStream in)" + " throws Exception"); f.writeTextLine("String text = \"\";"); f.writeTextLine("try {", '+'); f.writeTextLine("byte buf[] = new byte[1000];"); f.writeTextLine("while (true)"); f.writeTextLine("{", '+'); f.writeTextLine("int size = in.read(buf);"); f.writeTextLine("if (size <= 0) break;"); f.writeTextLine("String line = new String(buf, 0, size);"); f.writeTextLine("text = text + line;", '-'); f.writeTextLine("}"); f.writeTextLine("in.close();"); f.writeTextLine("return text;", '-'); f.writeTextLine("}"); f.writeTextLine("catch (IOException e) {", '+'); f.writeTextLine("throw new Exception(\"Error in reading input\");", '-'); f.writeTextLine("}"); f.endMethod(); f.startMethod("private static String readContent(Reader in)" + " throws Exception"); f.writeTextLine("BufferedReader r = new BufferedReader(in);"); f.writeTextLine("String text = \"\";"); f.writeTextLine("try {", '+'); f.writeTextLine("while (true)"); f.writeTextLine("{", '+'); f.writeTextLine("String line = r.readLine();"); f.writeTextLine("if (line == null) break;"); f.writeTextLine("if (!text.equals(\"\")) text = text + \"\\n\";"); f.writeTextLine("text = text + line;", '-'); f.writeTextLine("}"); f.writeTextLine("r.close();"); f.writeTextLine("return text;", '-'); f.writeTextLine("}"); f.writeTextLine("catch (IOException e) {", '+'); f.writeTextLine("throw new Exception(\"Error in reading input\");", '-'); f.writeTextLine("}"); f.endMethod(); f.skipLine(); f.declareAdaptiveMethod("public String init()", "bypassing -> *,tail,* to *", new String[] {"InitVisitor"}); f.declareAdaptiveMethod("public String validate()", "bypassing -> *,tail,* to *", new String[] {"ValidateVisitor"}); f.declareAdaptiveMethod("public void print()", "bypassing -> *,tail,* to *", new String[] {"DisplayVisitor"}); f.endClass("XMLDoc"); f.close(); return f.getErrors(); }} public static String generatePreprocessorCode(String filename, String behCodeType) throws Exception {{ Formatter f = new Formatter(filename, "Beh", behCodeType); f.open(); f.startClass("Preprocessor"); f.startMethod("public static String process(String in)"); f.writeTextLine("return in;"); f.endMethod(); f.endClass("Preprocessor"); f.close(); return f.getErrors(); }} }