Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame^] | 20 | |
| 21 | import static org.onosproject.yangutils.translator.tojava.utils.ClassDefinitionGenerator.generateClassDefinition; |
| 22 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 23 | import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET; |
| 24 | import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET; |
| 25 | import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET; |
| 26 | import static org.onosproject.yangutils.utils.UtilConstants.IMPORT; |
| 27 | import static org.onosproject.yangutils.utils.UtilConstants.LIST; |
| 28 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 29 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 30 | import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE; |
| 31 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
| 32 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Utility class to generate the java snippet. |
| 36 | */ |
| 37 | public 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 S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 48 | * @return the java file header comment |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 49 | */ |
| 50 | public static String getFileHeaderComment() { |
| 51 | |
| 52 | /** |
| 53 | * TODO return the file header. |
| 54 | */ |
| 55 | return null; |
| 56 | } |
| 57 | |
| 58 | /** |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 59 | * Get the textual java code information corresponding to the import list. |
| 60 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 61 | * @param importInfo import info |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 62 | * @return the textual java code information corresponding to the import |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 63 | * list |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 64 | */ |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 65 | public static String getImportText(JavaQualifiedTypeInfo importInfo) { |
| 66 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame^] | 67 | return IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN + NEW_LINE; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 68 | } |
| 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 S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 74 | * @param genFileTypes type of file being generated |
| 75 | * @param yangName YANG name |
| 76 | * @return corresponding textual java code information |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 77 | */ |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 78 | public static String getJavaClassDefStart(int genFileTypes, String yangName) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 79 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 80 | /* |
| 81 | * get the camel case name for java class / interface. |
| 82 | */ |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame^] | 83 | yangName = getCamelCase(yangName); |
| 84 | return generateClassDefinition(genFileTypes, yangName); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get the textual java code for attribute definition in class. |
| 89 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 90 | * @param javaAttributeTypePkg Package of the attribute type |
| 91 | * @param javaAttributeType java attribute type |
| 92 | * @param javaAttributeName name of the attribute |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 93 | * @param isList is list attribute |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 94 | * @return the textual java code for attribute definition in class |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 95 | */ |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 96 | public static String getJavaAttributeDefination(String javaAttributeTypePkg, String javaAttributeType, |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 97 | String javaAttributeName, boolean isList) { |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 98 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame^] | 99 | String attributeDefination = PRIVATE + SPACE; |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 100 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 101 | if (!isList) { |
| 102 | if (javaAttributeTypePkg != null) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame^] | 103 | attributeDefination = attributeDefination + javaAttributeTypePkg + PERIOD; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 104 | } |
| 105 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame^] | 106 | attributeDefination = attributeDefination + javaAttributeType + SPACE + javaAttributeName + SEMI_COLAN |
| 107 | + NEW_LINE; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 108 | } else { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame^] | 109 | attributeDefination = attributeDefination + LIST + DIAMOND_OPEN_BRACKET; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 110 | if (javaAttributeTypePkg != null) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame^] | 111 | attributeDefination = attributeDefination + javaAttributeTypePkg + PERIOD; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 112 | } |
| 113 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame^] | 114 | attributeDefination = attributeDefination + javaAttributeType + DIAMOND_CLOSE_BRACKET + SPACE |
| 115 | + javaAttributeName + SEMI_COLAN + NEW_LINE; |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 116 | } |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 117 | return attributeDefination; |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 118 | } |
| 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 S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 127 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame^] | 128 | return LIST + DIAMOND_OPEN_BRACKET + type + DIAMOND_CLOSE_BRACKET; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 132 | * Based on the file type and the YANG name of the file, generate the class |
| 133 | * / interface definition close. |
| 134 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 135 | * @return corresponding textual java code information |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 136 | */ |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 137 | public static String getJavaClassDefClose() { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 138 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame^] | 139 | return CLOSE_CURLY_BRACKET; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 140 | } |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 141 | } |