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