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