/**
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.activemq.openwire.tool;

import org.codehaus.gram.GramSupport;
import org.codehaus.jam.JClass;
import org.codehaus.jam.JField;
import org.codehaus.jam.JProperty;
import org.codehaus.jam.JMethod;

import java.util.Map;
import java.util.HashMap;

/**
 * @version $Revision: 1.1 $
 */
public abstract class OpenWireScript extends GramSupport {

    private Map enums = new HashMap();
    private int enumCounter;


    /**
     * Returns true if the given type is a valid message type
     */
    public boolean isMessageType(JClass it) {
        String name = it.getSimpleName();

        return it.isInterface() == false && it.isAbstract() == false && !name.startsWith("Abstract") &&
            name.indexOf("Queue") < 0 && name.indexOf("Topic") < 0
                && !name.startsWith("ActiveMQDestination")
                && !name.startsWith("ActiveMQXid")
                && !name.startsWith("MessageAcknowledge")
                && !name.startsWith("Packet")
                && !name.startsWith("ReceiptHolder")
                && !name.startsWith("TransactionType");
    }

    public boolean isValidProperty(JProperty it) {
        JMethod getter = it.getGetter();
        return getter != null && it.getSetter() != null && getter.isStatic() == false && getter.getAnnotation("Transient") == null;
    }

    /**
     * Returns the enum counter for the given type
     */
    public int getEnum(JClass type) {
        Integer value = (Integer) enums.get(type);
        if (value == null) {
            value = new Integer(++enumCounter);
            enums.put(type, value);
        }
        return value.intValue();
    }

}
