umbrello  2.39.2
Umbrello UML Modeller is a Unified Modelling Language (UML) diagram program based on KDE Technology
Public Types | Public Slots | Signals | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
CodeGenerator Class Referenceabstract

#include <codegenerator.h>

Inheritance diagram for CodeGenerator:
AdvancedCodeGenerator SimpleCodeGenerator CPPCodeGenerator DCodeGenerator JavaCodeGenerator RubyCodeGenerator ASWriter AdaWriter CSharpWriter CppWriter DWriter IDLWriter JSWriter JavaWriter PascalWriter PerlWriter Php5Writer PhpWriter PythonWriter RubyWriter SQLWriter TclWriter ValaWriter XMLSchemaWriter

Public Types

enum  GenerationState { Generated , Failed , Skipped }
 

Public Slots

virtual void syncCodeToDocument ()
 

Signals

void codeGenerated (UMLClassifier *classifier, bool generated)
 
void codeGenerated (UMLClassifier *classifier, CodeGenerator::GenerationState result)
 
void showGeneratedFile (const QString &filename)
 

Public Member Functions

 CodeGenerator ()
 
virtual ~CodeGenerator ()
 
bool addCodeDocument (CodeDocument *add_object)
 
bool removeCodeDocument (CodeDocument *remove_object)
 
CodeDocumentListgetCodeDocumentList ()
 
QString getUniqueID (CodeDocument *codeDoc)
 
virtual void saveToXMI (QXmlStreamWriter &writer)
 
CodeDocumentfindCodeDocumentByID (const QString &id)
 
virtual void writeCodeToFile ()
 
virtual void writeCodeToFile (UMLClassifierList &list)
 
void setIncludeHeadings (bool i)
 
bool includeHeadings () const
 
void setHeadingFileDir (const QString &)
 
QString headingFileDir () const
 
void setForceDoc (bool f)
 
bool forceDoc () const
 
void setSectionCommentPolicy (CodeGenerationPolicy::WriteSectionCommentsPolicy f)
 
bool forceSections () const
 
virtual QString getHeadingFile (const QString &file)
 
QString findFileName (CodeDocument *codeDocument)
 
virtual void loadFromXMI (QDomElement &element)
 
virtual Uml::ProgrammingLanguage::Enum language () const =0
 
CodeDocumentfindCodeDocumentByClassifier (UMLClassifier *classifier)
 
virtual QStringList defaultDatatypes () const
 
virtual bool isReservedKeyword (const QString &keyword)
 
virtual QStringList reservedKeywords () const
 
virtual void createDefaultStereotypes ()
 
virtual void initFromParentDocument ()=0
 

Static Public Member Functions

static QString cleanName (const QString &name)
 
static QString formatDoc (const QString &text, const QString &linePrefix=QStringLiteral(" *"), int lineWidth=80)
 
static QString formatFullDocBlock (const QString &text, const QString &blockHeader=QStringLiteral("/* "), const QString &blockFooter=QStringLiteral(" */"), const QString &linePrefix=QStringLiteral(" *"), int lineWidth=80)
 
static QString formatSourceCode (const QString &code, const QString &indentation)
 
static void findObjectsRelated (UMLClassifier *c, UMLPackageList &cList)
 

Protected Member Functions

QString overwritableName (const QString &name, const QString &extension)
 
bool openFile (QFile &file, const QString &name)
 
void writeListedCodeDocsToFile (CodeDocumentList *docs)
 
virtual void finalizeRun ()
 

Protected Attributes

QHash< QString, CodeDocument * > m_codeDocumentDictionary
 
bool m_applyToAllRemaining
 
UMLDocm_document
 

Private Member Functions

void loadCodeForOperation (const QString &id, const QDomElement &codeDocElement)
 

Private Attributes

CodeDocumentList m_codedocumentVector
 
int m_lastIDIndex
 

Detailed Description

This class collects together all of the code documents which form this project, and generates code for them in a given language.

CodeGenerator is the abstract base class for all types of code generators. It provides the interface through which all generators are invoked and provides most of the basic functionality.

If you want to implement a CodeGenerator for some language follow these steps:

Create a class which inherits SimpleCodeGenerator or AdvancedCodeGenerator. This class can have any name, I use names like CppCodeGenerator for the Cpp Generator, JavaCodeGenerator for the Java Generator and so on, but you can use what you want.

The code you generate should be output to getOutputDirectory() of the CodeGenerationPolicy instance and you should respect the OverwritePolicy specified. You should call findFileName(..) to get an appropriate file name, and then you can call openFile() and write the generated content to this file.

Member Enumeration Documentation

◆ GenerationState

Enumerator
Generated 
Failed 
Skipped 

Constructor & Destructor Documentation

◆ CodeGenerator()

CodeGenerator::CodeGenerator ( )

Constructor for a code generator.

◆ ~CodeGenerator()

CodeGenerator::~CodeGenerator ( )
virtual

Destructor.

Member Function Documentation

◆ addCodeDocument()

bool CodeGenerator::addCodeDocument ( CodeDocument doc)

Add a CodeDocument object to the m_codedocumentVector List.

Returns
boolean - will return false if it couldnt add a document

◆ cleanName()

QString CodeGenerator::cleanName ( const QString &  name)
static

Replaces spaces with underscores and capitalises as defined in m_modname

Returns
QString
Parameters
name

◆ codeGenerated [1/2]

void CodeGenerator::codeGenerated ( UMLClassifier classifier,
bool  generated 
)
signal

◆ codeGenerated [2/2]

void CodeGenerator::codeGenerated ( UMLClassifier classifier,
CodeGenerator::GenerationState  result 
)
signal

◆ createDefaultStereotypes()

void CodeGenerator::createDefaultStereotypes ( )
virtual

Create the default stereotypes for your language (constructor, int etc).

Reimplemented in CPPCodeGenerator.

◆ defaultDatatypes()

QStringList CodeGenerator::defaultDatatypes ( ) const
virtual

Return the default datatypes for your language (bool, int etc). Default implementation returns empty list.

Reimplemented in ValaWriter, SQLWriter, PostgreSQLWriter, MySQLWriter, PythonWriter, PerlWriter, PascalWriter, JavaWriter, JavaCodeGenerator, IDLWriter, DWriter, DCodeGenerator, CSharpWriter, CppWriter, CPPCodeGenerator, and AdaWriter.

◆ finalizeRun()

void CodeGenerator::finalizeRun ( )
protectedvirtual

A single call to writeCodeToFile() usually entails processing many items (e.g. as classifiers) for which code is generated. This method is called after all code of one call to writeCodeToFile() has been generated. It can be reimplemented by concrete code generators to perform additional cleanups or other actions that can only be performed once all code has been written.

Reimplemented in AdaWriter.

◆ findCodeDocumentByClassifier()

CodeDocument * CodeGenerator::findCodeDocumentByClassifier ( UMLClassifier classifier)

Find a code document by the given classifier. NOTE: FIX, this should be 'protected' or we could have problems with CPP code generator

Returns
CodeDocument
Parameters
classifier

◆ findCodeDocumentByID()

CodeDocument * CodeGenerator::findCodeDocumentByID ( const QString &  tag)

Find a code document by the given id string.

Returns
CodeDocument

◆ findFileName()

QString CodeGenerator::findFileName ( CodeDocument codeDocument)

Finds an appropriate file name for the given CodeDocument, taking into account the Overwrite Policy and asking the user what to do if need be (if policy == Ask).

Parameters
codeDocumentthe CodeDocument for which an output file name is desired.
Returns
the file name that should be used. (with extension) or NULL if none to be used

◆ findObjectsRelated()

void CodeGenerator::findObjectsRelated ( UMLClassifier c,
UMLPackageList cList 
)
static

Finds all classes in the current document to which objects of class c are in some way related. Possible relations are Associations (generalization, composition, etc) as well as parameters to methods and return values this is useful in deciding which classes/files to import/include in code generation

Parameters
cthe class for which relations are to be found
cLista reference to the list into which return the result

◆ forceDoc()

bool CodeGenerator::forceDoc ( ) const

◆ forceSections()

bool CodeGenerator::forceSections ( ) const

◆ formatDoc()

QString CodeGenerator::formatDoc ( const QString &  text,
const QString &  linePrefix = QStringLiteral(" *"),
int  lineWidth = 80 
)
static

Format documentation for output in source files

Parameters
textthe documentation which has to be formatted
linePrefixthe prefix which has to be added in the beginnig of each line
lineWidththe line width used for word-wrapping the documentation
Returns
the formatted documentation text

◆ formatFullDocBlock()

QString CodeGenerator::formatFullDocBlock ( const QString &  text,
const QString &  blockHeader = QStringLiteral("/* "),
const QString &  blockFooter = QStringLiteral(" */"),
const QString &  linePrefix = QStringLiteral(" *"),
int  lineWidth = 80 
)
static

Format full documentation block for output in source files

Parameters
textthe documentation which has to be formatted
blockHeaderthe prefix which has to be added in the beginning of each line (instead of the first)
blockFooterthe prefix which has to be added in the beginning of each line (instead of the first)
linePrefixthe prefix which has to be added in the beginning of each line (instead of the first)
lineWidththe line width used for word-wrapping the documentation
Returns
the formatted documentation text

◆ formatSourceCode()

QString CodeGenerator::formatSourceCode ( const QString &  code,
const QString &  indentation 
)
static

Format source code for output in source files by adding the correct indentation to every line of code.

Parameters
codethe source code block which has to be formatted
indentationthe blanks to indent

◆ getCodeDocumentList()

CodeDocumentList * CodeGenerator::getCodeDocumentList ( )

Get the list of CodeDocument objects held by m_codedocumentVector.

Returns
CodeDocumentList list of CodeDocument objects held by m_codedocumentVector

◆ getHeadingFile()

QString CodeGenerator::getHeadingFile ( const QString &  file)
virtual

Gets the heading file (as a string) to be inserted at the beginning of the generated file. you give the file type as parameter and get the string. if fileName starts with a period (.) then fileName is the extension (.cpp, .h, .java) if fileName starts with another character you are requesting a specific file (mylicensefile.txt). The files can have parameters which are denoted by parameter%.

current parameters are author% date% time% filepath%

Returns
QString
Parameters
file

◆ getUniqueID()

QString CodeGenerator::getUniqueID ( CodeDocument codeDoc)

Get a unique id for this codedocument.

Returns
id for the codedocument

◆ headingFileDir()

QString CodeGenerator::headingFileDir ( ) const

◆ includeHeadings()

bool CodeGenerator::includeHeadings ( ) const

◆ initFromParentDocument()

virtual void CodeGenerator::initFromParentDocument ( )
pure virtual

Initialize this code generator from its parent UMLDoc.

Implemented in SimpleCodeGenerator, CPPCodeGenerator, and AdvancedCodeGenerator.

◆ isReservedKeyword()

bool CodeGenerator::isReservedKeyword ( const QString &  keyword)
virtual

Check whether the given string is a reserved word for the language of this code generator.

Parameters
keywordstring to check

Reimplemented in PascalWriter, and AdaWriter.

◆ language()

virtual Uml::ProgrammingLanguage::Enum CodeGenerator::language ( ) const
pure virtual

◆ loadCodeForOperation()

void CodeGenerator::loadCodeForOperation ( const QString &  idStr,
const QDomElement &  codeDocElement 
)
private

Extract and load code for operations from xmi section. Probably we have code which was entered in classpropdlg for an operation.

◆ loadFromXMI()

void CodeGenerator::loadFromXMI ( QDomElement &  qElement)
virtual

A series of accessor method constructors that we need to define for any particular language.

Load codegenerator data from xmi.

Parameters
qElementthe element from which to load

◆ openFile()

bool CodeGenerator::openFile ( QFile &  file,
const QString &  fileName 
)
protected

Opens a file named "name" for writing in the outputDirectory. If something goes wrong, it informs the user if this function returns true, you know you can write to the file.

Parameters
filefile descriptor
fileNamethe name of the file
Returns
success state

◆ overwritableName()

QString CodeGenerator::overwritableName ( const QString &  name,
const QString &  extension 
)
protected

Returns a name that can be written to in the output directory, respecting the overwrite policy. If a file of the given name and extension does not exist, then just returns the name. If a file of the given name and extension does exist, then opens an overwrite dialog. In this case the name returned may be a modification of the input name. This method is invoked by findFileName().

Parameters
namethe proposed output file name
extensionthe extension to use
Returns
the real file name that should be used (including extension) or QString() if none to be used

◆ removeCodeDocument()

bool CodeGenerator::removeCodeDocument ( CodeDocument remove_object)

Remove a CodeDocument object from m_codedocumentVector List.

Returns
boolean - will return false if it couldnt remove a document

◆ reservedKeywords()

QStringList CodeGenerator::reservedKeywords ( ) const
virtual

◆ saveToXMI()

void CodeGenerator::saveToXMI ( QXmlStreamWriter &  writer)
virtual

Save the XMI representation of this object

Reimplemented in CPPCodeGenerator.

◆ setForceDoc()

void CodeGenerator::setForceDoc ( bool  f)

◆ setHeadingFileDir()

void CodeGenerator::setHeadingFileDir ( const QString &  )

◆ setIncludeHeadings()

void CodeGenerator::setIncludeHeadings ( bool  i)

◆ setSectionCommentPolicy()

void CodeGenerator::setSectionCommentPolicy ( CodeGenerationPolicy::WriteSectionCommentsPolicy  f)

◆ showGeneratedFile

void CodeGenerator::showGeneratedFile ( const QString &  filename)
signal

◆ syncCodeToDocument

void CodeGenerator::syncCodeToDocument ( )
virtualslot

Force a synchronize of this code generator, and its present contents, to that of the parent UMLDocument. "UserGenerated" code will be preserved, but Autogenerated contents will be updated/replaced or removed as is apppropriate.

◆ writeCodeToFile() [1/2]

void CodeGenerator::writeCodeToFile ( )
virtual

This method is here to provide class wizard the ability to write out only those classes which are selected by the user.

Reimplemented in SimpleCodeGenerator, and CPPCodeGenerator.

◆ writeCodeToFile() [2/2]

void CodeGenerator::writeCodeToFile ( UMLClassifierList concepts)
virtual

This method is here to provide class wizard the ability to write out only those classes which are selected by the user.

Reimplemented in CPPCodeGenerator, and SimpleCodeGenerator.

◆ writeListedCodeDocsToFile()

void CodeGenerator::writeListedCodeDocsToFile ( CodeDocumentList docs)
protected

The actual internal routine which writes code documents.

Member Data Documentation

◆ m_applyToAllRemaining

bool CodeGenerator::m_applyToAllRemaining
protected

Used by OverwriteDialog to know if the apply to all remaining files checkbox should be checked (is by default).

◆ m_codeDocumentDictionary

QHash<QString, CodeDocument*> CodeGenerator::m_codeDocumentDictionary
protected

◆ m_codedocumentVector

CodeDocumentList CodeGenerator::m_codedocumentVector
private

◆ m_document

UMLDoc* CodeGenerator::m_document
protected

The document object.

◆ m_lastIDIndex

int CodeGenerator::m_lastIDIndex
private

The documentation for this class was generated from the following files: