| 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 com.eprosima.idl.parser.exception.ParseException; |
| 18 |
import com.eprosima.idl.parser.typecode.Member; |
| 19 |
import com.eprosima.idl.parser.typecode.EnumMember; |
| 20 |
import com.eprosima.idl.parser.typecode.EnumTypeCode; |
| 21 |
import com.eprosima.idl.parser.typecode.TypeCode; |
| 22 |
|
| 23 |
public class AnnotationMember |
| 24 |
{ |
| 25 |
public AnnotationMember(String name, TypeCode typecode, String value) |
| 26 |
{ |
| 27 |
m_typecode = typecode; |
| 28 |
m_name = name; |
| 29 |
m_value = value; |
| 30 |
} |
| 31 |
|
| 32 |
public AnnotationMember(AnnotationMember ann) |
| 33 |
{ |
| 34 |
m_typecode = ann.m_typecode; |
| 35 |
m_name = ann.m_name; |
| 36 |
m_value = ann.m_value; |
| 37 |
} |
| 38 |
|
| 39 |
public String getName() |
| 40 |
{ |
| 41 |
return m_name; |
| 42 |
} |
| 43 |
|
| 44 |
/* |
| 45 |
* @brief This function is used with (previous c) types because array names contains []. |
| 46 |
*/ |
| 47 |
|
| 48 |
public TypeCode getTypecode() |
| 49 |
{ |
| 50 |
return m_typecode; |
| 51 |
} |
| 52 |
|
| 53 |
public String getValue() |
| 54 |
{ |
| 55 |
if (m_typecode.isIsEnumType()) |
| 56 |
{ |
| 57 |
EnumTypeCode enumTC = (EnumTypeCode)m_typecode; |
| 58 |
int idx = 0; |
| 59 |
int default_idx = 0; |
| 60 |
for (Member m : enumTC.getMembers()) |
| 61 |
{ |
| 62 |
if (m.getName().equals(m_value)) |
| 63 |
{ |
| 64 |
return Integer.toString(idx); |
| 65 |
} |
| 66 |
else if (m.isAnnotationDefaultLiteral()) |
| 67 |
{ |
| 68 |
default_idx = idx; |
| 69 |
} |
| 70 |
idx++; |
| 71 |
} |
| 72 |
return Integer.toString(default_idx); |
| 73 |
} |
| 74 |
else if (m_typecode.isIsStringType() || m_typecode.isIsWStringType()) |
| 75 |
{ |
| 76 |
if (m_value != null) |
| 77 |
{ |
| 78 |
if (m_value.startsWith("\"") && m_value.endsWith("\"")) |
| 79 |
{ |
| 80 |
return m_value.substring(1, m_value.length() - 1); |
| 81 |
} |
| 82 |
} |
| 83 |
if (m_typecode.isIsWStringType()) |
| 84 |
{ |
| 85 |
return "L\"\""; |
| 86 |
} |
| 87 |
return ""; |
| 88 |
} |
| 89 |
else if (m_typecode.isPrimitiveType()) |
| 90 |
{ |
| 91 |
if (m_value != null) |
| 92 |
{ |
| 93 |
// Check if the string starts with "0x" to determine if it's hexadecimal |
| 94 |
if (m_value.startsWith("0x")) { |
| 95 |
// If it's hexadecimal, parse it using parseInt with radix 16 |
| 96 |
return Integer.toString(Integer.parseInt(m_value.substring(2), 16)); |
| 97 |
} else { |
| 98 |
return m_value; |
| 99 |
} |
| 100 |
} |
| 101 |
return m_typecode.getInitialValue(); |
| 102 |
} |
| 103 |
return m_value; |
| 104 |
} |
| 105 |
|
| 106 |
public String getEnumStringValue() |
| 107 |
{ |
| 108 |
if (m_value != null && m_typecode.isIsEnumType()) |
| 109 |
{ |
| 110 |
EnumTypeCode enumTC = (EnumTypeCode)m_typecode; |
| 111 |
for (Member m : enumTC.getMembers()) |
| 112 |
{ |
| 113 |
String value = m_value; |
| 114 |
if (value.startsWith("\"") && value.endsWith("\"")) |
| 115 |
{ |
| 116 |
value = value.substring(1, value.length() - 1); |
| 117 |
} |
| 118 |
String[] value_with_scopes = value.split("::"); |
| 119 |
value = value_with_scopes[value_with_scopes.length - 1]; |
| 120 |
if (m.getName().equals(value)) |
| 121 |
{ |
| 122 |
return value; |
| 123 |
} |
| 124 |
} |
| 125 |
throw new ParseException(null, m_value + " is not a valid label for " + m_name); |
| 126 |
} |
| 127 |
return m_value; |
| 128 |
} |
| 129 |
|
| 130 |
public void setValue(String value) |
| 131 |
{ |
| 132 |
m_value = value; |
| 133 |
} |
| 134 |
|
| 135 |
public boolean isIsVerbatimPlacement() |
| 136 |
{ |
| 137 |
return getName().equals(Annotation.placement_str); |
| 138 |
} |
| 139 |
|
| 140 |
public boolean isIsVerbatimLanguage() |
| 141 |
{ |
| 142 |
return getName().equals(Annotation.language_str); |
| 143 |
} |
| 144 |
|
| 145 |
public boolean isIsVerbatimText() |
| 146 |
{ |
| 147 |
return getName().equals(Annotation.text_str); |
| 148 |
} |
| 149 |
|
| 150 |
public boolean isIsMax() |
| 151 |
{ |
| 152 |
return getName().equals(Annotation.max_str); |
| 153 |
} |
| 154 |
|
| 155 |
public boolean isIsMin() |
| 156 |
{ |
| 157 |
return getName().equals(Annotation.min_str); |
| 158 |
} |
| 159 |
|
| 160 |
private String m_name = null; |
| 161 |
|
| 162 |
private TypeCode m_typecode = null; |
| 163 |
|
| 164 |
private String m_value = null; |
| 165 |
} |