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

umbrello/umbrello

simplecodegenerator.cpp

Go to the documentation of this file.
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-2007                                               *
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 = package.simplified();
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.toLower();
00109         name = name.toLower();
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().absolutePath() + package);
00115         // does our complete output directory exist yet? if not, try to create it
00116         if (!pathDir.exists())
00117         {
00118             QStringList dirs = pathDir.absolutePath().split("/");
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.absolutePath() + i18n("\nPlease check the access rights"),
00130                                        i18n("Cannot Create Folder"));
00131                     return NULL;
00132                 }
00133             }
00134         }
00135     }
00136 
00137     name = name.simplified();
00138     name.replace(QRegExp(" "),"_");
00139 
00140     QString extension = ext.simplified();
00141     extension.replace(' ', '_');
00142 
00143     return overwritableName(concept, name, extension);
00144 }
00145 
00146 QString SimpleCodeGenerator::overwritableName(UMLPackage* concept, const QString &name, const QString &ext)
00147 {
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.absolutePath(),
00159                                          m_applyToAllRemaining, kapp->activeWindow() );
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 KDialog::Yes:  //overwrite file
00166             if ( overwriteDialogue.applyToAllRemaining() ) {
00167                 commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Ok);
00168             } else {
00169                 m_applyToAllRemaining = false;
00170             }
00171             break;
00172         case KDialog::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 KDialog::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 {
00218     UMLAttributeList atl = c->getAttributeList();
00219     foreach (UMLAttribute* at, atl ) {
00220         if(!at->getInitialValue().isEmpty())
00221             return true;
00222     }
00223     return false;
00224 }
00225 
00226 bool SimpleCodeGenerator::hasAbstractOps(UMLClassifier *c)
00227 {
00228     UMLOperationList opl(c->getOpList());
00229     foreach (UMLOperation* op, opl ) {
00230         if(op->getAbstract())
00231             return true;
00232     }
00233     return false;
00234 }
00235 
00241 CodeDocument * SimpleCodeGenerator::newClassifierCodeDocument(UMLClassifier* /*classifier*/)
00242 {
00243     return (CodeDocument*)NULL;
00244 }
00245 
00246 // write all concepts in project to file
00247 void SimpleCodeGenerator::writeCodeToFile ( )
00248 {
00249     m_fileMap.clear(); // need to do this, else just keep getting same directory to write to.
00250     UMLClassifierList concepts = m_doc->getClassesAndInterfaces();
00251     foreach (UMLClassifier* c, concepts ) {
00252         if (! Model_Utils::isCommonDataType(c->getName()))
00253             this->writeClass(c); // call the writer for each class.
00254     }
00255 }
00256 
00257 // write only selected concepts to file
00258 void SimpleCodeGenerator::writeCodeToFile ( UMLClassifierList & concepts)
00259 {
00260     m_fileMap.clear(); // ??
00261     foreach (UMLClassifier* c, concepts ) {
00262         this->writeClass(c); // call the writer for each class.
00263     }
00264 }
00265 
00266 void SimpleCodeGenerator::initFields ( UMLDoc * parentDoc )
00267 {
00268     // load Classifier documents from parent document
00269     // initFromParentDocument();
00270 
00271     m_fileMap.clear();
00272     m_applyToAllRemaining = true;
00273     m_doc = parentDoc;
00274 
00275     // this really is just being used to sync the internal params
00276     // to the codegenpolicy as there are no code documents to really sync.
00277     syncCodeToDocument();
00278 }
00279 
00280 // a little method to provide some compatibility between
00281 // the newer codegenpolicy object and the older class fields.
00282 void SimpleCodeGenerator::syncCodeToDocument()
00283 {
00284     CodeGenerationPolicy *policy = UMLApp::app()->getCommonPolicy();
00285 
00286     m_indentation = policy->getIndentation();
00287     m_endl = policy->getNewLineEndingChars();
00288 
00289 }
00290 
00291 
00292 // override parent method
00293 void SimpleCodeGenerator::initFromParentDocument( )
00294 {
00295     // Do nothing
00296 }
00297 
00298 
00299 #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"
  • kate
  •     kate
  • umbrello
  •   umbrello
Generated for kdesdk by doxygen 1.5.4
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