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