FileDocCategorySizeDatePackage
CountMembers.javaAPI DocExample6984Tue Dec 08 01:21:00 GMT 1998oisoft.togetherx.scripts

CountMembers.java

/*----------------------------------------------------------------------------
Copyright © 1998 Object International Software Ltd
----------------------------------------------------------------------------*/

package oisoft.togetherx.scripts;

import oisoft.togetherx.scriptapi.*;
import oisoft.togetherx.scriptapi.UML.*;
import oisoft.togetherx.scriptapi.UML.enums.*;
import java.io.*;
import java.awt.*;
import java.util.*;

public class CountMembers implements Script,ScriptResources {
  private UMLPackage umlRootPackage;
  private StringBuffer strBuff = new StringBuffer();
  private PrintWriter outputFile;
  private static String strEOL = System.getProperty("line.separator");
  //------------------------------------------------------------------------------
  public String getMenuItem() {
     return "CountMembers...|T";
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public void doIt ( APIManager apiManager ){
    umlRootPackage = apiManager.getUMLModel().getRootPackage();
    if ( umlRootPackage == null ){
      apiManager.createMessage("CountMembers",  "Root package is null ", "Error");
      return;
    }
    try{
      String outputStreamName = apiManager.getOutputStream(".txt");
      if (outputStreamName != null){
        txtFileCreater( outputStreamName );
        modelProcessing( umlRootPackage );
        txtFileClose();
        apiManager.openExternalViewer(outputStreamName);
        System.out.println( "Done" );
      }
    }
    catch( Exception e ){
       e.printStackTrace();
    }
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  private void modelProcessing ( UMLPackage umlPackage ){
    classDiagramContentsProcessing( umlPackage );
    UMLClassDiagramEnumeration classDiagrams = umlPackage.getClassDiagrams();
    while ( classDiagrams.hasMoreElements()){
      UMLPackage umlNextPackage = classDiagrams.getNextUMLClassDiagram();
      modelProcessing( umlNextPackage );
  	}
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  private void classDiagramContentsProcessing( UMLPackage umlPackage ){
    // classes processing
    UMLClassEnumeration umlClassEnum = ((UMLClassDiagram)(umlPackage)).getClasses();
    addLine();
    addLine();
    addComponentToOutputBuf( "", umlPackage.toLongString() );
    if ( umlClassEnum.hasMoreElements() ) {
      addComponentToOutputBuf( "CLASSES :","");
      addLine();
    }
    while( umlClassEnum.hasMoreElements() ){
      UMLClassifier umlClassifier = (UMLClassifier) umlClassEnum.getNextUMLClass();
      classifierProcessing( umlClassifier );
    }
    // interfaces processing
    UMLInterfaceEnumeration umlInterfaceEnum = ((UMLClassDiagram)(umlPackage)).getInterfaces();
    if ( umlInterfaceEnum.hasMoreElements() ) {
      addLine();
      addComponentToOutputBuf( "INTERFACES :","");
    }
    while( umlInterfaceEnum.hasMoreElements() ){
      UMLClassifier umlClassifier = (UMLClassifier) umlInterfaceEnum.getNextUMLInterface();
      classifierProcessing( umlClassifier );
    }
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  private void classifierProcessing( UMLClassifier umlClassifier ){
    addComponentToOutputBuf( " ",umlClassifier.getName());
    // operations processing
    UMLOperationEnumeration umlOperationEnumeration = umlClassifier.getOperations();
    operationsProcessing( umlOperationEnumeration );
    // attributes processing
    UMLAttributeEnumeration umlAttributeEnumeration = umlClassifier.getAttributes();
    attributesProcessing( umlAttributeEnumeration );
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  private void operationsProcessing( UMLOperationEnumeration umlOperationEnumeration ){
    int iCountOperation = 0;
    while( umlOperationEnumeration.hasMoreElements() ) {
      UMLOperation umlOperation = umlOperationEnumeration.getNextUMLOperation();
      iCountOperation ++;
    }
    // write couunt operations to output buffer
    String strCount = "" + iCountOperation;
    addComponentToOutputBuf( "  Number of operations : ", strCount );
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  private void attributesProcessing( UMLAttributeEnumeration umlAttributeEnumeration ){
    int iCountAttributes = 0;
    while( umlAttributeEnumeration.hasMoreElements() ){
      UMLAttribute umlAttribute = umlAttributeEnumeration.getNextUMLAttribute();
      iCountAttributes ++;
    }
    // write couunt attributes to output buffer
    String strCount = "" + iCountAttributes;
    addComponentToOutputBuf( "  Number of attributes : ", strCount );
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  private void txtFileCreater( String strFullName ){
    try{
      outputFile = new PrintWriter( new FileWriter( strFullName ));
      outputFile.println ( " --------------------- " );
      outputFile.println ( " Generated by Together " );
      outputFile.println ( " --------------------- " );
    }
    catch (Exception e){
      e.printStackTrace();
    }
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  private void txtFileClose(){
    String strDocumentation = new String( strBuff );
    if (! strDocumentation.equals("")) outputFile.println( strDocumentation );
    outputFile.println ( "" );
    outputFile.println ( "---------- End of document ----------" );
    outputFile.flush ();
    outputFile.close ();
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  private void addComponentToOutputBuf( String strComponentType, String strComponentName ){
    if (! strComponentType.equals("") ) strBuff.append( strComponentType );
    if (! strComponentName.equals("") ) strBuff.append( strComponentName );
    addLine();
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  private void addLine(){
    strBuff.append( strEOL );
  }
}