BehDemeterJPrintVisitor { {{ private BufferedWriter file; private final static String START_JAVA_TEXT = "{"+"{", END_JAVA_TEXT = "}"+"}"; private int currentLevel; private String checks; }} public void before(CDDef host) throws Exception {{ // Open the output file for writing the behavior file for the validating // visitor try { file = new BufferedWriter(new FileWriter(filename)); } catch (IOException e) { throw new Exception("Error in opening the behavior file for writing."); } currentLevel = 0; writeln("ValidateVisitor {"); writeln(""); indent('+'); writeln(indent() + "public void start() " + START_JAVA_TEXT); indent('+'); indent('-'); writeln(indent() + END_JAVA_TEXT); writeln(""); }} public void after(CDDef host) throws Exception {{ indent('-'); writeln("} // end of class ValidateVisitor"); // Close the output file try { file.close(); } catch (IOException e) { throw new Exception("Error in closing the behavior file after writing."); } }} public void before(DataClass host) throws Exception {{ checks = ""; indent('+'); }} public void after(DataClass host) throws Exception {{ indent('-'); if (!checks.equals("")) { writeln(indent() + "public void before(" + host.get_name() + " host)" + " throws Exception " + START_JAVA_TEXT); write(checks); writeln(indent() + END_JAVA_TEXT); writeln(""); } }} public void before(ClassPart host) throws Exception {{ String partName = host.get_name(); Constraint c = host.get_constraint(); if (c.min_occurs > 1) { checks = checks + indent() + "if (host.get_" + partName + "().size() < " + c.min_occurs + ")\n"; indent('+'); checks = checks + indent() + "throw new Exception (\"Size of '" + partName + "' list is less than " + c.min_occurs + ".\");\n"; indent('-'); } if ((c.max_occurs > 1) && (c.max_occurs < Integer.MAX_VALUE)) { checks = checks + indent() + "if (host.get_" + partName + "().size() > " + c.max_occurs + ")\n"; indent('+'); checks = checks + indent() + "throw new Exception (\"Size of '" + partName + "' list is greater than " + c.max_occurs + ".\");\n"; indent('-'); } if (c.min_value != null) { String op = "<"; if ((c.min_included == null) || (!c.min_included.booleanValue())) op = op + "="; String compare = ""; if ((host.get_type().equals("byte")) || (host.get_type().equals("double")) || (host.get_type().equals("float")) || (host.get_type().equals("int")) || (host.get_type().equals("short"))) { compare = "host.get_" + partName + "() " + op + " " + c.min_value; } else if (host.get_type().equals("char")) { compare = "host.get_" + partName + "() " + op + " '" + c.min_value + "'"; } else { compare = "host.get_" + partName + "().compare(" + c.min_value + ") " + op + " 0"; } checks = checks + indent() + "if (" + compare + ")\n"; indent('+'); checks = checks + indent() + "throw new Exception (\"Value of element '" + partName + "' is " + op + " " + c.min_value + ".\");\n"; indent('-'); } if (c.max_value != null) { String op = ">"; if ((c.max_included == null) || (!c.max_included.booleanValue())) op = op + "="; String compare = ""; if ((host.get_type().equals("byte")) || (host.get_type().equals("double")) || (host.get_type().equals("float")) || (host.get_type().equals("int")) || (host.get_type().equals("short"))) { compare = "host.get_" + partName + " " + op + " " + c.max_value; } else if (host.get_type().equals("char")) { compare = "host.get_" + partName + " " + op + " '" + c.max_value + "'"; } else { compare = "host.get_" + partName + ".compare(" + c.max_value + ") " + op + " 0"; } checks = checks + indent() + "if (" + compare + ")\n"; indent('+'); checks = checks + indent() + "throw new Exception (\"Value of element '" + partName + "' is " + op + " " + c.min_value + ".\");\n"; indent('-'); } }} // Private methods used for formatting text, etc. private void write(String text) throws Exception {{ try { file.write(text); } catch (IOException e) { throw new Exception("Error in writing to behavior file."); } }} private void writeln(String text) throws Exception {{ write(text + "\n"); }} private String indent() throws Exception {{ String text = ""; for (int i = 0; i < currentLevel; i++) text = text + " "; return text; }} private void indent(char c) throws Exception {{ if (c == '+') currentLevel++; else if (c == '-') currentLevel--; }} }