| 1 |
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package com.eprosima.idl.parser.tree; |
| 16 |
|
| 17 |
import java.util.Map; |
| 18 |
import java.util.HashMap; |
| 19 |
import java.util.Collection; |
| 20 |
|
| 21 |
import com.eprosima.idl.parser.exception.RuntimeGenerationException; |
| 22 |
import com.eprosima.idl.parser.typecode.TypeCode; |
| 23 |
|
| 24 |
public class Annotation |
| 25 |
{ |
| 26 |
public static final String null_default_value = "0"; |
| 27 |
public static final String true_str = "true"; |
| 28 |
public static final String capitalized_true_str = "TRUE"; |
| 29 |
public static final String empty_str = ""; |
| 30 |
public static final String any_str = "*"; |
| 31 |
|
| 32 |
public static final String ami_str = "ami"; |
| 33 |
|
| 34 |
//{{{ @autoid |
| 35 |
public static final String autoid_str = "autoid"; |
| 36 |
public static final String autoid_enum_str = "AutoidKind"; |
| 37 |
public static final String autoid_sequential_str = "SEQUENTIAL"; |
| 38 |
public static final String autoid_sequential_value_str = "0"; |
| 39 |
public static final String autoid_hash_str = "HASH"; |
| 40 |
public static final String autoid_hash_value_str = "1"; |
| 41 |
//}}} |
| 42 |
|
| 43 |
public static final String bit_bound_str = "bit_bound"; |
| 44 |
|
| 45 |
//{{{ @data_representation |
| 46 |
public static final String data_representation_mask_str = "DataRepresentationMask"; |
| 47 |
public static final String xcdr1_bitflag_str = "XCDR1"; |
| 48 |
public static final String xml_bitflag_str = "XML"; |
| 49 |
public static final String xcdr2_bitflag_str = "XCDR2"; |
| 50 |
public static final String data_representation_str = "data_representation"; |
| 51 |
public static final String allowed_kinds_str = "allowed_kinds"; |
| 52 |
//}}} |
| 53 |
|
| 54 |
public static final String default_str = "default"; |
| 55 |
public static final String default_literal_str = "default_literal"; |
| 56 |
public static final String default_nested_str = "default_nested"; |
| 57 |
|
| 58 |
//{{{ @extensibility |
| 59 |
public static final String extensibility_str = "extensibility"; |
| 60 |
public static final String extensibility_enum_str = "ExtensibilityKind"; |
| 61 |
public static final String final_str = "final"; |
| 62 |
public static final String appendable_str = "appendable"; |
| 63 |
public static final String mutable_str = "mutable"; |
| 64 |
public static final String ex_final_str = "FINAL"; |
| 65 |
public static final String ex_final_val = "0"; |
| 66 |
public static final String ex_appendable_str = "APPENDABLE"; |
| 67 |
public static final String ex_appendable_val = "1"; |
| 68 |
public static final String ex_mutable_str = "MUTABLE"; |
| 69 |
public static final String ex_mutable_val = "2"; |
| 70 |
//}}} |
| 71 |
|
| 72 |
public static final String external_str = "external"; |
| 73 |
public static final String hashid_str = "hashid"; |
| 74 |
public static final String id_str = "id"; |
| 75 |
public static final String ignore_literal_names_str = "ignore_literal_names"; |
| 76 |
|
| 77 |
///{{{ @key |
| 78 |
public static final String key_str = "key"; |
| 79 |
public static final String eprosima_key_str = "Key"; |
| 80 |
//}}} |
| 81 |
|
| 82 |
public static final String max_str = "max"; |
| 83 |
public static final String min_str = "min"; |
| 84 |
public static final String must_understand_str = "must_understand"; |
| 85 |
public static final String nested_str = "nested"; |
| 86 |
public static final String non_serialized_str = "non_serialized"; |
| 87 |
public static final String oneway_str = "oneway"; |
| 88 |
public static final String optional_str = "optional"; |
| 89 |
public static final String position_str = "position"; |
| 90 |
public static final String range_str = "range"; |
| 91 |
|
| 92 |
//{{{ @service |
| 93 |
public static final String service_str = "service"; |
| 94 |
public static final String platform_str = "platform"; |
| 95 |
//}}} |
| 96 |
|
| 97 |
//{{{ @topic |
| 98 |
public static final String topic_str = "topic"; |
| 99 |
public static final String name_str = "name"; |
| 100 |
//}}} |
| 101 |
|
| 102 |
//{{{ @try_construct |
| 103 |
public static final String try_construct_str = "try_construct"; |
| 104 |
public static final String try_construct_enum_str = "TryConstructFailAction"; |
| 105 |
public static final String try_construct_discard_str = "DISCARD"; |
| 106 |
public static final String try_construct_use_default_str = "USE_DEFAULT"; |
| 107 |
public static final String try_construct_trim_str = "TRIM"; |
| 108 |
//}}} |
| 109 |
|
| 110 |
public static final String unit_str = "unit"; |
| 111 |
public static final String value_str = "value"; |
| 112 |
|
| 113 |
//{{{ @verbatim |
| 114 |
public static final String verbatim_str = "verbatim"; |
| 115 |
public static final String placement_enum_str = "PlacementKind"; |
| 116 |
public static final String begin_file_str = "BEGIN_FILE"; |
| 117 |
public static final String before_declaration_str = "BEFORE_DECLARATION"; |
| 118 |
public static final String begin_declaration_str = "BEGIN_DECLARATION"; |
| 119 |
public static final String end_declaration_str = "END_DECLARATION"; |
| 120 |
public static final String after_declaration_str = "AFTER_DECLARATION"; |
| 121 |
public static final String end_file_str = "END_FILE"; |
| 122 |
public static final String language_str = "language"; |
| 123 |
public static final String placement_str = "placement"; |
| 124 |
public static final String text_str = "text"; |
| 125 |
//}}} |
| 126 |
|
| 127 |
public Annotation(AnnotationDeclaration declaration) |
| 128 |
{ |
| 129 |
m_declaration = declaration; |
| 130 |
m_members = new HashMap<String, AnnotationMember>(); |
| 131 |
|
| 132 |
for(AnnotationMember ann : declaration.getMembers()) |
| 133 |
{ |
| 134 |
AnnotationMember member = new AnnotationMember(ann); |
| 135 |
m_members.put(member.getName(), member); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public String getName() |
| 140 |
{ |
| 141 |
if(m_declaration != null) |
| 142 |
{ |
| 143 |
return m_declaration.getName(); |
| 144 |
} |
| 145 |
|
| 146 |
return null; |
| 147 |
} |
| 148 |
|
| 149 |
/*! |
| 150 |
* @brief Returns the full scoped name of the type, unless the developer uses |
| 151 |
* `TemplateSTGroup.enable_using_explicitly_modules()`, by removing from the full scoped name the current |
| 152 |
* `Context` scope. |
| 153 |
*/ |
| 154 |
public String getScopedname() throws RuntimeGenerationException |
| 155 |
{ |
| 156 |
String scoped_name = getFullScopedname(); |
| 157 |
|
| 158 |
if (!com.eprosima.idl.parser.typecode.TypeCode.ctx.is_enabled_custom_property_in_current_group(com.eprosima.idl.parser.typecode.TypeCode.ctx.using_explicitly_modules_custom_property)) |
| 159 |
{ |
| 160 |
return scoped_name; |
| 161 |
} |
| 162 |
|
| 163 |
String current_scope = com.eprosima.idl.parser.typecode.TypeCode.ctx.getScope(); |
| 164 |
|
| 165 |
if(current_scope.isEmpty() || !scoped_name.startsWith(current_scope + "::")) |
| 166 |
{ |
| 167 |
return scoped_name; |
| 168 |
} |
| 169 |
|
| 170 |
return scoped_name.replace(current_scope + "::", ""); |
| 171 |
} |
| 172 |
|
| 173 |
/*! |
| 174 |
* @brief Return the scoped name of the type. |
| 175 |
*/ |
| 176 |
public String getFullScopedname() throws RuntimeGenerationException |
| 177 |
{ |
| 178 |
if(m_declaration == null) |
| 179 |
{ |
| 180 |
throw new RuntimeGenerationException("Annotation declaration not initialized"); |
| 181 |
} |
| 182 |
return m_declaration.getScopedname(); |
| 183 |
} |
| 184 |
|
| 185 |
public String getROS2Scopedname() throws RuntimeGenerationException |
| 186 |
{ |
| 187 |
if(m_declaration == null) |
| 188 |
{ |
| 189 |
throw new RuntimeGenerationException("Annotation declaration not initialized"); |
| 190 |
} |
| 191 |
return m_declaration.getROS2Scopedname(); |
| 192 |
} |
| 193 |
|
| 194 |
public boolean addValue(String value) |
| 195 |
{ |
| 196 |
if(m_members.size() != 1) |
| 197 |
{ |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
((AnnotationMember)m_members.values().toArray()[0]).setValue(value); |
| 202 |
|
| 203 |
return true; |
| 204 |
} |
| 205 |
|
| 206 |
public boolean addValue(String attribute, String value) |
| 207 |
{ |
| 208 |
AnnotationMember member = m_members.get(attribute); |
| 209 |
|
| 210 |
if(member != null) |
| 211 |
{ |
| 212 |
member.setValue(value); |
| 213 |
// Check that in case of an Enum the set value is in the enumeration. |
| 214 |
// ParseException is thrown otherwise. |
| 215 |
member.getEnumStringValue(); |
| 216 |
} |
| 217 |
else |
| 218 |
{ |
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
return true; |
| 223 |
} |
| 224 |
|
| 225 |
public String getValue() throws RuntimeGenerationException |
| 226 |
{ |
| 227 |
if(m_members.size() != 1) |
| 228 |
{ |
| 229 |
throw new RuntimeGenerationException("Error in annotation " + getName() + |
| 230 |
": accessing value of a multiple parameter exception"); |
| 231 |
} |
| 232 |
|
| 233 |
return ((AnnotationMember)m_members.values().toArray()[0]).getValue(); |
| 234 |
} |
| 235 |
|
| 236 |
public String getValue(String attribute) |
| 237 |
{ |
| 238 |
return m_members.get(attribute).getValue(); |
| 239 |
} |
| 240 |
|
| 241 |
public Map<String, AnnotationMember> getValues() |
| 242 |
{ |
| 243 |
return m_members; |
| 244 |
} |
| 245 |
|
| 246 |
public Collection<AnnotationMember> getValueList() |
| 247 |
{ |
| 248 |
return m_members.values(); |
| 249 |
} |
| 250 |
|
| 251 |
public boolean isIsVerbatim() |
| 252 |
{ |
| 253 |
return getName().equals(Annotation.verbatim_str); |
| 254 |
} |
| 255 |
|
| 256 |
public boolean isIsUnit() |
| 257 |
{ |
| 258 |
return getName().equals(Annotation.unit_str); |
| 259 |
} |
| 260 |
|
| 261 |
public boolean isIsMax() |
| 262 |
{ |
| 263 |
return getName().equals(Annotation.max_str); |
| 264 |
} |
| 265 |
|
| 266 |
public boolean isIsMin() |
| 267 |
{ |
| 268 |
return getName().equals(Annotation.min_str); |
| 269 |
} |
| 270 |
|
| 271 |
public boolean isIsRange() |
| 272 |
{ |
| 273 |
return getName().equals(Annotation.range_str); |
| 274 |
} |
| 275 |
|
| 276 |
public boolean isIsHashId() |
| 277 |
{ |
| 278 |
return getName().equals(Annotation.hashid_str); |
| 279 |
} |
| 280 |
|
| 281 |
public boolean isIsBuiltin() |
| 282 |
{ |
| 283 |
if (getName().equals(Annotation.ami_str) || |
| 284 |
getName().equals(Annotation.appendable_str) || |
| 285 |
getName().equals(Annotation.autoid_str) || |
| 286 |
getName().equals(Annotation.bit_bound_str) || |
| 287 |
getName().equals(Annotation.data_representation_str) || |
| 288 |
getName().equals(Annotation.default_str) || |
| 289 |
getName().equals(Annotation.default_literal_str) || |
| 290 |
getName().equals(Annotation.default_nested_str) || |
| 291 |
getName().equals(Annotation.eprosima_key_str) || |
| 292 |
getName().equals(Annotation.extensibility_str) || |
| 293 |
getName().equals(Annotation.external_str) || |
| 294 |
getName().equals(Annotation.final_str) || |
| 295 |
getName().equals(Annotation.hashid_str) || |
| 296 |
getName().equals(Annotation.id_str) || |
| 297 |
getName().equals(Annotation.ignore_literal_names_str) || |
| 298 |
getName().equals(Annotation.key_str) || |
| 299 |
getName().equals(Annotation.max_str) || |
| 300 |
getName().equals(Annotation.min_str) || |
| 301 |
getName().equals(Annotation.must_understand_str) || |
| 302 |
getName().equals(Annotation.mutable_str) || |
| 303 |
getName().equals(Annotation.nested_str) || |
| 304 |
getName().equals(Annotation.non_serialized_str) || |
| 305 |
getName().equals(Annotation.oneway_str) || |
| 306 |
getName().equals(Annotation.optional_str) || |
| 307 |
getName().equals(Annotation.position_str) || |
| 308 |
getName().equals(Annotation.range_str) || |
| 309 |
getName().equals(Annotation.service_str) || |
| 310 |
getName().equals(Annotation.topic_str) || |
| 311 |
getName().equals(Annotation.try_construct_str) || |
| 312 |
getName().equals(Annotation.unit_str) || |
| 313 |
getName().equals(Annotation.value_str) || |
| 314 |
getName().equals(Annotation.verbatim_str)) |
| 315 |
{ |
| 316 |
return true; |
| 317 |
} |
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
public int getValuesSize() |
| 322 |
{ |
| 323 |
return m_members.size(); |
| 324 |
} |
| 325 |
|
| 326 |
public AnnotationDeclaration getAnnotationDeclaration() |
| 327 |
{ |
| 328 |
return m_declaration; |
| 329 |
} |
| 330 |
|
| 331 |
private HashMap<String, AnnotationMember> m_members = null; |
| 332 |
private AnnotationDeclaration m_declaration = null; |
| 333 |
} |