FileDocCategorySizeDatePackage
ProcessTags.javaAPI DocExample17827Thu Feb 17 20:00:42 GMT 2000com.togethersoft.modules.genidl.idl

ProcessTags.java

/*----------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft LLC. Patents pending. All rights reserved.
----------------------------------------------------------------------------*/
package com.togethersoft.modules.genidl.idl;

import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Hashtable;
import com.togethersoft.openapi.scriptapi.APIManager;
import com.togethersoft.openapi.scriptapi.TagsProcessing;
import com.togethersoft.openapi.scriptapi.UML.UMLClassifier;
import com.togethersoft.openapi.scriptapi.UML.UMLProperty;
import com.togethersoft.openapi.scriptapi.UML.UMLElement;
import com.togethersoft.openapi.scriptapi.UML.enums.UMLPropertyEnumeration;
import com.togethersoft.modules.genidl.ScopeProcessor;
import com.togethersoft.modules.genidl.Complainer;
import com.togethersoft.modules.genidl.IdlTags;

/** The processor of special pseudocomment tags. */
public final class ProcessTags implements TagsProcessing {
    public ProcessTags(ScopeProcessor scopeProcessor, Complainer complainer) {
        startPositionOnString = "";
        moduleCount = 0;
        myScopeProcessor = scopeProcessor;
        myComplainer = complainer;
    }
    //--------------------- METHODS------------------

    /**
     * Receive properties from UMLClassifier exepts next : idlTypedef, idlParamtype, idlInclude.
     * They are processed in next method as these declarations could be several times in one comment.
     */
    public String getPropertyValue(UMLElement umlElement, String propertyName) {
        UMLPropertyEnumeration properties = umlElement.getDocProperties();
        while (properties.hasMoreElements()) {
            UMLProperty property = properties.getNextUMLProperty();
            String name = property.getPropertyName();
            String value = property.getPropertyValue();
            if ((name != null) && (name.equals(propertyName))) {

                /*
                  //    we must receive Boolean tags:((
                  if( name.equals("idlOneway")&& value.equals("true"))
                  return value;
                  if( name.equals("idlReadonly") && value.equals("true"))
                  return "true";
                  if( name.equals("idlNative") && value.equals("true"))
                  return "true";
                */

                if (value != null) {
                    return value;
                }
                else {
                    return "";
                }
            }
        }
        return "";
    }

    /** Process idlTypedef, idlParamtype, idlInclude tags. */
    public void processProperties(UMLElement umlElem, boolean isFirst, boolean isMember) {
        typedefs = new Vector();
        includes = new Vector();
        paramtypes = new Hashtable();
        UMLPropertyEnumeration properties = umlElem.getDocProperties();
        while (properties.hasMoreElements()) {
            UMLProperty property = properties.getNextUMLProperty();
            String name = property.getPropertyName();
            String value = property.getPropertyValue();
            if (name != null && value != null) {
                if (name.equals(IdlTags.IDLTYPEDEF)) {
                    processingTag_idlTypedef(umlElem, value, isFirst, isMember);
                }
                if (name.equals("idlParamtype")) {
                    processingTag_idlParamtype(value, isFirst);
                }
                if (name.equals("idlInclude")) {
                    processingTag_idlInclude(value, isFirst);
                }
            }
        }
    }

    /** Process 'idlTypedef' tag for all member of UMLModel This tag can be not alone... */
    public void processingTag_idlTypedef(UMLElement elem, String avalue, boolean isFirst, boolean isMember) {
        String value = avalue.trim();
        if (isFirst) {
            String cacheVal = "::";
            if (!isMember) { // interface
                cacheVal = myScopeProcessor.getDestFQ((UMLClassifier)elem, false);
            }
            else { // it's member
                cacheVal = myScopeProcessor.getCurrentScope();
            }
            // simple cut last word in the string
            String newType = "";
            int point = value.lastIndexOf(" ");
            if (point != -1) {
                newType = value.substring(point + 1, value.length());
            }
            // add info to cache
            if (!cacheVal.equals("::")) {
                cacheVal += "::";
            }
            myScopeProcessor.addTypedefToScope(cacheVal + newType, myScopeProcessor.getFileName());
            // example : ( file1::CORBA::haha::myType, file1::CORBA::haha)
        }
        else {
            if (isMember) {
                if (!value.trim().startsWith("interface") && !value.trim().startsWith("global") && !value.trim().startsWith("module")) {
                    myScopeProcessor.addTypedefBeforeMember(value);
                    return;
                }
            }
            if (value.startsWith("interface") || value.startsWith("global") || value.startsWith("module")) {
                // here we cut substrign without "module" or " interface" or" global"
                if (value.trim().startsWith("interface"))
                    myScopeProcessor.addTypedefToInterface(value.substring(9, value.length()));
                else if (value.trim().startsWith("global")) {
                    myScopeProcessor.addTypedefToFile(value.substring(6, value.length()));
                }
                else {
                    if (value.trim().startsWith("module")) {
                        myScopeProcessor.addTypedefToModule(value.substring(6, value.length()));
                    }
                }
                return;
            }
            System.err.println("TagsProcessor: adding typedef to module: " + value);
            myScopeProcessor.addTypedefToModule(value);
        } // else
    }

    /** Process 'idlCode' tag for all members of UMLModel */
    public String processingTag_idlCode(String idlCode, boolean isMember) {
        if (isMember) // scope is interface
                return idlCode;
        else {
            // scope is undefined
            if (idlCode.trim().startsWith("interface") || idlCode.trim().startsWith("global") || idlCode.trim().startsWith("module")) {
                // here we cut substrign without "module" or " interface" or" global"
                if (idlCode.trim().startsWith("interface")) {
                    return idlCode.trim().substring(9, idlCode.length());
                }
                else {
                    return idlCode.trim().substring(6, idlCode.length());
                }
            }
            return idlCode; // default scope == "module"
        }
    }
    //--------------------------------------------------------------------------
    // interface & class tags...
    //--------------------------------------------------------------------------

    /**
     * Receive scopes and long file name if is nessesary...
     * @return globalName for idlFile...
     */
    public String processingTag_idlModule(String idlModule) {
        resetSettings();
        StringTokenizer token = new StringTokenizer(idlModule, "::");
        StringBuffer stringValue = new StringBuffer();
        //     boolean flag = true;
        while (token.hasMoreElements()) {
            stringValue.append(token.nextToken().toString());
            stringValue.append("_");
            //       if ( !flag ){
            startPositionOnString += "  "; // shift on 2 "_"
            moduleCount++;
            //       }
            //       flag = false;
        }
        return new String(stringValue);
    }

    /** Add to include files from this tag. This tag can be not alone... */
    public void processingTag_idlInclude(String value, boolean isFirst) {
        if (!isFirst) {
            myScopeProcessor.putIncludeFile(value);
        }
    }

    /**
     * Process additional inherites... At first pass receive files to be added to include...
     * At second pass match existing interfaces
     */
    public String processingTag_idlInherites(UMLElement elem, String value, boolean isFirst) {
        if (isFirst) { // add include files
            // nada
        }
        else { // return string of inheritance or error message
            //   if this class is absent in cache
            StringTokenizer token = new StringTokenizer(value, ",");
            String fileName = "";
            String className = "";
            while (token.hasMoreElements()) {
                //         add to include Cache
                className = token.nextToken().trim();
                fileName = myScopeProcessor.getFileByClass(className);
                if (fileName != null || className.startsWith("CORBA::")) {
                    if (fileName != null) {
                        //             if( fileName.endsWith("_bo"))
                        //               incName = fileName.substring( 0, fileName.length() - 3 );
                        //             else
                        //               incName = fileName + myConfigLoader.getInterfaceSuffix();
                        if (fileName != null && !fileName.equals("")) {
                            fileName += ".idl";
                            myScopeProcessor.putIncludeFile("\"" + fileName + "\"");
                        }
                    }
                }
                else {
                    myComplainer.error("Interface " + className + " inherited in " + elem.getName() + " not found.");
                }
            }
            return value.trim();
        }
        return "";
    }

    /** Process tag unionSelector for interface with 'union' stereoType */
    public String processingTag_idlUnionSelector(String idlUnionSelector) {
        return "";
    }
    //---------------------------------------
    //-------------- Methods & Attributes tags only
    //---------------------------------------

    /** Process 'idlType' tag... */
    public String processingTag_idlType(String idlType, boolean isFirst) {
        if (isFirst) { // add include files
        }
        else { // return string of type or error message
            //   if this class is absent in caching classes and
            //   typedefs
        }
        return idlType;
    }
    //----------------------------------------
    //--------------- Methods tags only
    //----------------------------------------

    /** Process 'idlParameters' tag */
    public String processingTag_idlParameters(String idlParameters, boolean isFirst) {
        if (isFirst) { // add include files
        }
        else { // return string of type or error message
            //   if this class is absent in caching classes and
            //   typedefs
            if (idlParameters != null) {
                StringTokenizer tokenAll = new StringTokenizer(idlParameters, ",");
                StringTokenizer tokenParm;
                String parmType = "";
                while (tokenAll.hasMoreElements()) {
                    tokenParm = new StringTokenizer(tokenAll.nextToken().trim(), " ");
                    if (tokenParm.hasMoreElements()) {
                        tokenParm.nextToken(); // skip parm attribute
                    }
                    if (tokenParm.hasMoreElements()) {
                        parmType = tokenParm.nextToken().trim();
                        myScopeProcessor.checkTypeAndConvert(false, myScopeProcessor.getCurrentScope(), parmType);
                    }
                }
            }
        }
        return "";
    }

    /** Is to process 'idlParamtype' tag This tag can be not alone... */
    public void processingTag_idlParamtype(String value, boolean isFirst) {
        if (isFirst) { // add include files
        }
        else { // return string of type or error message
            //   if this class is absent in cached classes and
            //   typedefs
            paramtypes.put(value.substring(0, value.trim().lastIndexOf(" ") + 1).trim(),
                value.substring(value.trim().lastIndexOf(" ") + 1, value.length()).trim());
        }
    }

    /**
     * Process 'idlOneway' tag.
     * @deprecated
     */
    public String processingTag_idlOneway(String idlOneway) {
        return "";
    }

    /** Process 'idlRaises' tag. */
    public String processingTag_idlRaise(String idlRaise) {
        StringTokenizer token = new StringTokenizer(idlRaise, ",");
        String exceptionName = "";
        String fileName = "";
        while (token.hasMoreElements()) {
            exceptionName = token.nextToken().trim();
            System.err.println("Exception name = " + exceptionName);
            System.err.println("Scope = " + myScopeProcessor.getCurrentScope());
            fileName = myScopeProcessor.getExceptionsFile(myScopeProcessor.getCurrentScope(), exceptionName);
            if (fileName != null) {
                myScopeProcessor.putIncludeFile("\"" + fileName + ".idl" + "\"");
            }
        }
        return "";
    }

    /**
     * Process 'idlContext' tag.
     * @deprecated
     */
    public String processingTag_idlContext(String idlContext) {
        return "";
    }
    //------------------------------------------------------
    //-------- Attributes tags only.
    //------------------------------------------------------

    /** Processs attribute in interface which have 'union' stereotype. For second parsing. */
    public String processingTag_idlCaseLabel(String idlCaseLabel) {
        return "";
    }

    /**
     * Process 'idlConst' tag If constExpression in absent in this tag - it must be taken
     * from native expression...  ( int n_attr = 6; // we'll take '6')
     */
    public String processingTag_idlConst(String idlConst, String attrInitExpression) {
        String value = idlConst;
        if (idlConst.trim().startsWith("interface") || idlConst.trim().startsWith("global") || idlConst.trim().startsWith("module")) {
            // here we cut substring without "module" or "interface" or "global"
            if (idlConst.trim().startsWith("interface")) {
                value = idlConst.trim().substring(9, idlConst.length());
            }
            else {
                value = idlConst.trim().substring(6, idlConst.length());
            }
        }
        if (!value.trim().equals("")) // default scope == "module" and we don't cut ...
                return value.trim();
        else { // tag was : @idlConst <scope>
            // only for java project's
            if (attrInitExpression != null && !attrInitExpression.trim().equals("")) {
                return attrInitExpression.trim();
            }
            else {
                return "";
            }
        }
    }

    /** Process 'idlSequence' tag */
    public String processingTag_idlSequence(String idlSequence) {
        return "";
    }

    /** Process 'idlStringcapacity'tag. For 'string' attribute only. */
    public String processingTag_idlStringcapacity(String idlStringcapacity) {
        return idlStringcapacity.trim();
    }

    /**
     * Process 'idlReadonly' tag.
     * @deprecated
     */
    public String processingTag_idlReadonly(String idlReadonly) {
        return "";
    }

    /**
     * Process 'idlFixed' tag.
     * @deprecated, use idlType
     */
    public String processingTag_idlFixed(String idlFixed) {
        String value = "fixed";
        int count = 0;
        StringTokenizer token = new StringTokenizer(idlFixed.trim(), " ");
        while (token.hasMoreElements()) {
            count++;
            if (count == 1) {
                value += "< " + token.nextElement();
            }
            if (count == 2) {
                value += ", " + token.nextElement() + " >";
            }
            if (count > 2) {
                break;
            }
        }
        if (count != 2) {
            myComplainer.error(" Wrong type declaration  : " + "fixed " + idlFixed);
            return " ";
        }
        return value;
    }

    /**
     * Process 'idlNative' tag.
     * @deprecated, use idlType
     */
    public String processingTag_idlNative(String idlNative) {
        return "native";
    }

    public String getPosition() {
        return startPositionOnString;
    }

    public int getModuleNumber() {
        return moduleCount;
    }

    public Vector getTypedefs() {
        return typedefs;
    }

    public Hashtable getParamtypes() {
        return paramtypes;
    }

    public String getValueByKey(String key) {
        if (paramtypes.containsKey(key)) {
            return paramtypes.get(key).toString();
        }
        else {
            return "";
        }
    }

    public Vector getIncludes() { // only for umlClassifier'a
        return includes;
    }

    public void resetSettings() {
        startPositionOnString = ""; // shift on 2 "_"
        moduleCount = 0;
    }

    //----------------------------------
    //---------- FIELDS
    //----------------------------------
    private Vector typedefs;
    private Vector includes;
    private Hashtable paramtypes;
    private ScopeProcessor myScopeProcessor;
    private Complainer myComplainer;
    private String startPositionOnString; // shift on 2 "_"
    private int moduleCount;
}