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