| 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.context.Context; |
| 18 |
import com.eprosima.idl.parser.exception.RuntimeGenerationException; |
| 19 |
|
| 20 |
import java.util.Map; |
| 21 |
import java.util.HashMap; |
| 22 |
|
| 23 |
import org.antlr.v4.runtime.Token; |
| 24 |
|
| 25 |
public class TreeNode implements Notebook |
| 26 |
{ |
| 27 |
public TreeNode(String scopeFile, boolean isInScope, String scope, String name, Token tk) |
| 28 |
{ |
| 29 |
m_scopeFile = scopeFile; |
| 30 |
m_isinscope = isInScope; |
| 31 |
m_name = name; |
| 32 |
m_scope = scope; |
| 33 |
m_annotations = new HashMap<String, Annotation>(); |
| 34 |
tk_ = tk; |
| 35 |
} |
| 36 |
|
| 37 |
public String getScopeFile() |
| 38 |
{ |
| 39 |
return m_scopeFile; |
| 40 |
} |
| 41 |
|
| 42 |
public boolean isInScope() |
| 43 |
{ |
| 44 |
return m_isinscope; |
| 45 |
} |
| 46 |
|
| 47 |
public String getName() |
| 48 |
{ |
| 49 |
return m_name; |
| 50 |
} |
| 51 |
|
| 52 |
public String getScopedname() |
| 53 |
{ |
| 54 |
if(m_scope == null || m_scope.isEmpty()) |
| 55 |
{ |
| 56 |
return m_name; |
| 57 |
} |
| 58 |
|
| 59 |
return m_scope + "::" + m_name; |
| 60 |
} |
| 61 |
|
| 62 |
public String getROS2Scopedname() |
| 63 |
{ |
| 64 |
if(m_scope == null || m_scope.isEmpty()) |
| 65 |
{ |
| 66 |
return m_name; |
| 67 |
} |
| 68 |
|
| 69 |
return m_scope + "::dds_::" + m_name + "_"; |
| 70 |
} |
| 71 |
|
| 72 |
public String getCScopedname() |
| 73 |
{ |
| 74 |
if(m_scope.isEmpty()) |
| 75 |
{ |
| 76 |
return m_name; |
| 77 |
} |
| 78 |
|
| 79 |
return m_scope.replace("::", "_") + "_" + m_name; |
| 80 |
} |
| 81 |
|
| 82 |
public String getJavaScopedname() |
| 83 |
{ |
| 84 |
if(m_scope.isEmpty()) |
| 85 |
{ |
| 86 |
return m_name; |
| 87 |
} |
| 88 |
|
| 89 |
return m_scope.replace("::", ".") + "." + m_name; |
| 90 |
} |
| 91 |
|
| 92 |
public String getScope() |
| 93 |
{ |
| 94 |
return m_scope; |
| 95 |
} |
| 96 |
|
| 97 |
/*! |
| 98 |
* @ingroup api_for_stg |
| 99 |
* @brief This function returns the namespace where the type would be declared. |
| 100 |
* @return Namespace where the type would be declared. |
| 101 |
*/ |
| 102 |
public String getNamespace() |
| 103 |
{ |
| 104 |
String namespace = m_scope; |
| 105 |
// Remove last scope when declared inside an Interface |
| 106 |
if (isDeclaredInsideInterface()) |
| 107 |
{ |
| 108 |
int last_index = m_scope.lastIndexOf("::"); |
| 109 |
if (last_index == -1) |
| 110 |
{ |
| 111 |
namespace = ""; |
| 112 |
} |
| 113 |
else |
| 114 |
{ |
| 115 |
namespace = m_scope.substring(0, last_index); |
| 116 |
} |
| 117 |
} |
| 118 |
return namespace; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @ingroup api_for_stg |
| 123 |
* @brief This function returns the name to use in the type object registration methods. |
| 124 |
* @return A string with the name to use in the type object registration methods. |
| 125 |
*/ |
| 126 |
public String getNameForTypeObjectRegistration() |
| 127 |
{ |
| 128 |
return interfacePrefix() + getName(); |
| 129 |
} |
| 130 |
|
| 131 |
protected String interfacePrefix() |
| 132 |
{ |
| 133 |
// Get last scope when inside an interface |
| 134 |
if (isDeclaredInsideInterface()) |
| 135 |
{ |
| 136 |
String[] scopes = m_scope.split("::"); |
| 137 |
if (scopes.length > 0) |
| 138 |
{ |
| 139 |
return scopes[scopes.length - 1] + "__"; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return ""; |
| 144 |
} |
| 145 |
|
| 146 |
protected boolean isDeclaredInsideInterface() |
| 147 |
{ |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
* @brief This function returns the scoped name of the interface but |
| 153 |
* changing "::" by "_". |
| 154 |
*/ |
| 155 |
public String getFormatedScopedname() |
| 156 |
{ |
| 157 |
String ret = null; |
| 158 |
|
| 159 |
if(m_scope == null || m_scope.isEmpty()) |
| 160 |
{ |
| 161 |
ret = m_name; |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
ret = m_scope + "::" + m_name; |
| 166 |
} |
| 167 |
|
| 168 |
return ret.replaceAll("::", "_"); |
| 169 |
} |
| 170 |
|
| 171 |
public boolean getHasScope() |
| 172 |
{ |
| 173 |
return !(m_scope == null || m_scope.isEmpty()); |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public void addAnnotation(Context ctx, Annotation annotation) |
| 178 |
{ |
| 179 |
if(annotation != null) |
| 180 |
{ |
| 181 |
m_annotations.put(annotation.getName(), annotation); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
@Override |
| 186 |
public Map<String, Annotation> getAnnotations() |
| 187 |
{ |
| 188 |
return m_annotations; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @ingroup api_for_stg |
| 193 |
* @brief This function checks if the node is annotated as nested. |
| 194 |
* |
| 195 |
* It is designed so it can be used in the templates with `$if(node.annotatedAsNested)$` |
| 196 |
* |
| 197 |
* @return true if the node is annotated as nested, false otherwise. |
| 198 |
*/ |
| 199 |
public boolean isAnnotatedAsNested() |
| 200 |
{ |
| 201 |
Annotation ann = m_annotations.get(Annotation.nested_str); |
| 202 |
if (ann != null) |
| 203 |
{ |
| 204 |
try |
| 205 |
{ |
| 206 |
return ann.getValue().toUpperCase().equals(Annotation.capitalized_true_str); |
| 207 |
} |
| 208 |
catch (RuntimeGenerationException ex) |
| 209 |
{ |
| 210 |
// Should not be called as @nested annotation has only one parameter |
| 211 |
} |
| 212 |
} |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
public Token getToken() |
| 217 |
{ |
| 218 |
return tk_; |
| 219 |
} |
| 220 |
|
| 221 |
private String m_scopeFile = null; |
| 222 |
private boolean m_isinscope = false; |
| 223 |
private String m_name = null; |
| 224 |
private String m_scope = null; |
| 225 |
//! Map that stores the annotations of the interface. |
| 226 |
private HashMap<String, Annotation> m_annotations = null; |
| 227 |
//! IDL Parser token |
| 228 |
Token tk_ = null; |
| 229 |
} |