• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • kdesdk
  • Sitemap
  • Contact Us
 

umbrello/umbrello

simplecodegenerator.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   This program is free software; you can redistribute it and/or modify  *
00003  *   it under the terms of the GNU General Public License as published by  *
00004  *   the Free Software Foundation; either version 2 of the License, or     *
00005  *   (at your option) any later version.                                   *
00006  *                                                                         *
00007  *   copyright (C) 2003      Brian Thomas <thomas@mail630.gsfc.nasa.gov>   *
00008  *   copyright (C) 2004-2008                                               *
00009  *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
00010  ***************************************************************************/
00011 
00012 // own header
00013 #include "simplecodegenerator.h"
00014 
00015 // app includes
00016 #include "dialogs/overwritedialogue.h"
00017 #include "model_utils.h"
00018 #include "attribute.h"
00019 #include "umloperationlist.h"
00020 #include "umlattributelist.h"
00021 #include "classifier.h"
00022 #include "codedocument.h"
00023 #include "codegenerationpolicy.h"
00024 #include "operation.h"
00025 #include "umldoc.h"
00026 #include "uml.h"
00027 
00028 // kde includes
00029 #include <klocale.h>
00030 #include <kdebug.h>
00031 #include <kmessagebox.h>
00032 #include <kdialog.h>
00033 #include <kapplication.h>
00034 
00035 // qt includes
00036 #include <QtCore/QDateTime>
00037 #include <QtCore/QRegExp>
00038 #include <QtCore/QDir>
00039 
00040 // system includes
00041 #include <cstdlib> //to get the user name
00042 
00046 SimpleCodeGenerator::SimpleCodeGenerator (bool createDirHierarchyForPackages)
00047 {
00048     m_indentLevel = 0;
00049     UMLDoc * parentDoc = UMLApp::app()->getDocument();
00050     parentDoc->disconnect(this); // disconnect from UMLDoc.. we arent planning to be synced at all
00051     m_createDirHierarchyForPackages = createDirHierarchyForPackages;
00052     initFields(parentDoc);
00053 }
00054 
00058 SimpleCodeGenerator::~SimpleCodeGenerator ()
00059 {
00060 }
00061 
00066 QString SimpleCodeGenerator::getIndent ()
00067 {
00068     QString myIndent;
00069     for (int i = 0 ; i < m_indentLevel; ++i) {
00070         myIndent.append(m_indentation);
00071     }
00072     return myIndent;
00073 }
00074 
00081 QString SimpleCodeGenerator::findFileName(UMLPackage* concept, const QString &ext)
00082 {
00083     //if we already know to which file this class was written/should be written, just return it.
00084     if (m_fileMap.contains(concept))
00085         return m_fileMap[concept];
00086 
00087     //else, determine the "natural" file name
00088     QString name;
00089     // Get the package name
00090     QString package = concept->getPackage(".");
00091 
00092     // Replace all white spaces with blanks
00093     package = package.simplified();
00094 
00095     // Replace all blanks with underscore
00096     package.replace(QRegExp(" "), "_");
00097 
00098     // Convert all "::" to "/" : Platform-specific path separator
00099     // package.replace(QRegExp("::"), "/");
00100 
00101     // if package is given add this as a directory to the file name
00102     if (!package.isEmpty() && m_createDirHierarchyForPackages) {
00103         name = package + '.' + concept->getName();
00104         name.replace(QRegExp("\\."),"/");
00105         package.replace(QRegExp("\\."), "/");
00106         package = '/' + package;
00107     } else {
00108         name = concept->getFullyQualifiedName("-");
00109     }
00110 
00111     if (! UMLApp::app()->activeLanguageIsCaseSensitive()) {
00112         package = package.toLower();
00113         name = name.toLower();
00114     }
00115 
00116     // if a package name exists check the existence of the package directory
00117     if (!package.isEmpty() && m_createDirHierarchyForPackages) {
00118         QDir pathDir(UMLApp::app()->getCommonPolicy()->getOutputDirectory().absolutePath() + package);
00119         // does our complete output directory exist yet? if not, try to create it
00120         if (!pathDir.exists())
00121         {
00122             const QStringList dirs = pathDir.absolutePath().split('/');
00123             QString currentDir = "";
00124 
00125             QStringList::const_iterator end(dirs.end());
00126             for (QStringList::const_iterator dir(dirs.begin()); dir != end; ++dir)
00127             {
00128                 currentDir += '/' + *dir;
00129                 if (! (pathDir.exists(currentDir)
00130                         || pathDir.mkdir(currentDir) ) )
00131                 {
00132                     KMessageBox::error(0, i18n("Cannot create the folder:\n") +
00133                                        pathDir.absolutePath() + i18n("\nPlease check the access rights"),
00134                                        i18n("Cannot Create Folder"));
00135                     return NULL;
00136                 }
00137             }
00138         }
00139     }
00140 
00141     name = name.simplified();
00142     name.replace(QRegExp(" "),"_");
00143 
00144     QString extension = ext.simplified();
00145     extension.replace(' ', '_');
00146 
00147     return overwritableName(concept, name, extension);
00148 }
00149 
00157 QString SimpleCodeGenerator::overwritableName(UMLPackage* concept, const QString &name, const QString &ext)
00158 {
00159     CodeGenerationPolicy *commonPolicy = UMLApp::app()->getCommonPolicy();
00160     QDir outputDir = commonPolicy->getOutputDirectory();
00161     QString filename = name + ext;
00162     if(!outputDir.exists(filename)) {
00163         m_fileMap.insert(concept,filename);
00164         return filename; //if not, "name" is OK and we have not much to to
00165     }
00166 
00167     int suffix;
00168     OverwriteDialogue overwriteDialogue( filename, outputDir.absolutePath(),
00169                                          m_applyToAllRemaining, kapp->activeWindow() );
00170     switch(commonPolicy->getOverwritePolicy()) {  //if it exists, check the OverwritePolicy we should use
00171     case CodeGenerationPolicy::Ok:                //ok to overwrite file
00172         break;
00173     case CodeGenerationPolicy::Ask:               //ask if we can overwrite
00174         switch(overwriteDialogue.exec()) {
00175         case KDialog::Yes:  //overwrite file
00176             if ( overwriteDialogue.applyToAllRemaining() ) {
00177                 commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Ok);
00178             } else {
00179                 m_applyToAllRemaining = false;
00180             }
00181             break;
00182         case KDialog::No: //generate similar name
00183             suffix = 1;
00184             while (1) {
00185                 filename = name + "__" + QString::number(suffix) + ext;
00186                 if (!outputDir.exists(filename))
00187                     break;
00188                 suffix++;
00189             }
00190             if ( overwriteDialogue.applyToAllRemaining() ) {
00191                 commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Never);
00192             } else {
00193                 m_applyToAllRemaining = false;
00194             }
00195             break;
00196         case KDialog::Cancel: //don't output anything
00197             if ( overwriteDialogue.applyToAllRemaining() ) {
00198                 commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Cancel);
00199             } else {
00200                 m_applyToAllRemaining = false;
00201             }
00202             return NULL;
00203             break;
00204         }
00205 
00206         break;
00207     case CodeGenerationPolicy::Never: //generate similar name
00208         suffix = 1;
00209         while (1) {
00210             filename = name + "__" + QString::number(suffix) + ext;
00211             if (!outputDir.exists(filename))
00212                 break;
00213             suffix++;
00214         }
00215         break;
00216     case CodeGenerationPolicy::Cancel: //don't output anything
00217         return NULL;
00218         break;
00219     }
00220 
00221     m_fileMap.insert(concept, filename);
00222     return filename;
00223 }
00224 
00230 bool SimpleCodeGenerator::hasDefaultValueAttr(UMLClassifier *c)
00231 {
00232     UMLAttributeList atl = c->getAttributeList();
00233     foreach (UMLAttribute* at, atl ) {
00234         if(!at->getInitialValue().isEmpty())
00235             return true;
00236     }
00237     return false;
00238 }
00239 
00245 bool SimpleCodeGenerator::hasAbstractOps(UMLClassifier *c)
00246 {
00247     UMLOperationList opl(c->getOpList());
00248     foreach (UMLOperation* op, opl ) {
00249         if(op->getAbstract())
00250             return true;
00251     }
00252     return false;
00253 }
00254 
00261 CodeDocument * SimpleCodeGenerator::newClassifierCodeDocument(UMLClassifier* classifier)
00262 {
00263     Q_UNUSED(classifier);
00264     return 0;
00265 }
00266 
00270 void SimpleCodeGenerator::writeCodeToFile ( )
00271 {
00272     m_fileMap.clear(); // need to do this, else just keep getting same directory to write to.
00273     UMLClassifierList concepts = m_doc->getClassesAndInterfaces();
00274     foreach (UMLClassifier* c, concepts ) {
00275         if (! Model_Utils::isCommonDataType(c->getName()))
00276             this->writeClass(c); // call the writer for each class.
00277     }
00278 }
00279 
00284 void SimpleCodeGenerator::writeCodeToFile ( UMLClassifierList & concepts)
00285 {
00286     m_fileMap.clear(); // ??
00287     foreach (UMLClassifier* c, concepts ) {
00288         this->writeClass(c); // call the writer for each class.
00289     }
00290 }
00291 
00296 void SimpleCodeGenerator::initFields ( UMLDoc * parentDoc )
00297 {
00298     // load Classifier documents from parent document
00299     // initFromParentDocument();
00300 
00301     m_fileMap.clear();
00302     m_applyToAllRemaining = true;
00303     m_doc = parentDoc;
00304 
00305     // this really is just being used to sync the internal params
00306     // to the codegenpolicy as there are no code documents to really sync.
00307     syncCodeToDocument();
00308 }
00309 
00314 void SimpleCodeGenerator::syncCodeToDocument()
00315 {
00316     CodeGenerationPolicy *policy = UMLApp::app()->getCommonPolicy();
00317 
00318     m_indentation = policy->getIndentation();
00319     m_endl = policy->getNewLineEndingChars();
00320 }
00321 
00325 void SimpleCodeGenerator::initFromParentDocument( )
00326 {
00327     // do nothing
00328 }
00329 
00330 #include "simplecodegenerator.moc"

umbrello/umbrello

Skip menu "umbrello/umbrello"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdesdk

Skip menu "kdesdk"
  • kapptemplate
  • kate
  •     kate
  • kbugbuster
  • kcachegrind
  • kompare
  • lokalize
  • umbrello
  •   umbrello
Generated for kdesdk by doxygen 1.5.9-20090814
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal