blob: 99c5c7d9aca7782e7625e1fcc843e098811101dd [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/**
35 * Utility class to generate the java snippet.
36 */
37public final class JavaCodeSnippetGen {
38
39 /**
40 * Default constructor.
41 */
42 private JavaCodeSnippetGen() {
43 }
44
45 /**
46 * Get the java file header comment.
47 *
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 saraswal870c56f2016-02-20 21:57:16 +053059 * Get the textual java code information corresponding to the import list.
60 *
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) {
66
Bharat saraswale2d51d62016-03-23 19:40:35 +053067 return IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +053068 }
69
70 /**
71 * Based on the file type and the YANG name of the file, generate the class
72 * / interface definition start.
73 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053074 * @param genFileTypes type of file being generated
75 * @param yangName YANG name
76 * @return corresponding textual java code information
Bharat saraswal870c56f2016-02-20 21:57:16 +053077 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053078 public static String getJavaClassDefStart(int genFileTypes, String yangName) {
Vinod Kumar S38046502016-03-23 15:30:27 +053079
Bharat saraswal870c56f2016-02-20 21:57:16 +053080 /*
81 * get the camel case name for java class / interface.
82 */
Bharat saraswale2d51d62016-03-23 19:40:35 +053083 yangName = getCamelCase(yangName);
84 return generateClassDefinition(genFileTypes, yangName);
Bharat saraswal870c56f2016-02-20 21:57:16 +053085 }
86
87 /**
88 * Get the textual java code for attribute definition in class.
89 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053090 * @param javaAttributeTypePkg Package of the attribute type
91 * @param javaAttributeType java attribute type
92 * @param javaAttributeName name of the attribute
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053093 * @param isList is list attribute
Vinod Kumar Sc4216002016-03-03 19:55:30 +053094 * @return the textual java code for attribute definition in class
Bharat saraswal870c56f2016-02-20 21:57:16 +053095 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053096 public static String getJavaAttributeDefination(String javaAttributeTypePkg, String javaAttributeType,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053097 String javaAttributeName, boolean isList) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +053098
Bharat saraswale2d51d62016-03-23 19:40:35 +053099 String attributeDefination = PRIVATE + SPACE;
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530100
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530101 if (!isList) {
102 if (javaAttributeTypePkg != null) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530103 attributeDefination = attributeDefination + javaAttributeTypePkg + PERIOD;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530104 }
105
Bharat saraswale2d51d62016-03-23 19:40:35 +0530106 attributeDefination = attributeDefination + javaAttributeType + SPACE + javaAttributeName + SEMI_COLAN
107 + NEW_LINE;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530108 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530109 attributeDefination = attributeDefination + LIST + DIAMOND_OPEN_BRACKET;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530110 if (javaAttributeTypePkg != null) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530111 attributeDefination = attributeDefination + javaAttributeTypePkg + PERIOD;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530112 }
113
Bharat saraswale2d51d62016-03-23 19:40:35 +0530114 attributeDefination = attributeDefination + javaAttributeType + DIAMOND_CLOSE_BRACKET + SPACE
115 + javaAttributeName + SEMI_COLAN + NEW_LINE;
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530116 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530117 return attributeDefination;
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530118 }
119
120 /**
121 * Returns list attribute string.
122 *
123 * @param type attribute type
124 * @return list attribute string
125 */
126 public static String getListAttribute(String type) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530127
Bharat saraswale2d51d62016-03-23 19:40:35 +0530128 return LIST + DIAMOND_OPEN_BRACKET + type + DIAMOND_CLOSE_BRACKET;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530129 }
130
131 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530132 * Based on the file type and the YANG name of the file, generate the class
133 * / interface definition close.
134 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530135 * @return corresponding textual java code information
Bharat saraswal870c56f2016-02-20 21:57:16 +0530136 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530137 public static String getJavaClassDefClose() {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530138
Bharat saraswale2d51d62016-03-23 19:40:35 +0530139 return CLOSE_CURLY_BRACKET;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530140 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530141}