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 | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 19 | import java.util.List; |
| 20 | import java.util.SortedSet; |
| 21 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangType; |
| 23 | import org.onosproject.yangutils.translator.GeneratedFileType; |
| 24 | import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes; |
| 25 | import org.onosproject.yangutils.translator.tojava.ImportInfo; |
| 26 | import org.onosproject.yangutils.utils.UtilConstants; |
| 27 | |
| 28 | /** |
| 29 | * Utility class to generate the java snippet. |
| 30 | */ |
| 31 | public final class JavaCodeSnippetGen { |
| 32 | |
| 33 | /** |
| 34 | * Default constructor. |
| 35 | */ |
| 36 | private JavaCodeSnippetGen() { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get the java file header comment. |
| 41 | * |
| 42 | * @return the java file header comment. |
| 43 | */ |
| 44 | public static String getFileHeaderComment() { |
| 45 | |
| 46 | /** |
| 47 | * TODO return the file header. |
| 48 | */ |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | /** |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 53 | * reorder the import list based on the ONOS import rules. |
| 54 | * |
| 55 | * @param importInfo the set of classes/interfaces to be imported. |
| 56 | * @return string of import info. |
| 57 | */ |
| 58 | public List<ImportInfo> sortImportOrder(SortedSet<ImportInfo> importInfo) { |
| 59 | /* TODO: reorder the import list based on the ONOS import rules. */ |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | /** |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 64 | * Get the textual java code information corresponding to the import list. |
| 65 | * |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 66 | * @param importInfo import info. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 67 | * @return the textual java code information corresponding to the import |
| 68 | * list. |
| 69 | */ |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 70 | public static String getImportText(ImportInfo importInfo) { |
| 71 | return UtilConstants.IMPORT + importInfo.getPkgInfo() + UtilConstants.PERIOD + importInfo.getClassInfo() |
| 72 | + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Based on the file type and the YANG name of the file, generate the class |
| 77 | * / interface definition start. |
| 78 | * |
| 79 | * @param genFileTypes type of file being generated. |
| 80 | * @param yangName YANG name. |
| 81 | * @return corresponding textual java code information. |
| 82 | */ |
| 83 | public static String getJavaClassDefStart(GeneratedFileType genFileTypes, String yangName) { |
| 84 | /* |
| 85 | * get the camel case name for java class / interface. |
| 86 | */ |
| 87 | yangName = JavaIdentifierSyntax.getCamelCase(yangName); |
| 88 | return ClassDefinitionGenerator.generateClassDefinition(genFileTypes, yangName); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get the textual java code for attribute definition in class. |
| 93 | * |
| 94 | * @param genFileTypes type of file being generated. |
| 95 | * @param yangName YANG name of the the attribute. |
| 96 | * @param type type of the the attribute. |
| 97 | * @return the textual java code for attribute definition in class. |
| 98 | */ |
| 99 | public static String getJavaAttributeInfo(GeneratedFileType genFileTypes, String yangName, YangType<?> type) { |
| 100 | yangName = JavaIdentifierSyntax.getCamelCase(yangName); |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 101 | if (type != null) { |
| 102 | return UtilConstants.PRIVATE + UtilConstants.SPACE + type.getDataTypeName() + UtilConstants.SPACE + yangName |
| 103 | + UtilConstants.SEMI_COLAN; |
| 104 | } |
| 105 | return UtilConstants.PRIVATE + UtilConstants.SPACE + JavaIdentifierSyntax.getCaptialCase(yangName) |
| 106 | + UtilConstants.SPACE + yangName + UtilConstants.SEMI_COLAN; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns list attribute string. |
| 111 | * |
| 112 | * @param type attribute type |
| 113 | * @return list attribute string |
| 114 | */ |
| 115 | public static String getListAttribute(String type) { |
| 116 | return UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + type + UtilConstants.DIAMOND_CLOSE_BRACKET; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Based on the file type and method type(s) and the YANG name of the |
| 121 | * method, generate the method definitions(s). |
| 122 | * |
| 123 | * @param genFileTypes type of file being generated |
| 124 | * @param yangName name if the attribute whose getter / setter is required. |
| 125 | * @param methodTypes getter and / or setter type of method indicator. |
| 126 | * @param returnType type return type of the method. |
| 127 | * @return based on the file type and method type(s) the method |
| 128 | * definitions(s). |
| 129 | */ |
| 130 | public static String getJavaMethodInfo(GeneratedFileType genFileTypes, String yangName, |
| 131 | GeneratedMethodTypes methodTypes, YangType<?> returnType) { |
| 132 | |
| 133 | yangName = JavaIdentifierSyntax.getCamelCase(yangName); |
| 134 | return MethodsGenerator.constructMethodInfo(genFileTypes, yangName, methodTypes, returnType); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Based on the file type and the YANG name of the file, generate the class |
| 139 | * / interface definition close. |
| 140 | * |
| 141 | * @param genFileTypes type of file being generated. |
| 142 | * @param yangName YANG name. |
| 143 | * @return corresponding textual java code information. |
| 144 | */ |
| 145 | public static String getJavaClassDefClose(GeneratedFileType genFileTypes, String yangName) { |
| 146 | |
| 147 | if (genFileTypes.equals(GeneratedFileType.INTERFACE)) { |
| 148 | |
| 149 | return UtilConstants.CLOSE_CURLY_BRACKET; |
| 150 | } else if (genFileTypes.equals(GeneratedFileType.BUILDER_CLASS)) { |
| 151 | |
| 152 | return UtilConstants.CLOSE_CURLY_BRACKET; |
| 153 | } |
| 154 | return null; |
| 155 | } |
| 156 | |
| 157 | } |