You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2006/07/25 01:13:23 UTC
svn commit: r425218 - in
/db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward:
BeanDescriptor.java BeanGenerator.java RepositoryXmlProcessor.java
SQLStructures.java
Author: arminw
Date: Mon Jul 24 16:13:23 2006
New Revision: 425218
URL: http://svn.apache.org/viewvc?rev=425218&view=rev
Log:
PATCH: forward engineering tool by Tom Antony, see OJB-112
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/BeanDescriptor.java
db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/BeanGenerator.java
db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/RepositoryXmlProcessor.java
db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/SQLStructures.java
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/BeanDescriptor.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/BeanDescriptor.java?rev=425218&r1=425217&r2=425218&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/BeanDescriptor.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/BeanDescriptor.java Mon Jul 24 16:13:23 2006
@@ -31,6 +31,7 @@
private String [] interfaces;
private ArrayList attributes;
private ArrayList types;
+ private ArrayList innerClasses;
/**
* Create a new BeanDescriptor object.
@@ -43,7 +44,7 @@
* @param types types for bean attributes, should match attributes in order
*/
public BeanDescriptor(String packageName, String className, String baseClass,
- String [] interfaces, ArrayList attributes, ArrayList types)
+ String [] interfaces, ArrayList attributes, ArrayList types, ArrayList innerClasses)
{
this.packageName = packageName;
this.className = className;
@@ -51,6 +52,7 @@
this.interfaces = interfaces;
this.attributes = attributes;
this.types = types;
+ this.innerClasses = innerClasses;
}
public String packageName()
@@ -82,4 +84,10 @@
{
return types;
}
+
+ public ArrayList innerClasses()
+ {
+ return innerClasses;
+ }
+
}
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/BeanGenerator.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/BeanGenerator.java?rev=425218&r1=425217&r2=425218&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/BeanGenerator.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/BeanGenerator.java Mon Jul 24 16:13:23 2006
@@ -48,11 +48,19 @@
return s;
}
- private static String generateTypeDeclaration(String attribute, String type)
+ private static String tabs(int indentLevel)
+ {
+ String str = "";
+ for(int i = 0; i < indentLevel; i++)
+ str += "\t";
+ return str;
+ }
+
+ private static String generateTypeDeclaration(String attribute, String type, int indentLevel)
{
StringBuffer buf = new StringBuffer(100);
- buf.append("\n\tprivate ");
+ buf.append("\n" + tabs(indentLevel) + "private ");
buf.append(type);
buf.append(" ");
buf.append(attribute);
@@ -61,11 +69,11 @@
return buf.toString();
}
- private static String generateGetMethod(String attribute, String type)
+ private static String generateGetMethod(String attribute, String type, int indentLevel)
{
StringBuffer buf = new StringBuffer(100);
- buf.append("\n\tpublic ");
+ buf.append("\n" + tabs(indentLevel) + "public ");
buf.append(type);
buf.append(" get");
buf.append(capitalizeMemberField(attribute));
@@ -77,11 +85,11 @@
return buf.toString();
}
- private static String generateSetMethod(String attribute, String type)
+ private static String generateSetMethod(String attribute, String type, int indentLevel)
{
StringBuffer buf = new StringBuffer(100);
- buf.append("\n\tpublic ");
+ buf.append("\n" + tabs(indentLevel) + "public ");
buf.append("void ");
buf.append(" set");
buf.append(capitalizeMemberField(attribute));
@@ -99,6 +107,85 @@
return buf.toString();
}
+ private String generateBeanSource(BeanDescriptor beanDescription)
+ {
+ return generateBeanSource(beanDescription, false, 1);
+ }
+
+ private String generateBeanSource(BeanDescriptor beanDescription, boolean innerClass, int indentLevel)
+ {
+ StringBuffer buf = new StringBuffer(100);
+
+ if(!innerClass)
+ {
+ if(beanDescription.packageName() != null && beanDescription.packageName().trim().length() > 0)
+ {
+ buf.append("package ");
+ buf.append(beanDescription.packageName());
+ buf.append(";\n");
+ }
+ }
+
+ buf.append("\n");
+ buf.append(tabs(indentLevel - 1) + "public class ");
+ buf.append(beanDescription.className());
+
+ if(beanDescription.baseClass() != null)
+ buf.append(" extends ").append(beanDescription.baseClass());
+
+ if(beanDescription.interfaces() != null)
+ {
+ buf.append(" implements ");
+ for(int i = 0; i < beanDescription.interfaces().length; i++)
+ {
+ buf.append(beanDescription.interfaces()[i]);
+ if(i < beanDescription.interfaces().length - 1) buf.append(" , ");
+ }
+ }
+
+ buf.append("\n");
+
+ buf.append(tabs(indentLevel - 1) + "{");
+
+ for(int i = 0; i < beanDescription.attributes().size(); i++)
+ {
+ String attribute = (String) beanDescription.attributes().get(i);
+ String type = (String) beanDescription.types().get(i);
+ buf.append(generateTypeDeclaration(attribute, type, indentLevel));
+ }
+
+ buf.append("\n");
+
+ for(int i = 0; i < beanDescription.attributes().size(); i++)
+ {
+ String attribute = (String) beanDescription.attributes().get(i);
+ String type = (String) beanDescription.types().get(i);
+
+ buf.append(generateSetMethod(attribute, type, indentLevel));
+ buf.append(generateGetMethod(attribute, type, indentLevel));
+
+ buf.append("\n");
+ }
+
+ buf.append("\n");
+
+ for(int i = 0; i < beanDescription.innerClasses().size(); i++)
+ {
+ BeanDescriptor descriptor = (BeanDescriptor) beanDescription.innerClasses().get(i);
+
+ buf.append(generateBeanSource(descriptor, true, indentLevel + 1));
+
+ buf.append("\n");
+ }
+
+ buf.append("\n");
+
+ buf.append(tabs(indentLevel - 1) + "}");
+
+ return buf.toString();
+
+ }
+
/**
* Generate the source for the Java bean.
*
@@ -106,62 +193,20 @@
*/
public void generate(BeanDescriptor beanDescription) throws Exception
{
- StringBuffer buf = new StringBuffer(100);
+ String javaStr = generateBeanSource(beanDescription);
- if(beanDescription.packageName() != null && beanDescription.packageName().trim().length() > 0)
- {
- buf.append("package ");
- buf.append(beanDescription.packageName());
- buf.append(";\n");
- }
-
- buf.append("\n");
- buf.append("public class ");
- buf.append(beanDescription.className());
-
- if(beanDescription.baseClass() != null)
- buf.append(" extends ").append(beanDescription.baseClass());
-
- if(beanDescription.interfaces() != null)
- {
- buf.append(" implements ");
- for(int i = 0; i < beanDescription.interfaces().length; i++)
- {
- buf.append(beanDescription.interfaces()[i]);
- if(i < beanDescription.interfaces().length - 1) buf.append(" , ");
- }
- }
-
- buf.append("\n");
-
- buf.append("{");
-
- for(int i = 0; i < beanDescription.attributes().size(); i++)
- {
- String attribute = (String) beanDescription.attributes().get(i);
- String type = (String) beanDescription.types().get(i);
- buf.append(generateTypeDeclaration(attribute, type));
- }
-
- buf.append("\n");
-
- for(int i = 0; i < beanDescription.attributes().size(); i++)
- {
- String attribute = (String) beanDescription.attributes().get(i);
- String type = (String) beanDescription.types().get(i);
-
- buf.append(generateSetMethod(attribute, type));
- buf.append(generateGetMethod(attribute, type));
-
- buf.append("\n");
- }
+ String finalOutputDirectory = outputDirectory;
- buf.append("\n");
+ if(beanDescription.packageName() != null && beanDescription.packageName().trim().length() > 0)
+ {
+ String packagePath = beanDescription.packageName().replace('.','/');
+ finalOutputDirectory = finalOutputDirectory + "/" + packagePath;
+ }
+ if(! new File(finalOutputDirectory).exists())
+ new File(finalOutputDirectory).mkdirs();
- buf.append("}");
-
- FileWriter fwr = new FileWriter(new File(outputDirectory, beanDescription.className() + ".java"));
- fwr.write(buf.toString());
+ FileWriter fwr = new FileWriter(new File(finalOutputDirectory, beanDescription.className() + ".java"));
+ fwr.write(javaStr);
fwr.close();
}
@@ -169,6 +214,23 @@
{
BeanGenerator gen = new BeanGenerator(null);
+ ArrayList innerAttributes = new ArrayList();
+ ArrayList innerTypes = new ArrayList();
+
+ innerAttributes.add("salary");
+ innerTypes.add("String");
+
+ innerAttributes.add("dept");
+ innerTypes.add("int");
+
+ BeanDescriptor innerDescription = new BeanDescriptor(null, "CustomerInner", null,
+ null,
+ innerAttributes, innerTypes, null
+ );
+
+ ArrayList innerClasses = new ArrayList();
+ innerClasses.add(innerDescription);
+
ArrayList attributes = new ArrayList();
ArrayList types = new ArrayList();
@@ -180,7 +242,7 @@
BeanDescriptor description = new BeanDescriptor(null, "Customer", "Person",
new String []{"XYZ", "ABC"},
- attributes, types
+ attributes, types, innerClasses
);
gen.generate(description);
}
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/RepositoryXmlProcessor.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/RepositoryXmlProcessor.java?rev=425218&r1=425217&r2=425218&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/RepositoryXmlProcessor.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/RepositoryXmlProcessor.java Mon Jul 24 16:13:23 2006
@@ -133,18 +133,18 @@
}
/*
- * Convinience method.
- */
+ * Convinience method.
+ */
private Table getTableInfo(Element clsDescElem)
{
return getTableInfo(clsDescElem, true);
}
- /*
- * Get the table description associated with a <class-descriptor> element.
- * If recurse is true, explore <reference-descriptor> elements, else return after
- * filling in the columns.
- */
+ /*
+ * Get the table description associated with a <class-descriptor> element.
+ * If recurse is true, explore <reference-descriptor> elements, else return after
+ * filling in the columns.
+ */
private Table getTableInfo(Element clsDescElem, boolean recurse)
{
Table table = new Table();
@@ -247,8 +247,8 @@
}
/*
- * Returns set of all table names.
- */
+ * Returns set of all table names.
+ */
private Set allTables(Document doc)
{
Set tableNames = new HashSet();
@@ -263,8 +263,8 @@
}
/*
- * Returns set of names of tables mapped to multiple classes.
- */
+ * Returns set of names of tables mapped to multiple classes.
+ */
private Set duplicateTables(Document doc)
{
Set workingSet = new HashSet();
@@ -284,8 +284,8 @@
}
/*
- * Merge the src table into the target.
- */
+ * Merge the src table into the target.
+ */
private void mergeTables(Table target, Table src)
{
// merge columns
@@ -484,90 +484,237 @@
fwr.close();
}
- /** Generate the source for Java beans mapped to tables. */
+ /**
+ Return bean descriptor for a class descriptor element.
+ This method always returns an empty list for inner classes.
+ */
+ private BeanDescriptor getBeanDescriptor(Element clsDescElem)
+ {
+ String clsName = clsDescElem.getAttribute("class");
+
+ String packageName = null;
+ String className;
+
+ /* Handle inner class names */
+ if(clsName.indexOf("$") != -1)
+ {
+ int index = clsName.indexOf("$");
+ className = clsName.substring(index + 1);
+ }
+ else
+ {
+ int indexDot = clsName.lastIndexOf(".");
+ if(indexDot == -1)
+ className = clsName;
+ else
+ {
+ packageName = clsName.substring(0, indexDot);
+ className = clsName.substring(indexDot + 1);
+ }
+ }
+
+ String baseClass = null;
+ ArrayList attributes = new ArrayList();
+ ArrayList types = new ArrayList();
+
+ NodeList refDescList = clsDescElem.getElementsByTagName("reference-descriptor");
+ for(int j = 0; j < refDescList.getLength(); j++)
+ {
+ Element refDescElem = (Element) refDescList.item(j);
+
+ String fieldName = refDescElem.getAttribute("name");
+ String classRef = refDescElem.getAttribute("class-ref");
+ classRef = classRef.replace('$', '.'); // handle inner classes
+
+ // handle super references
+ if(fieldName.equals("super"))
+ {
+ baseClass = classRef;
+ }
+ else
+ {
+ attributes.add(fieldName);
+ types.add(classRef);
+ }
+ }
+
+ NodeList fldDescList = clsDescElem.getElementsByTagName("field-descriptor");
+ for(int j = 0; j < fldDescList.getLength(); j++)
+ {
+ Element fldDescElem = (Element) fldDescList.item(j);
+
+ String fieldName = fldDescElem.getAttribute("name");
+ String jdbcType = fldDescElem.getAttribute("jdbc-type");
+ String primaryKey = fldDescElem.getAttribute("primarykey");
+ boolean isPrimaryKey = primaryKey != null && primaryKey.equals("true");
+ String type = (String) typeMap.get(jdbcType);
+
+ /*
+ * If baseClass is set using the 'super' reference then primary key
+ * fields are being repeated only for join purpose.
+ */
+ if(baseClass == null || !isPrimaryKey)
+ {
+ attributes.add(fieldName);
+ types.add(type);
+ }
+ }
+
+ NodeList colDescList = clsDescElem.getElementsByTagName("collection-descriptor");
+ for(int j = 0; j < colDescList.getLength(); j++)
+ {
+ Element colDescElem = (Element) colDescList.item(j);
+
+ String fieldName = colDescElem.getAttribute("name");
+ String type = "java.util.List";
+
+ attributes.add(fieldName);
+ types.add(type);
+ }
+
+ ArrayList innerClasses = new ArrayList();
+
+ BeanDescriptor descriptor = new BeanDescriptor(packageName, className, baseClass, null,
+ attributes, types, innerClasses);
+
+ return descriptor;
+ }
+
+ /**
+ * Generate the source for Java beans mapped to tables.
+ *
+ * For inner classes, if the parent is not a persistent class defined in the mapping metadata, then a holder parent
+ * class will be generated to hold the inner classes.
+ */
public void generateJavaCode() throws Exception
{
- NodeList clsDescList = doc.getElementsByTagName("class-descriptor");
+ /*
+ First form a mapping of inner classes to their parent classes, with parent class as the key
+ and a list of inner class names as the value.
+ */
+ HashMap innerOuterClassMap = new HashMap();
+
+ NodeList clsDescList = doc.getElementsByTagName("class-descriptor");
+
+ for(int i = 0; i < clsDescList.getLength(); i++)
+ {
+ Element clsDescElem = (Element) clsDescList.item(i);
+
+ String clsName = clsDescElem.getAttribute("class");
+
+ if(clsName.indexOf("$") != -1)
+ {
+ int index = clsName.indexOf("$");
+ String outerClass = clsName.substring(0,index);
+ String innerClass = clsName.substring(index + 1);
+ if(innerOuterClassMap.get(outerClass) == null)
+ {
+ ArrayList list = new ArrayList();
+ list.add(innerClass);
+ innerOuterClassMap.put(outerClass, list);
+ }
+ else
+ {
+ ArrayList list = (ArrayList) innerOuterClassMap.get(outerClass);
+ list.add(innerClass);
+ }
+ }
+ }
+
+ /* A map holding descriptions for inner classes. */
+ HashMap innerDescriptionsMap = new HashMap();
+
+ /* Also collect the inner class descriptions upfront before generating outer class definitions. */
for(int i = 0; i < clsDescList.getLength(); i++)
{
Element clsDescElem = (Element) clsDescList.item(i);
- String clsName = clsDescElem.getAttribute("class");
+ String clsName = clsDescElem.getAttribute("class");
- String packageName = null;
- String className;
- int indexDot = clsName.lastIndexOf(".");
- if(indexDot == -1)
- className = clsName;
- else
- {
- packageName = clsName.substring(0, indexDot);
- className = clsName.substring(indexDot + 1);
- }
+ if(clsName.indexOf("$") != -1)
+ {
+ int index = clsName.indexOf("$");
+ String innerClass = clsName.substring(index + 1);
- String baseClass = null;
- ArrayList attributes = new ArrayList();
- ArrayList types = new ArrayList();
+ BeanDescriptor descriptor = getBeanDescriptor(clsDescElem);
- NodeList refDescList = clsDescElem.getElementsByTagName("reference-descriptor");
- for(int j = 0; j < refDescList.getLength(); j++)
- {
- Element refDescElem = (Element) refDescList.item(j);
+ innerDescriptionsMap.put(innerClass, descriptor);
+ }
+ }
- String fieldName = refDescElem.getAttribute("name");
- String classRef = refDescElem.getAttribute("class-ref");
+ /* Now generate class definitions for persistent outer classes defined in metadata */
+ for(int i = 0; i < clsDescList.getLength(); i++)
+ {
+ Element clsDescElem = (Element) clsDescList.item(i);
- // handle super references
- if(fieldName.equals("super"))
- {
- baseClass = classRef;
- }
- else
- {
- attributes.add(fieldName);
- types.add(classRef);
- }
- }
+ String clsName = clsDescElem.getAttribute("class");
- NodeList fldDescList = clsDescElem.getElementsByTagName("field-descriptor");
- for(int j = 0; j < fldDescList.getLength(); j++)
- {
- Element fldDescElem = (Element) fldDescList.item(j);
+ if(clsName.indexOf("$") == -1)
+ {
+ BeanDescriptor descriptor = getBeanDescriptor(clsDescElem);
- String fieldName = fldDescElem.getAttribute("name");
- String jdbcType = fldDescElem.getAttribute("jdbc-type");
- String primaryKey = fldDescElem.getAttribute("primarykey");
- boolean isPrimaryKey = primaryKey != null && primaryKey.equals("true");
- String type = (String) typeMap.get(jdbcType);
-
- /*
- * If baseClass is set using the 'super' reference then primary key
- * fields are being repeated only for join purpose.
- */
- if(baseClass == null || !isPrimaryKey)
- {
- attributes.add(fieldName);
- types.add(type);
- }
- }
+ /* Set innerClasses list (if present) for the outer class. */
- NodeList colDescList = clsDescElem.getElementsByTagName("collection-descriptor");
- for(int j = 0; j < colDescList.getLength(); j++)
- {
- Element colDescElem = (Element) colDescList.item(j);
+ ArrayList innerDescriptors = descriptor.innerClasses();
- String fieldName = colDescElem.getAttribute("name");
- String type = "java.util.List";
+ ArrayList innerClassList = (ArrayList) innerOuterClassMap.get(clsName);
- attributes.add(fieldName);
- types.add(type);
- }
+ if(innerClassList != null)
+ {
+ for(int j = 0; j < innerClassList.size(); j++)
+ {
+ String innerClass = (String) innerClassList.get(j);
- BeanDescriptor descriptor = new BeanDescriptor(packageName, className, baseClass, null,
- attributes, types);
- BeanGenerator generator = new BeanGenerator(beanOutputDirectory);
- generator.generate(descriptor);
+ BeanDescriptor innerDescriptor = (BeanDescriptor) innerDescriptionsMap.get(innerClass);
+
+ if(descriptor != null)
+ innerDescriptors.add(innerDescriptor);
+ }
+ }
+
+ BeanGenerator generator = new BeanGenerator(beanOutputDirectory);
+ generator.generate(descriptor);
+
+ /* Remove persistent outer classes from mappings for the next stage. */
+ innerOuterClassMap.remove(clsName);
+ }
}
+
+ /* Generate holder outer classes for inner classes without persistent outer classes defined */
+ Iterator itr = innerOuterClassMap.keySet().iterator();
+ while(itr.hasNext())
+ {
+ String outerClass = (String) itr.next();
+
+ ArrayList innerDescriptors = new ArrayList();
+
+ ArrayList innerClassList = (ArrayList) innerOuterClassMap.get(outerClass);
+ for(int i = 0; i < innerClassList.size(); i++)
+ {
+ String innerClass = (String) innerClassList.get(i);
+ BeanDescriptor innerDescriptor = (BeanDescriptor) innerDescriptionsMap.get(innerClass);
+ innerDescriptors.add(innerDescriptor);
+ }
+
+ String packageName = null;
+ String className;
+ int indexDot = outerClass.lastIndexOf(".");
+ if(indexDot == -1)
+ className = outerClass;
+ else
+ {
+ packageName = outerClass.substring(0, indexDot);
+ className = outerClass.substring(indexDot + 1);
+ }
+
+ /* Generate an artificial outer class for holding the inner classes */
+ BeanDescriptor descriptor = new BeanDescriptor(packageName, className, null, null,
+ new ArrayList(), new ArrayList(), innerDescriptors);
+
+ BeanGenerator generator = new BeanGenerator(beanOutputDirectory);
+ generator.generate(descriptor);
+
+ }
}
Element findClass(Document doc, String name)
@@ -641,10 +788,11 @@
public static void main(String [] args) throws Exception
{
- RepositoryXmlProcessor processor = new RepositoryXmlProcessor
- ("repository.xml", "target/test", "target/test");
// RepositoryXmlProcessor processor = new RepositoryXmlProcessor
// ("repository.xml", "target/test", "target/test");
+ RepositoryXmlProcessor processor = new RepositoryXmlProcessor
+ ("E:/repository_test.xml", "target/test", "target/test");
+
processor.generateSQL();
processor.generateJavaCode();
}
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/SQLStructures.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/SQLStructures.java?rev=425218&r1=425217&r2=425218&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/SQLStructures.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/tools/org/apache/ojb/tools/mapping/forward/SQLStructures.java Mon Jul 24 16:13:23 2006
@@ -121,6 +121,7 @@
public String toString()
{
+
return ToStringBuilder.reflectionToString(this);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org