umbrello/umbrello
simplecodegenerator.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "simplecodegenerator.h"
00014
00015
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
00029 #include <klocale.h>
00030 #include <kdebug.h>
00031 #include <kmessagebox.h>
00032 #include <kdialog.h>
00033 #include <kapplication.h>
00034
00035
00036 #include <QtCore/QDateTime>
00037 #include <QtCore/QRegExp>
00038 #include <QtCore/QDir>
00039
00040
00041 #include <cstdlib>
00042
00046 SimpleCodeGenerator::SimpleCodeGenerator (bool createDirHierarchyForPackages)
00047 {
00048 m_indentLevel = 0;
00049 UMLDoc * parentDoc = UMLApp::app()->getDocument();
00050 parentDoc->disconnect(this);
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
00084 if (m_fileMap.contains(concept))
00085 return m_fileMap[concept];
00086
00087
00088 QString name;
00089
00090 QString package = concept->getPackage(".");
00091
00092
00093 package = package.simplified();
00094
00095
00096 package.replace(QRegExp(" "), "_");
00097
00098
00099
00100
00101
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
00117 if (!package.isEmpty() && m_createDirHierarchyForPackages) {
00118 QDir pathDir(UMLApp::app()->getCommonPolicy()->getOutputDirectory().absolutePath() + package);
00119
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;
00165 }
00166
00167 int suffix;
00168 OverwriteDialogue overwriteDialogue( filename, outputDir.absolutePath(),
00169 m_applyToAllRemaining, kapp->activeWindow() );
00170 switch(commonPolicy->getOverwritePolicy()) {
00171 case CodeGenerationPolicy::Ok:
00172 break;
00173 case CodeGenerationPolicy::Ask:
00174 switch(overwriteDialogue.exec()) {
00175 case KDialog::Yes:
00176 if ( overwriteDialogue.applyToAllRemaining() ) {
00177 commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Ok);
00178 } else {
00179 m_applyToAllRemaining = false;
00180 }
00181 break;
00182 case KDialog::No:
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:
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:
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:
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();
00273 UMLClassifierList concepts = m_doc->getClassesAndInterfaces();
00274 foreach (UMLClassifier* c, concepts ) {
00275 if (! Model_Utils::isCommonDataType(c->getName()))
00276 this->writeClass(c);
00277 }
00278 }
00279
00284 void SimpleCodeGenerator::writeCodeToFile ( UMLClassifierList & concepts)
00285 {
00286 m_fileMap.clear();
00287 foreach (UMLClassifier* c, concepts ) {
00288 this->writeClass(c);
00289 }
00290 }
00291
00296 void SimpleCodeGenerator::initFields ( UMLDoc * parentDoc )
00297 {
00298
00299
00300
00301 m_fileMap.clear();
00302 m_applyToAllRemaining = true;
00303 m_doc = parentDoc;
00304
00305
00306
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
00328 }
00329
00330 #include "simplecodegenerator.moc"