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