umbrello API Documentation

aswriter.cpp

00001 /***************************************************************************
00002     begin                : Sat Feb 08 2003
00003     copyright            : (C) 2003 by Alexander Blum
00004     email                : blum@kewbee.de
00005  ***************************************************************************/
00006 
00007 /***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #include "aswriter.h"
00017 #include "../association.h"
00018 #include "../classifier.h"
00019 #include "../operation.h"
00020 #include "../umldoc.h"
00021 #include "../attribute.h"
00022 
00023 #include <kdebug.h>
00024 #include <qregexp.h>
00025 #include <qtextstream.h>
00026 
00027 ASWriter::ASWriter() {
00028 }
00029 
00030 ASWriter::~ASWriter() {}
00031 
00032 
00033 void ASWriter::writeClass(UMLClassifier *c)
00034 {
00035     if(!c)
00036     {
00037         kDebug()<<"Cannot write class of NULL concept!" << endl;
00038         return;
00039     }
00040 
00041     QString classname = cleanName(c->getName());
00042     QString fileName = c->getName().lower();
00043 
00044     //find an appropriate name for our file
00045     fileName = findFileName(c,".as");
00046     if (fileName.isEmpty())
00047     {
00048         emit codeGenerated(c, false);
00049         return;
00050     }
00051 
00052     QFile fileas;
00053     if(!openFile(fileas,fileName))
00054     {
00055         emit codeGenerated(c, false);
00056         return;
00057     }
00058     QTextStream as(&fileas);
00059 
00061     //Start generating the code!!
00063 
00064 
00065     //try to find a heading file (license, coments, etc)
00066     QString str;
00067     str = getHeadingFile(".as");
00068     if(!str.isEmpty())
00069     {
00070         str.replace(QRegExp("%filename%"),fileName+".as");
00071         str.replace(QRegExp("%filepath%"),fileas.name());
00072         as << str << m_endl;
00073     }
00074 
00075 
00076     //write includes
00077     UMLPackageList includes;
00078     findObjectsRelated(c,includes);
00079     for (UMLPackage *conc = includes.first(); conc; conc = includes.next())
00080     {
00081         QString headerName = findFileName(conc, ".as");
00082         if ( !headerName.isEmpty() )
00083         {
00084             as << "#include \"" << findFileName(conc,".as") << "\"" << m_endl;
00085         }
00086     }
00087     as << m_endl;
00088 
00089     //Write class Documentation if there is somthing or if force option
00090     if(forceDoc() || !c->getDoc().isEmpty())
00091     {
00092         as << m_endl << "" << m_endl << m_endl;
00096     }
00097 
00098     UMLClassifierList superclasses = c->getSuperClasses();
00099     UMLAssociationList aggregations = c->getAggregations();
00100     UMLAssociationList compositions = c->getCompositions();
00101 
00102     //check if class is abstract and / or has abstract methods
00103     if(c->getAbstract() && !hasAbstractOps(c))
00104         as << "/******************************* Abstract Class ****************************" << m_endl << "  "
00105         << classname << " does not have any pure virtual methods, but its author" << m_endl
00106         << "  defined it as an abstract class, so you should not use it directly." << m_endl
00107         << "  Inherit from it instead and create only objects from the derived classes" << m_endl
00108         << "*****************************************************************************/" << m_endl << m_endl;
00109 
00110     as << classname << " = function ()" << m_endl;
00111     as << "{" << m_endl;
00112     as << m_indentation << "this._init ();" << m_endl;
00113     as << "}" << m_endl;
00114     as << m_endl;
00115 
00116     for(UMLClassifier *obj = superclasses.first();
00117             obj; obj = superclasses.next()) {
00118         as << classname << ".prototype = new " << cleanName(obj->getName()) << " ();" << m_endl;
00119     }
00120 
00121     as << m_endl;
00122 
00123     const bool isClass = !c->isInterface();
00124     if (isClass) {
00125 
00126         UMLAttributeList atl = c->getAttributeList();
00127 
00128         as << "" << m_endl;
00134         as << classname << ".prototype._init = function ()" << m_endl;
00135         as << "{" << m_endl;
00136         for(UMLAttribute *at = atl.first(); at ; at = atl.next())
00137         {
00138             if (forceDoc() || !at->getDoc().isEmpty())
00139             {
00140                 as << m_indentation << "" << m_endl;
00143             }
00144             if(!at->getInitialValue().isEmpty())
00145             {
00146                 as << m_indentation << "this.m_" << cleanName(at->getName()) << " = " << at->getInitialValue() << ";" << m_endl;
00147             }
00148             else
00149             {
00150                 as << m_indentation << "this.m_" << cleanName(at->getName()) << " = \"\";" << m_endl;
00151             }
00152         }
00153     }
00154 
00155     //associations
00156     if (forceSections() || !aggregations.isEmpty ())
00157     {
00158         as <<  m_endl << m_indentation << "/**Aggregations: */" << m_endl;
00159         writeAssociation(classname, aggregations , as );
00160 
00161     }
00162 
00163     if( forceSections() || !compositions.isEmpty())
00164     {
00165         as <<  m_endl << m_indentation << "/**Compositions: */" << m_endl;
00166         writeAssociation(classname, compositions , as );
00167     }
00168 
00169     as << m_endl;
00170     as << m_indentation << "/**Protected: */" << m_endl;
00171 
00172     if (isClass) {
00173         UMLAttributeList atl = c->getAttributeList();
00174         for (UMLAttribute *at = atl.first(); at ; at = atl.next())
00175         {
00176           if (at->getVisibility() == Uml::Visibility::Protected)
00177             {
00178                 as << m_indentation << "ASSetPropFlags (this, \"" << cleanName(at->getName()) << "\", 1);" << m_endl;
00179             }
00180         }
00181     }
00182 
00183     UMLOperationList opList(c->getOpList());
00184     for (UMLOperation *op = opList.first(); op; op = opList.next())
00185     {
00186           if (op->getVisibility() == Uml::Visibility::Protected)
00187         {
00188             as << m_indentation << "ASSetPropFlags (this, \"" << cleanName(op->getName()) << "\", 1);" << m_endl;
00189         }
00190     }
00191     as << m_endl;
00192     as << m_indentation << "/**Private: */" << m_endl;
00193     if (isClass) {
00194         UMLAttributeList atl = c->getAttributeList();
00195         for (UMLAttribute *at = atl.first(); at; at = atl.next())
00196         {
00197               if (at->getVisibility() == Uml::Visibility::Private)
00198             {
00199                 as << m_indentation << "ASSetPropFlags (this, \"" << cleanName(at->getName()) << "\", 7);" << m_endl;
00200             }
00201         }
00202     }
00203 
00204     for (UMLOperation *op = opList.first(); op; op = opList.next())
00205     {
00206           if (op->getVisibility() == Uml::Visibility::Protected)
00207         {
00208             as << m_indentation << "ASSetPropFlags (this, \"" << cleanName(op->getName()) << "\", 7);" << m_endl;
00209         }
00210     }
00211     as << "}" << m_endl;
00212 
00213     as << m_endl;
00214 
00215     //operations
00216     UMLOperationList ops(c->getOpList());
00217     writeOperations(classname, &ops, as);
00218 
00219     as << m_endl;
00220 
00221     //finish file
00222 
00223     //close files and notfiy we are done
00224     fileas.close();
00225     emit codeGenerated(c, true);
00226 }
00227 
00229 //  Helper Methods
00230 
00231 
00232 void ASWriter::writeAssociation(QString& classname, UMLAssociationList& assocList , QTextStream &as )
00233 {
00234     for(UMLAssociation *a = assocList.first(); a; a = assocList.next())
00235     {
00236         // association side
00237         Uml::Role_Type role = a->getObject(Uml::A)->getName() == classname ? Uml::B:Uml::A;
00238 
00239         QString roleName(cleanName(a->getRoleName(role)));
00240 
00241         if (!roleName.isEmpty()) {
00242 
00243             // association doc
00244             if (forceDoc() || !a->getDoc().isEmpty()) {
00245                 as << m_indentation << "" << m_endl;
00248             }
00249 
00250             // role doc
00251             if (forceDoc() || !a->getRoleDoc(role).isEmpty()) {
00252                 as << m_indentation << "" << m_endl;
00255             }
00256 
00257             bool okCvt;
00258             int nMulti = a->getMulti(role).toInt(&okCvt,10);
00259             bool isNotMulti = a->getMulti(role).isEmpty() || (okCvt && nMulti == 1);
00260 
00261             QString typeName(cleanName(a->getObject(role)->getName()));
00262 
00263             if (isNotMulti)
00264                 as << m_indentation << "this.m_" << roleName << " = new " << typeName << "();" << m_endl;
00265             else
00266                 as << m_indentation << "this.m_" << roleName << " = new Array();" << m_endl;
00267 
00268             // role visibility
00269             if (a->getVisibility(role) == Uml::Visibility::Private)
00270             {
00271                as << m_indentation << "ASSetPropFlags (this, \"m_" << roleName << "\", 7);" << m_endl;
00272             }
00273             else if (a->getVisibility(role)== Uml::Visibility::Protected)
00274             {
00275                 as << m_indentation << "ASSetPropFlags (this, \"m_" << roleName << "\", 1);" << m_endl;
00276             }
00277         }
00278     }
00279 }
00280 
00281 void ASWriter::writeOperations(QString classname, UMLOperationList *opList, QTextStream &as)
00282 {
00283     UMLOperation *op;
00284     UMLAttributeList atl;
00285     UMLAttribute *at;
00286 
00287     for(op = opList->first(); op; op = opList->next())
00288     {
00289         atl = op -> getParmList();
00290         //write method doc if we have doc || if at least one of the params has doc
00291         bool writeDoc = forceDoc() || !op->getDoc().isEmpty();
00292         for (at = atl.first(); at ; at = atl.next())
00293             writeDoc |= !at->getDoc().isEmpty();
00294 
00295         if( writeDoc )  //write method documentation
00296         {
00297             as << "" << m_endl;
00308         }//end if : write method documentation
00309 
00310         as << classname << ".prototype." << cleanName(op->getName()) << " = function " << "(";
00311 
00312         int i= atl.count();
00313         int j=0;
00314         for (at = atl.first(); at; at = atl.next(),j++)
00315         {
00316             as << cleanName(at->getName())
00317             << (!(at->getInitialValue().isEmpty()) ? (QString(" = ")+at->getInitialValue()) : QString(""))
00318             << ((j < i-1)?", ":"");
00319         }
00320         as << ")" << m_endl << "{" << m_endl <<
00321         m_indentation << m_endl << "}" << m_endl;
00322         as <<  m_endl << m_endl;
00323     }//end for
00324 }
00325 
00329 Uml::Programming_Language ASWriter::getLanguage() {
00330     return Uml::pl_ActionScript;
00331 }
00332 
00333 const QStringList ASWriter::reservedKeywords() const {
00334 
00335     static QStringList keywords;
00336 
00337     if ( keywords.isEmpty() ) {
00338         keywords << "abs"
00339         << "acos"
00340         << "add"
00341         << "addListener"
00342         << "addProperty"
00343         << "align"
00344         << "_alpha"
00345         << "and"
00346         << "appendChild"
00347         << "apply"
00348         << "Array"
00349         << "asin"
00350         << "atan"
00351         << "atan2"
00352         << "attachMovie"
00353         << "attachSound"
00354         << "attributes"
00355         << "autoSize"
00356         << "background"
00357         << "backgroundColor"
00358         << "BACKSPACE"
00359         << "beginFill"
00360         << "beginGradientFill"
00361         << "blockIndent"
00362         << "bold"
00363         << "Boolean"
00364         << "border"
00365         << "borderColor"
00366         << "bottomScroll"
00367         << "break"
00368         << "bullet"
00369         << "call"
00370         << "callee"
00371         << "caller"
00372         << "capabilities"
00373         << "CAPSLOCK"
00374         << "case"
00375         << "ceil"
00376         << "charAt"
00377         << "charCodeAt"
00378         << "childNodes"
00379         << "chr"
00380         << "clear"
00381         << "clearInterval"
00382         << "cloneNode"
00383         << "close"
00384         << "color"
00385         << "Color"
00386         << "comment"
00387         << "concat"
00388         << "connect"
00389         << "contentType"
00390         << "continue"
00391         << "CONTROL"
00392         << "cos"
00393         << "createElement"
00394         << "createEmptyMovieClip"
00395         << "createTextField"
00396         << "createTextNode"
00397         << "_currentframe"
00398         << "curveTo"
00399         << "Date"
00400         << "default"
00401         << "delete"
00402         << "DELETEKEY"
00403         << "do"
00404         << "docTypeDecl"
00405         << "DOWN"
00406         << "_droptarget"
00407         << "duplicateMovieClip"
00408         << "duration"
00409         << "E"
00410         << "else"
00411         << "embedFonts"
00412         << "enabled"
00413         << "END"
00414         << "endFill"
00415         << "endinitclip"
00416         << "ENTER"
00417         << "eq"
00418         << "escape"
00419         << "ESCAPE"
00420         << "eval"
00421         << "evaluate"
00422         << "exp"
00423         << "false"
00424         << "firstChild"
00425         << "floor"
00426         << "focusEnabled"
00427         << "_focusrect"
00428         << "font"
00429         << "for"
00430         << "_framesloaded"
00431         << "fromCharCode"
00432         << "fscommand"
00433         << "function"
00434         << "ge"
00435         << "get"
00436         << "getAscii"
00437         << "getBeginIndex"
00438         << "getBounds"
00439         << "getBytesLoaded"
00440         << "getBytesTotal"
00441         << "getCaretIndex"
00442         << "getCode"
00443         << "getDate"
00444         << "getDay"
00445         << "getDepth"
00446         << "getEndIndex"
00447         << "getFocus"
00448         << "getFontList"
00449         << "getFullYear"
00450         << "getHours"
00451         << "getMilliseconds"
00452         << "getMinutes"
00453         << "getMonth"
00454         << "getNewTextFormat"
00455         << "getPan"
00456         << "getProperty"
00457         << "getRGB"
00458         << "getSeconds"
00459         << "getTextExtent"
00460         << "getTextFormat"
00461         << "getTime"
00462         << "getTimer"
00463         << "getTimezoneOffset"
00464         << "getTransform"
00465         << "getURL"
00466         << "getUTCDate"
00467         << "getUTCDay"
00468         << "getUTCFullYear"
00469         << "getUTCHours"
00470         << "getUTCMilliseconds"
00471         << "getUTCMinutes"
00472         << "getUTCMonth"
00473         << "getUTCSeconds"
00474         << "getVersion"
00475         << "getVolume"
00476         << "getYear"
00477         << "_global"
00478         << "globalToLocal"
00479         << "goto"
00480         << "gotoAndPlay"
00481         << "gotoAndStop"
00482         << "gt"
00483         << "hasAccessibility"
00484         << "hasAudio"
00485         << "hasAudioEncoder"
00486         << "hasChildNodes"
00487         << "hasMP3"
00488         << "hasVideoEncoder"
00489         << "height"
00490         << "_height"
00491         << "hide"
00492         << "_highquality"
00493         << "hitArea"
00494         << "hitTest"
00495         << "HOME"
00496         << "hscroll"
00497         << "html"
00498         << "htmlText"
00499         << "if"
00500         << "ifFrameLoaded"
00501         << "ignoreWhite"
00502         << "in"
00503         << "include"
00504         << "indent"
00505         << "indexOf"
00506         << "initclip"
00507         << "INSERT"
00508         << "insertBefore"
00509         << "install"
00510         << "instanceof"
00511         << "int"
00512         << "isActive"
00513         << "isDown"
00514         << "isFinite"
00515         << "isNaN"
00516         << "isToggled"
00517         << "italic"
00518         << "join"
00519         << "lastChild"
00520         << "lastIndexOf"
00521         << "le"
00522         << "leading"
00523         << "LEFT"
00524         << "leftMargin"
00525         << "length"
00526         << "_level"
00527         << "lineStyle"
00528         << "lineTo"
00529         << "list"
00530         << "LN10"
00531         << "LN2"
00532         << "load"
00533         << "loaded"
00534         << "loadMovie"
00535         << "loadMovieNum"
00536         << "loadSound"
00537         << "loadVariables"
00538         << "loadVariablesNum"
00539         << "LoadVars"
00540         << "localToGlobal"
00541         << "log"
00542         << "LOG10E"
00543         << "LOG2E"
00544         << "max"
00545         << "maxChars"
00546         << "maxhscroll"
00547         << "maxscroll"
00548         << "MAX_VALUE"
00549         << "mbchr"
00550         << "mblength"
00551         << "mbord"
00552         << "mbsubstring"
00553         << "method"
00554         << "min"
00555         << "MIN_VALUE"
00556         << "moveTo"
00557         << "multiline"
00558         << "_name"
00559         << "NaN"
00560         << "ne"
00561         << "NEGATIVE_INFINITY"
00562         << "new"
00563         << "newline"
00564         << "nextFrame"
00565         << "nextScene"
00566         << "nextSibling"
00567         << "nodeName"
00568         << "nodeType"
00569         << "nodeValue"
00570         << "not"
00571         << "null"
00572         << "Number"
00573         << "Object"
00574         << "on"
00575         << "onChanged"
00576         << "onClipEvent"
00577         << "onClose"
00578         << "onConnect"
00579         << "onData"
00580         << "onDragOut"
00581         << "onDragOver"
00582         << "onEnterFrame"
00583         << "onKeyDown"
00584         << "onKeyUp"
00585         << "onKillFocus"
00586         << "onLoad"
00587         << "onMouseDown"
00588         << "onMouseMove"
00589         << "onMouseUp"
00590         << "onPress"
00591         << "onRelease"
00592         << "onReleaseOutside"
00593         << "onResize"
00594         << "onRollOut"
00595         << "onRollOver"
00596         << "onScroller"
00597         << "onSetFocus"
00598         << "onSoundComplete"
00599         << "onUnload"
00600         << "onUpdate"
00601         << "onXML"
00602         << "or"
00603         << "ord"
00604         << "_parent"
00605         << "parentNode"
00606         << "parseFloat"
00607         << "parseInt"
00608         << "parseXML"
00609         << "password"
00610         << "PGDN"
00611         << "PGUP"
00612         << "PI"
00613         << "pixelAspectRatio"
00614         << "play"
00615         << "pop"
00616         << "position"
00617         << "POSITIVE_INFINITY"
00618         << "pow"
00619         << "prevFrame"
00620         << "previousSibling"
00621         << "prevScene"
00622         << "print"
00623         << "printAsBitmap"
00624         << "printAsBitmapNum"
00625         << "printNum"
00626         << "__proto__"
00627         << "prototype"
00628         << "push"
00629         << "_quality"
00630         << "random"
00631         << "registerClass"
00632         << "removeListener"
00633         << "removeMovieClip"
00634         << "removeNode"
00635         << "removeTextField"
00636         << "replaceSel"
00637         << "restrict"
00638         << "return"
00639         << "reverse"
00640         << "RIGHT"
00641         << "rightMargin"
00642         << "_root"
00643         << "_rotation"
00644         << "round"
00645         << "scaleMode"
00646         << "screenColor"
00647         << "screenDPI"
00648         << "screenResolutionX"
00649         << "screenResolutionY"
00650         << "scroll"
00651         << "selectable"
00652         << "send"
00653         << "sendAndLoad"
00654         << "set"
00655         << "setDate"
00656         << "setFocus"
00657         << "setFullYear"
00658         << "setHours"
00659         << "setInterval"
00660         << "setMask"
00661         << "setMilliseconds"
00662         << "setMinutes"
00663         << "setMonth"
00664         << "setNewTextFormat"
00665         << "setPan"
00666         << "setProperty"
00667         << "setRGB"
00668         << "setSeconds"
00669         << "setSelection"
00670         << "setTextFormat"
00671         << "setTime"
00672         << "setTransform"
00673         << "setUTCDate"
00674         << "setUTCFullYear"
00675         << "setUTCHours"
00676         << "setUTCMilliseconds"
00677         << "setUTCMinutes"
00678         << "setUTCMonth"
00679         << "setUTCSeconds"
00680         << "setVolume"
00681         << "setYear"
00682         << "shift"
00683         << "SHIFT"
00684         << "show"
00685         << "showMenu"
00686         << "sin"
00687         << "size"
00688         << "slice"
00689         << "sort"
00690         << "sortOn"
00691         << "Sound"
00692         << "_soundbuftime"
00693         << "SPACE"
00694         << "splice"
00695         << "split"
00696         << "sqrt"
00697         << "SQRT1_2"
00698         << "SQRT2"
00699         << "start"
00700         << "startDrag"
00701         << "status"
00702         << "stop"
00703         << "stopAllSounds"
00704         << "stopDrag"
00705         << "String"
00706         << "substr"
00707         << "substring"
00708         << "super"
00709         << "swapDepths"
00710         << "switch"
00711         << "TAB"
00712         << "tabChildren"
00713         << "tabEnabled"
00714         << "tabIndex"
00715         << "tabStops"
00716         << "tan"
00717         << "target"
00718         << "_target"
00719         << "targetPath"
00720         << "tellTarget"
00721         << "text"
00722         << "textColor"
00723         << "TextFormat"
00724         << "textHeight"
00725         << "textWidth"
00726         << "this"
00727         << "toggleHighQuality"
00728         << "toLowerCase"
00729         << "toString"
00730         << "_totalframes"
00731         << "toUpperCase"
00732         << "trace"
00733         << "trackAsMenu"
00734         << "true"
00735         << "type"
00736         << "typeof"
00737         << "undefined"
00738         << "underline"
00739         << "unescape"
00740         << "uninstall"
00741         << "unloadMovie"
00742         << "unloadMovieNum"
00743         << "unshift"
00744         << "unwatch"
00745         << "UP"
00746         << "updateAfterEvent"
00747         << "url"
00748         << "_url"
00749         << "useHandCursor"
00750         << "UTC"
00751         << "valueOf"
00752         << "var"
00753         << "variable"
00754         << "_visible"
00755         << "void"
00756         << "watch"
00757         << "while"
00758         << "width"
00759         << "_width"
00760         << "with"
00761         << "wordWrap"
00762         << "_x"
00763         << "XML"
00764         << "xmlDecl"
00765         << "XMLSocket"
00766         << "_xmouse"
00767         << "_xscale"
00768         << "_y"
00769         << "_ymouse";
00770     }
00771 
00772     return keywords;
00773 }
00774 
00775 #include "aswriter.moc"
KDE Logo
This file is part of the documentation for umbrello Version 3.1.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 26 08:07:54 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003