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