Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
| 19 | import org.onosproject.yangutils.datamodel.YangCompilerAnnotation; |
| 20 | import org.onosproject.yangutils.datamodel.YangType; |
| 21 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
| 22 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BOOLEAN; |
| 27 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DECIMAL64; |
| 28 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.EMPTY; |
| 29 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT16; |
| 30 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32; |
| 31 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT64; |
| 32 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT8; |
| 33 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT16; |
| 34 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT32; |
| 35 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT64; |
| 36 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT8; |
| 37 | import static org.onosproject.yangutils.translator.tojava.utils.BracketType.OPEN_CLOSE_BRACKET; |
| 38 | import static org.onosproject.yangutils.translator.tojava.utils.BracketType.OPEN_CLOSE_BRACKET_WITH_VALUE; |
| 39 | import static org.onosproject.yangutils.translator.tojava.utils.BracketType.OPEN_CLOSE_BRACKET_WITH_VALUE_AND_RETURN_TYPE; |
| 40 | import static org.onosproject.yangutils.translator.tojava.utils.MethodClassTypes.CLASS_TYPE; |
| 41 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getIfConditionForAddToListMethod; |
| 42 | import static org.onosproject.yangutils.utils.UtilConstants.ADD_STRING; |
| 43 | import static org.onosproject.yangutils.utils.UtilConstants.AND; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 44 | import static org.onosproject.yangutils.utils.UtilConstants.APPEND; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.utils.UtilConstants.AT; |
| 46 | import static org.onosproject.yangutils.utils.UtilConstants.BIG_DECIMAL; |
| 47 | import static org.onosproject.yangutils.utils.UtilConstants.BIG_INTEGER; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 48 | import static org.onosproject.yangutils.utils.UtilConstants.BITSET; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_DATA_TYPE; |
| 50 | import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_WRAPPER; |
| 51 | import static org.onosproject.yangutils.utils.UtilConstants.BUILDER; |
| 52 | import static org.onosproject.yangutils.utils.UtilConstants.BUILDER_LOWER_CASE; |
| 53 | import static org.onosproject.yangutils.utils.UtilConstants.BYTE; |
| 54 | import static org.onosproject.yangutils.utils.UtilConstants.BYTE_WRAPPER; |
| 55 | import static org.onosproject.yangutils.utils.UtilConstants.CATCH; |
| 56 | import static org.onosproject.yangutils.utils.UtilConstants.CHECK_NOT_NULL_STRING; |
| 57 | import static org.onosproject.yangutils.utils.UtilConstants.CLASS; |
| 58 | import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET; |
| 59 | import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS; |
| 60 | import static org.onosproject.yangutils.utils.UtilConstants.COLON; |
| 61 | import static org.onosproject.yangutils.utils.UtilConstants.COMMA; |
| 62 | import static org.onosproject.yangutils.utils.UtilConstants.COMPARE_TO; |
| 63 | import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_CAPS; |
| 64 | import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET; |
| 65 | import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET; |
| 66 | import static org.onosproject.yangutils.utils.UtilConstants.DOUBLE; |
| 67 | import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION; |
| 68 | import static org.onosproject.yangutils.utils.UtilConstants.ELSE; |
| 69 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
| 70 | import static org.onosproject.yangutils.utils.UtilConstants.EQUAL; |
| 71 | import static org.onosproject.yangutils.utils.UtilConstants.EXCEPTION; |
| 72 | import static org.onosproject.yangutils.utils.UtilConstants.EXCEPTION_VAR; |
| 73 | import static org.onosproject.yangutils.utils.UtilConstants.EXTEND; |
| 74 | import static org.onosproject.yangutils.utils.UtilConstants.FALSE; |
| 75 | import static org.onosproject.yangutils.utils.UtilConstants.FOR; |
| 76 | import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION; |
| 77 | import static org.onosproject.yangutils.utils.UtilConstants.FROM_STRING_METHOD_NAME; |
| 78 | import static org.onosproject.yangutils.utils.UtilConstants.GET; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 79 | import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_METHOD_STRING; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 80 | import static org.onosproject.yangutils.utils.UtilConstants.IF; |
| 81 | import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS; |
| 82 | import static org.onosproject.yangutils.utils.UtilConstants.IMPORT; |
| 83 | import static org.onosproject.yangutils.utils.UtilConstants.IN; |
| 84 | import static org.onosproject.yangutils.utils.UtilConstants.INT; |
| 85 | import static org.onosproject.yangutils.utils.UtilConstants.INTEGER_WRAPPER; |
| 86 | import static org.onosproject.yangutils.utils.UtilConstants.INT_MAX_RANGE; |
| 87 | import static org.onosproject.yangutils.utils.UtilConstants.INT_MIN_RANGE; |
| 88 | import static org.onosproject.yangutils.utils.UtilConstants.LIST; |
| 89 | import static org.onosproject.yangutils.utils.UtilConstants.LONG; |
| 90 | import static org.onosproject.yangutils.utils.UtilConstants.LONG_MAX_RANGE; |
| 91 | import static org.onosproject.yangutils.utils.UtilConstants.LONG_MIN_RANGE; |
| 92 | import static org.onosproject.yangutils.utils.UtilConstants.LONG_WRAPPER; |
| 93 | import static org.onosproject.yangutils.utils.UtilConstants.MAP; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 94 | import static org.onosproject.yangutils.utils.UtilConstants.MORE_OBJ_ATTR; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 95 | import static org.onosproject.yangutils.utils.UtilConstants.NEW; |
| 96 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 97 | import static org.onosproject.yangutils.utils.UtilConstants.OBJECT; |
| 98 | import static org.onosproject.yangutils.utils.UtilConstants.OBJECT_STRING; |
| 99 | import static org.onosproject.yangutils.utils.UtilConstants.OF; |
| 100 | import static org.onosproject.yangutils.utils.UtilConstants.OMIT_NULL_VALUE_STRING; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 101 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CLOSE_BRACKET_STRING; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 102 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET; |
| 103 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS; |
| 104 | import static org.onosproject.yangutils.utils.UtilConstants.OVERRIDE; |
| 105 | import static org.onosproject.yangutils.utils.UtilConstants.PARSE_BOOLEAN; |
| 106 | import static org.onosproject.yangutils.utils.UtilConstants.PARSE_BYTE; |
| 107 | import static org.onosproject.yangutils.utils.UtilConstants.PARSE_INT; |
| 108 | import static org.onosproject.yangutils.utils.UtilConstants.PARSE_LONG; |
| 109 | import static org.onosproject.yangutils.utils.UtilConstants.PARSE_SHORT; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 110 | import static org.onosproject.yangutils.utils.UtilConstants.PATTERN; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 111 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 112 | import static org.onosproject.yangutils.utils.UtilConstants.PUT; |
| 113 | import static org.onosproject.yangutils.utils.UtilConstants.QUEUE; |
| 114 | import static org.onosproject.yangutils.utils.UtilConstants.QUOTES; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 115 | import static org.onosproject.yangutils.utils.UtilConstants.QUOTE_STRING; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 116 | import static org.onosproject.yangutils.utils.UtilConstants.RETURN; |
| 117 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLON; |
| 118 | import static org.onosproject.yangutils.utils.UtilConstants.SET; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 119 | import static org.onosproject.yangutils.utils.UtilConstants.SET_VALUE_PARA; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 120 | import static org.onosproject.yangutils.utils.UtilConstants.SHORT; |
| 121 | import static org.onosproject.yangutils.utils.UtilConstants.SHORT_MAX_RANGE; |
| 122 | import static org.onosproject.yangutils.utils.UtilConstants.SHORT_MIN_RANGE; |
| 123 | import static org.onosproject.yangutils.utils.UtilConstants.SHORT_WRAPPER; |
| 124 | import static org.onosproject.yangutils.utils.UtilConstants.SIXTEEN_SPACE_INDENTATION; |
| 125 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 126 | import static org.onosproject.yangutils.utils.UtilConstants.STRING_BUILDER; |
| 127 | import static org.onosproject.yangutils.utils.UtilConstants.STRING_BUILDER_VAR; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 128 | import static org.onosproject.yangutils.utils.UtilConstants.THIS; |
| 129 | import static org.onosproject.yangutils.utils.UtilConstants.TMP_VAL; |
| 130 | import static org.onosproject.yangutils.utils.UtilConstants.TRY; |
| 131 | import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION; |
| 132 | import static org.onosproject.yangutils.utils.UtilConstants.TWENTY_SPACE_INDENTATION; |
| 133 | import static org.onosproject.yangutils.utils.UtilConstants.UINT8_MAX_RANGE; |
| 134 | import static org.onosproject.yangutils.utils.UtilConstants.UINT8_MIN_RANGE; |
| 135 | import static org.onosproject.yangutils.utils.UtilConstants.UINT_MAX_RANGE; |
| 136 | import static org.onosproject.yangutils.utils.UtilConstants.UINT_MIN_RANGE; |
| 137 | import static org.onosproject.yangutils.utils.UtilConstants.ULONG_MAX_RANGE; |
| 138 | import static org.onosproject.yangutils.utils.UtilConstants.ULONG_MIN_RANGE; |
| 139 | import static org.onosproject.yangutils.utils.UtilConstants.VALIDATE_RANGE; |
| 140 | import static org.onosproject.yangutils.utils.UtilConstants.VALUE; |
| 141 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO_LOWER_CASE; |
| 142 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_UTILS_TODO; |
| 143 | import static org.onosproject.yangutils.utils.UtilConstants.ZERO; |
| 144 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast; |
| 145 | |
| 146 | /** |
| 147 | * Represents string generator for translator. |
| 148 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 149 | public final class StringGenerator { |
| 150 | |
| 151 | private StringGenerator() { |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Returns compare to string. |
| 156 | * |
| 157 | * @return compare to string |
| 158 | */ |
| 159 | static String getCompareToString() { |
| 160 | return PERIOD + COMPARE_TO; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Returns lesser than and equals condition. |
| 165 | * |
| 166 | * @return lesser than and equals condition |
| 167 | */ |
| 168 | static String getLesserThanCondition() { |
| 169 | return SPACE + DIAMOND_OPEN_BRACKET + EQUAL + SPACE; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Returns greater than and equals condition. |
| 174 | * |
| 175 | * @return greater than and equals condition |
| 176 | */ |
| 177 | static String getGreaterThanCondition() { |
| 178 | return SPACE + DIAMOND_CLOSE_BRACKET + EQUAL + SPACE; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Returns && conditional string. |
| 183 | * |
| 184 | * @param cond1 condition one |
| 185 | * @param cond2 condition two |
| 186 | * @return && conditional string |
| 187 | */ |
| 188 | static String ifAndAndCondition(String cond1, String cond2) { |
| 189 | return cond1 + SPACE + AND + AND + SPACE + cond2; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Returns equal equal conditional string. |
| 194 | * |
| 195 | * @param cond1 condition one |
| 196 | * @param cond2 condition two |
| 197 | * @return equal equal conditional string |
| 198 | */ |
| 199 | static String ifEqualEqualCondition(String cond1, String cond2) { |
| 200 | return cond1 + SPACE + EQUAL + EQUAL + SPACE + cond2; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Returns new instance string. |
| 205 | * |
| 206 | * @param returnType return type |
| 207 | * @param varName variable name |
| 208 | * @param space spaces |
| 209 | * @param value value |
| 210 | * @return new instance string |
| 211 | */ |
| 212 | static String getNewInstance(String returnType, String varName, |
| 213 | String space, String value) { |
| 214 | return space + returnType + SPACE + varName + SPACE + EQUAL + SPACE + |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 215 | NEW + SPACE + returnType + brackets( |
| 216 | OPEN_CLOSE_BRACKET_WITH_VALUE, value, null) + signatureClose(); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Returns return string. |
| 221 | * |
| 222 | * @param value value to be returned |
| 223 | * @param space spaces |
| 224 | * @return return string |
| 225 | */ |
| 226 | static String getReturnString(String value, String space) { |
| 227 | return space + RETURN + SPACE + value; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Returns new line string with spaces. |
| 232 | * |
| 233 | * @param space spaces |
| 234 | * @return new line string with spaces |
| 235 | */ |
| 236 | static String getNewLineAndSpace(String space) { |
| 237 | return NEW_LINE + space; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Returns method close string. |
| 242 | * |
| 243 | * @param type indentation type |
| 244 | * @return method close string |
| 245 | */ |
| 246 | static String methodClose(IndentationType type) { |
| 247 | switch (type) { |
| 248 | case EIGHT_SPACE: |
| 249 | return EIGHT_SPACE_INDENTATION + CLOSE_CURLY_BRACKET + |
| 250 | NEW_LINE; |
| 251 | case TWELVE_SPACE: |
| 252 | return TWELVE_SPACE_INDENTATION + CLOSE_CURLY_BRACKET + |
| 253 | NEW_LINE; |
| 254 | case SIXTEEN_SPACE: |
| 255 | return SIXTEEN_SPACE_INDENTATION + CLOSE_CURLY_BRACKET + |
| 256 | NEW_LINE; |
| 257 | case TWENTY_SPACE: |
| 258 | return TWENTY_SPACE_INDENTATION + CLOSE_CURLY_BRACKET + |
| 259 | NEW_LINE; |
| 260 | case TWENTY_EIGHT_SPACE: |
| 261 | return FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET + |
| 262 | NEW_LINE; |
| 263 | case TWENTY_FOUR_SPACE: |
| 264 | return FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET + |
| 265 | NEW_LINE; |
| 266 | default: |
| 267 | return FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET + |
| 268 | NEW_LINE; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Returns body of the method. |
| 274 | * |
| 275 | * @param type type of method body |
| 276 | * @param paraName parameter name |
| 277 | * @param methodName method name |
| 278 | * @param space space to be given before body |
| 279 | * @param prefix prefix for internal method |
| 280 | * @param paramType parameter type |
| 281 | * @param isBuilderSetter is for builder setter |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 282 | * @param setterVal value to set in setter |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 283 | * @return body of the method |
| 284 | */ |
| 285 | static String methodBody(MethodBodyTypes type, String paraName, |
| 286 | String methodName, |
| 287 | String space, String prefix, |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 288 | String paramType, boolean isBuilderSetter, String setterVal) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 289 | StringBuilder builder = new StringBuilder(); |
| 290 | String body; |
| 291 | switch (type) { |
| 292 | case GETTER: |
| 293 | return getReturnString(paraName, space); |
| 294 | case SETTER: |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 295 | if (setterVal == null) { |
| 296 | setterVal = paraName; |
| 297 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 298 | body = space + THIS + PERIOD + paraName + SPACE + EQUAL + SPACE + |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 299 | setterVal + signatureClose(); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 300 | builder.append(body); |
| 301 | if (isBuilderSetter) { |
| 302 | body = getReturnString(THIS + signatureClose(), space); |
| 303 | builder.append(body); |
| 304 | } |
| 305 | return builder.toString(); |
| 306 | case BUILD: |
| 307 | return getReturnString( |
| 308 | NEW + SPACE + paraName + brackets( |
| 309 | OPEN_CLOSE_BRACKET_WITH_VALUE, THIS, null) + |
| 310 | signatureClose(), space); |
| 311 | case CONSTRUCTOR: |
| 312 | return space + THIS + PERIOD + paraName + SPACE + |
| 313 | EQUAL + SPACE + BUILDER_LOWER_CASE + OBJECT + PERIOD + |
| 314 | prefix + methodName + |
| 315 | brackets(OPEN_CLOSE_BRACKET, null, null) + |
| 316 | signatureClose(); |
| 317 | case DEFAULT_CONSTRUCTOR: |
| 318 | return EMPTY_STRING; |
| 319 | case ADD_TO_LIST: |
| 320 | return space + getIfConditionForAddToListMethod(paraName) + |
| 321 | space + paraName + |
| 322 | brackets(OPEN_CLOSE_BRACKET, null, null) + PERIOD + |
| 323 | ADD_STRING + |
| 324 | brackets(OPEN_CLOSE_BRACKET_WITH_VALUE, VALUE, null) + |
| 325 | signatureClose() + getReturnString( |
| 326 | THIS + signatureClose(), space); |
| 327 | case AUGMENTED_MAP_ADD: |
| 328 | return space + YANG_AUGMENTED_INFO_LOWER_CASE + MAP + |
| 329 | PERIOD + PUT + OPEN_PARENTHESIS + CLASS + |
| 330 | OBJECT_STRING + COMMA + SPACE + VALUE + |
| 331 | CLOSE_PARENTHESIS + signatureClose(); |
| 332 | case AUGMENTED_MAP_GET_VALUE: |
| 333 | return getReturnString( |
| 334 | YANG_AUGMENTED_INFO_LOWER_CASE + MAP + PERIOD + GET + |
| 335 | brackets(OPEN_CLOSE_BRACKET_WITH_VALUE, CLASS + |
| 336 | OBJECT_STRING, null) + signatureClose(), |
| 337 | space); |
| 338 | case AUGMENTED_MAP_GETTER: |
| 339 | return getReturnString(YANG_AUGMENTED_INFO_LOWER_CASE + MAP + |
| 340 | signatureClose(), space); |
| 341 | case MANAGER_METHODS: |
| 342 | body = space + YANG_UTILS_TODO + NEW_LINE; |
| 343 | builder.append(body); |
| 344 | if (paramType != null) { |
| 345 | body = getReturnString(parseTypeForReturnValue(paramType), |
| 346 | space); |
| 347 | builder.append(body) |
| 348 | .append(signatureClose()); |
| 349 | } |
| 350 | return builder.toString(); |
| 351 | case OF_METHOD: |
| 352 | return getReturnString( |
| 353 | NEW + SPACE + paraName + brackets( |
| 354 | OPEN_CLOSE_BRACKET_WITH_VALUE, VALUE, null) |
| 355 | + signatureClose(), space); |
| 356 | case TO_STRING: |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 357 | return getToStringMethodsAddString(space, paraName) + paraName + |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 358 | CLOSE_PARENTHESIS; |
| 359 | case EQUALS_METHOD: |
| 360 | default: |
| 361 | return null; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /** |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 366 | * Returns to string method's add string. |
| 367 | * |
| 368 | * @param space indentation |
| 369 | * @param paraName parameter name |
| 370 | * @return to string method's add string |
| 371 | */ |
| 372 | static String getToStringMethodsAddString(String space, String paraName) { |
| 373 | return space + PERIOD + ADD_STRING + OPEN_PARENTHESIS + |
| 374 | getQuotedString(paraName) + COMMA + SPACE; |
| 375 | } |
| 376 | |
| 377 | /** |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 378 | * Returns end of line. |
| 379 | * |
| 380 | * @return end of line |
| 381 | */ |
| 382 | static String signatureClose() { |
| 383 | return SEMI_COLON + NEW_LINE; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Returns method signature close for method class type. |
| 388 | * |
| 389 | * @param type method class type |
| 390 | * @return method signature close for method class type |
| 391 | */ |
| 392 | static String methodSignatureClose(MethodClassTypes type) { |
| 393 | switch (type) { |
| 394 | case INTERFACE_TYPE: |
| 395 | return signatureClose(); |
| 396 | case CLASS_TYPE: |
| 397 | return SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 398 | default: |
| 399 | return null; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Returns method param. |
| 405 | * |
| 406 | * @param type type of param |
| 407 | * @param name name of param |
| 408 | * @return method param |
| 409 | */ |
| 410 | private static String methodParam(String type, String name) { |
| 411 | return type + SPACE + name; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Returns comma followed by a space. |
| 416 | * |
| 417 | * @return comma followed by a space |
| 418 | */ |
| 419 | private static String commaWithSpace() { |
| 420 | return COMMA + SPACE; |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Returns bracket string for the given type. |
| 425 | * |
| 426 | * @param type bracket type |
| 427 | * @param value value to be added in brackets |
| 428 | * @param returnType returns type to be added for value |
| 429 | * @return bracket for the given type. |
| 430 | */ |
| 431 | static String brackets(BracketType type, String value, |
| 432 | String returnType) { |
| 433 | String ret = EMPTY_STRING; |
| 434 | switch (type) { |
| 435 | case OPEN_CLOSE_BRACKET: |
| 436 | return OPEN_PARENTHESIS + CLOSE_PARENTHESIS; |
| 437 | case OPEN_CLOSE_BRACKET_WITH_VALUE: |
| 438 | return OPEN_PARENTHESIS + value + CLOSE_PARENTHESIS; |
| 439 | case OPEN_CLOSE_BRACKET_WITH_VALUE_AND_RETURN_TYPE: |
| 440 | if (returnType != null) { |
| 441 | ret = returnType + SPACE; |
| 442 | } |
| 443 | return OPEN_PARENTHESIS + ret + value + |
| 444 | CLOSE_PARENTHESIS; |
| 445 | case OPEN_BRACKET_WITH_VALUE: |
| 446 | ret = EMPTY_STRING; |
| 447 | if (returnType != null) { |
| 448 | ret = returnType + SPACE; |
| 449 | } |
| 450 | return OPEN_PARENTHESIS + ret + value + |
| 451 | COMMA; |
| 452 | case CLOSE_BRACKET_WITH_VALUE: |
| 453 | ret = EMPTY_STRING; |
| 454 | if (returnType != null) { |
| 455 | ret = returnType + SPACE; |
| 456 | } |
| 457 | return SPACE + ret + value + |
| 458 | CLOSE_PARENTHESIS; |
| 459 | case OPEN_CLOSE_DIAMOND: |
| 460 | return DIAMOND_OPEN_BRACKET + DIAMOND_CLOSE_BRACKET; |
| 461 | case OPEN_CLOSE_DIAMOND_WITH_VALUE: |
| 462 | return DIAMOND_OPEN_BRACKET + value + DIAMOND_CLOSE_BRACKET; |
| 463 | default: |
| 464 | return null; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Returns method signature for multi attribute methods. |
| 470 | * |
| 471 | * @param methodName method name |
| 472 | * @param prefix prefix for method |
| 473 | * @param modifier modifier for method |
| 474 | * @param methodReturnType method's return type |
| 475 | * @param params parameters |
| 476 | * @param type type of method |
| 477 | * @return method signature for multi attribute methods |
| 478 | */ |
| 479 | static String multiAttrMethodSignature(String methodName, String |
| 480 | prefix, String modifier, String methodReturnType, |
| 481 | Map<String, String> params, |
| 482 | MethodClassTypes type) { |
| 483 | StringBuilder methodBuilder = new StringBuilder(FOUR_SPACE_INDENTATION); |
| 484 | String method = EMPTY_STRING; |
| 485 | if (modifier != null) { |
| 486 | method = modifier + SPACE; |
| 487 | } |
| 488 | methodBuilder.append(method); |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 489 | if (prefix == null) { |
| 490 | prefix = EMPTY_STRING; |
| 491 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 492 | if (methodReturnType != null) { |
| 493 | method = methodReturnType + SPACE + prefix + methodName; |
| 494 | } else { |
| 495 | method = prefix + methodName; |
| 496 | } |
| 497 | //Append method beginning. |
| 498 | methodBuilder.append(method) |
| 499 | .append(OPEN_PARENTHESIS); |
| 500 | for (Map.Entry<String, String> param : params.entrySet()) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 501 | methodBuilder.append(methodParam(param.getValue(), param.getKey())); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 502 | methodBuilder.append(commaWithSpace()); |
| 503 | } |
| 504 | String para = methodBuilder.toString(); |
| 505 | String[] array = {SPACE, COMMA}; |
| 506 | para = trimAtLast(para, array); |
| 507 | methodBuilder = new StringBuilder(para) |
| 508 | .append(CLOSE_PARENTHESIS) |
| 509 | .append(methodSignatureClose(type)); |
| 510 | |
| 511 | return methodBuilder.toString(); |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Returns method signature for interface and implementation classes. |
| 516 | * |
| 517 | * @param methodName name of the method |
| 518 | * @param prefix prefix which needs to be added for method |
| 519 | * @param modifier modifier which needs to be added for method |
| 520 | * @param paraVal value which needs to be added as parameter |
| 521 | * @param methodReturnType returns type to be added for method |
| 522 | * @param paraReturnType return type to be added for parameter |
| 523 | * @param type method class type |
| 524 | * @return method signature for interface and implementation classes |
| 525 | */ |
| 526 | static String methodSignature( |
| 527 | String methodName, String prefix, String modifier, String paraVal, |
| 528 | String methodReturnType, String paraReturnType, |
| 529 | MethodClassTypes type) { |
| 530 | StringBuilder methodBuilder = new StringBuilder(FOUR_SPACE_INDENTATION); |
| 531 | String method = EMPTY_STRING; |
| 532 | if (modifier != null) { |
| 533 | method = modifier + SPACE; |
| 534 | } |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 535 | if (prefix == null) { |
| 536 | prefix = EMPTY_STRING; |
| 537 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 538 | methodBuilder.append(method); |
| 539 | if (methodReturnType != null) { |
| 540 | method = methodReturnType + SPACE + prefix + methodName; |
| 541 | } else { |
| 542 | method = prefix + methodName; |
| 543 | } |
| 544 | //Append method beginning. |
| 545 | methodBuilder.append(method); |
| 546 | |
| 547 | if (paraVal != null) { |
| 548 | methodBuilder.append(brackets( |
| 549 | OPEN_CLOSE_BRACKET_WITH_VALUE_AND_RETURN_TYPE, |
| 550 | paraVal, paraReturnType)); |
| 551 | } else { |
| 552 | methodBuilder.append(brackets(OPEN_CLOSE_BRACKET, null, |
| 553 | null)); |
| 554 | } |
| 555 | |
| 556 | methodBuilder.append(methodSignatureClose(type)); |
| 557 | |
| 558 | return methodBuilder.toString(); |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Returns list attribute. |
| 563 | * |
| 564 | * @param attrType attribute type |
| 565 | * @param compilerAnnotation compiler annotations |
| 566 | * @return list attribute |
| 567 | */ |
| 568 | |
| 569 | static String getListAttribute(String attrType, |
| 570 | YangCompilerAnnotation compilerAnnotation) { |
| 571 | String listAttr; |
| 572 | if (compilerAnnotation != null && |
| 573 | compilerAnnotation.getYangAppDataStructure() != null) { |
| 574 | switch (compilerAnnotation.getYangAppDataStructure() |
| 575 | .getDataStructure()) { |
| 576 | case QUEUE: { |
| 577 | listAttr = QUEUE + DIAMOND_OPEN_BRACKET + attrType + |
| 578 | DIAMOND_CLOSE_BRACKET; |
| 579 | break; |
| 580 | } |
| 581 | case SET: { |
| 582 | listAttr = SET + DIAMOND_OPEN_BRACKET + attrType + |
| 583 | DIAMOND_CLOSE_BRACKET; |
| 584 | break; |
| 585 | } |
| 586 | case LIST: { |
| 587 | listAttr = getListString() + attrType + |
| 588 | DIAMOND_CLOSE_BRACKET; |
| 589 | break; |
| 590 | } |
| 591 | default: { |
| 592 | listAttr = getListString() + attrType + |
| 593 | DIAMOND_CLOSE_BRACKET; |
| 594 | } |
| 595 | } |
| 596 | } else { |
| 597 | listAttr = getListString() + attrType + DIAMOND_CLOSE_BRACKET; |
| 598 | } |
| 599 | return listAttr; |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Returns getters for value and select leaf. |
| 604 | * |
| 605 | * @return getters for value and select leaf |
| 606 | */ |
| 607 | static String getGettersForValueAndSelectLeaf() { |
| 608 | return "\n" + |
| 609 | " @Override\n" + |
| 610 | " public boolean isLeafValueSet(LeafIdentifier leaf) {\n" + |
| 611 | " return getValueLeafFlags().get(leaf.getLeafIndex());\n" + |
| 612 | " }\n" + |
| 613 | "\n" + |
| 614 | " @Override\n" + |
| 615 | " public boolean isSelectLeaf(LeafIdentifier leaf) {\n" + |
| 616 | " return getSelectLeafFlags().get(leaf.getLeafIndex());\n" + |
| 617 | " }\n"; |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Returns getter methods for operation attributes. |
| 622 | * |
| 623 | * @return getter methods for operation attributes |
| 624 | */ |
| 625 | static String getOperationAttributesGetters() { |
| 626 | return "\n" + |
| 627 | " /**\n" + |
| 628 | " * Returns the valueLeafFlags.\n" + |
| 629 | " *\n" + |
| 630 | " * @return value of valueLeafFlags\n" + |
| 631 | " */\n" + |
| 632 | " public BitSet getValueLeafFlags() {\n" + |
| 633 | " return valueLeafFlags;\n" + |
| 634 | " }\n" + |
| 635 | "\n" + |
| 636 | " /**\n" + |
| 637 | " * Returns the selectLeafFlags.\n" + |
| 638 | " *\n" + |
| 639 | " * @return value of selectLeafFlags\n" + |
| 640 | " */\n" + |
| 641 | " public BitSet getSelectLeafFlags() {\n" + |
| 642 | " return selectLeafFlags;\n" + |
| 643 | " }\n" + |
| 644 | "\n"; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Returns interface leaf identifier enum method. |
| 649 | * |
| 650 | * @return interface leaf identifier enum method |
| 651 | */ |
| 652 | static String getInterfaceLeafIdEnumMethods() { |
| 653 | return "\n\n private int leafIndex;\n" + |
| 654 | "\n" + |
| 655 | " public int getLeafIndex() {\n" + |
| 656 | " return leafIndex;\n" + |
| 657 | " }\n" + |
| 658 | "\n" + |
| 659 | " LeafIdentifier(int value) {\n" + |
| 660 | " this.leafIndex = value;\n" + |
| 661 | " }\n" + |
| 662 | " }\n"; |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * Returns if condition string for typedef constructor. |
| 667 | * |
| 668 | * @param type type of conflict |
| 669 | * @param addFirst true int/long need to be added first |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 670 | * @param val value to set |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 671 | * @return if condition string for typedef constructor |
| 672 | */ |
| 673 | static String ifConditionForIntInTypeDefConstructor(ValidatorTypeForUnionTypes type, |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 674 | boolean addFirst, String val) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 675 | String condition = |
| 676 | EIGHT_SPACE_INDENTATION + IF + SPACE + OPEN_PARENTHESIS + |
| 677 | VALIDATE_RANGE + OPEN_PARENTHESIS; |
| 678 | |
| 679 | switch (type) { |
| 680 | case INT_TYPE_CONFLICT: |
| 681 | if (addFirst) { |
| 682 | condition = condition + INT_MIN_RANGE + COMMA + SPACE + |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 683 | INT_MAX_RANGE + COMMA + SPACE + val; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 684 | } else { |
| 685 | condition = condition + UINT_MIN_RANGE + COMMA + SPACE + |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 686 | UINT_MAX_RANGE + COMMA + SPACE + val; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 687 | } |
| 688 | break; |
| 689 | case LONG_TYPE_CONFLICT: |
| 690 | if (addFirst) { |
| 691 | condition = condition + LONG_MIN_RANGE + COMMA + SPACE + |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 692 | LONG_MAX_RANGE + COMMA + SPACE + val; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 693 | } else { |
| 694 | condition = condition + ULONG_MIN_RANGE + COMMA + SPACE + |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 695 | ULONG_MAX_RANGE + COMMA + SPACE + val; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 696 | } |
| 697 | break; |
| 698 | case SHORT_TYPE_CONFLICT: |
| 699 | if (addFirst) { |
| 700 | condition = condition + SHORT_MIN_RANGE + COMMA + SPACE + |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 701 | SHORT_MAX_RANGE + COMMA + SPACE + val; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 702 | } else { |
| 703 | condition = condition + UINT8_MIN_RANGE + COMMA + SPACE + |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 704 | UINT8_MAX_RANGE + COMMA + SPACE + val; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 705 | } |
| 706 | break; |
| 707 | default: |
| 708 | return null; |
| 709 | } |
| 710 | |
| 711 | return condition + CLOSE_PARENTHESIS + CLOSE_PARENTHESIS + SPACE + |
| 712 | OPEN_CURLY_BRACKET + NEW_LINE; |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * Returns from string method parsed string. |
| 717 | * |
| 718 | * @param targetDataType target data type |
| 719 | * @param yangType YANG type |
| 720 | * @return parsed string |
| 721 | */ |
| 722 | static String getParseFromStringMethod(String targetDataType, |
| 723 | YangType<?> yangType) { |
| 724 | YangDataTypes type = yangType.getDataType(); |
| 725 | |
| 726 | switch (type) { |
| 727 | case INT8: |
| 728 | return BYTE_WRAPPER + PERIOD + PARSE_BYTE; |
| 729 | case INT16: |
| 730 | return SHORT_WRAPPER + PERIOD + PARSE_SHORT; |
| 731 | case INT32: |
| 732 | return INTEGER_WRAPPER + PERIOD + PARSE_INT; |
| 733 | case INT64: |
| 734 | return LONG_WRAPPER + PERIOD + PARSE_LONG; |
| 735 | case UINT8: |
| 736 | return SHORT_WRAPPER + PERIOD + PARSE_SHORT; |
| 737 | case UINT16: |
| 738 | return INTEGER_WRAPPER + PERIOD + PARSE_INT; |
| 739 | case UINT32: |
| 740 | return LONG_WRAPPER + PERIOD + PARSE_LONG; |
| 741 | case UINT64: |
| 742 | return NEW + SPACE + BIG_INTEGER; |
| 743 | case DECIMAL64: |
| 744 | return NEW + SPACE + BIG_DECIMAL; |
| 745 | case STRING: |
| 746 | case IDENTITYREF: |
| 747 | return EMPTY_STRING; |
| 748 | case EMPTY: |
| 749 | case BOOLEAN: |
| 750 | return BOOLEAN_WRAPPER + PERIOD + PARSE_BOOLEAN; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 751 | case ENUMERATION: |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 752 | return targetDataType + PERIOD + OF; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 753 | case DERIVED: |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 754 | case UNION: |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 755 | return targetDataType + PERIOD + FROM_STRING_METHOD_NAME; |
| 756 | default: |
| 757 | throw new TranslatorException("given data type is not " + |
| 758 | "supported. " + |
| 759 | yangType.getDataTypeName() + |
| 760 | " in " + |
| 761 | yangType.getLineNumber() + |
| 762 | " at " + |
| 763 | yangType.getCharPosition() + |
| 764 | " in " + |
| 765 | yangType.getFileName()); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * Returns sub string with catch statement for union's from string method. |
| 771 | * |
| 772 | * @return sub string with catch statement for union's from string method |
| 773 | */ |
| 774 | static String getCatchSubString() { |
| 775 | return CLOSE_CURLY_BRACKET + SPACE + CATCH + SPACE + |
| 776 | brackets(OPEN_CLOSE_BRACKET_WITH_VALUE_AND_RETURN_TYPE, EXCEPTION_VAR, |
| 777 | EXCEPTION) + SPACE + OPEN_CURLY_BRACKET; |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Returns sub string with return statement for union's from string method. |
| 782 | * |
| 783 | * @return sub string with return statement for union's from string method |
| 784 | */ |
| 785 | static String getReturnOfSubString() { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 786 | return getReturnString(OF, TWELVE_SPACE_INDENTATION) + |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 787 | brackets(OPEN_CLOSE_BRACKET_WITH_VALUE, TMP_VAL, null) + |
| 788 | signatureClose(); |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * Returns sub string with try statement for union's from string method. |
| 793 | * |
| 794 | * @return sub string with try statement for union's from string method |
| 795 | */ |
| 796 | static String getTrySubString() { |
| 797 | return TRY + SPACE + OPEN_CURLY_BRACKET; |
| 798 | } |
| 799 | |
| 800 | /* |
| 801 | * Returns omit null value string. |
| 802 | * |
| 803 | * @return omit null value string |
| 804 | */ |
| 805 | static String getOmitNullValueString() { |
| 806 | return TWELVE_SPACE_INDENTATION + PERIOD + OMIT_NULL_VALUE_STRING + |
| 807 | NEW_LINE; |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * Returns collection's iterator method. |
| 812 | * |
| 813 | * @param indentation indentation |
| 814 | * @param loopVar loop variable |
| 815 | * @param collection collection |
| 816 | * @return collection's iterator method |
| 817 | */ |
| 818 | static String getCollectionIteratorForLoopBegin(String indentation, |
| 819 | String loopVar, |
| 820 | String collection) { |
| 821 | return indentation + FOR + SPACE + OPEN_PARENTHESIS + loopVar + SPACE + |
| 822 | COLON + SPACE + collection + CLOSE_PARENTHESIS + SPACE + |
| 823 | OPEN_CURLY_BRACKET + NEW_LINE; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * Returns if else condition's signature. |
| 828 | * |
| 829 | * @param indentation indentation |
| 830 | * @param condition conditions |
| 831 | * @return if else condition's signature |
| 832 | */ |
| 833 | static String getElseIfConditionBegin(String indentation, |
| 834 | String condition) { |
| 835 | return indentation + CLOSE_CURLY_BRACKET + SPACE + ELSE + SPACE + |
| 836 | getIfConditionBegin(EMPTY_STRING, condition); |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * Returns if condition's signature. |
| 841 | * |
| 842 | * @param indentation indentation |
| 843 | * @param condition conditions |
| 844 | * @return if condition's signature |
| 845 | */ |
| 846 | static String getIfConditionBegin(String indentation, String condition) { |
| 847 | return indentation + IF + SPACE + brackets( |
| 848 | OPEN_CLOSE_BRACKET_WITH_VALUE, condition, EMPTY_STRING) + |
| 849 | methodSignatureClose(CLASS_TYPE); |
| 850 | } |
| 851 | |
| 852 | /** |
| 853 | * Returns whether the data type is of primitive data type. |
| 854 | * |
| 855 | * @param dataType data type to be checked |
| 856 | * @return true, if data type can have primitive data type, false otherwise |
| 857 | */ |
| 858 | static boolean isPrimitiveDataType(YangDataTypes dataType) { |
| 859 | return dataType == INT8 || |
| 860 | dataType == INT16 || |
| 861 | dataType == INT32 || |
| 862 | dataType == INT64 || |
| 863 | dataType == UINT8 || |
| 864 | dataType == UINT16 || |
| 865 | dataType == UINT32 || |
| 866 | dataType == UINT64 || |
| 867 | dataType == DECIMAL64 || |
| 868 | dataType == BOOLEAN || |
| 869 | dataType == EMPTY; |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * Returns list string. |
| 874 | * |
| 875 | * @return list string |
| 876 | */ |
| 877 | private static String getListString() { |
| 878 | return LIST + DIAMOND_OPEN_BRACKET; |
| 879 | } |
| 880 | |
| 881 | /** |
| 882 | * Returns override string. |
| 883 | * |
| 884 | * @return override string |
| 885 | */ |
| 886 | public static String getOverRideString() { |
| 887 | return NEW_LINE + FOUR_SPACE_INDENTATION + OVERRIDE + NEW_LINE; |
| 888 | } |
| 889 | |
| 890 | /** |
| 891 | * Returns value leaf flag setter. |
| 892 | * |
| 893 | * @param name name of leaf |
| 894 | * @return value leaf flag setter |
| 895 | */ |
| 896 | static String getValueLeafSetString(String name) { |
| 897 | return "\n valueLeafFlags.set(LeafIdentifier." + |
| 898 | name.toUpperCase() + ".getLeafIndex());\n"; |
| 899 | } |
| 900 | |
| 901 | /*Provides string to return for type.*/ |
| 902 | private static String parseTypeForReturnValue(String type) { |
| 903 | switch (type) { |
| 904 | case BYTE: |
| 905 | case INT: |
| 906 | case SHORT: |
| 907 | case LONG: |
| 908 | case DOUBLE: |
| 909 | return ZERO; |
| 910 | case BOOLEAN_DATA_TYPE: |
| 911 | return FALSE; |
| 912 | default: |
| 913 | return null; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * Returns check not null string. |
| 919 | * |
| 920 | * @param name attribute name |
| 921 | * @return check not null string |
| 922 | */ |
| 923 | static String getCheckNotNull(String name) { |
| 924 | return EIGHT_SPACE_INDENTATION + CHECK_NOT_NULL_STRING + |
| 925 | OPEN_PARENTHESIS + name + COMMA + SPACE + name + |
| 926 | CLOSE_PARENTHESIS + SEMI_COLON + NEW_LINE; |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * Returns definition close string. |
| 931 | * |
| 932 | * @return definition close string |
| 933 | */ |
| 934 | private static String defCloseString() { |
| 935 | return SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE; |
| 936 | } |
| 937 | |
| 938 | /** |
| 939 | * Returns default class definition for java file when extends a |
| 940 | * interface. |
| 941 | * |
| 942 | * @param classType class type |
| 943 | * @param name name of class |
| 944 | * @param modifier modifier for class |
| 945 | * @param extend extends class name |
| 946 | * @return class definition |
| 947 | */ |
| 948 | static String getDefaultDefinitionWithExtends(String classType, |
| 949 | String name, String |
| 950 | modifier, |
| 951 | String extend) { |
| 952 | String mod = EMPTY_STRING; |
| 953 | if (modifier != null) { |
| 954 | mod = modifier + SPACE; |
| 955 | } |
| 956 | return mod + classType + SPACE + name + SPACE + EXTEND + SPACE |
| 957 | + extend + defCloseString(); |
| 958 | } |
| 959 | |
| 960 | /** |
| 961 | * Returns default class definition for java file when implements a |
| 962 | * interface. |
| 963 | * |
| 964 | * @param classType class type |
| 965 | * @param name name of class |
| 966 | * @param modifier modifier for class |
| 967 | * @param impl implements class name |
| 968 | * @return class definition |
| 969 | */ |
| 970 | static String getDefaultDefinitionWithImpl(String classType, |
| 971 | String name, String |
| 972 | modifier, |
| 973 | String impl) { |
| 974 | String mod = EMPTY_STRING; |
| 975 | if (modifier != null) { |
| 976 | mod = modifier + SPACE; |
| 977 | } |
| 978 | return mod + classType + SPACE + name + SPACE + IMPLEMENTS + |
| 979 | SPACE + impl + defCloseString(); |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * Returns default class definition for java file. |
| 984 | * |
| 985 | * @param classType class type |
| 986 | * @param name name of class |
| 987 | * @param modifier modifier for class |
| 988 | * @return class definition |
| 989 | */ |
| 990 | static String getDefaultDefinition(String classType, |
| 991 | String name, String modifier) { |
| 992 | String mod = EMPTY_STRING; |
| 993 | if (modifier != null) { |
| 994 | mod = modifier + SPACE; |
| 995 | } |
| 996 | return mod + classType + SPACE + name + defCloseString(); |
| 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * Returns string for service class. |
| 1001 | * |
| 1002 | * @param name1 name of even listener class |
| 1003 | * @param name2 name of even class |
| 1004 | * @return listener service string for service class |
| 1005 | */ |
| 1006 | static String getEventExtendsString(String name1, String type, |
| 1007 | String name2) { |
| 1008 | StringBuilder builder = new StringBuilder(); |
| 1009 | builder.append(type).append(DIAMOND_OPEN_BRACKET).append(name1) |
| 1010 | .append(COMMA).append(SPACE).append(name2).append |
| 1011 | (DIAMOND_CLOSE_BRACKET); |
| 1012 | return builder.toString(); |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * Returns import string. |
| 1017 | * |
| 1018 | * @param pkg package |
| 1019 | * @param cls class |
| 1020 | * @return import string |
| 1021 | */ |
| 1022 | public static String getImportString(String pkg, String cls) { |
| 1023 | StringBuilder builder = new StringBuilder() |
| 1024 | .append(IMPORT).append(pkg).append(PERIOD).append(cls) |
| 1025 | .append(signatureClose()); |
| 1026 | return builder.toString(); |
| 1027 | } |
| 1028 | |
| 1029 | /** |
| 1030 | * Returns static modifier string. |
| 1031 | * |
| 1032 | * @param modifier modifier |
| 1033 | * @return static modifier string |
| 1034 | */ |
| 1035 | static String getSpecificModifier(String modifier, String keyWord) { |
| 1036 | return new StringBuilder(modifier).append(SPACE).append(keyWord) |
| 1037 | .toString(); |
| 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * Returns builder string for class definition. |
| 1042 | * |
| 1043 | * @param yangName class name |
| 1044 | * @return builder string for class definition |
| 1045 | */ |
| 1046 | static String getBuilderImplStringClassDef(String yangName) { |
| 1047 | return new StringBuilder(yangName).append(PERIOD).append(yangName) |
| 1048 | .append(BUILDER).toString(); |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * Returns default name string. |
| 1053 | * |
| 1054 | * @param yangName class name |
| 1055 | * @return default name string |
| 1056 | */ |
| 1057 | static String getDefaultName(String yangName) { |
| 1058 | return new StringBuilder(DEFAULT_CAPS).append(yangName).toString(); |
| 1059 | } |
| 1060 | |
| 1061 | /** |
| 1062 | * Returns suffixed name string. |
| 1063 | * |
| 1064 | * @param yangName class name |
| 1065 | * @param suffix suffix to append to name |
| 1066 | * @return suffixed name string |
| 1067 | */ |
| 1068 | static String getSuffixedName(String yangName, String suffix) { |
| 1069 | return new StringBuilder(yangName).append(suffix).toString(); |
| 1070 | } |
| 1071 | |
| 1072 | /** |
| 1073 | * Returns error msg string. |
| 1074 | * |
| 1075 | * @param msg message |
| 1076 | * @param name name of node |
| 1077 | * @param line line number |
| 1078 | * @param position char position |
| 1079 | * @param fileName file name |
| 1080 | * @return error message string |
| 1081 | */ |
| 1082 | static String getErrorMsg(String msg, String name, int line, int position, |
| 1083 | String fileName) { |
| 1084 | return new StringBuilder().append(msg).append(name).append(IN) |
| 1085 | .append(line).append(AT).append(position).append(IN) |
| 1086 | .append(fileName).toString(); |
| 1087 | } |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame^] | 1088 | |
| 1089 | /** |
| 1090 | * Returns string builder attribute string; |
| 1091 | * |
| 1092 | * @param init first param to be appended to string builder |
| 1093 | * @param space indentation space |
| 1094 | * @return string builder attribute |
| 1095 | */ |
| 1096 | static String getStringBuilderAttr(String init, String space) { |
| 1097 | StringBuilder builder = new StringBuilder(space); |
| 1098 | builder.append(STRING_BUILDER).append(SPACE).append(STRING_BUILDER_VAR) |
| 1099 | .append(SPACE).append(EQUAL).append(SPACE).append(NEW) |
| 1100 | .append(SPACE).append(STRING_BUILDER).append( |
| 1101 | brackets(OPEN_CLOSE_BRACKET_WITH_VALUE, getQuotedString(init), null)) |
| 1102 | .append(signatureClose()); |
| 1103 | return builder.toString(); |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * Returns quoted string. |
| 1108 | * |
| 1109 | * @param name name to be quoted |
| 1110 | * @return quoted string |
| 1111 | */ |
| 1112 | static String getQuotedString(String name) { |
| 1113 | return QUOTES + name + QUOTES; |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * Returns string builder's append string. |
| 1118 | * |
| 1119 | * @param append data to be append |
| 1120 | * @param space indentation |
| 1121 | * @return string builder's append string |
| 1122 | */ |
| 1123 | static String getStringBuilderAppendString(String append, String space) { |
| 1124 | return space + STRING_BUILDER_VAR + PERIOD + APPEND + OPEN_PARENTHESIS + |
| 1125 | append + CLOSE_PARENTHESIS + signatureClose(); |
| 1126 | } |
| 1127 | |
| 1128 | /** |
| 1129 | * Returns pattern quote string. |
| 1130 | * |
| 1131 | * @param type type for pattern is needed |
| 1132 | * @return pattern quote string |
| 1133 | */ |
| 1134 | static String getPatternQuoteString(String type) { |
| 1135 | return PATTERN + PERIOD + QUOTE_STRING + brackets( |
| 1136 | OPEN_CLOSE_BRACKET_WITH_VALUE, getQuotedString(type), null); |
| 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * Returns bitset attribute. |
| 1141 | * |
| 1142 | * @param indentation indentation |
| 1143 | * @return bitset attribute |
| 1144 | */ |
| 1145 | static String getBitSetAttr(String indentation) { |
| 1146 | StringBuilder builder = new StringBuilder(indentation); |
| 1147 | return builder.append(BITSET).append(SPACE).append(TMP_VAL) |
| 1148 | .append(SPACE).append(EQUAL).append(SPACE).append(NEW) |
| 1149 | .append(SPACE).append(BITSET).append(OPEN_CLOSE_BRACKET_STRING) |
| 1150 | .append(signatureClose()).toString(); |
| 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * Returns for loop string |
| 1155 | * |
| 1156 | * @param space indentation |
| 1157 | * @param type data type |
| 1158 | * @param var variable |
| 1159 | * @param data data variable/collection |
| 1160 | * @return for loop string |
| 1161 | */ |
| 1162 | static String getForLoopString(String space, String type, String var, |
| 1163 | String data) { |
| 1164 | return space + FOR + SPACE + OPEN_PARENTHESIS + type + SPACE + var + |
| 1165 | SPACE + COLON + SPACE + data + CLOSE_PARENTHESIS + |
| 1166 | methodSignatureClose(CLASS_TYPE); |
| 1167 | } |
| 1168 | |
| 1169 | /** |
| 1170 | * Returns set value parameter's get string for union to string method. |
| 1171 | * |
| 1172 | * @param count count of type |
| 1173 | * @return get string |
| 1174 | */ |
| 1175 | static String getSetValueParaCondition(int count) { |
| 1176 | return SET_VALUE_PARA + PERIOD + GET + brackets(OPEN_CLOSE_BRACKET_WITH_VALUE, |
| 1177 | count + EMPTY_STRING, null); |
| 1178 | } |
| 1179 | |
| 1180 | /** |
| 1181 | * Returns more object attr for union to string method. |
| 1182 | * |
| 1183 | * @return more object attr for union to string method |
| 1184 | */ |
| 1185 | static String getMoreObjectAttr() { |
| 1186 | StringBuilder attr = new StringBuilder(EIGHT_SPACE_INDENTATION); |
| 1187 | String[] array = {NEW_LINE}; |
| 1188 | attr.append(MORE_OBJ_ATTR).append(GOOGLE_MORE_OBJECT_METHOD_STRING) |
| 1189 | .append(NEW_LINE).append(FOUR_SPACE_INDENTATION).append(trimAtLast( |
| 1190 | getOmitNullValueString(), array)).append(signatureClose()); |
| 1191 | return attr.toString(); |
| 1192 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1193 | } |