Dia2Code v. 0.4
SUMMARY
This program generates C++ and Java code from an UML Dia Diagram.
ABSTRACT
This program is a small utility that makes code from a Dia diagram.
It's intended purpose is to ease the programmer's work by generating
the structure of the classes in an Object Oriented language
(like C++ and Java) from a graphical representation of them
(a Dia Diagram).
STATUS
The program is useful even if it is only at version 0.4. It can generate C++ and
Java files from the UML specification. Templates are currently not supported,
but will come in the near future.
The generation of #include and import directives is far from perfect. Currently
the program includes files for the parent classes, but not for the attributes nor
for the parameters. You will need to add them by hand.
Dia2Code looks only for Generalizations, Realizations and Implements in the diagram,
not for other relationships, like UML - Dependencies.
Feel free to have a try and send me your comments.
LICENSE
This program is distributed under the GNU GPL. Read the COPYING
file for details.
REQUIREMENTS
I've only tested it under Linux. It should work with orther platforms,
but your mileage may vary.
- Dia (I work with 0.86 but older versions *may* be OK). Not for
the compilation part but to make some nice diagrams with it.
- libxml headers and development libraries (I use 1.8.6 but it *may* be
OK if you use an older version).
- A C compiler.
- automake and autoconf (actually, optional)
INSTALLATION
Usually:
$ ./configure
$ make
# make install
Read the INSTALL file for details.
If that doesn't work for you, try (from the innermost dia2code directory):
$ cc -lxml -o dia2code -O2 *.c
$ strip dia2code
I must apologize for I'm still a newbie with autoconf and automake.
OPERATION
$ dia2code [-t (cpp|java)] [-d <dir>] [-nc] [-cl <classlist>] [-v] <diagramfile>
-t
tells dia2code to either create C++ (cpp) or Java (java) files.
The default is C++.
-d
tells it to put the files in the specified directory.
The default is to output files in the current directory.
-nc
asks dia2code to not overwrite existing files.
-cl <classlist>
generates code only for the classes specified in the
comma-separated <classlist>.
-v
inverts the class list selection. When used alone
prevents any file from being created.
The last parameter (and the only mandatory one) is the diagram
file name.
EXAMPLES
$ dia2code -t java test.dia
Will generate the *.java files for the classes in the test.dia diagram
and put them in the current directory.
$ dia2code -nc -d ~/C++ hw.dia
Will generate the *.cpp and *.h files for the classes in the test.dia
diagram and put them in the directory ~/C++. It won't overwrite any
existant file.
$ dia2code -t java -cl Base,Derived test.dia
Will create the Java files only for classes "Base" and "Derived".
$ dia2code -cl Foo -v foobar.dia
Will create C++ code for all classes but "Foo".
$ dia2code -v test.dia
Will not create any files. Don't know if it may be useful, but it surely
is syntactically correct.
HOW IT WORKS
1. Parse the diagram file with xmlParseFile().
2. Parse the tree generated in 1 for UML classes to build an
umlclasslist (type defined here).
3. Parse the tree again for UML Generalizations and Realizations
to add parent information to the class list.
4. Generate the structure of the classes (write it into files)
from the class list.
Steps 1-3 are done in parse_diagram().
Step 4 is done in generate_code_???().
Both funcions are called from main().
NOTES ON UML
Stereotypes
The Stereotype attribute of a class is not important for C++ code generation,
but it may be so for Java. In C++, an abstract class is not explicitly
declared as it is in Java. To have a class declared as "abstract" you have
to set its "abstract" flag. The "stereotype" is only taken into account if it is
"interface".
Visibility
Dia2Code does not handle the "implementation" visibility for methods (yet).
So when you have a class that implements a method that was declared
abstract in a base class, you have to give it the same visibility of the
parent class' method. The visibility of the method is not printed if it is
"implementation"; this may be a source of bugs.
Method's return type
If you leave the "type" entry in the method declaration empty, then no
type will be declared for it. This is useful with constructors, when the
return type is implicit (both in C++ and Java). If the return type is
void, you should explicitly declare it in the UML diagram. I don't know
if this is a good practice, I just thought it was reasonable. Everyone is
welcome to discuss it.
INFORMATION FOR DEVELOPERS
(more will come in the future)
Code Generators:
A code generator function is a function that takes a pointer to a
batch structure. The structure looks like this:
struct batch {
umlclasslist classlist;
char * outdir;
int clobber;
namelist classes;
int mask;
};
The classes to generate are in classlist.
The output directory is outdir. The files that the generate_code
opens for writing MUST have this output directory as a prefix, UNLESS
it is NULL, in which case, it will default to ".". The function
SHOULD check the length of the directory for possible buffer overflows
inside the function.
If the clobber flag is set, any already existing file in the output
directory MUST NOT be overwritten.
Classes is a list of the classes to create code for. Mask is a
flag that inverts this selection.
Some example code:
generate_code_foo(batch *b){
umlclasslist tmplist;
char *tmpname;
char outfilename[256];
FILE *dummyfile;
if (b->outdir == NULL) {
b->outdir = ".";
}
tmplist = b->classlist;
while ( tmplist != NULL ) {
if ( ! ( is_present(b->classes,tmplist->key->name) ^ b->mask ) ) {
tmpname=strtolower(tmplist->key->name);
/*
Buffer overflow control here
*/
sprintf(outfilename,"%s/%s.h",b->outdir,tmpname);
dummyfile=fopen(outfilename,"r");
if ( b->clobber || ! dummyfile ) {
/*
We generate all the code here
*/
fclose(dummyfile);
}
free(tmpname);
}
tmplist=tmplist->next;
}
}
EXIT CODES
0 OK
1 Not enough memory
2 Wrong parameters in the command line
3 Cannot write to disk
4 Name of output dir plus output file too long
BUGS
Note: some bugs may not be listed here.
- No checking for full filesystem while writing files.
- Does not follow the DTD of a Dia Diagram, rather parses as it sees fit.
- Allocates memory proportionally to the size of the diagrams. It should work
OK with small diagrams but may slow down with BIG ones.
AUTHOR
Javier O'Hara <joh314@users.sourceforge.net>
NOTES
You can write me in English
Mi potete scrivere in Italiano
Me podéis escríbir en Español
THANKS
Thanks to all the people that have contacted me with suggestions
and bug reports. If you haven't received an answering mail back then
it is because I'm having trouble sending it to your ISP, please
contact me again.
---------------------------------------------------------
"If a program is useless, it will have to be documented."