[dia] Bug 631848 - Improvements to Java code generation



commit ee4dafafb52aee808c778304d8466035f839d18c
Author: Hans Breuer <hans breuer org>
Date:   Sun Oct 17 20:36:49 2010 +0200

    Bug 631848 - Improvements to Java code generation
    
    Patch from Manuel Argüelles:
    a patch for codegen.py in JavaRenderer class. It works fine on Linux.
    Bugs fixed:
      * Visibilities "private" and "protected" were reversed.
      * Comments for classes, attributes, methods and methods parameters.
      * Comments are divided into lines if they exceed 79 characters.
      * Splits the classes in separate files.
      * Not write "NULL" in empty comments.
      * Writes the default values of the arguments of the methods.
      * Makes public all the classes and interfaces.
      * Not write "static" in all the methods.
    Features:
      * Comments for classes, attributes, methods and methods parameters are
        supported. Uses a comment style javaDoc-like. The comments are
        divided into lines if they exceed 79 characters.
      * Splits the classes in separate files. Creates a file that contains
        the dia/codegen.py firm :)
    
    Merge to master and not doing file path gymnastics by rfind("/"),
    which fails on win32 by me.

 plug-ins/python/codegen.py |   97 ++++++++++++++++++++++++++++++++++----------
 1 files changed, 75 insertions(+), 22 deletions(-)
---
diff --git a/plug-ins/python/codegen.py b/plug-ins/python/codegen.py
index 2b49766..6d439df 100644
--- a/plug-ins/python/codegen.py
+++ b/plug-ins/python/codegen.py
@@ -387,21 +387,52 @@ class PascalRenderer(ObjRenderer) :
 		f.close()
 		ObjRenderer.end_render(self)
 
+# ######################################################################
+# JavaRenderer: export Dia UML diagram to Java
+# improved by: Manuel Arguelles
+#
+# Features:
+#	* Comments for classes, attributes, methods and methods parameters are
+#	  supported. Uses a comment style javaDoc-like. The comments are
+#     divided into lines if they exceed 79 characters.
+#	* Splits the classes in separate files. Creates a file that contains
+#	  the dia/codegen.py firm :)
+#
+# Fixes:
+#	* Visibilities "private" and "protected" were reversed.
+#   * Comments for classes, attributes, methods and methods parameters.
+#	* Comments are divided into lines if they exceed 79 characters.
+#	* Splits the classes in separate files.
+#	* Not write "NULL" in empty comments.
+#	* Writes the default values of the arguments of the methods.
+#	* Makes public all the classes and interfaces.
+#	* Not write "static" in all the methods.
+# ######################################################################
+
 class JavaRenderer(ObjRenderer) :
 	def __init__(self) :
 		ObjRenderer.__init__(self)
 		
 	def end_render(self) :
-		f = open(self.filename, "w")
-		
 		visibilities = {0:"public", 1:"private", 2:"protected"}
 
+		mainfile = open(self.filename, "w")
+		mainfile.write("/* Generated by dia/codegen.py\n *\n * Generated files:\n")
+		#mainfile.close()
 		for name, klass in self.klasses.iteritems() :
-			if len(klass.comment) > 0 :
-				f.write ("/*" + klass.comment + "*/\n")
-			if klass.inheritance_type == "template": classtype = "interface"
-			elif klass.inheritance_type == "abstract": classtype = "abstract class"
-			else: classtype = "class"
+			# splits the classes in separate files
+			classfile = self.filename[:self.filename.rfind("/")+1] + name.capitalize() + ".java"
+			f = open(classfile, "w")
+			# class comment
+			f.write("/**\n")
+			if len(klass.comment) > 1:
+				for l in range(len(klass.comment)/77+1):
+					f.write(" * %s\n" % klass.comment[l*77:(l+1)*77])
+			f.write(" * @author\n *\n */\n")
+
+			if klass.inheritance_type == "template": classtype = "public interface"
+			elif klass.inheritance_type == "abstract": classtype = "public abstract class"
+			else: classtype = "public class"
 			f.write ("%s %s" % (classtype, name))
 			if klass.parents:
 				f.write (" extends %s" % klass.parents[0])
@@ -409,15 +440,21 @@ class JavaRenderer(ObjRenderer) :
 				f.write (" implements %s" % ", ".join(klass.templates))
 			f.write(" {\n")
 			
+			# attributes
 			for attrname, (type, visibility, value, comment) in klass.attributes :
-				#TODO: use comment
+				if comment:
+					f.write("\t/**\n")
+					for l in range(len(comment)/73+1):
+						f.write("\t * %s\n" % comment[l*73:(l+1)*73])
+					f.write("\t */\n")
 				if visibility in visibilities:
 					vis = visibilities[visibility]+" "
 				else: vis = ""
 				f.write("\t%s%s %s" % (vis, type, attrname))
-				if value != "": f.write(" = %s" % value)
+				if value: f.write(" = %s" % value)
 				f.write(";\n")
 			
+			# dafault constructor
 			if not klass.inheritance_type == "template":
 				f.write ("\n\tpublic %s() {\n\t\t\n\t}\n\n" % name)
 			
@@ -429,7 +466,6 @@ class JavaRenderer(ObjRenderer) :
 			for template in klass.templates:
 				parmethods.extend(self.klasses[template].operations)
 			
-			
 			for pName, pMethod in parmethods:
 				pTypes = [p[1] for p in pMethod[2]]
 				for name, pars in [(n,m[2]) for n,m in klass.operations]:
@@ -439,28 +475,45 @@ class JavaRenderer(ObjRenderer) :
 				else: klass.operations.append((pName,pMethod))
 			
 			for methodname, method in klass.operations :
-				if method[4]: f.write("\t/** %s */\n" % method[4])
+				# method comment
+				f.write("\t/**\n")
+				if method[4]:
+					for l in range(len(method[4])/73+1):
+						f.write("\t * %s\n" % method[4][l*73:(l+1)*73])
+				if method[2]:
+					for param in method[2]:
+						f.write("\t * @param %s\n" % param[0]) #param[0]->name
+				f.write("\t * @return %s\n\t */\n" % (method[0]=="" and "void" or method[0]))
 				# if there are no parameter names, something else should appear
 				pars = []
 				v = ord("a")
 				for name, type, value, comment, kind in method[2]:
-					#TODO: also use: value, comment, kind
+					#TODO: also use: kind
 					if not name:
-						pars.append((type,chr(v)))
+						name = chr(v)
 						v += 1
-					else: pars.append((type,name))
-				pars = ", ".join([type+" "+name for type, name in pars])
+					if value:
+						value = "=" + value
+					pars.append((type, name, value))			
+				pars = ", ".join([type+" "+name+value for type, name, value in pars])
 				
 				vis = method[1] in visibilities and visibilities[method[1]] or ""
-				returntype = method[0]=="" and "void" or method[0]
-				inheritance_type = method[3]==0 and "abstract " or ""
-				static = method[4] and "static " or ""
-				f.write("\t%s %s%s%s %s (%s)" % (vis, static, inheritance_type, returntype, methodname, pars))
-				if klass.inheritance_type == "template" or method[3]==0:
+				return_type = method[0] == "" and "void" or method[0]
+				inheritance_type = method[3] == 0 and "abstract " or ""
+				# this is wrong!
+				#static = method[4] and "static " or ""
+				static = ""
+				f.write("\t%s %s%s%s %s(%s)" % (vis, static, inheritance_type, \
+												return_type, methodname, pars))
+				if klass.inheritance_type == "template" or method[3] == 0:
 					f.write(";\n\n")
-				else: f.write(" {\n\t\t\n\t}\n\n")
+				else:
+					f.write(" {\n\t\t// TODO Auto-generated method stub\n\t}\n\n")
 			f.write ("}\n\n")
-		f.close()
+			f.close()
+			mainfile.write(" *\t%s\n" % classfile)
+		mainfile.write(" */\n")
+		mainfile.close()
 		ObjRenderer.end_render(self)
 
 # dia-python keeps a reference to the renderer class and uses it on demand



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]