Version 1 of XMI2TXT

Updated 2007-11-21 14:33:29 by LV

#!/usr/bin/tclsh8.4

  # Pekka Järvinen 2006
  # XMI2TXT TCL
  # Creates list of classes with variables and methods
  # usage: cat my_xmi_file.xmi | ./xmi2txt.tcl
  #
  # Example output:
  # companyHandler:
  #   V mixed $this->db
  #   M void __construct (mixed $db)
  #   M bool add (string $name, int$parentid)
  #   M bool edit (int $id, string $name, int $parentid)
  #   M array getList (array $notids)
  #   M array getDetails (int $id)
  #   M int getVisitsByCompany (int $company)
  #
  # V = variable M = method
  # M <output> <method name> (<parameter type> <parameter name>)
  # V <type> <name inside class>
  #
  # Tested with: Ubuntu Dapper Linux
  # XMI file was created with http://tech.motion-twin.com/php_php2xmi.html
  #
  # This script was created because I wanted to print classes and methods easily 
  # without too much info on paper

  package require tdom

  fconfigure stdin -buffering line

  set packet "";
  while {![eof stdin]} {
    append packet [gets stdin];
  }

  set doc [dom parse $packet];
  set root [$doc documentElement];

  set classData [$root selectNodes {descendant::UML:Class}];

  foreach class $classData {
    foreach node $class {

      set attList [$node attributes *];

      foreach attribute $attList {

        if {[string tolower $attribute] == "name"} {
          set className [$node getAttribute $attribute];
          puts "$className:";

          set classVarData [$root selectNodes "descendant::UML:Class\[@name='$className'\]/UML:Classifier.feature/UML:Attribute"];

          foreach innerClass $classVarData {
            foreach innerNode $innerClass {
              set vName [$innerNode selectNodes "string(@name)"];
              set vType [$innerNode selectNodes "string(@type)"];
              puts "  V $vType \$this->$vName";
            }
          }

          set classInnerData [$root selectNodes "descendant::UML:Class\[@name='$className'\]/UML:Classifier.feature/UML:Operation"];

          foreach innerClass $classInnerData {
            foreach innerNode $innerClass {
              set mName [$innerNode selectNodes "string(@name)"];
              set mType [$innerNode selectNodes "string(@type)"];

              set methodData [$root selectNodes "descendant::UML:Class\[@name='$className'\]/UML:Classifier.feature/UML:Operation\[@name='$mName'\]/UML:BehavioralFeature.parameter/UML:Parameter"];
              set mParams {};

              foreach method $methodData {
                foreach methodNode $method {
                  set mParamType [$methodNode selectNodes "string(@type)"];
                  set mParam [$methodNode selectNodes "string(@name)"];
                  lappend mParams "$mParamType \$$mParam";
                }
              }

              puts "  M $mType $mName ([join $mParams ", "])";

            }
          }

          puts "";
          puts "";

        }

      }

    }
  }