FileDocCategorySizeDatePackage
CORBA_IDL.javaAPI DocExample15168Tue Dec 08 01:21:00 GMT 1998oisoft.togetherx.scripts

CORBA_IDL.java

/*----------------------------------------------------------------------------
Copyright © 1998 Object International Software Ltd
----------------------------------------------------------------------------*/
package oisoft.togetherx.scripts;

import java.io.File;
import java.lang.Integer;
import java.io.FileWriter;
import java.io.PrintWriter;

import java.util.Hashtable;
import java.util.Enumeration;
import java.util.StringTokenizer;

import oisoft.togetherx.scriptapi.UML.UMLElement;
import oisoft.togetherx.scripts.idl.IDL;
import oisoft.togetherx.scripts.idl.IDLOptioner;

import oisoft.togetherx.scriptapi.Script;
import oisoft.togetherx.scriptapi.APIManager;
import oisoft.togetherx.scriptapi.ScriptResources;

import oisoft.togetherx.scriptapi.UML.UMLPackage;

public class CORBA_IDL implements ScriptResources,IDLOptioner,Script{
  private String prefixIn     = "in";
  private String prefixOut    = "out";
  private String prefixInOut  = "inout";
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public String getMenuItem(){
		 return "IDL for CORBA...";
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
	 public void doIt ( APIManager apiManager ){
    this.apiManager = apiManager;
    rootPackage = apiManager.getUMLModel().getRootPackage();
    idlBaseFunctionality = new IDL(this);
    createCppToIdlTable();
    idlBaseFunctionality.idlFabric(apiManager, idlCORBA);
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public Hashtable getJavaToIdlTable(){
    includeFiles = new Hashtable();
    types    = new StringBuffer();
    Hashtable Javahashtable = new Hashtable();
    for ( int i=0; i < javaAndIdlTypes.length; i++ ){
      Javahashtable.put( javaAndIdlTypes[i][0], javaAndIdlTypes[i][1]);
    }
    return Javahashtable;
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public Hashtable getIncludeFiles() {
    return includeFiles;
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public boolean isCorrectType(){
    return currentType;
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public void setCorrectType( boolean state ){
    currentType = state;
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public void putIncludeFile( String includeFile ){
    if ( includeFile.endsWith( idlBaseFunctionality.postfix )){
      int position = includeFile.lastIndexOf( idlBaseFunctionality.postfix );
      String strIDLName = includeFile.substring(0,position);
      includeFiles.put( strIDLName, strIDLName );
    }
    else{
      setCorrectType( false );
      apiManager.createMessage("IDL","Type of "+ includeFile +" is unknown.","");
    }
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public void createBaseDataTables(){
    types           = new StringBuffer();
    includeFiles    = new Hashtable();
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  private void createCppToIdlTable(){
    idlFiles        = new Hashtable();
    cppToIdlTable   = new Hashtable();
    for ( int i=0; i < ( cppToIdlTypes.length ); i++ ){
      cppToIdlTable.put( cppToIdlTypes[i][0], cppToIdlTypes[i][1] );
    }
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public String getConvertedCppType(String type){
    return (String) cppToIdlTable.get( type );
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public void convertCppToIdlType(String type){
    convertedType = "";
    if ((! type.endsWith( "[" )) && (! type.endsWith( "<" ))){
      convertedType = getConvertedCppType( type );
      if ( convertedType == null ){
        StringTokenizer tokenType = new StringTokenizer( type, "*&[]<>" );
        if ( tokenType.hasMoreElements() ) {
          convertedType = tokenType.nextToken();
          putIncludeFile( convertedType.trim() );
        }
      }
    }
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public String getIdlType(){
    if (convertedType == null) return "";
    else return convertedType;
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public String getArrayDeclaration(int countElements, String typeDeclaration, String memberName){
    String type = "type_" + memberName;
    String arrayDeclaration = ( countElements > 0 ) ?
			 ("  typedef " + typeDeclaration + " " + type + "[" + countElements + "];" ):
			 ("  typedef sequence<" + typeDeclaration + "> " + type + ";");
    types.append( EOL );
    types.append( arrayDeclaration );
    return type;
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public String getOperationDeclaration( String type,       String name,
                                         String parameters, String exception ){
    return (type +" "+ name +"("+ parameters +")"+ exception +";" );
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public String getParameterModificator( String parameter, String memberType, boolean arrayAttribute ){
    String parameterModificator = "";
    if ( parameter.startsWith( prefixInOut )){
      parameterModificator = in_out_parameter;
    }
    else if ( parameter.startsWith( prefixIn )){
      parameterModificator = in_parameter;
    }
    else if ( parameter.startsWith( prefixOut )){
      parameterModificator = out_parameter;
    }
    else {
        if ((memberType.indexOf("&") >= 0) || (memberType.indexOf("*") >= 0)) {
          parameterModificator = in_out_parameter;
        }
        else {
          parameterModificator = in_parameter;
        }
    }

    return parameterModificator;
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public void setPackages( String packages ){
    fullQualifiedName = packages;
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public void putIdlFile( String idlFile ){
    idlFile.trim();
    idlFiles.put( idlFile, idlFile );
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public String getAttributeTitle(){
    return attribute;
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public String getDelimiters(){
    return delimiters;
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public boolean creationIdlFile(String language, String path, String fileName, String extenceFiles){
    this.language = language;
    try{
      int position = fileName.lastIndexOf( idlBaseFunctionality.postfix );
      idlFileName = fileName.substring(0,position);

      extences = extenceFiles;
      putIdlFile( idlFileName );
      if ( language.equals( UMLElement.CPP )){
        path += "idl" + File.separator;
        File file = new File(path);
        if (! file.exists()) {
          file.mkdir();
        }
      }
      outputFile = new PrintWriter ( new FileWriter( path + idlFileName + ".idl" ));
      return true;
    }
    catch (Exception e){
      e.printStackTrace();
      return false;
    }
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public void genereateIdlFileTitle(){
    outputFile.println ( "" );
    outputFile.println ( "// Generated by Together" );
    outputFile.println ( "" );
    outputFile.println ( "" );
    countModules = 0;
    int iPreviousPos = 0;
    int iCurrentPos = fullQualifiedName.indexOf( "." );
    while ( iCurrentPos >= 0 ){
      String strNamePackage = fullQualifiedName.substring( iPreviousPos, iCurrentPos );
      iPreviousPos = iCurrentPos + 1;
      iCurrentPos = fullQualifiedName.indexOf( ".", iPreviousPos );
      outputFile.println ( "module " + strNamePackage + " {" );
      countModules++;
    }
    if (! language.equals( UMLElement.CPP )){
      outputFile.println ( "module idl { " );
      countModules++;
    }
    outputFile.println ( "" );
    outputFile.println ( "#ifndef " + idlFileName + "_idl" );
    outputFile.println ( "#define " + idlFileName + "_idl" );
    outputFile.println ( "" );
    outputFile.println ( "interface " + idlFileName + ";");

    Enumeration files = getIncludeFiles().elements();
    while ( files.hasMoreElements()){
      String fileName = (String) files.nextElement();
      if (! idlFileName.equals( fileName ))
        outputFile.println ( "#include " + "\"" + fileName + ".idl" + "\"" );
    }
    outputFile.println ( "" );
    outputFile.println ( "// ---------------------------------------- " );
    outputFile.println ( "" );
    outputFile.println ( "// ---------------------------------------- " );
    outputFile.println ( "" );
    outputFile.print   ( "  interface " + idlFileName );
    if (! extences.equals("")) outputFile.print ( " : " + extences );
    outputFile.println ( " {" );
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  public void closeIdlFile( StringBuffer outBuff, StringBuffer exceptions ){
    String strExceptions = new String( exceptions );
    String strTypes = new String( types );
    String strIdlFunctionality = new String( outBuff );

    if (! strExceptions.equals("")) outputFile.println( strExceptions );
    if (! strTypes.equals("")) outputFile.println( strTypes );
    if (! strIdlFunctionality.equals("")) outputFile.println( strIdlFunctionality );

    outputFile.println ( "  };" );
    outputFile.println ( "" );
    outputFile.println ( "#endif" );
    outputFile.println ( "" );

    for ( int i = 0; i < countModules; i++ )
      outputFile.println ( "};" );

    outputFile.flush ();
    outputFile.close ();
  }
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  private String[][] javaAndIdlTypes = {
    { "long",         "long long" },
    { "int",          "long"      },
    { "short",        "short"     },
    { "byte",         "octet"     },
    { "char",         "wchar"     },
    { "double",       "double"    },
    { "float",        "float"     },
    { "boolean",      "boolean"   },
    { "Long",         "long long" },
    { "Integer",      "long"      },
    { "Short",        "short"     },
    { "Byte",         "octet"     },
    { "Character",    "wchar"     },
    { "Double",       "double"    },
    { "Float",        "float"     },
    { "Boolean",      "boolean"   },
    { "String",       "string"    },
    { "StringBuffer", "string"    },
    { "void",         "void"      },
    { "Object",       "any"       },
    { "Vector",       "any"       },
    { "Hashtable",    "any"       },
    { "Enumeration",  "any"       },
    { "Calendar",     "any"       },
    { "Date",         "any"}};


	 private String[][] cppToIdlTypes = {
    { "char",                "char"},
    { "short int",          "short"},
    { "int",                "long"},
    { "long",               "long"},
    { "long int",           "long long"},
    { "boolean",            "boolean"},
    { "float",              "float"},
    { "double",             "double"},
    { "long double",        "double"},
    { "unsigned char",      "char"},
    { "unsigned short int", "unsigned short"},
    { "unsigned int",       "unsigned long"},
    { "unsigned long int",  "unsigned long long"},
    { "signed char",        "short"},
    { "signed short int",   "short"},
    { "signed int",         "long"},
    { "signed long int",    "long long"},
    { "string",             "string"},
    { "String",             "string"},
    { "BOOL",               "boolean"},
    { "bool",               "boolean"},
    { "LONG",               "long long"},
    { "ULONG",              "unsigned long"},
    { "DWORD",              "long"},
    { "void",               "void"},
    { "__stdcall",          ""},
    { "const",              "" }};

  private APIManager apiManager;
  private static final String star = "*";
  private static String EOL = System.getProperty("line.separator");
  private static final String idlCORBA = "CORBA";

  private Hashtable cppToIdlTable;
  private Hashtable includeFiles;
  private Hashtable idlFiles;

  private StringBuffer types;

  private PrintWriter outputFile;
  private UMLPackage rootPackage;
  private int countModules = 0;

  private String idlFileName;
  private String extences;
  private String fullQualifiedName;
  private String attribute        = "attribute ";
  private String out_parameter    = "out ";
  private String in_out_parameter = "inout ";
  private String in_parameter     = "in ";
  private String delimiters       = " &*";
  private String convertedType;
  private String language;
  private boolean currentType     = true;
  private IDL idlBaseFunctionality;
  //--------------------------------------------------------------------------
}