Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 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; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.utils.io.impl.YangPluginConfig; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 21 | |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 22 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 23 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getSmallCase; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 24 | import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST; |
| 25 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.utils.UtilConstants.COMMA; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET; |
| 30 | import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.utils.UtilConstants.EQUAL; |
| 32 | import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.utils.UtilConstants.IMPORT; |
| 34 | import static org.onosproject.yangutils.utils.UtilConstants.LIST; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.utils.UtilConstants.NEW; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 39 | import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE; |
| 40 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
| 41 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 42 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
| 43 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ENUM_ATTRIBUTE; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 44 | |
| 45 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 46 | * Represents utility class to generate the java snippet. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 47 | */ |
| 48 | public final class JavaCodeSnippetGen { |
| 49 | |
| 50 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 51 | * Creates an instance of java code snippet gen. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 52 | */ |
| 53 | private JavaCodeSnippetGen() { |
| 54 | } |
| 55 | |
| 56 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 57 | * Returns the java file header comment. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 58 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 59 | * @return the java file header comment |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 60 | */ |
| 61 | public static String getFileHeaderComment() { |
| 62 | |
| 63 | /** |
| 64 | * TODO return the file header. |
| 65 | */ |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 70 | * Returns the textual java code information corresponding to the import list. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 71 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 72 | * @param importInfo import info |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 73 | * @return the textual java code information corresponding to the import |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 74 | * list |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 75 | */ |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 76 | public static String getImportText(JavaQualifiedTypeInfo importInfo) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 77 | return IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN + NEW_LINE; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 81 | * Returns the textual java code for attribute definition in class. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 82 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 83 | * @param javaAttributeTypePkg Package of the attribute type |
| 84 | * @param javaAttributeType java attribute type |
| 85 | * @param javaAttributeName name of the attribute |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 86 | * @param isList is list attribute |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 87 | * @return the textual java code for attribute definition in class |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 88 | */ |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 89 | public static String getJavaAttributeDefination(String javaAttributeTypePkg, String javaAttributeType, |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 90 | String javaAttributeName, boolean isList) { |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 91 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 92 | String attributeDefination = PRIVATE + SPACE; |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 93 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 94 | if (!isList) { |
| 95 | if (javaAttributeTypePkg != null) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 96 | attributeDefination = attributeDefination + javaAttributeTypePkg + PERIOD; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 97 | } |
| 98 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 99 | attributeDefination = attributeDefination + javaAttributeType + SPACE + javaAttributeName + SEMI_COLAN |
| 100 | + NEW_LINE; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 101 | } else { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 102 | attributeDefination = attributeDefination + LIST + DIAMOND_OPEN_BRACKET; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 103 | if (javaAttributeTypePkg != null) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 104 | attributeDefination = attributeDefination + javaAttributeTypePkg + PERIOD; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 105 | } |
| 106 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 107 | attributeDefination = attributeDefination + javaAttributeType + DIAMOND_CLOSE_BRACKET + SPACE |
| 108 | + javaAttributeName + SEMI_COLAN + NEW_LINE; |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 109 | } |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 110 | return attributeDefination; |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns list attribute string. |
| 115 | * |
| 116 | * @param type attribute type |
| 117 | * @return list attribute string |
| 118 | */ |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 119 | public static String getListAttribute(String type) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 120 | return LIST + DIAMOND_OPEN_BRACKET + type + DIAMOND_CLOSE_BRACKET; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 124 | * Returns attribute of augmented info for generated impl file. |
| 125 | * |
| 126 | * @return attribute of augmented info for generated impl file |
| 127 | */ |
| 128 | public static String getAugmentedInfoAttribute() { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 129 | return NEW_LINE + FOUR_SPACE_INDENTATION + PRIVATE + SPACE + getListAttribute(AUGMENTED_INFO) + SPACE |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 130 | + getSmallCase(AUGMENTED_INFO) + LIST + SPACE + EQUAL + SPACE + NEW + SPACE + ARRAY_LIST |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 131 | + DIAMOND_OPEN_BRACKET + DIAMOND_CLOSE_BRACKET + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 135 | * Returns based on the file type and the YANG name of the file, generate the class |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 136 | * / interface definition close. |
| 137 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 138 | * @return corresponding textual java code information |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 139 | */ |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 140 | public static String getJavaClassDefClose() { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 141 | return CLOSE_CURLY_BRACKET; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 142 | } |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * Returns string for enum's attribute. |
| 146 | * |
| 147 | * @param name name of attribute |
| 148 | * @param value value of the enum |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 149 | * @param pluginConfig plugin configurations |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 150 | * @return string for enum's attribute |
| 151 | */ |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 152 | public static String generateEnumAttributeString(String name, int value, YangPluginConfig pluginConfig) { |
| 153 | return getJavaDoc(ENUM_ATTRIBUTE, name, false, pluginConfig) + FOUR_SPACE_INDENTATION |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 154 | + getEnumJavaAttribute(name).toUpperCase() + OPEN_PARENTHESIS |
| 155 | + value + CLOSE_PARENTHESIS + COMMA + NEW_LINE; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 156 | } |
| 157 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 158 | } |