FileDocCategorySizeDatePackage
CaseLabelGenerator.javaAPI DocExample4589Thu Feb 17 20:00:42 GMT 2000com.togethersoft.modules.genidl

CaseLabelGenerator

public class CaseLabelGenerator extends Object
Generator of case labels for union switches.

Fields Summary
private static final int
SWITCH_INTEGER
Types of union switches
private static final int
SWITCH_BOOLEAN
private static final int
SWITCH_CHAR
private static final int
SWITCH_ENUM
private Hashtable
usedLabels
private Object
voidObject
private int
mySwitchType
private int
mySwitchCount
private Enumeration
myLabelEnumeration
Constructors Summary
public CaseLabelGenerator(String aSwitchType)
Constructor.

param
switchType string denoting switch type.
exception
IllegalArgumentException if switchType is enumeration which can not be parsed

        String switchType;
        if (aSwitchType != null) {
            switchType = aSwitchType.toLowerCase().trim();
            if (switchType.equals("long") || switchType.equals("long long") || switchType.equals("int"))
                mySwitchType = SWITCH_INTEGER;
            else if (switchType.equals("char") || switchType.equals("wchar"))
                mySwitchType = SWITCH_CHAR;
            else if (switchType.equals("boolean"))
                mySwitchType = SWITCH_BOOLEAN;
            else if (switchType.startsWith("enum")) {
                mySwitchType = SWITCH_ENUM;
                parseEnum(switchType);
            }
        }
        usedLabels = new Hashtable();
    
Methods Summary
public booleanisUsedLabel(java.lang.String label)

param
label label to check
return
true if given label is used

        return usedLabels.containsKey(label);
    
public java.lang.StringnextCaseLabel()
Generate new case label according switch type

        String label = null;
        boolean keepSeeking = true;
        do {
            ++mySwitchCount;
            switch (mySwitchType) {
                case SWITCH_INTEGER:
                    label = (new Integer(mySwitchCount)).toString();
                    break;
                case SWITCH_BOOLEAN:
                    label = (new Boolean(mySwitchCount % 2 == 0)).toString().toUpperCase();
                    if (usedLabels.containsKey(label)) {
                        label = null;
                        keepSeeking = false;
                    }
                    break;
                case SWITCH_CHAR:
                    label = "'" + String.valueOf((char)('A" + mySwitchCount - 1)) + "'";
                    break;
                case SWITCH_ENUM:
                    if (myLabelEnumeration.hasMoreElements()) {
                        label = (String)myLabelEnumeration.nextElement();
                    }
                    else {
                        label = null;
                    }
                    break;
                default:
                    label = null;
                    keepSeeking = false;
            }
        }
        while (label != null && keepSeeking && usedLabels.containsKey(label));
        if (label != null) {
            usedLabels.put(label, voidObject);
        }
        return label;
    
public java.lang.StringnextCaseLabel(java.lang.String label)
Passthrough the argument label and update used labels list

        usedLabels.put(label, voidObject);
        return label;
    
private voidparseEnum(java.lang.String expression)

        int i1 = expression.indexOf("{");
        int i2 = expression.indexOf("}");
        Vector labelVector = new Vector();
        if (i2 - i1 > 1) {
            String inside = expression.substring(i1 + 1, i2);
            StringTokenizer tokenizer = new StringTokenizer(inside, ",");
            String element;
            while (tokenizer.hasMoreTokens()) {
                element = tokenizer.nextToken();
                labelVector.addElement(element.trim());
            }
        }
        else {
            throw new IllegalArgumentException("IDL enumeration is empty");
        }
        myLabelEnumeration = labelVector.elements();