001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.fusesource.hawtbuf.proto.compiler;
018    
019    import java.util.ArrayList;
020    import java.util.LinkedHashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    public class ProtoDescriptor {
025    
026        private String packageName;
027        private Map<String, OptionDescriptor> options = new LinkedHashMap<String, OptionDescriptor>();
028        private Map<String, MessageDescriptor> messages = new LinkedHashMap<String, MessageDescriptor>();
029        private Map<String, EnumDescriptor> enums = new LinkedHashMap<String, EnumDescriptor>();
030        private List<MessageDescriptor> extendsList = new ArrayList<MessageDescriptor>();
031        private Map<String, ServiceDescriptor> services = new LinkedHashMap<String, ServiceDescriptor>();
032        List<String> imports = new ArrayList<String>();
033        Map<String,ProtoDescriptor> importProtoDescriptors = new LinkedHashMap<String, ProtoDescriptor>();
034        private String name;
035        
036        public void setPackageName(String packageName) {
037            this.packageName = packageName;
038        }
039    
040        public void setOptions(Map<String,OptionDescriptor> options) {
041            this.options = options;
042        }
043    
044        public void setMessages(Map<String,MessageDescriptor> messages) {
045            this.messages = messages;
046        }
047    
048        public void setEnums(Map<String,EnumDescriptor> enums) {
049            this.enums = enums;
050        }
051    
052        public void setExtends(List<MessageDescriptor> extendsList) {
053            this.extendsList = extendsList;
054        }
055    
056        public List<MessageDescriptor> getExtends() {
057            return extendsList;
058        }
059    
060        public String getPackageName() {
061            return packageName;
062        }
063    
064        public Map<String, OptionDescriptor> getOptions() {
065            return options;
066        }
067    
068        public Map<String,MessageDescriptor> getMessages() {
069            return messages;
070        }
071    
072        public Map<String,EnumDescriptor> getEnums() {
073            return enums;
074        }
075    
076        public void setServices(Map<String,ServiceDescriptor> services) {
077            this.services = services;
078        }
079    
080        public Map<String,ServiceDescriptor> getServices() {
081            return services;
082        }
083    
084        /**
085         * Checks for validation errors in the proto definition and fills them 
086         * into the errors list.
087         * 
088         * @return
089         */
090        public void validate(List<String> errors) {
091            for (ProtoDescriptor o : importProtoDescriptors.values()) {
092                o.validate(errors);
093            }
094            for (OptionDescriptor o : options.values()) {
095                o.validate(errors);
096            }
097            for (MessageDescriptor o : messages.values()) {
098                o.validate(errors);
099            }
100            for (EnumDescriptor o : enums.values()) {
101                o.validate(errors);
102            }
103            for (MessageDescriptor o : extendsList) {
104                o.validate(errors);
105            }
106            for (ServiceDescriptor o : services.values()) {
107                o.validate(errors);
108            }
109        }
110    
111        public List<String> getImports() {
112            return imports;
113        }
114    
115        public void setImports(List<String> imports) {
116            this.imports = imports;
117        }
118    
119        public Map<String, ProtoDescriptor> getImportProtoDescriptors() {
120            return importProtoDescriptors;
121        }
122    
123        public void setImportProtoDescriptors(Map<String, ProtoDescriptor> importProtoDescriptors) {
124            this.importProtoDescriptors = importProtoDescriptors;
125        }
126    
127        public TypeDescriptor getType(String type) {
128            for (MessageDescriptor o : messages.values()) {
129                if( type.equals(o.getName()) ) {
130                    return o;
131                }
132                if( type.startsWith(o.getName()+".") ) {
133                    return o.getType( type.substring(o.getName().length()+1) );
134                }
135            }
136            for (EnumDescriptor o : enums.values()) {
137                if( type.equals(o.getName()) ) {
138                    return o;
139                }
140            }
141            // Check to see if the type was qualified with the package name...
142            for (ProtoDescriptor o : importProtoDescriptors.values()) {
143                if( o.getPackageName()!=null && type.startsWith(o.getPackageName()+".") ) {
144                    return o.getType( type.substring(o.getPackageName().length()+1) );
145                }
146            }
147            for (ProtoDescriptor o : importProtoDescriptors.values()) {
148                TypeDescriptor rc = o.getType(type);
149                if (rc != null) {
150                    return rc;
151                }
152            }
153            return null;
154        }
155    
156        public String getName() {
157            return name;
158        }
159    
160        public void setName(String name) {
161            this.name = name;
162        }
163    
164    }