blob: 861ef4df27613627d91cbeeb92771c98984e500a [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Bharat saraswal870c56f2016-02-20 21:57:16 +05303 *
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 saraswale2d51d62016-03-23 19:40:35 +053020
21import static org.onosproject.yangutils.translator.tojava.utils.ClassDefinitionGenerator.generateClassDefinition;
22import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
23import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
24import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
25import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
26import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
27import static org.onosproject.yangutils.utils.UtilConstants.LIST;
28import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
29import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
30import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
31import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
32import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswal870c56f2016-02-20 21:57:16 +053033
34/**
Bharat saraswald9822e92016-04-05 15:13:44 +053035 * Represents utility class to generate the java snippet.
Bharat saraswal870c56f2016-02-20 21:57:16 +053036 */
37public final class JavaCodeSnippetGen {
38
39 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053040 * Creates an instance of java code snippet gen.
Bharat saraswal870c56f2016-02-20 21:57:16 +053041 */
42 private JavaCodeSnippetGen() {
43 }
44
45 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053046 * Returns the java file header comment.
Bharat saraswal870c56f2016-02-20 21:57:16 +053047 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053048 * @return the java file header comment
Bharat saraswal870c56f2016-02-20 21:57:16 +053049 */
50 public static String getFileHeaderComment() {
51
52 /**
53 * TODO return the file header.
54 */
55 return null;
56 }
57
58 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053059 * Returns the textual java code information corresponding to the import list.
Bharat saraswal870c56f2016-02-20 21:57:16 +053060 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053061 * @param importInfo import info
Bharat saraswal870c56f2016-02-20 21:57:16 +053062 * @return the textual java code information corresponding to the import
Vinod Kumar Sc4216002016-03-03 19:55:30 +053063 * list
Bharat saraswal870c56f2016-02-20 21:57:16 +053064 */
Vinod Kumar S38046502016-03-23 15:30:27 +053065 public static String getImportText(JavaQualifiedTypeInfo importInfo) {
Bharat saraswale2d51d62016-03-23 19:40:35 +053066 return IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +053067 }
68
69 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053070 * Returns based on the file type and the YANG name of the file, generate the class
Bharat saraswal870c56f2016-02-20 21:57:16 +053071 * / interface definition start.
72 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053073 * @param genFileTypes type of file being generated
74 * @param yangName YANG name
75 * @return corresponding textual java code information
Bharat saraswal870c56f2016-02-20 21:57:16 +053076 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053077 public static String getJavaClassDefStart(int genFileTypes, String yangName) {
Vinod Kumar S38046502016-03-23 15:30:27 +053078
Bharat saraswal870c56f2016-02-20 21:57:16 +053079 /*
80 * get the camel case name for java class / interface.
81 */
janani bde4ffab2016-04-15 16:18:30 +053082 yangName = getCamelCase(yangName, null);
Bharat saraswale2d51d62016-03-23 19:40:35 +053083 return generateClassDefinition(genFileTypes, yangName);
Bharat saraswal870c56f2016-02-20 21:57:16 +053084 }
85
86 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053087 * Returns the textual java code for attribute definition in class.
Bharat saraswal870c56f2016-02-20 21:57:16 +053088 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053089 * @param javaAttributeTypePkg Package of the attribute type
90 * @param javaAttributeType java attribute type
91 * @param javaAttributeName name of the attribute
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053092 * @param isList is list attribute
Vinod Kumar Sc4216002016-03-03 19:55:30 +053093 * @return the textual java code for attribute definition in class
Bharat saraswal870c56f2016-02-20 21:57:16 +053094 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053095 public static String getJavaAttributeDefination(String javaAttributeTypePkg, String javaAttributeType,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053096 String javaAttributeName, boolean isList) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +053097
Bharat saraswale2d51d62016-03-23 19:40:35 +053098 String attributeDefination = PRIVATE + SPACE;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053099
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530100 if (!isList) {
101 if (javaAttributeTypePkg != null) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530102 attributeDefination = attributeDefination + javaAttributeTypePkg + PERIOD;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530103 }
104
Bharat saraswale2d51d62016-03-23 19:40:35 +0530105 attributeDefination = attributeDefination + javaAttributeType + SPACE + javaAttributeName + SEMI_COLAN
106 + NEW_LINE;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530107 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530108 attributeDefination = attributeDefination + LIST + DIAMOND_OPEN_BRACKET;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530109 if (javaAttributeTypePkg != null) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530110 attributeDefination = attributeDefination + javaAttributeTypePkg + PERIOD;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530111 }
112
Bharat saraswale2d51d62016-03-23 19:40:35 +0530113 attributeDefination = attributeDefination + javaAttributeType + DIAMOND_CLOSE_BRACKET + SPACE
114 + javaAttributeName + SEMI_COLAN + NEW_LINE;
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530115 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530116 return attributeDefination;
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530117 }
118
119 /**
120 * Returns list attribute string.
121 *
122 * @param type attribute type
123 * @return list attribute string
124 */
125 public static String getListAttribute(String type) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530126 return LIST + DIAMOND_OPEN_BRACKET + type + DIAMOND_CLOSE_BRACKET;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530127 }
128
129 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530130 * Returns based on the file type and the YANG name of the file, generate the class
Bharat saraswal870c56f2016-02-20 21:57:16 +0530131 * / interface definition close.
132 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530133 * @return corresponding textual java code information
Bharat saraswal870c56f2016-02-20 21:57:16 +0530134 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530135 public static String getJavaClassDefClose() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530136 return CLOSE_CURLY_BRACKET;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530137 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530138}