Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +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 | 4bf8b15 | 2016-02-25 02:26:43 +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 | |
| 19 | import java.io.File; |
| 20 | import java.io.IOException; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 21 | import java.util.ArrayList; |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 22 | import java.util.List; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangNode; |
| 24 | import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo; |
| 25 | import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles; |
| 26 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 27 | |
| 28 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK; |
| 29 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 30 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 31 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_RPC_INTERFACE; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS; |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK; |
| 35 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK; |
| 36 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK; |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 41 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 42 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK; |
| 43 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK; |
| 44 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK; |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 46 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_IMPL_MASK; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK; |
| 48 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK; |
| 49 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getAugmentedInfoAttribute; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 51 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getDataFromTempFileHandle; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 52 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getEnumsValueAttribute; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 53 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.initiateJavaFileGeneration; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 54 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 55 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 56 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAddAugmentInfoMethodImpl; |
| 57 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAugmentInfoListImpl; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 58 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructorStart; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 59 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEnumsConstrcutor; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 60 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethodClose; |
| 61 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethodOpen; |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 62 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethodClose; |
| 63 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethodSignature; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 64 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetter; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 65 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodClose; |
| 66 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodOpen; |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 67 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOmitNullValueString; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 68 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRemoveAugmentationImpl; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 69 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethodClose; |
| 70 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethodOpen; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 71 | import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isHasAugmentationExtended; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 72 | import static org.onosproject.yangutils.utils.UtilConstants.BUILDER; |
| 73 | import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 74 | import static org.onosproject.yangutils.utils.UtilConstants.COMMA; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 75 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
| 76 | import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION; |
| 77 | import static org.onosproject.yangutils.utils.UtilConstants.IMPL; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 78 | import static org.onosproject.yangutils.utils.UtilConstants.INT; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 79 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 80 | import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE; |
| 81 | import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 82 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 83 | import static org.onosproject.yangutils.utils.UtilConstants.SERVICE_METHOD_STRING; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 84 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD; |
| 85 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_CONSTRUCTOR; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 86 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 87 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 88 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.partString; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 89 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast; |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 90 | |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 91 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 92 | * Representation of java file generator. |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 93 | */ |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 94 | public final class JavaFileGenerator { |
| 95 | |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 96 | /** |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 97 | * Flag to check whether generated interface file need to extends any class. |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 98 | */ |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 99 | private static boolean isExtendsList = false; |
| 100 | |
| 101 | /** |
| 102 | * List of classes to be extended by generated interface file. |
| 103 | */ |
| 104 | private static List<String> extendsList = new ArrayList<>(); |
| 105 | |
| 106 | /** |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 107 | * Creates an instance of java file generator. |
| 108 | */ |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 109 | private JavaFileGenerator() { |
| 110 | } |
| 111 | |
| 112 | /** |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 113 | * Returns true if extends list is not empty. |
| 114 | * |
| 115 | * @return true or false |
| 116 | */ |
| 117 | public static boolean isExtendsList() { |
| 118 | return isExtendsList; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Sets the value of is extends list. |
| 123 | * |
| 124 | * @param isExtends true or false |
| 125 | */ |
| 126 | public static void setIsExtendsList(boolean isExtends) { |
| 127 | isExtendsList = isExtends; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns list of extended classes. |
| 132 | * |
| 133 | * @return list of extended classes |
| 134 | */ |
| 135 | public static List<String> getExtendsList() { |
| 136 | return extendsList; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Sets the list of extended classes. |
| 141 | * |
| 142 | * @param extendList list of extended classes |
| 143 | */ |
| 144 | public static void setExtendsList(List<String> extendList) { |
| 145 | extendsList = extendList; |
| 146 | } |
| 147 | |
| 148 | /** |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 149 | * Returns generated interface file for current node. |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 150 | * |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 151 | * @param file file |
| 152 | * @param imports imports for the file |
| 153 | * @param curNode current YANG node |
| 154 | * @param isAttrPresent if any attribute is present or not |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 155 | * @return interface file |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 156 | * @throws IOException when fails to write in file |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 157 | */ |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 158 | public static File generateInterfaceFile(File file, List<String> imports, YangNode curNode, boolean isAttrPresent) |
| 159 | throws IOException { |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 160 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 161 | JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo(); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 162 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 163 | String className = getCaptialCase(javaFileInfo.getJavaName()); |
| 164 | String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath(); |
| 165 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 166 | initiateJavaFileGeneration(file, className, INTERFACE_MASK, imports, path); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 167 | |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 168 | if (isAttrPresent) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 169 | /** |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 170 | * Add getter methods to interface file. |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 171 | */ |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 172 | try { |
| 173 | /** |
| 174 | * Getter methods. |
| 175 | */ |
| 176 | insertDataIntoJavaFile(file, getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK, curNode)); |
| 177 | } catch (IOException e) { |
| 178 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 179 | + " while interface file generation"); |
| 180 | } |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 181 | } |
| 182 | return file; |
| 183 | } |
| 184 | |
| 185 | /** |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 186 | * Returns generated builder interface file for current node. |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 187 | * |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 188 | * @param file file |
| 189 | * @param curNode current YANG node |
| 190 | * @param isAttrPresent if any attribute is present or not |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 191 | * @return builder interface file |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 192 | * @throws IOException when fails to write in file |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 193 | */ |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 194 | public static File generateBuilderInterfaceFile(File file, YangNode curNode, boolean isAttrPresent) |
| 195 | throws IOException { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 196 | |
| 197 | JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo(); |
| 198 | |
| 199 | String className = getCaptialCase(javaFileInfo.getJavaName()); |
| 200 | String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath(); |
| 201 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 202 | initiateJavaFileGeneration(file, className, BUILDER_INTERFACE_MASK, null, path); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 203 | List<String> methods = new ArrayList<>(); |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 204 | if (isAttrPresent) { |
| 205 | try { |
| 206 | /** |
| 207 | * Getter methods. |
| 208 | */ |
| 209 | methods.add(FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK, curNode)); |
| 210 | /** |
| 211 | * Setter methods. |
| 212 | */ |
| 213 | methods.add(NEW_LINE); |
| 214 | methods.add(FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(SETTER_FOR_INTERFACE_MASK, curNode)); |
| 215 | } catch (IOException e) { |
| 216 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 217 | + " while builder interface file generation"); |
| 218 | } |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 219 | } |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 220 | /** |
| 221 | * Add build method to builder interface file. |
| 222 | */ |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 223 | methods.add( |
| 224 | ((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles().addBuildMethodForInterface()); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * Add getters and setters in builder interface. |
| 228 | */ |
| 229 | for (String method : methods) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 230 | insertDataIntoJavaFile(file, method); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 231 | } |
| 232 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 233 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 234 | return file; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Returns generated builder class file for current node. |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 239 | * |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 240 | * @param file file |
| 241 | * @param imports imports for the file |
| 242 | * @param curNode current YANG node |
| 243 | * @param isAttrPresent if any attribute is present or not |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 244 | * @return builder class file |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 245 | * @throws IOException when fails to write in file |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 246 | */ |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 247 | public static File generateBuilderClassFile(File file, List<String> imports, YangNode curNode, |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 248 | boolean isAttrPresent) throws IOException { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 249 | |
| 250 | JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo(); |
| 251 | |
| 252 | String className = getCaptialCase(javaFileInfo.getJavaName()); |
| 253 | String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath(); |
| 254 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 255 | initiateJavaFileGeneration(file, className, BUILDER_CLASS_MASK, imports, path); |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 256 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 257 | List<String> methods = new ArrayList<>(); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 258 | |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 259 | if (isAttrPresent) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 260 | /** |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 261 | * Add attribute strings. |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 262 | */ |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 263 | try { |
| 264 | insertDataIntoJavaFile(file, |
| 265 | NEW_LINE + FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(ATTRIBUTES_MASK, curNode)); |
| 266 | } catch (IOException e) { |
| 267 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 268 | + " while builder class file generation"); |
| 269 | } |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 270 | |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 271 | try { |
| 272 | /** |
| 273 | * Getter methods. |
| 274 | */ |
| 275 | methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode)); |
| 276 | /** |
| 277 | * Setter methods. |
| 278 | */ |
| 279 | methods.add(getDataFromTempFileHandle(SETTER_FOR_CLASS_MASK, curNode) + NEW_LINE); |
| 280 | } catch (IOException e) { |
| 281 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 282 | + " while builder class file generation"); |
| 283 | } |
| 284 | } else { |
| 285 | insertDataIntoJavaFile(file, NEW_LINE); |
| 286 | } |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 287 | /** |
| 288 | * Add default constructor and build method impl. |
| 289 | */ |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 290 | methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles().addBuildMethodImpl()); |
| 291 | methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles() |
| 292 | .addDefaultConstructor(PUBLIC, BUILDER)); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 293 | |
| 294 | /** |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 295 | * Add methods in builder class. |
| 296 | */ |
| 297 | for (String method : methods) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 298 | insertDataIntoJavaFile(file, method); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 299 | } |
| 300 | return file; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Returns generated impl class file for current node. |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 305 | * |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 306 | * @param file file |
| 307 | * @param curNode current YANG node |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 308 | * @param isAttrPresent if any attribute is present or not |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 309 | * @return impl class file |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 310 | * @throws IOException when fails to write in file |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 311 | */ |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 312 | public static File generateImplClassFile(File file, YangNode curNode, boolean isAttrPresent) |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 313 | throws IOException { |
| 314 | |
| 315 | JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo(); |
| 316 | |
| 317 | String className = getCaptialCase(javaFileInfo.getJavaName()); |
| 318 | String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath(); |
| 319 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 320 | initiateJavaFileGeneration(file, className, IMPL_CLASS_MASK, null, path); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 321 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 322 | List<String> methods = new ArrayList<>(); |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 323 | if (isAttrPresent) { |
| 324 | /** |
| 325 | * Add attribute strings. |
| 326 | */ |
| 327 | try { |
| 328 | insertDataIntoJavaFile(file, |
| 329 | NEW_LINE + FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(ATTRIBUTES_MASK, curNode)); |
| 330 | } catch (IOException e) { |
| 331 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 332 | + " while impl class file generation"); |
| 333 | } |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 334 | |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 335 | /** |
| 336 | * Add attribute for augmented info's list. |
| 337 | */ |
| 338 | if (isHasAugmentationExtended(getExtendsList())) { |
| 339 | insertDataIntoJavaFile(file, getAugmentedInfoAttribute()); |
| 340 | } |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 341 | insertDataIntoJavaFile(file, NEW_LINE); |
| 342 | try { |
| 343 | /** |
| 344 | * Getter methods. |
| 345 | */ |
| 346 | methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode)); |
| 347 | |
| 348 | /** |
| 349 | * Hash code method. |
| 350 | */ |
| 351 | methods.add(getHashCodeMethodClose(getHashCodeMethodOpen() + partString( |
| 352 | getDataFromTempFileHandle(HASH_CODE_IMPL_MASK, curNode).replace(NEW_LINE, EMPTY_STRING)))); |
| 353 | /** |
| 354 | * Equals method. |
| 355 | */ |
| 356 | methods.add(getEqualsMethodClose( |
| 357 | getEqualsMethodOpen(className + IMPL) + getDataFromTempFileHandle(EQUALS_IMPL_MASK, curNode))); |
| 358 | /** |
| 359 | * To string method. |
| 360 | */ |
| 361 | methods.add(getToStringMethodOpen() + getDataFromTempFileHandle(TO_STRING_IMPL_MASK, curNode) |
| 362 | + getToStringMethodClose()); |
| 363 | |
| 364 | } catch (IOException e) { |
| 365 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 366 | + " while impl class file generation"); |
| 367 | } |
| 368 | } else { |
| 369 | insertDataIntoJavaFile(file, NEW_LINE); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 370 | } |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 371 | try { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 372 | /** |
| 373 | * Constructor. |
| 374 | */ |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 375 | methods.add(getConstructorStart(className) + getDataFromTempFileHandle(CONSTRUCTOR_IMPL_MASK, curNode) |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 376 | + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 377 | } catch (IOException e) { |
| 378 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 379 | + " while impl class file generation"); |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 380 | } |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 381 | |
| 382 | /** |
| 383 | * Add method for augment info's list. |
| 384 | */ |
| 385 | if (isHasAugmentationExtended(getExtendsList())) { |
| 386 | methods.add(getAddAugmentInfoMethodImpl()); |
| 387 | methods.add(getAugmentInfoListImpl()); |
| 388 | methods.add(getRemoveAugmentationImpl()); |
| 389 | } |
| 390 | |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 391 | /** |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 392 | * Add methods in impl class. |
| 393 | */ |
| 394 | for (String method : methods) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 395 | insertDataIntoJavaFile(file, FOUR_SPACE_INDENTATION + method + NEW_LINE); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 396 | } |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 397 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 398 | |
| 399 | return file; |
| 400 | } |
| 401 | |
| 402 | /** |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 403 | * Generates class file for type def. |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 404 | * |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 405 | * @param file generated file |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 406 | * @param curNode current YANG node |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 407 | * @param imports imports for file |
| 408 | * @return type def class file |
| 409 | * @throws IOException when fails to generate class file |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 410 | */ |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 411 | public static File generateTypeDefClassFile(File file, YangNode curNode, List<String> imports) throws IOException { |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 412 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 413 | JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo(); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 414 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 415 | String className = getCaptialCase(javaFileInfo.getJavaName()); |
| 416 | String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath(); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 417 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 418 | initiateJavaFileGeneration(file, className, GENERATE_TYPEDEF_CLASS, imports, path); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 419 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 420 | List<String> methods = new ArrayList<>(); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 421 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 422 | /** |
| 423 | * Add attribute strings. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 424 | */ |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 425 | try { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 426 | insertDataIntoJavaFile(file, |
| 427 | NEW_LINE + FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(ATTRIBUTES_MASK, curNode)); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 428 | } catch (IOException e) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 429 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 430 | + " while type def class file generation"); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 431 | } |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 432 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 433 | /** |
| 434 | * Default constructor. |
| 435 | */ |
| 436 | methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles() |
| 437 | .addDefaultConstructor(PRIVATE, EMPTY_STRING)); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 438 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 439 | try { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 440 | |
| 441 | /** |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 442 | * Type constructor. |
| 443 | */ |
| 444 | methods.add(getDataFromTempFileHandle(CONSTRUCTOR_FOR_TYPE_MASK, curNode)); |
| 445 | |
| 446 | /** |
| 447 | * Of method. |
| 448 | */ |
| 449 | methods.add(getDataFromTempFileHandle(OF_STRING_IMPL_MASK, curNode)); |
| 450 | |
| 451 | /** |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 452 | * Getter method. |
| 453 | */ |
| 454 | methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode)); |
| 455 | |
| 456 | /** |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 457 | * Hash code method. |
| 458 | */ |
| 459 | methods.add(getHashCodeMethodClose(getHashCodeMethodOpen() + partString( |
| 460 | getDataFromTempFileHandle(HASH_CODE_IMPL_MASK, curNode).replace(NEW_LINE, EMPTY_STRING)))); |
| 461 | |
| 462 | /** |
| 463 | * Equals method. |
| 464 | */ |
| 465 | methods.add(getEqualsMethodClose(getEqualsMethodOpen(className + EMPTY_STRING) |
| 466 | + getDataFromTempFileHandle(EQUALS_IMPL_MASK, curNode))); |
| 467 | |
| 468 | /** |
| 469 | * To string method. |
| 470 | */ |
| 471 | methods.add(getToStringMethodOpen() + getDataFromTempFileHandle(TO_STRING_IMPL_MASK, curNode) |
| 472 | + getToStringMethodClose()); |
| 473 | |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 474 | /** |
| 475 | * From string method. |
| 476 | */ |
| 477 | methods.add(getFromStringMethodSignature(className) |
| 478 | + getDataFromTempFileHandle(FROM_STRING_IMPL_MASK, curNode) |
| 479 | + getFromStringMethodClose()); |
| 480 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 481 | } catch (IOException e) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 482 | throw new IOException("No data found in temporary java code fragment files for " + className |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 483 | + " while type def class file generation"); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 484 | } |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 485 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 486 | for (String method : methods) { |
| 487 | insertDataIntoJavaFile(file, method); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 488 | } |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 489 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 490 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 491 | return file; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 492 | } |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 493 | |
| 494 | /** |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 495 | * Generates class file for union type. |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 496 | * |
| 497 | * @param file generated file |
| 498 | * @param curNode current YANG node |
| 499 | * @param imports imports for file |
| 500 | * @return type def class file |
| 501 | * @throws IOException when fails to generate class file |
| 502 | */ |
| 503 | public static File generateUnionClassFile(File file, YangNode curNode, List<String> imports) throws IOException { |
| 504 | |
| 505 | JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo(); |
| 506 | |
| 507 | String className = getCaptialCase(javaFileInfo.getJavaName()); |
| 508 | String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath(); |
| 509 | |
| 510 | initiateJavaFileGeneration(file, className, GENERATE_UNION_CLASS, imports, path); |
| 511 | |
| 512 | List<String> methods = new ArrayList<>(); |
| 513 | |
| 514 | /** |
| 515 | * Add attribute strings. |
| 516 | */ |
| 517 | try { |
| 518 | insertDataIntoJavaFile(file, |
| 519 | NEW_LINE + FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(ATTRIBUTES_MASK, curNode)); |
| 520 | } catch (IOException e) { |
| 521 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 522 | + " while union class file generation"); |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Default constructor. |
| 527 | */ |
| 528 | methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles() |
| 529 | .addDefaultConstructor(PRIVATE, EMPTY_STRING)); |
| 530 | |
| 531 | try { |
| 532 | |
| 533 | /** |
| 534 | * Type constructor. |
| 535 | */ |
| 536 | methods.add(getDataFromTempFileHandle(CONSTRUCTOR_FOR_TYPE_MASK, curNode)); |
| 537 | |
| 538 | /** |
| 539 | * Of string method. |
| 540 | */ |
| 541 | methods.add(getDataFromTempFileHandle(OF_STRING_IMPL_MASK, curNode)); |
| 542 | |
| 543 | /** |
| 544 | * Getter method. |
| 545 | */ |
| 546 | methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode)); |
| 547 | |
| 548 | /** |
| 549 | * Hash code method. |
| 550 | */ |
| 551 | methods.add(getHashCodeMethodClose(getHashCodeMethodOpen() + partString( |
| 552 | getDataFromTempFileHandle(HASH_CODE_IMPL_MASK, curNode).replace(NEW_LINE, EMPTY_STRING)))); |
| 553 | |
| 554 | /** |
| 555 | * Equals method. |
| 556 | */ |
| 557 | methods.add(getEqualsMethodClose(getEqualsMethodOpen(className + EMPTY_STRING) |
| 558 | + getDataFromTempFileHandle(EQUALS_IMPL_MASK, curNode))); |
| 559 | |
| 560 | /** |
| 561 | * To string method. |
| 562 | */ |
| 563 | methods.add(getToStringMethodOpen() + getOmitNullValueString() + |
| 564 | getDataFromTempFileHandle(TO_STRING_IMPL_MASK, curNode) + getToStringMethodClose()); |
| 565 | |
| 566 | /** |
| 567 | * From string method. |
| 568 | */ |
| 569 | methods.add(getFromStringMethodSignature(className) |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 570 | + getDataFromTempFileHandle(FROM_STRING_IMPL_MASK, curNode) |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 571 | + getFromStringMethodClose()); |
| 572 | |
| 573 | } catch (IOException e) { |
| 574 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 575 | + " while union class file generation"); |
| 576 | } |
| 577 | |
| 578 | for (String method : methods) { |
| 579 | insertDataIntoJavaFile(file, method); |
| 580 | } |
| 581 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE); |
| 582 | |
| 583 | return file; |
| 584 | } |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 585 | |
| 586 | /** |
| 587 | * Generates class file for type enum. |
| 588 | * |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 589 | * @param file generated file |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 590 | * @param curNode current YANG node |
| 591 | * @return class file for type enum |
| 592 | * @throws IOException when fails to generate class file |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 593 | */ |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 594 | public static File generateEnumClassFile(File file, YangNode curNode) throws IOException { |
| 595 | |
| 596 | JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo(); |
| 597 | |
| 598 | String className = getCaptialCase(javaFileInfo.getJavaName()); |
| 599 | String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath(); |
| 600 | |
| 601 | initiateJavaFileGeneration(file, className, GENERATE_ENUM_CLASS, null, path); |
| 602 | /** |
| 603 | * Add attribute strings. |
| 604 | */ |
| 605 | try { |
| 606 | insertDataIntoJavaFile(file, |
| 607 | trimAtLast(trimAtLast(getDataFromTempFileHandle(ENUM_IMPL_MASK, curNode), COMMA), NEW_LINE) |
| 608 | + SEMI_COLAN + NEW_LINE); |
| 609 | } catch (IOException e) { |
| 610 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 611 | + " while enum class file generation"); |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * Add an attribute to get the enum's values. |
| 616 | */ |
| 617 | insertDataIntoJavaFile(file, getEnumsValueAttribute(className)); |
| 618 | |
| 619 | /** |
| 620 | * Add a constructor for enum. |
| 621 | */ |
| 622 | insertDataIntoJavaFile(file, |
| 623 | getJavaDoc(TYPE_CONSTRUCTOR, getSmallCase(className), false) + getEnumsConstrcutor(className) |
| 624 | + NEW_LINE); |
| 625 | |
| 626 | /** |
| 627 | * Add a getter method for enum. |
| 628 | */ |
| 629 | insertDataIntoJavaFile(file, |
| 630 | getJavaDoc(GETTER_METHOD, getSmallCase(className), false) + getGetter(INT, getSmallCase(className)) |
| 631 | + NEW_LINE); |
| 632 | |
| 633 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE); |
| 634 | |
| 635 | return file; |
| 636 | } |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 637 | |
| 638 | /** |
| 639 | * Generates interface file for rpc. |
| 640 | * |
| 641 | * @param file generated file |
| 642 | * @param curNode current YANG node |
| 643 | * @param imports imports for file |
| 644 | * @return type def class file |
| 645 | * @throws IOException when fails to generate class file |
| 646 | */ |
| 647 | public static File generateRpcInterfaceFile(File file, YangNode curNode, List<String> imports) throws IOException { |
| 648 | |
| 649 | JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo(); |
| 650 | |
| 651 | String className = getCaptialCase(javaFileInfo.getJavaName()) + SERVICE_METHOD_STRING; |
| 652 | String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath(); |
| 653 | |
| 654 | initiateJavaFileGeneration(file, className, GENERATE_RPC_INTERFACE, imports, path); |
| 655 | |
| 656 | List<String> methods = new ArrayList<>(); |
| 657 | |
| 658 | try { |
| 659 | |
| 660 | /** |
| 661 | * Rpc methods |
| 662 | */ |
| 663 | methods.add(getDataFromTempFileHandle(RPC_IMPL_MASK, curNode)); |
| 664 | |
| 665 | } catch (IOException e) { |
| 666 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 667 | + " while rpc class file generation"); |
| 668 | } |
| 669 | |
| 670 | for (String method : methods) { |
| 671 | insertDataIntoJavaFile(file, method); |
| 672 | } |
| 673 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE); |
| 674 | |
| 675 | return file; |
| 676 | } |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 677 | } |