umbrello/umbrello
simplecodegenerator.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "simplecodegenerator.h"
00019
00020 #include <cstdlib>
00021
00022 #include <qdatetime.h>
00023 #include <qregexp.h>
00024 #include <qdir.h>
00025
00026 #include <klocale.h>
00027 #include <kdebug.h>
00028 #include <kmessagebox.h>
00029 #include <kdialog.h>
00030 #include <kapplication.h>
00031
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
00045
00046
00047 SimpleCodeGenerator::SimpleCodeGenerator (bool createDirHierarchyForPackages )
00048 {
00049 m_indentLevel = 0;
00050 UMLDoc * parentDoc = UMLApp::app()->getDocument();
00051 parentDoc->disconnect(this);
00052 m_createDirHierarchyForPackages = createDirHierarchyForPackages;
00053 initFields(parentDoc);
00054 }
00055
00056 SimpleCodeGenerator::~SimpleCodeGenerator ( ) { }
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
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
00080 if (m_fileMap.contains(concept))
00081 return m_fileMap[concept];
00082
00083
00084 QString name;
00085
00086 QString package = concept->getPackage(".");
00087
00088
00089 package = package.simplified();
00090
00091
00092 package.replace(QRegExp(" "), "_");
00093
00094
00095
00096
00097
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
00113 if (!package.isEmpty() && m_createDirHierarchyForPackages) {
00114 QDir pathDir(UMLApp::app()->getCommonPolicy()->getOutputDirectory().absolutePath() + package);
00115
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
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;
00155 }
00156
00157 int suffix;
00158 OverwriteDialogue overwriteDialogue( filename, outputDir.absolutePath(),
00159 m_applyToAllRemaining, kapp->activeWindow() );
00160 switch(commonPolicy->getOverwritePolicy()) {
00161 case CodeGenerationPolicy::Ok:
00162 break;
00163 case CodeGenerationPolicy::Ask:
00164 switch(overwriteDialogue.exec()) {
00165 case KDialog::Yes:
00166 if ( overwriteDialogue.applyToAllRemaining() ) {
00167 commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Ok);
00168 } else {
00169 m_applyToAllRemaining = false;
00170 }
00171 break;
00172 case KDialog::No:
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:
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:
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:
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* )
00242 {
00243 return (CodeDocument*)NULL;
00244 }
00245
00246
00247 void SimpleCodeGenerator::writeCodeToFile ( )
00248 {
00249 m_fileMap.clear();
00250 UMLClassifierList concepts = m_doc->getClassesAndInterfaces();
00251 foreach (UMLClassifier* c, concepts ) {
00252 if (! Model_Utils::isCommonDataType(c->getName()))
00253 this->writeClass(c);
00254 }
00255 }
00256
00257
00258 void SimpleCodeGenerator::writeCodeToFile ( UMLClassifierList & concepts)
00259 {
00260 m_fileMap.clear();
00261 foreach (UMLClassifier* c, concepts ) {
00262 this->writeClass(c);
00263 }
00264 }
00265
00266 void SimpleCodeGenerator::initFields ( UMLDoc * parentDoc )
00267 {
00268
00269
00270
00271 m_fileMap.clear();
00272 m_applyToAllRemaining = true;
00273 m_doc = parentDoc;
00274
00275
00276
00277 syncCodeToDocument();
00278 }
00279
00280
00281
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
00293 void SimpleCodeGenerator::initFromParentDocument( )
00294 {
00295
00296 }
00297
00298
00299 #include "simplecodegenerator.moc"