//caso o packageName seja null retira a declaracao do package no template
if (debug >= 1) System.out.println(" Inserting package name in template...");
if (packageName == null) {
tableTemplate = new Regex("\\<packageDeclaration.*;\\s*\\>").substitute(tableTemplate, "");
} else {
tableTemplate = new Regex("\\<packageDeclaration\\s*(.*;\\s*)\\>").substitute(tableTemplate, "$1");
tableTemplate = new Regex("\\<packageName\\>", Regex.DEFAULT_MASK).substitute(tableTemplate, packageName, Regex.SUBSTITUTE_ALL);
}
//le a declaracao de um field do template
if (debug >= 1) System.out.println(" Reading field declaration from template...");
Regex regex1 = new Regex("\\<fieldDeclaration(.*;)\\>");
if (!regex1.contains(tableTemplate)) {
throw new Exception("invalid syntax in template::field declaration");
}
String fieldDeclaration = regex1.getGroup(1);
//substitui a declaracao completa, pela tag de declaracao
if (debug >= 1) System.out.println(" Replacing field declaration in template...");
tableTemplate = new Regex("(\\<fieldDeclaration).*;(\\>)").substitute(tableTemplate, "$1$2");
//le a declaracao de um table field do template
if (debug >= 1) System.out.println(" Reading tableField declaration from template...");
Regex regex2 = new Regex("\\<tableFieldDeclaration(.*;)\\>");
if (!regex2.contains(tableTemplate))
throw new Exception("invalid syntax in template::table field declaration");
String tableFieldDeclaration = regex2.getGroup(1);
//substitui a declaracao completa, pela tag de declaracao
if (debug >= 1) System.out.println(" Replacing tableField declaration in template...");
tableTemplate = new Regex("(\\<tableFieldDeclaration).*;(\\>)").substitute(tableTemplate, "$1$2");
//le a declaracao do metodo de adicionar uma tabela
if (debug >= 1) System.out.println(" Reading addTable declaration from template...");
Regex regex3 = new Regex("\\<addTableDeclaration(.*;)\\>");
if (!regex3.contains(tableTemplate)) {
throw new Exception("invalid syntax in template::add table declaration");
}
String addTableDeclaration = regex3.getGroup(1);
//substitui a declaracao completa, pela tag de declaracao
if (debug >= 1) System.out.println(" Replacing table declaration from template...");
tableTemplate = new Regex("(\\<addTableDeclaration).*;(\\>)").substitute(tableTemplate, "$1$2");
//le a declaracao do metodo de adicionar um field a Table
if (debug >= 1) System.out.println(" Reading addField declaration from template...");
Regex regex4 = new Regex("\\<addFieldDeclaration(.*;)\\>");
if (!regex4.contains(tableTemplate)) {
throw new Exception("invalid syntax in template::add field declaration");
}
String addFieldDeclaration = regex4.getGroup(1);
//substitui a declaracao completa, pela tag de declaracao
if (debug >= 1) System.out.println(" Replacing addField declaration from template...");
tableTemplate = new Regex("(\\<addFieldDeclaration).*;(\\>)").substitute(tableTemplate, "$1$2");
if (debug >= 1) System.out.println(" Making templates for each view...");
for (Enumeration en = views.keys(); en.hasMoreElements(); ) {
//comeca a gerar uma nova view
String newTable = tableTemplate;
String viewName = (String) en.nextElement();
if (debug >= 2) System.out.println(" Replacing view name tag by: '"+viewName+"'...");
newTable = new Regex("\\<viewName\\>", Regex.DEFAULT_MASK).substitute(newTable, viewName, Regex.SUBSTITUTE_ALL);
if (debug >= 2) System.out.println(" Getting view '"+viewName+"'...");
Hashtable view = (Hashtable) views.get(viewName);
if (debug >= 2) System.out.println(" Getting view table name...");
String tableName = (String) view.get("tableName");
if (debug >= 2) System.out.println(" Getting table '"+tableName+"'...");
Hashtable table = (Hashtable) tables.get(tableName);
if (debug >= 2) System.out.println(" Getting table class name...");
String tableClassName = (String) table.get("className");
if (debug >= 2) System.out.println(" Getting view class name...");
String viewClassName = (String) view.get("className");
if (debug >= 2) System.out.println(" Getting view full class name...");
String fullViewClassName = (String) view.get("fullClassName");
if (debug >= 2) System.out.println(" Replacing viewClassName tag '"+viewClassName+"'...");
newTable = new Regex("<viewClassName>", Regex.DEFAULT_MASK).substitute(newTable, viewClassName, Regex.SUBSTITUTE_ALL);
if (debug >= 2) System.out.println(" Replacing fullViewClassName tag '"+fullViewClassName+"'...");
newTable = new Regex("<fullViewClassName>", Regex.DEFAULT_MASK).substitute(newTable, fullViewClassName, Regex.SUBSTITUTE_ALL);
if (debug >= 2) System.out.println(" Replacing className tag '"+tableClassName+"'...");
newTable = new Regex("<className>", Regex.DEFAULT_MASK).substitute(newTable, tableClassName, Regex.SUBSTITUTE_ALL);
String viewType = (String) view.get("type");
newTable = new Regex("<viewType>", Regex.DEFAULT_MASK).substitute(newTable, viewType, Regex.SUBSTITUTE_ALL);
int max = Integer.parseInt((String) view.get("max"));
Hashtable fields = (Hashtable) view.get("fields");
Vector orderedFields = (Vector) view.get("orderedFields");
Vector viewTables = (Vector) view.get("tables");
StringBuffer fieldDeclarations = new StringBuffer();
StringBuffer tableFieldDeclarations = new StringBuffer();
StringBuffer addTableDeclarations = new StringBuffer();
StringBuffer addFieldDeclarations = new StringBuffer();
for (int i = 1; i < viewTables.size(); i++) {
String thisTableName = (String) viewTables.elementAt(i);
String newAddTableDeclaration = new Regex("<className>", Regex.DEFAULT_MASK).substitute(addTableDeclaration, thisTableName+"Table", Regex.SUBSTITUTE_ALL);
if (addTableDeclarations.length() != 0) addTableDeclarations.append(lineSeparator);
addTableDeclarations.append(newAddTableDeclaration);
}
for (Enumeration enum1 = orderedFields.elements(); enum1.hasMoreElements(); ) {
String fieldName = (String) enum1.nextElement();
Hashtable field = (Hashtable) fields.get(fieldName);
String fieldId = (String) field.get("id");
String fieldTableName = (String) field.get("tableName");
String tableFieldId = getFieldId((String) field.get("tableFieldName"));
fieldId = StringUtil.appendChars(fieldId, ' ', max);
String newFieldDeclaration = new Regex("<fieldId>").substitute(fieldDeclaration, fieldId);
newFieldDeclaration = new Regex("<fieldName>").substitute(newFieldDeclaration, fieldName);
String newTableFieldDeclaration = new Regex("<fieldId>").substitute(tableFieldDeclaration, fieldId);
newTableFieldDeclaration = new Regex("<tableName>").substitute(newTableFieldDeclaration, fieldTableName + "Table");
newTableFieldDeclaration = new Regex("<tableFieldId>").substitute(newTableFieldDeclaration, tableFieldId);
String newAddFieldDeclaration = new Regex("<fieldId>", Regex.DEFAULT_MASK).substitute(addFieldDeclaration, fieldId, Regex.SUBSTITUTE_ALL);
newAddFieldDeclaration = new Regex("<className>", Regex.DEFAULT_MASK).substitute(newAddFieldDeclaration, fieldTableName+"Table", Regex.SUBSTITUTE_ALL);
if (sizes) {
String fieldSize = (String) field.get("size");
if (fieldSize == null) fieldSize = "";
newAddFieldDeclaration = new Regex("<fieldSize>").substitute(newAddFieldDeclaration, fieldSize);
} else {
newAddFieldDeclaration = new Regex(",\\s+<fieldSize>").substitute(newAddFieldDeclaration, "");
}
if (fieldDeclarations.length() != 0) fieldDeclarations.append(lineSeparator);
fieldDeclarations.append(newFieldDeclaration);
if (tableFieldDeclarations.length() != 0) tableFieldDeclarations.append(lineSeparator);
tableFieldDeclarations.append(newTableFieldDeclaration);
if (addFieldDeclarations.length() != 0) addFieldDeclarations.append(lineSeparator);
addFieldDeclarations.append(newAddFieldDeclaration);
}
newTable = new Regex("<fieldDeclaration>").substitute(newTable, fieldDeclarations.toString()+lineSeparator);
newTable = new Regex("<tableFieldDeclaration>").substitute(newTable, tableFieldDeclarations.toString());
newTable = new Regex("<addTableDeclaration>").substitute(newTable, addTableDeclarations.toString());
newTable = new Regex("<addFieldDeclaration>").substitute(newTable, addFieldDeclarations.toString());
writeFile(destinationDir+fileSeparator+viewName+"ViewTable.java", newTable);
}
} catch (Exception e) {
System.err.println("ERROR: "+e.getMessage());