summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/codegenerators/simplecodegenerator.cpp
blob: 8463babc668ecd68451d2219e663200c0046a424 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   copyright (C) 2004-2006                                               *
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 ***************************************************************************/

/*  This code generated by:
 *      Author : thomas
 *      Date   : Sep Mon 1 2003
 */

// own header
#include "simplecodegenerator.h"
// system includes
#include <cstdlib> //to get the user name
// qt includes
#include <tqdatetime.h>
#include <tqregexp.h>
#include <tqdir.h>
// kde includes
#include <klocale.h>
#include <kdebug.h>
#include <kmessagebox.h>
#include <kdialog.h>
#include <tdeapplication.h>
// app includes
#include "../dialogs/overwritedialogue.h"
#include "../model_utils.h"
#include "../attribute.h"
#include "../umloperationlist.h"
#include "../umlattributelist.h"
#include "../classifier.h"
#include "../codedocument.h"
#include "../codegenerationpolicy.h"
#include "../operation.h"
#include "../umldoc.h"
#include "../uml.h"

// Constructors/Destructors
//

SimpleCodeGenerator::SimpleCodeGenerator (bool createDirHierarchyForPackages /* =true */)
{
    m_indentLevel = 0;
    UMLDoc * parentDoc = UMLApp::app()->getDocument();
    parentDoc->disconnect(this); // disconnect from UMLDoc.. we arent planning to be synced at all
    m_createDirHierarchyForPackages = createDirHierarchyForPackages;
    initFields(parentDoc);
}

SimpleCodeGenerator::~SimpleCodeGenerator ( ) { }

//
// Methods
//

// Accessor methods
//


// Other methods
//

TQString SimpleCodeGenerator::getIndent ()
{
    TQString myIndent;
    for (int i = 0 ; i < m_indentLevel ; i++)
        myIndent.append(m_indentation);
    return myIndent;
}

TQString SimpleCodeGenerator::findFileName(UMLPackage* concept, const TQString &ext) {

    //if we already know to which file this class was written/should be written, just return it.
    if (m_fileMap.contains(concept))
        return m_fileMap[concept];

    //else, determine the "natural" file name
    TQString name;
    // Get the package name
    TQString package = concept->getPackage(".");

    // Replace all white spaces with blanks
    package.simplifyWhiteSpace();

    // Replace all blanks with underscore
    package.replace(TQRegExp(" "), "_");

    // Convert all "::" to "/" : Platform-specific path separator
    // package.replace(TQRegExp("::"), "/");

    // if package is given add this as a directory to the file name
    if (!package.isEmpty() && m_createDirHierarchyForPackages) {
        name = package + '.' + concept->getName();
        name.replace(TQRegExp("\\."),"/");
        package.replace(TQRegExp("\\."), "/");
        package = '/' + package;
    } else {
        name = concept->getFullyQualifiedName("-");
    }

    if (! UMLApp::app()->activeLanguageIsCaseSensitive()) {
        package = package.lower();
        name = name.lower();
    }

    // if a package name exists check the existence of the package directory
    if (!package.isEmpty() && m_createDirHierarchyForPackages) {
        TQDir pathDir(UMLApp::app()->getCommonPolicy()->getOutputDirectory().absPath() + package);
        // does our complete output directory exist yet? if not, try to create it
        if (!pathDir.exists())
        {
            TQStringList dirs = TQStringList::split("/",pathDir.absPath());
            TQString currentDir = "";

            TQStringList::iterator end(dirs.end());
            for (TQStringList::iterator dir(dirs.begin()); dir != end; ++dir)
            {
                currentDir += '/' + *dir;
                if (! (pathDir.exists(currentDir)
                        || pathDir.mkdir(currentDir) ) )
                {
                    KMessageBox::error(0, i18n("Cannot create the folder:\n") +
                                       pathDir.absPath() + i18n("\nPlease check the access rights"),
                                       i18n("Cannot Create Folder"));
                    return NULL;
                }
            }
        }
    }


    name.simplifyWhiteSpace();
    name.replace(TQRegExp(" "),"_");

    TQString extension = ext.simplifyWhiteSpace();
    extension.replace(' ', '_');

    return overwritableName(concept, name, extension);
}

TQString SimpleCodeGenerator::overwritableName(UMLPackage* concept, const TQString &name, const TQString &ext) {
    //check if a file named "name" with extension "ext" already exists
    CodeGenerationPolicy *commonPolicy = UMLApp::app()->getCommonPolicy();
    TQDir outputDir = commonPolicy->getOutputDirectory();
    TQString filename = name + ext;
    if(!outputDir.exists(filename)) {
        m_fileMap.insert(concept,filename);
        return filename; //if not, "name" is OK and we have not much to to
    }

    int suffix;
    OverwriteDialogue overwriteDialogue( filename, outputDir.absPath(),
                                         m_applyToAllRemaining, kapp -> mainWidget() );
    switch(commonPolicy->getOverwritePolicy()) {  //if it exists, check the OverwritePolicy we should use
    case CodeGenerationPolicy::Ok:                //ok to overwrite file
        break;
    case CodeGenerationPolicy::Ask:               //ask if we can overwrite
        switch(overwriteDialogue.exec()) {
        case KDialogBase::Yes:  //overwrite file
            if ( overwriteDialogue.applyToAllRemaining() ) {
                commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Ok);
            } else {
                m_applyToAllRemaining = false;
            }
            break;
        case KDialogBase::No: //generate similar name
            suffix = 1;
            while (1) {
                filename = name + "__" + TQString::number(suffix) + ext;
                if (!outputDir.exists(filename))
                    break;
                suffix++;
            }
            if ( overwriteDialogue.applyToAllRemaining() ) {
                commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Never);
            } else {
                m_applyToAllRemaining = false;
            }
            break;
        case KDialogBase::Cancel: //don't output anything
            if ( overwriteDialogue.applyToAllRemaining() ) {
                commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Cancel);
            } else {
                m_applyToAllRemaining = false;
            }
            return NULL;
            break;
        }

        break;
    case CodeGenerationPolicy::Never: //generate similar name
        suffix = 1;
        while (1) {
            filename = name + "__" + TQString::number(suffix) + ext;
            if (!outputDir.exists(filename))
                break;
            suffix++;
        }
        break;
    case CodeGenerationPolicy::Cancel: //don't output anything
        return NULL;
        break;
    }

    m_fileMap.insert(concept, filename);
    return filename;
}


bool SimpleCodeGenerator::hasDefaultValueAttr(UMLClassifier *c) {
    UMLAttributeList atl = c->getAttributeList();
    for(UMLAttribute *at = atl.first(); at; at = atl.next())
        if(!at->getInitialValue().isEmpty())
            return true;
    return false;
}

bool SimpleCodeGenerator::hasAbstractOps(UMLClassifier *c) {
    UMLOperationList opl(c->getOpList());
    for(UMLOperation *op = opl.first(); op ; op = opl.next())
        if(op->getAbstract())
            return true;
    return false;
}

/**
 * @return      ClassifierCodeDocument
 * @param       classifier
 * @param       this This package generator object.
 */
CodeDocument * SimpleCodeGenerator::newClassifierCodeDocument(UMLClassifier* /*classifier*/)
{
    return (CodeDocument*)NULL;
}

// write all concepts in project to file
void SimpleCodeGenerator::writeCodeToFile ( ) {
    m_fileMap.clear(); // need to do this, else just keep getting same directory to write to.
    UMLClassifierList concepts = m_doc->getClassesAndInterfaces();
    for (UMLClassifier *c = concepts.first(); c; c = concepts.next()) {
        if (! Model_Utils::isCommonDataType(c->getName()))
            this->writeClass(c); // call the writer for each class.
    }
}

// write only selected concepts to file
void SimpleCodeGenerator::writeCodeToFile ( UMLClassifierList & concepts) {
    m_fileMap.clear(); // ??
    for (UMLClassifier *c = concepts.first(); c; c = concepts.next())
        this->writeClass(c); // call the writer for each class.
}

void SimpleCodeGenerator::initFields ( UMLDoc * parentDoc ) {

    // load Classifier documents from parent document
    // initFromParentDocument();

    m_fileMap.clear();
    m_applyToAllRemaining = true;
    m_doc = parentDoc;

    // this really is just being used to sync the internal params
    // to the codegenpolicy as there are no code documents to really sync.
    syncCodeToDocument();
}

// a little method to provide some compatability between
// the newer codegenpolicy object and the older class fields.
void SimpleCodeGenerator::syncCodeToDocument() {

    CodeGenerationPolicy *policy = UMLApp::app()->getCommonPolicy();

    m_indentation = policy->getIndentation();
    m_endl = policy->getNewLineEndingChars();

}


// override parent method
void SimpleCodeGenerator::initFromParentDocument( )
{
    // Do nothing
}


#include "simplecodegenerator.moc"