summaryrefslogtreecommitdiff
path: root/gentools/src/org/apache/qpid/gentools/AmqpField.java
blob: 7c721cf913f35266b77469f483ce4b5c8f4a8394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 *
 * 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.qpid.gentools;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class AmqpField implements Printable, NodeAware, VersionConsistencyCheck
{

    private final AmqpVersionSet _versionSet = new AmqpVersionSet();
    private final AmqpDomainVersionMap _domainMap = new AmqpDomainVersionMap();
    private final AmqpOrdinalVersionMap _ordinalMap = new AmqpOrdinalVersionMap();

    private final String _name;
    private final Generator _generator;

    private final Map<AmqpVersion, String> _versionToDomainMap = new HashMap<AmqpVersion, String>();
    private final Map<AmqpVersion, Integer> _versionToOrdinalMap = new HashMap<AmqpVersion, Integer>();


    public AmqpField(String name, Generator generator)
    {
        _name = name;
        _generator = generator;

    }

    public boolean addFromNode(Node fieldNode, int ordinal, AmqpVersion version)
            throws AmqpParseException, AmqpTypeMappingException
    {
        _versionSet.add(version);
        String domainType;
        // Early versions of the spec (8.0) used the "type" attribute instead of "domain" for some fields.
        try
        {
            domainType = _generator.prepareDomainName(Utils.getNamedAttribute(fieldNode, Utils.ATTRIBUTE_DOMAIN));
        }
        catch (AmqpParseException e)
        {
            domainType = _generator.prepareDomainName(Utils.getNamedAttribute(fieldNode, Utils.ATTRIBUTE_TYPE));
        }
        AmqpVersionSet thisVersionList = _domainMap.get(domainType);
        if (thisVersionList == null) // First time, create new entry
        {
            thisVersionList = new AmqpVersionSet();
            _domainMap.put(domainType, thisVersionList);
        }

        _versionToDomainMap.put(version, domainType);
        _versionToOrdinalMap.put(version, ordinal);

        thisVersionList.add(version);
        thisVersionList = _ordinalMap.get(ordinal);
        if (thisVersionList == null) // First time, create new entry
        {
            thisVersionList = new AmqpVersionSet();
            _ordinalMap.put(ordinal, thisVersionList);
        }
        thisVersionList.add(version);
        NodeList nList = fieldNode.getChildNodes();
        for (int i = 0; i < nList.getLength(); i++)
        {
            Node child = nList.item(i);
            if (child.getNodeName().compareTo(Utils.ELEMENT_CODEGEN) == 0)
            {
                String value = Utils.getNamedAttribute(child, Utils.ATTRIBUTE_VALUE);
                if (value.compareTo("no-gen") == 0)
                {
                    return false;
                }
            }
        }
        return true;
    }

    public void removeVersion(AmqpVersion version)
    {
        _domainMap.removeVersion(version);
        _ordinalMap.removeVersion(version);
        _versionSet.remove(version);
    }

    public boolean isCodeTypeConsistent(LanguageConverter converter)
            throws AmqpTypeMappingException
    {
        if (_domainMap.size() == 1)
        {
            return true; // By definition
        }
        ArrayList<String> codeTypeList = new ArrayList<String>();
        for (String thisDomainName : _domainMap.keySet())
        {
            AmqpVersionSet versionSet = _domainMap.get(thisDomainName);
            String codeType = converter.getGeneratedType(thisDomainName, versionSet.first());
            if (!codeTypeList.contains(codeType))
            {
                codeTypeList.add(codeType);
            }
        }
        return codeTypeList.size() == 1;
    }

    public boolean isConsistent(Generator generator)
            throws AmqpTypeMappingException
    {
        if (!isCodeTypeConsistent(generator))
        {
            return false;
        }
        if (_ordinalMap.size() != 1)
        {
            return false;
        }
        // Since the various doamin names map to the same code type, add the version occurrences
        // across all domains to see we have all possible versions covered
        int vCntr = 0;
        for (String thisDomainName : _domainMap.keySet())
        {
            vCntr += _domainMap.get(thisDomainName).size();
        }
        return vCntr == generator.getVersionSet().size();
    }

    public boolean isTypeAndNameConsistent(Generator generator)
            throws AmqpTypeMappingException
    {
        if (!isCodeTypeConsistent(generator))
        {
            return false;
        }
        // Since the various doamin names map to the same code type, add the version occurrences
        // across all domains to see we have all possible versions covered
        int vCntr = 0;
        for (String thisDomainName : _domainMap.keySet())
        {
            vCntr += _domainMap.get(thisDomainName).size();
        }
        return vCntr == getVersionSet().size();
    }


    public void print(PrintStream out, int marginSize, int tabSize)
    {
        String margin = Utils.createSpaces(marginSize);
        out.println(margin + "[F] " + _name + ": " + _versionSet);

        for (Integer thisOrdinal : _ordinalMap.keySet())
        {
            AmqpVersionSet versionList = _ordinalMap.get(thisOrdinal);
            out.println(margin + "  [O] " + thisOrdinal + " : " + versionList.toString());
        }

        for (String thisDomainName : _domainMap.keySet())
        {
            AmqpVersionSet versionList = _domainMap.get(thisDomainName);
            out.println(margin + "  [D] " + thisDomainName + " : " + versionList.toString());
        }
    }

    public boolean isVersionConsistent(AmqpVersionSet globalVersionSet)
    {
        if (!_versionSet.equals(globalVersionSet))
        {
            return false;
        }
        if (!_domainMap.isVersionConsistent(globalVersionSet))
        {
            return false;
        }
        if (!_ordinalMap.isVersionConsistent(globalVersionSet))
        {
            return false;
        }
        return true;
    }


    public boolean isVersionInterfaceConsistent(AmqpVersionSet globalVersionSet)
    {
        if (!_versionSet.equals(globalVersionSet))
        {
            return false;
        }
        if (!_domainMap.isVersionConsistent(globalVersionSet))
        {
            return false;
        }
        if (!_ordinalMap.isVersionConsistent(globalVersionSet))
        {
            return false;
        }
        return true;
    }

    public String getDomain(AmqpVersion version)
    {
        return _versionToDomainMap.get(version);
    }

    public String getConsistentNativeType()
    {
        return _generator.getNativeType(_generator.getDomainType(getDomain(_versionSet.first()),_versionSet.first()));
    }

    public int getOrdinal(AmqpVersion version)
    {
        return _versionToOrdinalMap.get(version);
    }

    public AmqpVersionSet getVersionSet()
    {
        return _versionSet;
    }

    public AmqpDomainVersionMap getDomainMap()
    {
        return _domainMap;
    }

    public AmqpOrdinalVersionMap getOrdinalMap()
    {
        return _ordinalMap;
    }

    public String getName()
    {
        return _name;
    }

    public LanguageConverter getGenerator()
    {
        return _generator;
    }

    public Map<AmqpVersion, String> getVersionToDomainMap()
    {
        return _versionToDomainMap;
    }

    public Map<AmqpVersion, Integer> getVersionToOrdinalMap()
    {
        return _versionToOrdinalMap;
    }

}