All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.activemq.protobuf.compiler.MessageDescriptor Maven / Gradle / Ivy

There is a newer version: 6.1.2
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.activemq.protobuf.compiler;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.sun.org.apache.bcel.internal.generic.BASTORE;


public class MessageDescriptor implements TypeDescriptor {

    private String name;
    private ExtensionsDescriptor extensions;
    private Map fields = new LinkedHashMap();
    private Map messages = new LinkedHashMap();
    private Map enums = new LinkedHashMap();
    private final ProtoDescriptor protoDescriptor;
    private List extendsList = new ArrayList();
    private Map options = new LinkedHashMap();
    private List associatedEnumFieldDescriptors = new ArrayList();
    
    private final MessageDescriptor parent;
	private MessageDescriptor baseType;

    public MessageDescriptor(ProtoDescriptor protoDescriptor, MessageDescriptor parent) {
        this.protoDescriptor = protoDescriptor;
        this.parent = parent;
    }
    
    public void validate(List errors) {
        String baseName = getOption(getOptions(), "base_type", null);
        if( baseName!=null ) {
            if( baseType==null ) {
                baseType = (MessageDescriptor) getType(baseName);
            }
            if( baseType == null ) {
                baseType = (MessageDescriptor) getProtoDescriptor().getType(baseName);
            }
            if( baseType == null ) {
                errors.add("base_type option not valid, type not found: "+baseName);
            }
            
            // Assert that all the fields in the base type are defined in this message defintion too.
            HashSet baseFieldNames = new HashSet(baseType.getFields().keySet());
            baseFieldNames.removeAll(getFields().keySet());
            
            // Some fields were not defined in the sub class..
            if( !baseFieldNames.isEmpty() ) {
            	for (String fieldName : baseFieldNames) {
                    errors.add("base_type "+baseName+" field "+fieldName+" not defined in "+getName());
				}
            }
        }

        for (FieldDescriptor field : fields.values()) {
            field.validate(errors);
        }
        for (EnumDescriptor o : enums.values()) {
            o.validate(errors);
        }
        for (MessageDescriptor o : messages.values()) {
            o.validate(errors);
        }
    }
    
    public String getOption(Map options, String optionName, String defaultValue) {
        OptionDescriptor optionDescriptor = options.get(optionName);
        if (optionDescriptor == null) {
            return defaultValue;
        }
        return optionDescriptor.getValue();
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setExtensions(ExtensionsDescriptor extensions) {
        this.extensions = extensions;
    }

    public void setExtends(List extendsList) {
        this.extendsList = extendsList;
    }
    public List getExtends() {
        return extendsList;
    }

    public void setFields(Map fields) {
        this.fields = fields;
    }

    public void setMessages(Map messages) {
        this.messages = messages;
    }

    public void setEnums(Map enums) {
        this.enums = enums;
    }

    public String getName() {
        return name;
    }

    public String getQName() {
        if( parent==null ) {
            return name;
        } else {
            return parent.getQName()+"."+name;
        }
    }

    public ExtensionsDescriptor getExtensions() {
        return extensions;
    }

    public Map getFields() {
        return fields;
    }

    public Map getMessages() {
        return messages;
    }

    public Map getEnums() {
        return enums;
    }

    public ProtoDescriptor getProtoDescriptor() {
        return protoDescriptor;
    }

    public Map getOptions() {
        return options;
    }

    public void setOptions(Map options) {
        this.options = options;
    }

    public MessageDescriptor getParent() {
        return parent;
    }

    public TypeDescriptor getType(String t) {
        for (MessageDescriptor o : messages.values()) {
            if( t.equals(o.getName()) ) {
                return o;
            }
            if( t.startsWith(o.getName()+".") ) {
                return o.getType( t.substring(o.getName().length()+1) );
            }
        }
        for (EnumDescriptor o : enums.values()) {
            if( t.equals(o.getName()) ) {
                return o;
            }
        }
        return null;
    }

    public boolean isEnum() {
        return false;
    }

	public MessageDescriptor getBaseType() {
		return baseType;
	}

    public void associate(EnumFieldDescriptor desc) {
        associatedEnumFieldDescriptors.add(desc);
    }

    public List getAssociatedEnumFieldDescriptors() {
        return associatedEnumFieldDescriptors;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy