blob: dfe2826dc09200664a3df285b4d0a37cf26e3a83 [file] [log] [blame]
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301/*
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
17package org.onosproject.yangutils.translator.tojava.utils;
18
Bharat saraswal0663aff2016-10-18 23:16:14 +053019import org.onosproject.yangutils.datamodel.YangAugment;
20import org.onosproject.yangutils.datamodel.YangChoice;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053021import org.onosproject.yangutils.datamodel.YangCompilerAnnotation;
Bharat saraswal0663aff2016-10-18 23:16:14 +053022import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053023import org.onosproject.yangutils.datamodel.YangType;
24import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
25import org.onosproject.yangutils.translator.exception.TranslatorException;
Bharat saraswal0663aff2016-10-18 23:16:14 +053026import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator;
27import org.onosproject.yangutils.utils.io.YangPluginConfig;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053028
Bharat saraswal0663aff2016-10-18 23:16:14 +053029import java.util.ArrayList;
30import java.util.List;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053031import java.util.Map;
32
Bharat saraswal9fab16b2016-09-23 23:27:24 +053033import static org.onosproject.yangutils.translator.tojava.utils.BracketType.OPEN_CLOSE_BRACKET;
34import static org.onosproject.yangutils.translator.tojava.utils.BracketType.OPEN_CLOSE_BRACKET_WITH_VALUE;
35import static org.onosproject.yangutils.translator.tojava.utils.BracketType.OPEN_CLOSE_BRACKET_WITH_VALUE_AND_RETURN_TYPE;
36import static org.onosproject.yangutils.translator.tojava.utils.MethodClassTypes.CLASS_TYPE;
37import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getIfConditionForAddToListMethod;
Bharat saraswal0663aff2016-10-18 23:16:14 +053038import static org.onosproject.yangutils.translator.tojava.utils.SubtreeFilteringMethodsGenerator.getQualifiedInfo;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053039import static org.onosproject.yangutils.utils.UtilConstants.ADD_STRING;
40import static org.onosproject.yangutils.utils.UtilConstants.AND;
Bharat saraswal54e4bab2016-10-05 23:32:14 +053041import static org.onosproject.yangutils.utils.UtilConstants.APPEND;
Bharat saraswal0663aff2016-10-18 23:16:14 +053042import static org.onosproject.yangutils.utils.UtilConstants.APP_INSTANCE;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053043import static org.onosproject.yangutils.utils.UtilConstants.AT;
44import static org.onosproject.yangutils.utils.UtilConstants.BIG_DECIMAL;
45import static org.onosproject.yangutils.utils.UtilConstants.BIG_INTEGER;
Bharat saraswal54e4bab2016-10-05 23:32:14 +053046import static org.onosproject.yangutils.utils.UtilConstants.BITSET;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053047import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_DATA_TYPE;
48import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_WRAPPER;
49import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
50import static org.onosproject.yangutils.utils.UtilConstants.BUILDER_LOWER_CASE;
51import static org.onosproject.yangutils.utils.UtilConstants.BYTE;
52import static org.onosproject.yangutils.utils.UtilConstants.BYTE_WRAPPER;
53import static org.onosproject.yangutils.utils.UtilConstants.CATCH;
54import static org.onosproject.yangutils.utils.UtilConstants.CHECK_NOT_NULL_STRING;
55import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
56import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
57import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS;
58import static org.onosproject.yangutils.utils.UtilConstants.COLON;
59import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
60import static org.onosproject.yangutils.utils.UtilConstants.COMPARE_TO;
61import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_CAPS;
62import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
63import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
64import static org.onosproject.yangutils.utils.UtilConstants.DOUBLE;
65import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
66import static org.onosproject.yangutils.utils.UtilConstants.ELSE;
67import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
68import static org.onosproject.yangutils.utils.UtilConstants.EQUAL;
Bharat saraswal0663aff2016-10-18 23:16:14 +053069import static org.onosproject.yangutils.utils.UtilConstants.EQUALS_STRING;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053070import static org.onosproject.yangutils.utils.UtilConstants.EXCEPTION;
71import static org.onosproject.yangutils.utils.UtilConstants.EXCEPTION_VAR;
72import static org.onosproject.yangutils.utils.UtilConstants.EXTEND;
73import static org.onosproject.yangutils.utils.UtilConstants.FALSE;
74import static org.onosproject.yangutils.utils.UtilConstants.FOR;
75import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
76import static org.onosproject.yangutils.utils.UtilConstants.FROM_STRING_METHOD_NAME;
77import static org.onosproject.yangutils.utils.UtilConstants.GET;
Bharat saraswal94844d62016-10-13 13:28:03 +053078import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_METHOD_STATIC_STRING;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053079import static org.onosproject.yangutils.utils.UtilConstants.IF;
80import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS;
81import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
82import static org.onosproject.yangutils.utils.UtilConstants.IN;
83import static org.onosproject.yangutils.utils.UtilConstants.INT;
84import static org.onosproject.yangutils.utils.UtilConstants.INTEGER_WRAPPER;
85import static org.onosproject.yangutils.utils.UtilConstants.INT_MAX_RANGE;
86import static org.onosproject.yangutils.utils.UtilConstants.INT_MIN_RANGE;
Bharat saraswal0663aff2016-10-18 23:16:14 +053087import static org.onosproject.yangutils.utils.UtilConstants.LEAF_IDENTIFIER;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053088import static org.onosproject.yangutils.utils.UtilConstants.LIST;
89import static org.onosproject.yangutils.utils.UtilConstants.LONG;
90import static org.onosproject.yangutils.utils.UtilConstants.LONG_MAX_RANGE;
91import static org.onosproject.yangutils.utils.UtilConstants.LONG_MIN_RANGE;
92import static org.onosproject.yangutils.utils.UtilConstants.LONG_WRAPPER;
Bharat saraswal54e4bab2016-10-05 23:32:14 +053093import static org.onosproject.yangutils.utils.UtilConstants.MORE_OBJ_ATTR;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053094import static org.onosproject.yangutils.utils.UtilConstants.NEW;
95import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
96import static org.onosproject.yangutils.utils.UtilConstants.OBJECT;
97import static org.onosproject.yangutils.utils.UtilConstants.OBJECT_STRING;
98import static org.onosproject.yangutils.utils.UtilConstants.OF;
99import static org.onosproject.yangutils.utils.UtilConstants.OMIT_NULL_VALUE_STRING;
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530100import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CLOSE_BRACKET_STRING;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530101import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
102import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS;
103import static org.onosproject.yangutils.utils.UtilConstants.OVERRIDE;
104import static org.onosproject.yangutils.utils.UtilConstants.PARSE_BOOLEAN;
105import static org.onosproject.yangutils.utils.UtilConstants.PARSE_BYTE;
106import static org.onosproject.yangutils.utils.UtilConstants.PARSE_INT;
107import static org.onosproject.yangutils.utils.UtilConstants.PARSE_LONG;
108import static org.onosproject.yangutils.utils.UtilConstants.PARSE_SHORT;
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530109import static org.onosproject.yangutils.utils.UtilConstants.PATTERN;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530110import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
111import static org.onosproject.yangutils.utils.UtilConstants.PUT;
112import static org.onosproject.yangutils.utils.UtilConstants.QUEUE;
113import static org.onosproject.yangutils.utils.UtilConstants.QUOTES;
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530114import static org.onosproject.yangutils.utils.UtilConstants.QUOTE_STRING;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530115import static org.onosproject.yangutils.utils.UtilConstants.RETURN;
116import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLON;
117import static org.onosproject.yangutils.utils.UtilConstants.SET;
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530118import static org.onosproject.yangutils.utils.UtilConstants.SET_VALUE_PARA;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530119import static org.onosproject.yangutils.utils.UtilConstants.SHORT;
120import static org.onosproject.yangutils.utils.UtilConstants.SHORT_MAX_RANGE;
121import static org.onosproject.yangutils.utils.UtilConstants.SHORT_MIN_RANGE;
122import static org.onosproject.yangutils.utils.UtilConstants.SHORT_WRAPPER;
123import static org.onosproject.yangutils.utils.UtilConstants.SIXTEEN_SPACE_INDENTATION;
124import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530125import static org.onosproject.yangutils.utils.UtilConstants.STRING_BUILDER;
126import static org.onosproject.yangutils.utils.UtilConstants.STRING_BUILDER_VAR;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530127import static org.onosproject.yangutils.utils.UtilConstants.THIS;
128import static org.onosproject.yangutils.utils.UtilConstants.TMP_VAL;
Bharat saraswal94844d62016-10-13 13:28:03 +0530129import static org.onosproject.yangutils.utils.UtilConstants.TO_STRING_METHOD;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530130import static org.onosproject.yangutils.utils.UtilConstants.TRY;
131import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
132import static org.onosproject.yangutils.utils.UtilConstants.TWENTY_SPACE_INDENTATION;
133import static org.onosproject.yangutils.utils.UtilConstants.UINT8_MAX_RANGE;
134import static org.onosproject.yangutils.utils.UtilConstants.UINT8_MIN_RANGE;
135import static org.onosproject.yangutils.utils.UtilConstants.UINT_MAX_RANGE;
136import static org.onosproject.yangutils.utils.UtilConstants.UINT_MIN_RANGE;
137import static org.onosproject.yangutils.utils.UtilConstants.ULONG_MAX_RANGE;
138import static org.onosproject.yangutils.utils.UtilConstants.ULONG_MIN_RANGE;
139import static org.onosproject.yangutils.utils.UtilConstants.VALIDATE_RANGE;
140import static org.onosproject.yangutils.utils.UtilConstants.VALUE;
Bharat saraswal94844d62016-10-13 13:28:03 +0530141import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO_MAP;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530142import static org.onosproject.yangutils.utils.UtilConstants.YANG_UTILS_TODO;
143import static org.onosproject.yangutils.utils.UtilConstants.ZERO;
144import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
145
146/**
147 * Represents string generator for translator.
148 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530149public 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 saraswal0663aff2016-10-18 23:16:14 +0530215 NEW + SPACE + returnType + getOpenCloseParaWithValue(value) +
216 signatureClose();
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530217 }
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 */
Bharat saraswal94844d62016-10-13 13:28:03 +0530246 public static String methodClose(IndentationType type) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530247 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:
Bharat saraswal0663aff2016-10-18 23:16:14 +0530261 return TWENTY_SPACE_INDENTATION + EIGHT_SPACE_INDENTATION +
262 CLOSE_CURLY_BRACKET + NEW_LINE;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530263 case TWENTY_FOUR_SPACE:
Bharat saraswal0663aff2016-10-18 23:16:14 +0530264 return TWENTY_SPACE_INDENTATION + FOUR_SPACE_INDENTATION +
265 CLOSE_CURLY_BRACKET + NEW_LINE;
266 case FOUR_SPACE:
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530267 return FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET +
268 NEW_LINE;
269 default:
Bharat saraswal0663aff2016-10-18 23:16:14 +0530270 return CLOSE_CURLY_BRACKET + NEW_LINE;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530271 }
272 }
273
274 /**
275 * Returns body of the method.
276 *
277 * @param type type of method body
278 * @param paraName parameter name
279 * @param methodName method name
280 * @param space space to be given before body
281 * @param prefix prefix for internal method
282 * @param paramType parameter type
283 * @param isBuilderSetter is for builder setter
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530284 * @param setterVal value to set in setter
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530285 * @return body of the method
286 */
287 static String methodBody(MethodBodyTypes type, String paraName,
288 String methodName,
289 String space, String prefix,
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530290 String paramType, boolean isBuilderSetter, String setterVal) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530291 StringBuilder builder = new StringBuilder();
292 String body;
Bharat saraswal94844d62016-10-13 13:28:03 +0530293 String cond;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530294 switch (type) {
295 case GETTER:
296 return getReturnString(paraName, space);
297 case SETTER:
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530298 if (setterVal == null) {
299 setterVal = paraName;
300 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530301 body = space + THIS + PERIOD + paraName + SPACE + EQUAL + SPACE +
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530302 setterVal + signatureClose();
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530303 builder.append(body);
304 if (isBuilderSetter) {
305 body = getReturnString(THIS + signatureClose(), space);
306 builder.append(body);
307 }
308 return builder.toString();
309 case BUILD:
310 return getReturnString(
Bharat saraswal0663aff2016-10-18 23:16:14 +0530311 NEW + SPACE + paraName + getOpenCloseParaWithValue(
312 THIS) + signatureClose(), space);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530313 case CONSTRUCTOR:
314 return space + THIS + PERIOD + paraName + SPACE +
315 EQUAL + SPACE + BUILDER_LOWER_CASE + OBJECT + PERIOD +
316 prefix + methodName +
317 brackets(OPEN_CLOSE_BRACKET, null, null) +
318 signatureClose();
319 case DEFAULT_CONSTRUCTOR:
320 return EMPTY_STRING;
321 case ADD_TO_LIST:
322 return space + getIfConditionForAddToListMethod(paraName) +
323 space + paraName +
324 brackets(OPEN_CLOSE_BRACKET, null, null) + PERIOD +
Bharat saraswal0663aff2016-10-18 23:16:14 +0530325 ADD_STRING + getOpenCloseParaWithValue(VALUE) +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530326 signatureClose() + getReturnString(
327 THIS + signatureClose(), space);
328 case AUGMENTED_MAP_ADD:
Bharat saraswal94844d62016-10-13 13:28:03 +0530329 return space + YANG_AUGMENTED_INFO_MAP +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530330 PERIOD + PUT + OPEN_PARENTHESIS + CLASS +
331 OBJECT_STRING + COMMA + SPACE + VALUE +
332 CLOSE_PARENTHESIS + signatureClose();
333 case AUGMENTED_MAP_GET_VALUE:
334 return getReturnString(
Bharat saraswal0663aff2016-10-18 23:16:14 +0530335 YANG_AUGMENTED_INFO_MAP + PERIOD + GET + getOpenCloseParaWithValue(
336 CLASS + OBJECT_STRING) + signatureClose(), space);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530337 case AUGMENTED_MAP_GETTER:
Bharat saraswal94844d62016-10-13 13:28:03 +0530338 return getReturnString(YANG_AUGMENTED_INFO_MAP +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530339 signatureClose(), space);
340 case MANAGER_METHODS:
341 body = space + YANG_UTILS_TODO + NEW_LINE;
342 builder.append(body);
343 if (paramType != null) {
344 body = getReturnString(parseTypeForReturnValue(paramType),
345 space);
346 builder.append(body)
347 .append(signatureClose());
348 }
349 return builder.toString();
350 case OF_METHOD:
351 return getReturnString(
Bharat saraswal0663aff2016-10-18 23:16:14 +0530352 NEW + SPACE + paraName + getOpenCloseParaWithValue(
353 VALUE) + signatureClose(), space);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530354 case TO_STRING:
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530355 return getToStringMethodsAddString(space, paraName) + paraName +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530356 CLOSE_PARENTHESIS;
357 case EQUALS_METHOD:
358 default:
359 return null;
360 }
361 }
362
363 /**
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530364 * Returns to string method's add string.
365 *
366 * @param space indentation
367 * @param paraName parameter name
368 * @return to string method's add string
369 */
370 static String getToStringMethodsAddString(String space, String paraName) {
371 return space + PERIOD + ADD_STRING + OPEN_PARENTHESIS +
372 getQuotedString(paraName) + COMMA + SPACE;
373 }
374
375 /**
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530376 * Returns end of line.
377 *
378 * @return end of line
379 */
380 static String signatureClose() {
381 return SEMI_COLON + NEW_LINE;
382 }
383
Bharat saraswal0663aff2016-10-18 23:16:14 +0530384
385 /**
386 * Returns value assignment.
387 *
388 * @return value assignment
389 */
390 static String valueAssign(String param, String value, String indentation) {
391 return indentation + param + SPACE + EQUAL + SPACE + value +
392 signatureClose();
393 }
394
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530395 /**
396 * Returns method signature close for method class type.
397 *
398 * @param type method class type
399 * @return method signature close for method class type
400 */
401 static String methodSignatureClose(MethodClassTypes type) {
402 switch (type) {
403 case INTERFACE_TYPE:
404 return signatureClose();
405 case CLASS_TYPE:
406 return SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
407 default:
408 return null;
409 }
410 }
411
412 /**
413 * Returns method param.
414 *
415 * @param type type of param
416 * @param name name of param
417 * @return method param
418 */
419 private static String methodParam(String type, String name) {
420 return type + SPACE + name;
421 }
422
423 /**
424 * Returns comma followed by a space.
425 *
426 * @return comma followed by a space
427 */
428 private static String commaWithSpace() {
429 return COMMA + SPACE;
430 }
431
432 /**
433 * Returns bracket string for the given type.
434 *
435 * @param type bracket type
436 * @param value value to be added in brackets
437 * @param returnType returns type to be added for value
438 * @return bracket for the given type.
439 */
440 static String brackets(BracketType type, String value,
441 String returnType) {
442 String ret = EMPTY_STRING;
443 switch (type) {
444 case OPEN_CLOSE_BRACKET:
445 return OPEN_PARENTHESIS + CLOSE_PARENTHESIS;
446 case OPEN_CLOSE_BRACKET_WITH_VALUE:
447 return OPEN_PARENTHESIS + value + CLOSE_PARENTHESIS;
448 case OPEN_CLOSE_BRACKET_WITH_VALUE_AND_RETURN_TYPE:
449 if (returnType != null) {
450 ret = returnType + SPACE;
451 }
452 return OPEN_PARENTHESIS + ret + value +
453 CLOSE_PARENTHESIS;
454 case OPEN_BRACKET_WITH_VALUE:
455 ret = EMPTY_STRING;
456 if (returnType != null) {
457 ret = returnType + SPACE;
458 }
459 return OPEN_PARENTHESIS + ret + value +
460 COMMA;
461 case CLOSE_BRACKET_WITH_VALUE:
462 ret = EMPTY_STRING;
463 if (returnType != null) {
464 ret = returnType + SPACE;
465 }
466 return SPACE + ret + value +
467 CLOSE_PARENTHESIS;
468 case OPEN_CLOSE_DIAMOND:
469 return DIAMOND_OPEN_BRACKET + DIAMOND_CLOSE_BRACKET;
470 case OPEN_CLOSE_DIAMOND_WITH_VALUE:
471 return DIAMOND_OPEN_BRACKET + value + DIAMOND_CLOSE_BRACKET;
472 default:
473 return null;
474 }
475 }
476
477 /**
478 * Returns method signature for multi attribute methods.
479 *
480 * @param methodName method name
481 * @param prefix prefix for method
482 * @param modifier modifier for method
483 * @param methodReturnType method's return type
484 * @param params parameters
485 * @param type type of method
486 * @return method signature for multi attribute methods
487 */
488 static String multiAttrMethodSignature(String methodName, String
489 prefix, String modifier, String methodReturnType,
490 Map<String, String> params,
491 MethodClassTypes type) {
492 StringBuilder methodBuilder = new StringBuilder(FOUR_SPACE_INDENTATION);
493 String method = EMPTY_STRING;
494 if (modifier != null) {
495 method = modifier + SPACE;
496 }
497 methodBuilder.append(method);
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530498 if (prefix == null) {
499 prefix = EMPTY_STRING;
500 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530501 if (methodReturnType != null) {
502 method = methodReturnType + SPACE + prefix + methodName;
503 } else {
504 method = prefix + methodName;
505 }
506 //Append method beginning.
507 methodBuilder.append(method)
508 .append(OPEN_PARENTHESIS);
509 for (Map.Entry<String, String> param : params.entrySet()) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530510 methodBuilder.append(methodParam(param.getValue(), param.getKey()));
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530511 methodBuilder.append(commaWithSpace());
512 }
513 String para = methodBuilder.toString();
514 String[] array = {SPACE, COMMA};
515 para = trimAtLast(para, array);
516 methodBuilder = new StringBuilder(para)
517 .append(CLOSE_PARENTHESIS)
518 .append(methodSignatureClose(type));
519
520 return methodBuilder.toString();
521 }
522
523 /**
524 * Returns method signature for interface and implementation classes.
525 *
526 * @param methodName name of the method
527 * @param prefix prefix which needs to be added for method
528 * @param modifier modifier which needs to be added for method
529 * @param paraVal value which needs to be added as parameter
530 * @param methodReturnType returns type to be added for method
531 * @param paraReturnType return type to be added for parameter
532 * @param type method class type
533 * @return method signature for interface and implementation classes
534 */
535 static String methodSignature(
536 String methodName, String prefix, String modifier, String paraVal,
537 String methodReturnType, String paraReturnType,
538 MethodClassTypes type) {
539 StringBuilder methodBuilder = new StringBuilder(FOUR_SPACE_INDENTATION);
540 String method = EMPTY_STRING;
541 if (modifier != null) {
542 method = modifier + SPACE;
543 }
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530544 if (prefix == null) {
545 prefix = EMPTY_STRING;
546 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530547 methodBuilder.append(method);
548 if (methodReturnType != null) {
549 method = methodReturnType + SPACE + prefix + methodName;
550 } else {
551 method = prefix + methodName;
552 }
553 //Append method beginning.
554 methodBuilder.append(method);
555
556 if (paraVal != null) {
557 methodBuilder.append(brackets(
558 OPEN_CLOSE_BRACKET_WITH_VALUE_AND_RETURN_TYPE,
559 paraVal, paraReturnType));
560 } else {
561 methodBuilder.append(brackets(OPEN_CLOSE_BRACKET, null,
562 null));
563 }
564
565 methodBuilder.append(methodSignatureClose(type));
566
567 return methodBuilder.toString();
568 }
569
570 /**
571 * Returns list attribute.
572 *
573 * @param attrType attribute type
574 * @param compilerAnnotation compiler annotations
575 * @return list attribute
576 */
577
578 static String getListAttribute(String attrType,
579 YangCompilerAnnotation compilerAnnotation) {
580 String listAttr;
581 if (compilerAnnotation != null &&
582 compilerAnnotation.getYangAppDataStructure() != null) {
583 switch (compilerAnnotation.getYangAppDataStructure()
584 .getDataStructure()) {
585 case QUEUE: {
586 listAttr = QUEUE + DIAMOND_OPEN_BRACKET + attrType +
587 DIAMOND_CLOSE_BRACKET;
588 break;
589 }
590 case SET: {
591 listAttr = SET + DIAMOND_OPEN_BRACKET + attrType +
592 DIAMOND_CLOSE_BRACKET;
593 break;
594 }
595 case LIST: {
596 listAttr = getListString() + attrType +
597 DIAMOND_CLOSE_BRACKET;
598 break;
599 }
600 default: {
601 listAttr = getListString() + attrType +
602 DIAMOND_CLOSE_BRACKET;
603 }
604 }
605 } else {
606 listAttr = getListString() + attrType + DIAMOND_CLOSE_BRACKET;
607 }
608 return listAttr;
609 }
610
611 /**
612 * Returns getters for value and select leaf.
613 *
614 * @return getters for value and select leaf
615 */
Bharat saraswal94844d62016-10-13 13:28:03 +0530616 static String getIsValueLeafSet() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530617 return "\n" +
618 " @Override\n" +
619 " public boolean isLeafValueSet(LeafIdentifier leaf) {\n" +
Bharat saraswal94844d62016-10-13 13:28:03 +0530620 " return valueLeafFlags.get(leaf.getLeafIndex());\n" +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530621 " }\n" +
Bharat saraswal94844d62016-10-13 13:28:03 +0530622 "\n";
623 }
624
625 /**
626 * Returns is select leaf set.
627 *
628 * @return is select leaf set
629 */
630 static String getIsSelectLeafSet() {
631 return " @Override\n" +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530632 " public boolean isSelectLeaf(LeafIdentifier leaf) {\n" +
Bharat saraswal94844d62016-10-13 13:28:03 +0530633 " return selectLeafFlags.get(leaf.getLeafIndex());\n" +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530634 " }\n";
635 }
636
637 /**
638 * Returns getter methods for operation attributes.
639 *
640 * @return getter methods for operation attributes
641 */
Bharat saraswal94844d62016-10-13 13:28:03 +0530642 static String getValueLeafGetters() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530643 return "\n" +
644 " /**\n" +
645 " * Returns the valueLeafFlags.\n" +
646 " *\n" +
647 " * @return value of valueLeafFlags\n" +
648 " */\n" +
649 " public BitSet getValueLeafFlags() {\n" +
650 " return valueLeafFlags;\n" +
651 " }\n" +
Bharat saraswal94844d62016-10-13 13:28:03 +0530652 "\n";
653 }
654
655 /**
656 * Returns getter methods for operation attributes.
657 *
658 * @return getter methods for operation attributes
659 */
660 static String getSelectLeafGetters() {
661 return " /**\n" +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530662 " * Returns the selectLeafFlags.\n" +
663 " *\n" +
664 " * @return value of selectLeafFlags\n" +
665 " */\n" +
666 " public BitSet getSelectLeafFlags() {\n" +
667 " return selectLeafFlags;\n" +
668 " }\n" +
669 "\n";
670 }
671
672 /**
673 * Returns interface leaf identifier enum method.
674 *
675 * @return interface leaf identifier enum method
676 */
677 static String getInterfaceLeafIdEnumMethods() {
678 return "\n\n private int leafIndex;\n" +
679 "\n" +
680 " public int getLeafIndex() {\n" +
681 " return leafIndex;\n" +
682 " }\n" +
683 "\n" +
684 " LeafIdentifier(int value) {\n" +
685 " this.leafIndex = value;\n" +
686 " }\n" +
687 " }\n";
688 }
689
690 /**
691 * Returns if condition string for typedef constructor.
692 *
693 * @param type type of conflict
694 * @param addFirst true int/long need to be added first
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530695 * @param val value to set
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530696 * @return if condition string for typedef constructor
697 */
698 static String ifConditionForIntInTypeDefConstructor(ValidatorTypeForUnionTypes type,
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530699 boolean addFirst, String val) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530700 String condition =
701 EIGHT_SPACE_INDENTATION + IF + SPACE + OPEN_PARENTHESIS +
702 VALIDATE_RANGE + OPEN_PARENTHESIS;
703
704 switch (type) {
705 case INT_TYPE_CONFLICT:
706 if (addFirst) {
707 condition = condition + INT_MIN_RANGE + COMMA + SPACE +
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530708 INT_MAX_RANGE + COMMA + SPACE + val;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530709 } else {
710 condition = condition + UINT_MIN_RANGE + COMMA + SPACE +
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530711 UINT_MAX_RANGE + COMMA + SPACE + val;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530712 }
713 break;
714 case LONG_TYPE_CONFLICT:
715 if (addFirst) {
716 condition = condition + LONG_MIN_RANGE + COMMA + SPACE +
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530717 LONG_MAX_RANGE + COMMA + SPACE + val;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530718 } else {
719 condition = condition + ULONG_MIN_RANGE + COMMA + SPACE +
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530720 ULONG_MAX_RANGE + COMMA + SPACE + val;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530721 }
722 break;
723 case SHORT_TYPE_CONFLICT:
724 if (addFirst) {
725 condition = condition + SHORT_MIN_RANGE + COMMA + SPACE +
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530726 SHORT_MAX_RANGE + COMMA + SPACE + val;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530727 } else {
728 condition = condition + UINT8_MIN_RANGE + COMMA + SPACE +
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530729 UINT8_MAX_RANGE + COMMA + SPACE + val;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530730 }
731 break;
732 default:
733 return null;
734 }
735
736 return condition + CLOSE_PARENTHESIS + CLOSE_PARENTHESIS + SPACE +
737 OPEN_CURLY_BRACKET + NEW_LINE;
738 }
739
740 /**
741 * Returns from string method parsed string.
742 *
743 * @param targetDataType target data type
744 * @param yangType YANG type
745 * @return parsed string
746 */
747 static String getParseFromStringMethod(String targetDataType,
748 YangType<?> yangType) {
749 YangDataTypes type = yangType.getDataType();
750
751 switch (type) {
752 case INT8:
753 return BYTE_WRAPPER + PERIOD + PARSE_BYTE;
754 case INT16:
755 return SHORT_WRAPPER + PERIOD + PARSE_SHORT;
756 case INT32:
757 return INTEGER_WRAPPER + PERIOD + PARSE_INT;
758 case INT64:
759 return LONG_WRAPPER + PERIOD + PARSE_LONG;
760 case UINT8:
761 return SHORT_WRAPPER + PERIOD + PARSE_SHORT;
762 case UINT16:
763 return INTEGER_WRAPPER + PERIOD + PARSE_INT;
764 case UINT32:
765 return LONG_WRAPPER + PERIOD + PARSE_LONG;
766 case UINT64:
767 return NEW + SPACE + BIG_INTEGER;
768 case DECIMAL64:
769 return NEW + SPACE + BIG_DECIMAL;
770 case STRING:
771 case IDENTITYREF:
772 return EMPTY_STRING;
773 case EMPTY:
774 case BOOLEAN:
775 return BOOLEAN_WRAPPER + PERIOD + PARSE_BOOLEAN;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530776 case ENUMERATION:
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530777 return targetDataType + PERIOD + OF;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530778 case DERIVED:
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530779 case UNION:
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530780 return targetDataType + PERIOD + FROM_STRING_METHOD_NAME;
781 default:
782 throw new TranslatorException("given data type is not " +
783 "supported. " +
784 yangType.getDataTypeName() +
785 " in " +
786 yangType.getLineNumber() +
787 " at " +
788 yangType.getCharPosition() +
789 " in " +
790 yangType.getFileName());
791 }
792 }
793
794 /**
795 * Returns sub string with catch statement for union's from string method.
796 *
797 * @return sub string with catch statement for union's from string method
798 */
799 static String getCatchSubString() {
800 return CLOSE_CURLY_BRACKET + SPACE + CATCH + SPACE +
801 brackets(OPEN_CLOSE_BRACKET_WITH_VALUE_AND_RETURN_TYPE, EXCEPTION_VAR,
802 EXCEPTION) + SPACE + OPEN_CURLY_BRACKET;
803 }
804
805 /**
806 * Returns sub string with return statement for union's from string method.
807 *
808 * @return sub string with return statement for union's from string method
809 */
810 static String getReturnOfSubString() {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530811 return getReturnString(OF, TWELVE_SPACE_INDENTATION) +
Bharat saraswal0663aff2016-10-18 23:16:14 +0530812 getOpenCloseParaWithValue(TMP_VAL) + signatureClose();
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530813 }
814
815 /**
816 * Returns sub string with try statement for union's from string method.
817 *
818 * @return sub string with try statement for union's from string method
819 */
820 static String getTrySubString() {
821 return TRY + SPACE + OPEN_CURLY_BRACKET;
822 }
823
824 /*
825 * Returns omit null value string.
826 *
827 * @return omit null value string
828 */
829 static String getOmitNullValueString() {
830 return TWELVE_SPACE_INDENTATION + PERIOD + OMIT_NULL_VALUE_STRING +
831 NEW_LINE;
832 }
833
834 /**
835 * Returns collection's iterator method.
836 *
837 * @param indentation indentation
838 * @param loopVar loop variable
839 * @param collection collection
840 * @return collection's iterator method
841 */
842 static String getCollectionIteratorForLoopBegin(String indentation,
843 String loopVar,
844 String collection) {
845 return indentation + FOR + SPACE + OPEN_PARENTHESIS + loopVar + SPACE +
846 COLON + SPACE + collection + CLOSE_PARENTHESIS + SPACE +
847 OPEN_CURLY_BRACKET + NEW_LINE;
848 }
849
850 /**
851 * Returns if else condition's signature.
852 *
853 * @param indentation indentation
854 * @param condition conditions
855 * @return if else condition's signature
856 */
857 static String getElseIfConditionBegin(String indentation,
858 String condition) {
859 return indentation + CLOSE_CURLY_BRACKET + SPACE + ELSE + SPACE +
860 getIfConditionBegin(EMPTY_STRING, condition);
861 }
862
863 /**
864 * Returns if condition's signature.
865 *
866 * @param indentation indentation
867 * @param condition conditions
868 * @return if condition's signature
869 */
870 static String getIfConditionBegin(String indentation, String condition) {
Bharat saraswal0663aff2016-10-18 23:16:14 +0530871 return indentation + IF + SPACE + getOpenCloseParaWithValue(condition) +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530872 methodSignatureClose(CLASS_TYPE);
873 }
874
875 /**
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530876 * Returns list string.
877 *
878 * @return list string
879 */
880 private static String getListString() {
881 return LIST + DIAMOND_OPEN_BRACKET;
882 }
883
884 /**
885 * Returns override string.
886 *
887 * @return override string
888 */
889 public static String getOverRideString() {
890 return NEW_LINE + FOUR_SPACE_INDENTATION + OVERRIDE + NEW_LINE;
891 }
892
893 /**
894 * Returns value leaf flag setter.
895 *
Bharat saraswal0663aff2016-10-18 23:16:14 +0530896 * @param name name of leaf
897 * @param flag flag to set values
898 * @param indentation indentation
899 * @param prefix prefix of method
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530900 * @return value leaf flag setter
901 */
Bharat saraswal0663aff2016-10-18 23:16:14 +0530902 static String getLeafFlagSetString(String name, String flag, String indentation, String prefix) {
903 return indentation + flag + PERIOD + prefix +
904 getOpenCloseParaWithValue(LEAF_IDENTIFIER + PERIOD + name
905 .toUpperCase() + ".getLeafIndex()");
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530906 }
907
908 /*Provides string to return for type.*/
909 private static String parseTypeForReturnValue(String type) {
910 switch (type) {
911 case BYTE:
912 case INT:
913 case SHORT:
914 case LONG:
915 case DOUBLE:
916 return ZERO;
917 case BOOLEAN_DATA_TYPE:
918 return FALSE;
919 default:
920 return null;
921 }
922 }
923
924 /**
925 * Returns check not null string.
926 *
927 * @param name attribute name
928 * @return check not null string
929 */
930 static String getCheckNotNull(String name) {
931 return EIGHT_SPACE_INDENTATION + CHECK_NOT_NULL_STRING +
932 OPEN_PARENTHESIS + name + COMMA + SPACE + name +
933 CLOSE_PARENTHESIS + SEMI_COLON + NEW_LINE;
934 }
935
936 /**
937 * Returns definition close string.
938 *
939 * @return definition close string
940 */
941 private static String defCloseString() {
Bharat saraswal94844d62016-10-13 13:28:03 +0530942 return SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530943 }
944
945 /**
946 * Returns default class definition for java file when extends a
947 * interface.
948 *
949 * @param classType class type
950 * @param name name of class
951 * @param modifier modifier for class
952 * @param extend extends class name
953 * @return class definition
954 */
955 static String getDefaultDefinitionWithExtends(String classType,
956 String name, String
957 modifier,
958 String extend) {
959 String mod = EMPTY_STRING;
960 if (modifier != null) {
961 mod = modifier + SPACE;
962 }
963 return mod + classType + SPACE + name + SPACE + EXTEND + SPACE
964 + extend + defCloseString();
965 }
966
967 /**
968 * Returns default class definition for java file when implements a
969 * interface.
970 *
971 * @param classType class type
972 * @param name name of class
973 * @param modifier modifier for class
974 * @param impl implements class name
975 * @return class definition
976 */
977 static String getDefaultDefinitionWithImpl(String classType,
978 String name, String
979 modifier,
980 String impl) {
981 String mod = EMPTY_STRING;
982 if (modifier != null) {
983 mod = modifier + SPACE;
984 }
985 return mod + classType + SPACE + name + SPACE + IMPLEMENTS +
986 SPACE + impl + defCloseString();
987 }
988
989 /**
990 * Returns default class definition for java file.
991 *
992 * @param classType class type
993 * @param name name of class
994 * @param modifier modifier for class
995 * @return class definition
996 */
997 static String getDefaultDefinition(String classType,
998 String name, String modifier) {
999 String mod = EMPTY_STRING;
1000 if (modifier != null) {
1001 mod = modifier + SPACE;
1002 }
1003 return mod + classType + SPACE + name + defCloseString();
1004 }
1005
1006 /**
1007 * Returns string for service class.
1008 *
1009 * @param name1 name of even listener class
1010 * @param name2 name of even class
1011 * @return listener service string for service class
1012 */
1013 static String getEventExtendsString(String name1, String type,
1014 String name2) {
1015 StringBuilder builder = new StringBuilder();
1016 builder.append(type).append(DIAMOND_OPEN_BRACKET).append(name1)
1017 .append(COMMA).append(SPACE).append(name2).append
1018 (DIAMOND_CLOSE_BRACKET);
1019 return builder.toString();
1020 }
1021
1022 /**
1023 * Returns import string.
1024 *
1025 * @param pkg package
1026 * @param cls class
1027 * @return import string
1028 */
1029 public static String getImportString(String pkg, String cls) {
1030 StringBuilder builder = new StringBuilder()
1031 .append(IMPORT).append(pkg).append(PERIOD).append(cls)
1032 .append(signatureClose());
1033 return builder.toString();
1034 }
1035
1036 /**
1037 * Returns static modifier string.
1038 *
1039 * @param modifier modifier
1040 * @return static modifier string
1041 */
1042 static String getSpecificModifier(String modifier, String keyWord) {
1043 return new StringBuilder(modifier).append(SPACE).append(keyWord)
1044 .toString();
1045 }
1046
1047 /**
1048 * Returns builder string for class definition.
1049 *
1050 * @param yangName class name
1051 * @return builder string for class definition
1052 */
1053 static String getBuilderImplStringClassDef(String yangName) {
1054 return new StringBuilder(yangName).append(PERIOD).append(yangName)
1055 .append(BUILDER).toString();
1056 }
1057
1058 /**
1059 * Returns default name string.
1060 *
1061 * @param yangName class name
1062 * @return default name string
1063 */
1064 static String getDefaultName(String yangName) {
1065 return new StringBuilder(DEFAULT_CAPS).append(yangName).toString();
1066 }
1067
1068 /**
1069 * Returns suffixed name string.
1070 *
1071 * @param yangName class name
1072 * @param suffix suffix to append to name
1073 * @return suffixed name string
1074 */
1075 static String getSuffixedName(String yangName, String suffix) {
1076 return new StringBuilder(yangName).append(suffix).toString();
1077 }
1078
1079 /**
1080 * Returns error msg string.
1081 *
1082 * @param msg message
1083 * @param name name of node
1084 * @param line line number
1085 * @param position char position
1086 * @param fileName file name
1087 * @return error message string
1088 */
1089 static String getErrorMsg(String msg, String name, int line, int position,
1090 String fileName) {
1091 return new StringBuilder().append(msg).append(name).append(IN)
1092 .append(line).append(AT).append(position).append(IN)
1093 .append(fileName).toString();
1094 }
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301095
1096 /**
1097 * Returns string builder attribute string;
1098 *
1099 * @param init first param to be appended to string builder
1100 * @param space indentation space
1101 * @return string builder attribute
1102 */
1103 static String getStringBuilderAttr(String init, String space) {
1104 StringBuilder builder = new StringBuilder(space);
1105 builder.append(STRING_BUILDER).append(SPACE).append(STRING_BUILDER_VAR)
1106 .append(SPACE).append(EQUAL).append(SPACE).append(NEW)
1107 .append(SPACE).append(STRING_BUILDER).append(
Bharat saraswal0663aff2016-10-18 23:16:14 +05301108 getOpenCloseParaWithValue(getQuotedString(init)))
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301109 .append(signatureClose());
1110 return builder.toString();
1111 }
1112
1113 /**
1114 * Returns quoted string.
1115 *
1116 * @param name name to be quoted
1117 * @return quoted string
1118 */
1119 static String getQuotedString(String name) {
1120 return QUOTES + name + QUOTES;
1121 }
1122
1123 /**
1124 * Returns string builder's append string.
1125 *
1126 * @param append data to be append
1127 * @param space indentation
1128 * @return string builder's append string
1129 */
1130 static String getStringBuilderAppendString(String append, String space) {
1131 return space + STRING_BUILDER_VAR + PERIOD + APPEND + OPEN_PARENTHESIS +
1132 append + CLOSE_PARENTHESIS + signatureClose();
1133 }
1134
1135 /**
1136 * Returns pattern quote string.
1137 *
1138 * @param type type for pattern is needed
1139 * @return pattern quote string
1140 */
1141 static String getPatternQuoteString(String type) {
Bharat saraswal0663aff2016-10-18 23:16:14 +05301142 return PATTERN + PERIOD + QUOTE_STRING + getOpenCloseParaWithValue(
1143 getQuotedString(type));
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301144 }
1145
1146 /**
1147 * Returns bitset attribute.
1148 *
1149 * @param indentation indentation
1150 * @return bitset attribute
1151 */
1152 static String getBitSetAttr(String indentation) {
1153 StringBuilder builder = new StringBuilder(indentation);
1154 return builder.append(BITSET).append(SPACE).append(TMP_VAL)
1155 .append(SPACE).append(EQUAL).append(SPACE).append(NEW)
1156 .append(SPACE).append(BITSET).append(OPEN_CLOSE_BRACKET_STRING)
1157 .append(signatureClose()).toString();
1158 }
1159
1160 /**
1161 * Returns for loop string
1162 *
1163 * @param space indentation
1164 * @param type data type
1165 * @param var variable
1166 * @param data data variable/collection
1167 * @return for loop string
1168 */
1169 static String getForLoopString(String space, String type, String var,
1170 String data) {
1171 return space + FOR + SPACE + OPEN_PARENTHESIS + type + SPACE + var +
1172 SPACE + COLON + SPACE + data + CLOSE_PARENTHESIS +
1173 methodSignatureClose(CLASS_TYPE);
1174 }
1175
1176 /**
1177 * Returns set value parameter's get string for union to string method.
1178 *
1179 * @param count count of type
1180 * @return get string
1181 */
1182 static String getSetValueParaCondition(int count) {
Bharat saraswal0663aff2016-10-18 23:16:14 +05301183 return SET_VALUE_PARA + PERIOD + GET + getOpenCloseParaWithValue(
1184 count + EMPTY_STRING);
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301185 }
1186
1187 /**
1188 * Returns more object attr for union to string method.
1189 *
Bharat saraswal94844d62016-10-13 13:28:03 +05301190 * @param name name of generate class
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301191 * @return more object attr for union to string method
1192 */
Bharat saraswal94844d62016-10-13 13:28:03 +05301193 static String getMoreObjectAttr(String name) {
1194 String cls = name + PERIOD + CLASS;
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301195 StringBuilder attr = new StringBuilder(EIGHT_SPACE_INDENTATION);
1196 String[] array = {NEW_LINE};
Bharat saraswal94844d62016-10-13 13:28:03 +05301197 attr.append(MORE_OBJ_ATTR).append(GOOGLE_MORE_OBJECT_METHOD_STATIC_STRING)
Bharat saraswal0663aff2016-10-18 23:16:14 +05301198 .append(getOpenCloseParaWithValue(cls)).append(NEW_LINE)
1199 .append(FOUR_SPACE_INDENTATION).append(trimAtLast(
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301200 getOmitNullValueString(), array)).append(signatureClose());
1201 return attr.toString();
1202 }
Bharat saraswal94844d62016-10-13 13:28:03 +05301203
1204 /**
1205 * Returns to string call.
1206 *
1207 * @param name name of attribute
1208 * @return to string call for attribute
1209 */
1210 static String getToStringCall(String name) {
1211 return name + PERIOD +
1212 TO_STRING_METHOD + OPEN_CLOSE_BRACKET_STRING;
1213 }
Bharat saraswal0663aff2016-10-18 23:16:14 +05301214
1215 /**
1216 * Returns value in brackets.
1217 *
1218 * @param name value
1219 * @return value in brackets
1220 */
1221 static String getOpenCloseParaWithValue(String name) {
1222 return brackets(OPEN_CLOSE_BRACKET_WITH_VALUE, name, null);
1223 }
1224
1225 /**
1226 * Returns equals comparision.
1227 *
1228 * @param para1 param
1229 * @param para2 param
1230 * @return equals comparision
1231 */
1232 static String getTwoParaEqualsString(String para1, String para2) {
1233 return para1 + PERIOD + EQUALS_STRING + getOpenCloseParaWithValue(para2);
1234 }
1235
1236 /**
1237 * Returns equal equal condition.
1238 *
1239 * @param para param
1240 * @param val value
1241 * @return equal equal condition
1242 */
1243 static String getEqualEqualString(String para, String val) {
1244 return para + SPACE + EQUAL + EQUAL + SPACE + val;
1245 }
1246
1247 /**
1248 * Returns app instance method call.
1249 *
1250 * @param name attr name
1251 * @return app instance method call
1252 */
1253 static String getAppInstanceAttrString(String name) {
1254 return APP_INSTANCE + PERIOD + name + OPEN_CLOSE_BRACKET_STRING;
1255 }
1256
1257 /**
1258 * Returns qualified name.
1259 *
1260 * @param pkg package
1261 * @param cls class info
1262 * @return qualified name
1263 */
1264 static String getQualifiedString(String pkg, String cls) {
1265 return pkg + PERIOD + cls;
1266 }
1267
1268 /**
1269 * Returns import list for node list.
1270 *
1271 * @param nodes node list
1272 * @param config plugin config
1273 * @return import list
1274 */
1275 public static List<JavaQualifiedTypeInfoTranslator> getNodesImports(List<YangNode> nodes,
1276 YangPluginConfig config) {
1277 List<JavaQualifiedTypeInfoTranslator> imports = new ArrayList<>();
1278 for (YangNode node : nodes) {
1279 JavaQualifiedTypeInfoTranslator qInfo = getQualifiedInfo(node,
1280 config);
1281 imports.add(qInfo);
1282 }
1283 return imports;
1284 }
1285
1286 /**
1287 * Returns list of child node for choice.
1288 *
1289 * @param choice choice node
1290 * @return list of child nodes
1291 */
1292 public static List<YangNode> getChoiceChildNodes(YangChoice choice) {
1293 List<YangNode> childs = new ArrayList<>();
1294 YangNode child = choice.getChild();
1295 while (child != null) {
1296 childs.add(child);
1297 child = child.getNextSibling();
1298 }
1299
1300 List<YangAugment> augments = choice.getAugmentedInfoList();
1301 YangNode augmentCase;
1302 for (YangAugment augment : augments) {
1303 augmentCase = augment.getChild();
1304 while (augmentCase != null) {
1305 childs.add(augmentCase);
1306 augmentCase = augmentCase.getNextSibling();
1307 }
1308 }
1309 return childs;
1310 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301311}