blob: ab8ee5c53f9cd1a3c0947119e7f48b609227320b [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
2 * Copyright 2016 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
Vinod Kumar S38046502016-03-23 15:30:27 +053019import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
Bharat saraswal870c56f2016-02-20 21:57:16 +053020import org.onosproject.yangutils.utils.UtilConstants;
21
22/**
23 * Utility class to generate the java snippet.
24 */
25public final class JavaCodeSnippetGen {
26
27 /**
28 * Default constructor.
29 */
30 private JavaCodeSnippetGen() {
31 }
32
33 /**
34 * Get the java file header comment.
35 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053036 * @return the java file header comment
Bharat saraswal870c56f2016-02-20 21:57:16 +053037 */
38 public static String getFileHeaderComment() {
39
40 /**
41 * TODO return the file header.
42 */
43 return null;
44 }
45
46 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053047 * Get the textual java code information corresponding to the import list.
48 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053049 * @param importInfo import info
Bharat saraswal870c56f2016-02-20 21:57:16 +053050 * @return the textual java code information corresponding to the import
Vinod Kumar Sc4216002016-03-03 19:55:30 +053051 * list
Bharat saraswal870c56f2016-02-20 21:57:16 +053052 */
Vinod Kumar S38046502016-03-23 15:30:27 +053053 public static String getImportText(JavaQualifiedTypeInfo importInfo) {
54
Bharat saraswal594bc6d2016-02-22 22:15:21 +053055 return UtilConstants.IMPORT + importInfo.getPkgInfo() + UtilConstants.PERIOD + importInfo.getClassInfo()
Vinod Kumar Sc4216002016-03-03 19:55:30 +053056 + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +053057 }
58
59 /**
60 * Based on the file type and the YANG name of the file, generate the class
61 * / interface definition start.
62 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053063 * @param genFileTypes type of file being generated
64 * @param yangName YANG name
65 * @return corresponding textual java code information
Bharat saraswal870c56f2016-02-20 21:57:16 +053066 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053067 public static String getJavaClassDefStart(int genFileTypes, String yangName) {
Vinod Kumar S38046502016-03-23 15:30:27 +053068
Bharat saraswal870c56f2016-02-20 21:57:16 +053069 /*
70 * get the camel case name for java class / interface.
71 */
72 yangName = JavaIdentifierSyntax.getCamelCase(yangName);
73 return ClassDefinitionGenerator.generateClassDefinition(genFileTypes, yangName);
74 }
75
76 /**
77 * Get the textual java code for attribute definition in class.
78 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053079 * @param javaAttributeTypePkg Package of the attribute type
80 * @param javaAttributeType java attribute type
81 * @param javaAttributeName name of the attribute
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053082 * @param isList is list attribute
Vinod Kumar Sc4216002016-03-03 19:55:30 +053083 * @return the textual java code for attribute definition in class
Bharat saraswal870c56f2016-02-20 21:57:16 +053084 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053085 public static String getJavaAttributeDefination(String javaAttributeTypePkg, String javaAttributeType,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053086 String javaAttributeName, boolean isList) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +053087
88 String attributeDefination = UtilConstants.PRIVATE
89 + UtilConstants.SPACE;
90
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053091 if (!isList) {
92 if (javaAttributeTypePkg != null) {
93 attributeDefination = attributeDefination
Vinod Kumar S38046502016-03-23 15:30:27 +053094 + javaAttributeTypePkg + UtilConstants.PERIOD;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053095 }
96
Vinod Kumar Sc4216002016-03-03 19:55:30 +053097 attributeDefination = attributeDefination
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053098 + javaAttributeType
99 + UtilConstants.SPACE
100 + javaAttributeName
Vinod Kumar S38046502016-03-23 15:30:27 +0530101 + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530102 } else {
103 attributeDefination = attributeDefination + UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET;
104 if (javaAttributeTypePkg != null) {
105 attributeDefination = attributeDefination
Vinod Kumar S38046502016-03-23 15:30:27 +0530106 + javaAttributeTypePkg + UtilConstants.PERIOD;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530107 }
108
109 attributeDefination = attributeDefination
110 + javaAttributeType + UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE
Vinod Kumar S38046502016-03-23 15:30:27 +0530111 + javaAttributeName + UtilConstants.SUFIX_S + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530112 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530113 return attributeDefination;
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530114 }
115
116 /**
117 * Returns list attribute string.
118 *
119 * @param type attribute type
120 * @return list attribute string
121 */
122 public static String getListAttribute(String type) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530123
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530124 return UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + type + UtilConstants.DIAMOND_CLOSE_BRACKET;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530125 }
126
127 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530128 * Based on the file type and the YANG name of the file, generate the class
129 * / interface definition close.
130 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530131 * @return corresponding textual java code information
Bharat saraswal870c56f2016-02-20 21:57:16 +0530132 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530133 public static String getJavaClassDefClose() {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530134
Vinod Kumar S38046502016-03-23 15:30:27 +0530135 return UtilConstants.CLOSE_CURLY_BRACKET;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530136 }
137
138}