blob: 07da946eda8a0b5d622af0634066fcf94035c07c [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;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053023import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
24import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST;
25import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
Bharat saraswale2d51d62016-03-23 19:40:35 +053026import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053027import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS;
Bharat saraswale2d51d62016-03-23 19:40:35 +053028import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
29import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053030import static org.onosproject.yangutils.utils.UtilConstants.EQUAL;
31import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
Bharat saraswale2d51d62016-03-23 19:40:35 +053032import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
33import static org.onosproject.yangutils.utils.UtilConstants.LIST;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053034import static org.onosproject.yangutils.utils.UtilConstants.NEW;
Bharat saraswale2d51d62016-03-23 19:40:35 +053035import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053036import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS;
Bharat saraswale2d51d62016-03-23 19:40:35 +053037import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
38import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
39import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
40import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswal870c56f2016-02-20 21:57:16 +053041
42/**
Bharat saraswald9822e92016-04-05 15:13:44 +053043 * Represents utility class to generate the java snippet.
Bharat saraswal870c56f2016-02-20 21:57:16 +053044 */
45public final class JavaCodeSnippetGen {
46
47 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053048 * Creates an instance of java code snippet gen.
Bharat saraswal870c56f2016-02-20 21:57:16 +053049 */
50 private JavaCodeSnippetGen() {
51 }
52
53 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053054 * Returns the java file header comment.
Bharat saraswal870c56f2016-02-20 21:57:16 +053055 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053056 * @return the java file header comment
Bharat saraswal870c56f2016-02-20 21:57:16 +053057 */
58 public static String getFileHeaderComment() {
59
60 /**
61 * TODO return the file header.
62 */
63 return null;
64 }
65
66 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053067 * Returns the textual java code information corresponding to the import list.
Bharat saraswal870c56f2016-02-20 21:57:16 +053068 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053069 * @param importInfo import info
Bharat saraswal870c56f2016-02-20 21:57:16 +053070 * @return the textual java code information corresponding to the import
Vinod Kumar Sc4216002016-03-03 19:55:30 +053071 * list
Bharat saraswal870c56f2016-02-20 21:57:16 +053072 */
Vinod Kumar S38046502016-03-23 15:30:27 +053073 public static String getImportText(JavaQualifiedTypeInfo importInfo) {
Bharat saraswale2d51d62016-03-23 19:40:35 +053074 return IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +053075 }
76
77 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053078 * Returns based on the file type and the YANG name of the file, generate the class
Bharat saraswal870c56f2016-02-20 21:57:16 +053079 * / interface definition start.
80 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053081 * @param genFileTypes type of file being generated
82 * @param yangName YANG name
83 * @return corresponding textual java code information
Bharat saraswal870c56f2016-02-20 21:57:16 +053084 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053085 public static String getJavaClassDefStart(int genFileTypes, String yangName) {
Vinod Kumar S38046502016-03-23 15:30:27 +053086
Bharat saraswal870c56f2016-02-20 21:57:16 +053087 /*
88 * get the camel case name for java class / interface.
89 */
janani bde4ffab2016-04-15 16:18:30 +053090 yangName = getCamelCase(yangName, null);
Bharat saraswale2d51d62016-03-23 19:40:35 +053091 return generateClassDefinition(genFileTypes, yangName);
Bharat saraswal870c56f2016-02-20 21:57:16 +053092 }
93
94 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053095 * Returns the textual java code for attribute definition in class.
Bharat saraswal870c56f2016-02-20 21:57:16 +053096 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053097 * @param javaAttributeTypePkg Package of the attribute type
98 * @param javaAttributeType java attribute type
99 * @param javaAttributeName name of the attribute
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530100 * @param isList is list attribute
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530101 * @return the textual java code for attribute definition in class
Bharat saraswal870c56f2016-02-20 21:57:16 +0530102 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530103 public static String getJavaAttributeDefination(String javaAttributeTypePkg, String javaAttributeType,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530104 String javaAttributeName, boolean isList) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530105
Bharat saraswale2d51d62016-03-23 19:40:35 +0530106 String attributeDefination = PRIVATE + SPACE;
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530107
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530108 if (!isList) {
109 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 + SPACE + javaAttributeName + SEMI_COLAN
114 + NEW_LINE;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530115 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530116 attributeDefination = attributeDefination + LIST + DIAMOND_OPEN_BRACKET;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530117 if (javaAttributeTypePkg != null) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530118 attributeDefination = attributeDefination + javaAttributeTypePkg + PERIOD;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530119 }
120
Bharat saraswale2d51d62016-03-23 19:40:35 +0530121 attributeDefination = attributeDefination + javaAttributeType + DIAMOND_CLOSE_BRACKET + SPACE
122 + javaAttributeName + SEMI_COLAN + NEW_LINE;
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530123 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530124 return attributeDefination;
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530125 }
126
127 /**
128 * Returns list attribute string.
129 *
130 * @param type attribute type
131 * @return list attribute string
132 */
133 public static String getListAttribute(String type) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530134 return LIST + DIAMOND_OPEN_BRACKET + type + DIAMOND_CLOSE_BRACKET;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530135 }
136
137 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530138 * Returns attribute of augmented info for generated impl file.
139 *
140 * @return attribute of augmented info for generated impl file
141 */
142 public static String getAugmentedInfoAttribute() {
143 return FOUR_SPACE_INDENTATION + PRIVATE + SPACE + getListAttribute(AUGMENTED_INFO) + SPACE
144 + getSmallCase(AUGMENTED_INFO) + LIST + SPACE + EQUAL + SPACE + NEW + SPACE + ARRAY_LIST
145 + DIAMOND_OPEN_BRACKET + DIAMOND_CLOSE_BRACKET + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN
146 + NEW_LINE;
147 }
148
149 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530150 * Returns based on the file type and the YANG name of the file, generate the class
Bharat saraswal870c56f2016-02-20 21:57:16 +0530151 * / interface definition close.
152 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530153 * @return corresponding textual java code information
Bharat saraswal870c56f2016-02-20 21:57:16 +0530154 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530155 public static String getJavaClassDefClose() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530156 return CLOSE_CURLY_BRACKET;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530157 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530158}