umbrello API Documentation

simplecodegenerator.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  *   copyright (C) 2004-2006                                               *
00009  *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
00010  ***************************************************************************/
00011 
00012 /*  This code generated by:
00013  *      Author : thomas
00014  *      Date   : Sep Mon 1 2003
00015  */
00016 
00017 // own header
00018 #include "simplecodegenerator.h"
00019 // system includes
00020 #include <cstdlib> //to get the user name
00021 // qt includes
00022 #include <qdatetime.h>
00023 #include <qregexp.h>
00024 #include <qdir.h>
00025 // kde includes
00026 #include <klocale.h>
00027 #include <kdebug.h>
00028 #include <kmessagebox.h>
00029 #include <kdialog.h>
00030 #include <kapplication.h>
00031 // app includes
00032 #include "../dialogs/overwritedialogue.h"
00033 #include "../model_utils.h"
00034 #include "../attribute.h"
00035 #include "../umloperationlist.h"
00036 #include "../umlattributelist.h"
00037 #include "../classifier.h"
00038 #include "../codedocument.h"
00039 #include "../codegenerationpolicy.h"
00040 #include "../operation.h"
00041 #include "../umldoc.h"
00042 #include "../uml.h"
00043 
00044 // Constructors/Destructors
00045 //
00046 
00047 SimpleCodeGenerator::SimpleCodeGenerator (bool createDirHierarchyForPackages /* =true */)
00048 {
00049     m_indentLevel = 0;
00050     UMLDoc * parentDoc = UMLApp::app()->getDocument();
00051     parentDoc->disconnect(this); // disconnect from UMLDoc.. we arent planning to be synced at all
00052     m_createDirHierarchyForPackages = createDirHierarchyForPackages;
00053     initFields(parentDoc);
00054 }
00055 
00056 SimpleCodeGenerator::~SimpleCodeGenerator ( ) { }
00057 
00058 //
00059 // Methods
00060 //
00061 
00062 // Accessor methods
00063 //
00064 
00065 
00066 // Other methods
00067 //
00068 
00069 QString SimpleCodeGenerator::getIndent ()
00070 {
00071     QString myIndent;
00072     for (int i = 0 ; i < m_indentLevel ; i++)
00073         myIndent.append(m_indentation);
00074     return myIndent;
00075 }
00076 
00077 QString SimpleCodeGenerator::findFileName(UMLPackage* concept, const QString &ext) {
00078 
00079     //if we already know to which file this class was written/should be written, just return it.
00080     if (m_fileMap.contains(concept))
00081         return m_fileMap[concept];
00082 
00083     //else, determine the "natural" file name
00084     QString name;
00085     // Get the package name
00086     QString package = concept->getPackage(".");
00087 
00088     // Replace all white spaces with blanks
00089     package.simplifyWhiteSpace();
00090 
00091     // Replace all blanks with underscore
00092     package.replace(QRegExp(" "), "_");
00093 
00094     // Convert all "::" to "/" : Platform-specific path separator
00095     // package.replace(QRegExp("::"), "/");
00096 
00097     // if package is given add this as a directory to the file name
00098     if (!package.isEmpty() && m_createDirHierarchyForPackages) {
00099         name = package + '.' + concept->getName();
00100         name.replace(QRegExp("\\."),"/");
00101         package.replace(QRegExp("\\."), "/");
00102         package = '/' + package;
00103     } else {
00104         name = concept->getFullyQualifiedName("-");
00105     }
00106 
00107     if (! UMLApp::app()->activeLanguageIsCaseSensitive()) {
00108         package = package.lower();
00109         name = name.lower();
00110     }
00111 
00112     // if a package name exists check the existence of the package directory
00113     if (!package.isEmpty() && m_createDirHierarchyForPackages) {
00114         QDir pathDir(UMLApp::app()->getCommonPolicy()->getOutputDirectory().absPath() + package);
00115         // does our complete output directory exist yet? if not, try to create it
00116         if (!pathDir.exists())
00117         {
00118             QStringList dirs = QStringList::split("/",pathDir.absPath());
00119             QString currentDir = "";
00120 
00121             QStringList::iterator end(dirs.end());
00122             for (QStringList::iterator dir(dirs.begin()); dir != end; ++dir)
00123             {
00124                 currentDir += '/' + *dir;
00125                 if (! (pathDir.exists(currentDir)
00126                         || pathDir.mkdir(currentDir) ) )
00127                 {
00128                     KMessageBox::error(0, i18n("Cannot create the folder:\n") +
00129                                        pathDir.absPath() + i18n("\nPlease check the access rights"),
00130                                        i18n("Cannot Create Folder"));
00131                     return NULL;
00132                 }
00133             }
00134         }
00135     }
00136 
00137 
00138     name.simplifyWhiteSpace();
00139     name.replace(QRegExp(" "),"_");
00140 
00141     QString extension = ext.simplifyWhiteSpace();
00142     extension.replace(' ', '_');
00143 
00144     return overwritableName(concept, name, extension);
00145 }
00146 
00147 QString SimpleCodeGenerator::overwritableName(UMLPackage* concept, const QString &name, const QString &ext) {
00148     //check if a file named "name" with extension "ext" already exists
00149     CodeGenerationPolicy *commonPolicy = UMLApp::app()->getCommonPolicy();
00150     QDir outputDir = commonPolicy->getOutputDirectory();
00151     QString filename = name + ext;
00152     if(!outputDir.exists(filename)) {
00153         m_fileMap.insert(concept,filename);
00154         return filename; //if not, "name" is OK and we have not much to to
00155     }
00156 
00157     int suffix;
00158     OverwriteDialogue overwriteDialogue( filename, outputDir.absPath(),
00159                                          m_applyToAllRemaining, kapp -> mainWidget() );
00160     switch(commonPolicy->getOverwritePolicy()) {  //if it exists, check the OverwritePolicy we should use
00161     case CodeGenerationPolicy::Ok:                //ok to overwrite file
00162         break;
00163     case CodeGenerationPolicy::Ask:               //ask if we can overwrite
00164         switch(overwriteDialogue.exec()) {
00165         case KDialogBase::Yes:  //overwrite file
00166             if ( overwriteDialogue.applyToAllRemaining() ) {
00167                 commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Ok);
00168             } else {
00169                 m_applyToAllRemaining = false;
00170             }
00171             break;
00172         case KDialogBase::No: //generate similar name
00173             suffix = 1;
00174             while (1) {
00175                 filename = name + "__" + QString::number(suffix) + ext;
00176                 if (!outputDir.exists(filename))
00177                     break;
00178                 suffix++;
00179             }
00180             if ( overwriteDialogue.applyToAllRemaining() ) {
00181                 commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Never);
00182             } else {
00183                 m_applyToAllRemaining = false;
00184             }
00185             break;
00186         case KDialogBase::Cancel: //don't output anything
00187             if ( overwriteDialogue.applyToAllRemaining() ) {
00188                 commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Cancel);
00189             } else {
00190                 m_applyToAllRemaining = false;
00191             }
00192             return NULL;
00193             break;
00194         }
00195 
00196         break;
00197     case CodeGenerationPolicy::Never: //generate similar name
00198         suffix = 1;
00199         while (1) {
00200             filename = name + "__" + QString::number(suffix) + ext;
00201             if (!outputDir.exists(filename))
00202                 break;
00203             suffix++;
00204         }
00205         break;
00206     case CodeGenerationPolicy::Cancel: //don't output anything
00207         return NULL;
00208         break;
00209     }
00210 
00211     m_fileMap.insert(concept, filename);
00212     return filename;
00213 }
00214 
00215 
00216 bool SimpleCodeGenerator::hasDefaultValueAttr(UMLClassifier *c) {
00217     UMLAttributeList atl = c->getAttributeList();
00218     for(UMLAttribute *at = atl.first(); at; at = atl.next())
00219         if(!at->getInitialValue().isEmpty())
00220             return true;
00221     return false;
00222 }
00223 
00224 bool SimpleCodeGenerator::hasAbstractOps(UMLClassifier *c) {
00225     UMLOperationList opl(c->getOpList());
00226     for(UMLOperation *op = opl.first(); op ; op = opl.next())
00227         if(op->getAbstract())
00228             return true;
00229     return false;
00230 }
00231 
00237 CodeDocument * SimpleCodeGenerator::newClassifierCodeDocument(UMLClassifier* /*classifier*/)
00238 {
00239     return (CodeDocument*)NULL;
00240 }
00241 
00242 // write all concepts in project to file
00243 void SimpleCodeGenerator::writeCodeToFile ( ) {
00244     m_fileMap.clear(); // need to do this, else just keep getting same directory to write to.
00245     UMLClassifierList concepts = m_doc->getClassesAndInterfaces();
00246     for (UMLClassifier *c = concepts.first(); c; c = concepts.next()) {
00247         if (! Model_Utils::isCommonDataType(c->getName()))
00248             this->writeClass(c); // call the writer for each class.
00249     }
00250 }
00251 
00252 // write only selected concepts to file
00253 void SimpleCodeGenerator::writeCodeToFile ( UMLClassifierList & concepts) {
00254     m_fileMap.clear(); // ??
00255     for (UMLClassifier *c = concepts.first(); c; c = concepts.next())
00256         this->writeClass(c); // call the writer for each class.
00257 }
00258 
00259 void SimpleCodeGenerator::initFields ( UMLDoc * parentDoc ) {
00260 
00261     // load Classifier documents from parent document
00262     // initFromParentDocument();
00263 
00264     m_fileMap.clear();
00265     m_applyToAllRemaining = true;
00266     m_doc = parentDoc;
00267 
00268     // this really is just being used to sync the internal params
00269     // to the codegenpolicy as there are no code documents to really sync.
00270     syncCodeToDocument();
00271 }
00272 
00273 // a little method to provide some compatability between
00274 // the newer codegenpolicy object and the older class fields.
00275 void SimpleCodeGenerator::syncCodeToDocument() {
00276 
00277     CodeGenerationPolicy *policy = UMLApp::app()->getCommonPolicy();
00278 
00279     m_indentation = policy->getIndentation();
00280     m_endl = policy->getNewLineEndingChars();
00281 
00282 }
00283 
00284 
00285 // override parent method
00286 void SimpleCodeGenerator::initFromParentDocument( )
00287 {
00288     // Do nothing
00289 }
00290 
00291 
00292 #include "simplecodegenerator.moc"
KDE Logo
This file is part of the documentation for umbrello Version 3.1.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 26 08:08:00 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003