blob: e8bade32933b1d3f3894bb30bf32a767c6f6af68 [file] [log] [blame]
Bharat saraswal97459962016-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
Bharat saraswal97459962016-02-20 21:57:16 +053019import org.onosproject.yangutils.datamodel.YangType;
20import org.onosproject.yangutils.translator.GeneratedFileType;
21import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
22import org.onosproject.yangutils.translator.tojava.ImportInfo;
23import org.onosproject.yangutils.utils.UtilConstants;
24
25/**
26 * Utility class to generate the java snippet.
27 */
28public final class JavaCodeSnippetGen {
29
30 /**
31 * Default constructor.
32 */
33 private JavaCodeSnippetGen() {
34 }
35
36 /**
37 * Get the java file header comment.
38 *
39 * @return the java file header comment.
40 */
41 public static String getFileHeaderComment() {
42
43 /**
44 * TODO return the file header.
45 */
46 return null;
47 }
48
49 /**
Bharat saraswal97459962016-02-20 21:57:16 +053050 * Get the textual java code information corresponding to the import list.
51 *
Bharat saraswal5e3c45c2016-02-22 22:15:21 +053052 * @param importInfo import info.
Bharat saraswal97459962016-02-20 21:57:16 +053053 * @return the textual java code information corresponding to the import
54 * list.
55 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +053056 public static String getImportText(ImportInfo importInfo) {
57 return UtilConstants.IMPORT + importInfo.getPkgInfo() + UtilConstants.PERIOD + importInfo.getClassInfo()
58 + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
Bharat saraswal97459962016-02-20 21:57:16 +053059 }
60
61 /**
62 * Based on the file type and the YANG name of the file, generate the class
63 * / interface definition start.
64 *
65 * @param genFileTypes type of file being generated.
66 * @param yangName YANG name.
67 * @return corresponding textual java code information.
68 */
69 public static String getJavaClassDefStart(GeneratedFileType genFileTypes, String yangName) {
70 /*
71 * get the camel case name for java class / interface.
72 */
73 yangName = JavaIdentifierSyntax.getCamelCase(yangName);
74 return ClassDefinitionGenerator.generateClassDefinition(genFileTypes, yangName);
75 }
76
77 /**
78 * Get the textual java code for attribute definition in class.
79 *
80 * @param genFileTypes type of file being generated.
81 * @param yangName YANG name of the the attribute.
82 * @param type type of the the attribute.
83 * @return the textual java code for attribute definition in class.
84 */
85 public static String getJavaAttributeInfo(GeneratedFileType genFileTypes, String yangName, YangType<?> type) {
86 yangName = JavaIdentifierSyntax.getCamelCase(yangName);
Bharat saraswal5e3c45c2016-02-22 22:15:21 +053087 if (type != null) {
88 return UtilConstants.PRIVATE + UtilConstants.SPACE + type.getDataTypeName() + UtilConstants.SPACE + yangName
89 + UtilConstants.SEMI_COLAN;
90 }
91 return UtilConstants.PRIVATE + UtilConstants.SPACE + JavaIdentifierSyntax.getCaptialCase(yangName)
92 + UtilConstants.SPACE + yangName + UtilConstants.SEMI_COLAN;
93 }
94
95 /**
96 * Returns list attribute string.
97 *
98 * @param type attribute type
99 * @return list attribute string
100 */
101 public static String getListAttribute(String type) {
102 return UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + type + UtilConstants.DIAMOND_CLOSE_BRACKET;
Bharat saraswal97459962016-02-20 21:57:16 +0530103 }
104
105 /**
106 * Based on the file type and method type(s) and the YANG name of the
107 * method, generate the method definitions(s).
108 *
109 * @param genFileTypes type of file being generated
110 * @param yangName name if the attribute whose getter / setter is required.
111 * @param methodTypes getter and / or setter type of method indicator.
112 * @param returnType type return type of the method.
113 * @return based on the file type and method type(s) the method
114 * definitions(s).
115 */
116 public static String getJavaMethodInfo(GeneratedFileType genFileTypes, String yangName,
117 GeneratedMethodTypes methodTypes, YangType<?> returnType) {
118
119 yangName = JavaIdentifierSyntax.getCamelCase(yangName);
120 return MethodsGenerator.constructMethodInfo(genFileTypes, yangName, methodTypes, returnType);
121 }
122
123 /**
124 * Based on the file type and the YANG name of the file, generate the class
125 * / interface definition close.
126 *
127 * @param genFileTypes type of file being generated.
128 * @param yangName YANG name.
129 * @return corresponding textual java code information.
130 */
131 public static String getJavaClassDefClose(GeneratedFileType genFileTypes, String yangName) {
132
133 if (genFileTypes.equals(GeneratedFileType.INTERFACE)) {
134
135 return UtilConstants.CLOSE_CURLY_BRACKET;
136 } else if (genFileTypes.equals(GeneratedFileType.BUILDER_CLASS)) {
137
138 return UtilConstants.CLOSE_CURLY_BRACKET;
139 }
140 return null;
141 }
142
143}