Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
| 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; |
| 23 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 24 | import org.apache.commons.io.FileUtils; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangNode; |
| 26 | import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo; |
| 27 | import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles; |
| 28 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
| 29 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.utils.UtilConstants; |
| 31 | import org.onosproject.yangutils.utils.io.impl.CopyrightHeader; |
| 32 | import org.onosproject.yangutils.utils.io.impl.FileSystemUtil; |
| 33 | import org.onosproject.yangutils.utils.io.impl.JavaDocGen; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 35 | |
| 36 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK; |
| 37 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK; |
| 38 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS; |
| 39 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK; |
| 40 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK; |
| 41 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK; |
| 42 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK; |
| 43 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK; |
| 44 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK; |
| 45 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK; |
| 46 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK; |
| 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; |
| 50 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase; |
| 51 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString; |
| 52 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructorStart; |
| 53 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString; |
| 54 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethodClose; |
| 55 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethodOpen; |
| 56 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodClose; |
| 57 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodOpen; |
| 58 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethodClose; |
| 59 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethodOpen; |
| 60 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString; |
| 61 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.partString; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 62 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 63 | /** |
| 64 | * Generates java file. |
| 65 | */ |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 66 | public final class JavaFileGenerator { |
| 67 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 68 | /** |
| 69 | * Default constructor. |
| 70 | */ |
| 71 | private JavaFileGenerator() { |
| 72 | } |
| 73 | |
| 74 | /** |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 75 | * Returns a file object for generated file. |
| 76 | * |
| 77 | * @param fileName file name |
| 78 | * @param filePath file package path |
| 79 | * @param extension file extension |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 80 | * @param handle cached file handle |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 81 | * @return file object |
| 82 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 83 | public static File getFileObject(String filePath, String fileName, String extension, JavaFileInfo handle) { |
| 84 | |
| 85 | return new File(handle.getBaseCodeGenPath() + filePath + File.separator + fileName + extension); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 89 | * Returns generated interface file for current node. |
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 | * @param file file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 92 | * @param imports imports for the file |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 93 | * @param curNode current YANG node |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 94 | * @return interface file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 95 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 96 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 97 | public static File generateInterfaceFile(File file, List<String> imports, YangNode curNode) throws IOException { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 98 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 99 | JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo(); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 100 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 101 | String className = getCaptialCase(javaFileInfo.getJavaName()); |
| 102 | String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath(); |
| 103 | |
| 104 | initiateFile(file, className, INTERFACE_MASK, imports, path); |
| 105 | |
| 106 | /** |
| 107 | * Add getter methods to interface file. |
| 108 | */ |
| 109 | try { |
| 110 | appendMethod(file, getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK, curNode)); |
| 111 | } catch (IOException e) { |
| 112 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 113 | + " while interface file generation"); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 114 | } |
| 115 | return file; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Return generated builder interface file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 120 | * |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 121 | * @param file file |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 122 | * @param curNode current YANG node |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 123 | * @return builder interface file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 124 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 125 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 126 | public static File generateBuilderInterfaceFile(File file, YangNode curNode) throws IOException { |
| 127 | |
| 128 | JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo(); |
| 129 | |
| 130 | String className = getCaptialCase(javaFileInfo.getJavaName()); |
| 131 | String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath(); |
| 132 | |
| 133 | initiateFile(file, className, BUILDER_INTERFACE_MASK, null, path); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 134 | List<String> methods = new ArrayList<>(); |
| 135 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 136 | try { |
| 137 | methods.add(UtilConstants.FOUR_SPACE_INDENTATION |
| 138 | + getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK, curNode)); |
| 139 | methods.add(UtilConstants.NEW_LINE); |
| 140 | methods.add(UtilConstants.FOUR_SPACE_INDENTATION |
| 141 | + getDataFromTempFileHandle(SETTER_FOR_INTERFACE_MASK, curNode)); |
| 142 | } catch (IOException e) { |
| 143 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 144 | + " while builder interface file generation"); |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 145 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 146 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 147 | /** |
| 148 | * Add build method to builder interface file. |
| 149 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 150 | methods.add(parseBuilderInterfaceBuildMethodString(className)); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * Add getters and setters in builder interface. |
| 154 | */ |
| 155 | for (String method : methods) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 156 | appendMethod(file, method); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE); |
| 160 | return file; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Returns generated builder class file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 165 | * |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 166 | * @param file file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 167 | * @param imports imports for the file |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 168 | * @param curNode current YANG node |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 169 | * @return builder class file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 170 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 171 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 172 | public static File generateBuilderClassFile(File file, List<String> imports, YangNode curNode) throws IOException { |
| 173 | |
| 174 | JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo(); |
| 175 | |
| 176 | String className = getCaptialCase(javaFileInfo.getJavaName()); |
| 177 | String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath(); |
| 178 | |
| 179 | initiateFile(file, className, BUILDER_CLASS_MASK, imports, path); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 180 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 181 | List<String> methods = new ArrayList<>(); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 182 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 183 | /** |
| 184 | * Add attribute strings. |
| 185 | */ |
| 186 | try { |
| 187 | insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION |
| 188 | + getDataFromTempFileHandle(ATTRIBUTES_MASK, curNode)); |
| 189 | } catch (IOException e) { |
| 190 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 191 | + " while builder class file generation"); |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 192 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 193 | |
| 194 | try { |
| 195 | methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode)); |
| 196 | methods.add(getDataFromTempFileHandle(SETTER_FOR_CLASS_MASK, curNode) + UtilConstants.NEW_LINE); |
| 197 | } catch (IOException e) { |
| 198 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 199 | + " while builder class file generation"); |
| 200 | } |
| 201 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 202 | /** |
| 203 | * Add default constructor and build method impl. |
| 204 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 205 | methods.add(getBuildString(className) + UtilConstants.NEW_LINE); |
| 206 | methods.add(UtilConstants.NEW_LINE |
| 207 | + getDefaultConstructorString(className + UtilConstants.BUILDER, UtilConstants.PUBLIC)); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 208 | |
| 209 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 210 | * Add methods in builder class. |
| 211 | */ |
| 212 | for (String method : methods) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 213 | appendMethod(file, method); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 214 | } |
| 215 | return file; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Returns generated impl class file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 220 | * |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 221 | * @param file file |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 222 | * @param curNode current YANG node |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 223 | * @return impl class file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 224 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 225 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 226 | public static File generateImplClassFile(File file, YangNode curNode) |
| 227 | throws IOException { |
| 228 | |
| 229 | JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo(); |
| 230 | |
| 231 | String className = getCaptialCase(javaFileInfo.getJavaName()); |
| 232 | String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath(); |
| 233 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 234 | initiateFile(file, className, IMPL_CLASS_MASK, null, path); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 235 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 236 | List<String> methods = new ArrayList<>(); |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 237 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 238 | /** |
| 239 | * Add attribute strings. |
| 240 | */ |
| 241 | try { |
| 242 | insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION |
| 243 | + getDataFromTempFileHandle(ATTRIBUTES_MASK, curNode)); |
| 244 | } catch (IOException e) { |
| 245 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 246 | + " while impl class file generation"); |
| 247 | } |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 248 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 249 | insert(file, UtilConstants.NEW_LINE); |
| 250 | try { |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 251 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 252 | methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode)); |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 253 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 254 | methods.add(getHashCodeMethodClose(getHashCodeMethodOpen() + partString( |
| 255 | getDataFromTempFileHandle(HASH_CODE_IMPL_MASK, curNode).replace(UtilConstants.NEW_LINE, |
| 256 | UtilConstants.EMPTY_STRING)))); |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 257 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 258 | methods.add(getEqualsMethodClose(getEqualsMethodOpen(className + UtilConstants.IMPL) |
| 259 | + getDataFromTempFileHandle(EQUALS_IMPL_MASK, curNode))); |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 260 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 261 | methods.add(getToStringMethodOpen() + getDataFromTempFileHandle(TO_STRING_IMPL_MASK, curNode) |
| 262 | + getToStringMethodClose()); |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 263 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 264 | } catch (IOException e) { |
| 265 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 266 | + " while impl class file generation"); |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 267 | } |
| 268 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 269 | try { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 270 | methods.add(getConstructorStart(className) + getDataFromTempFileHandle(CONSTRUCTOR_IMPL_MASK, curNode) |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 271 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 272 | } catch (IOException e) { |
| 273 | throw new IOException("No data found in temporary java code fragment files for " + className |
| 274 | + " while impl class file generation"); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 275 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 276 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 277 | * Add methods in impl class. |
| 278 | */ |
| 279 | for (String method : methods) { |
| 280 | appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE); |
| 281 | } |
| 282 | insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE); |
| 283 | |
| 284 | return file; |
| 285 | } |
| 286 | |
| 287 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 288 | * Return data stored in temporary files. |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 289 | * |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 290 | * @param curNode current YANG node |
| 291 | * @param generatedTempFiles mask for the types of files being generated |
| 292 | * @return data stored in temporary files |
| 293 | * @throws IOException when failed to get the data from temporary file |
| 294 | * handle |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 295 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 296 | private static String getDataFromTempFileHandle(int generatedTempFiles, YangNode curNode) throws IOException { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 297 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 298 | TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles = ((HasTempJavaCodeFragmentFiles) curNode) |
| 299 | .getTempJavaCodeFragmentFiles(); |
| 300 | |
| 301 | if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) { |
| 302 | return tempJavaCodeFragmentFiles |
| 303 | .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getAttributesTempFileHandle()); |
| 304 | } else if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) { |
| 305 | return tempJavaCodeFragmentFiles |
| 306 | .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getGetterInterfaceTempFileHandle()); |
| 307 | } else if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) { |
| 308 | return tempJavaCodeFragmentFiles |
| 309 | .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getSetterInterfaceTempFileHandle()); |
| 310 | } else if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) { |
| 311 | return tempJavaCodeFragmentFiles |
| 312 | .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getGetterImplTempFileHandle()); |
| 313 | } else if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) { |
| 314 | return tempJavaCodeFragmentFiles |
| 315 | .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getSetterImplTempFileHandle()); |
| 316 | } else if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) { |
| 317 | return tempJavaCodeFragmentFiles |
| 318 | .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getConstructorImplTempFileHandle()); |
| 319 | } else if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) { |
| 320 | return tempJavaCodeFragmentFiles |
| 321 | .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getHashCodeImplTempFileHandle()); |
| 322 | } else if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) { |
| 323 | return tempJavaCodeFragmentFiles |
| 324 | .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getEqualsImplTempFileHandle()); |
| 325 | } else if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) { |
| 326 | return tempJavaCodeFragmentFiles |
| 327 | .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getToStringImplTempFileHandle()); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 328 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 329 | return null; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 333 | * Initiate generation of file based on generated file type. |
| 334 | * |
| 335 | * @param file generated file |
| 336 | * @param className generated file class name |
| 337 | * @param type generated file type |
| 338 | * @param imports imports for the file |
| 339 | * @param pkg generated file package |
| 340 | * @throws IOException when fails to generate a file |
| 341 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 342 | private static void initiateFile(File file, String className, int type, List<String> imports, |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 343 | String pkg) throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 344 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 345 | try { |
| 346 | file.createNewFile(); |
| 347 | appendContents(file, className, type, imports, pkg); |
| 348 | } catch (IOException e) { |
| 349 | throw new IOException("Failed to create " + file.getName() + " class file."); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Appends the temp files to main files. |
| 355 | * |
| 356 | * @param appendFile temp file |
| 357 | * @param srcFile main file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 358 | * @throws IOException when fails to append contents |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 359 | */ |
| 360 | public static void appendFileContents(File appendFile, File srcFile) throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 361 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 362 | try { |
| 363 | FileSystemUtil.appendFileContents(appendFile, srcFile); |
| 364 | } catch (IOException e) { |
| 365 | throw new IOException("Failed to append " + appendFile + " in " + srcFile); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Append methods to the generated files. |
| 371 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 372 | * @param file file in which method needs to be appended |
| 373 | * @param method method which needs to be appended |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 374 | * @throws IOException IO operation failure |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 375 | */ |
| 376 | private static void appendMethod(File file, String method) throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 377 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 378 | insert(file, method); |
| 379 | } |
| 380 | |
| 381 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 382 | * Appends all the contents into a generated java file. |
| 383 | * |
| 384 | * @param file generated file |
| 385 | * @param fileName generated file name |
| 386 | * @param type generated file type |
| 387 | * @param pkg generated file package |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 388 | * @param importsList list of java imports. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 389 | * @throws IOException when fails to append contents |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 390 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 391 | private static void appendContents(File file, String fileName, int type, List<String> importsList, |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 392 | String pkg) throws IOException { |
| 393 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 394 | String pkgString = parsePackageString(pkg, importsList); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 395 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 396 | if ((type & IMPL_CLASS_MASK) != 0) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 397 | |
| 398 | write(file, fileName, type, JavaDocType.IMPL_CLASS); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 399 | } else if ((type & BUILDER_INTERFACE_MASK) != 0) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 400 | |
| 401 | write(file, fileName, type, JavaDocType.BUILDER_INTERFACE); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 402 | } else if ((type & GENERATE_TYPEDEF_CLASS) != 0) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 403 | appendHeaderContents(file, pkgString, importsList); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 404 | write(file, fileName, type, JavaDocType.IMPL_CLASS); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 405 | } else if ((type & INTERFACE_MASK) != 0) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 406 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 407 | appendHeaderContents(file, pkgString, importsList); |
| 408 | write(file, fileName, type, JavaDocType.INTERFACE); |
| 409 | } else if ((type & BUILDER_CLASS_MASK) != 0) { |
| 410 | |
| 411 | appendHeaderContents(file, pkgString, importsList); |
| 412 | write(file, fileName, type, JavaDocType.BUILDER_CLASS); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Removes base directory path from package and generates package string for |
| 418 | * file. |
| 419 | * |
| 420 | * @param pkg generated package |
| 421 | * @param importsList list of imports |
| 422 | * @return package string |
| 423 | */ |
| 424 | private static String parsePackageString(String pkg, List<String> importsList) { |
| 425 | |
| 426 | if (pkg.contains(UtilConstants.ORG)) { |
| 427 | String[] strArray = pkg.split(UtilConstants.ORG); |
| 428 | pkg = UtilConstants.ORG + strArray[1].replace(UtilConstants.SLASH, UtilConstants.PERIOD); |
| 429 | } |
| 430 | if (importsList != null) { |
| 431 | if (!importsList.isEmpty()) { |
| 432 | return UtilConstants.PACKAGE + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN |
| 433 | + UtilConstants.NEW_LINE; |
| 434 | } else { |
| 435 | return UtilConstants.PACKAGE + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN; |
| 436 | } |
| 437 | } else { |
| 438 | return UtilConstants.PACKAGE + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Appends other contents to interface, builder and typedef classes. for |
| 444 | * example : ONOS copyright, imports and package. |
| 445 | * |
| 446 | * @param file generated file |
| 447 | * @param pkg generated package |
| 448 | * @param importsList list of imports |
| 449 | * @throws IOException when fails to append contents. |
| 450 | */ |
| 451 | private static void appendHeaderContents(File file, String pkg, List<String> importsList) throws IOException { |
| 452 | |
| 453 | insert(file, CopyrightHeader.getCopyrightHeader()); |
| 454 | insert(file, pkg); |
| 455 | |
| 456 | /* |
| 457 | * TODO: add the file header using |
| 458 | * JavaCodeSnippetGen.getFileHeaderComment |
| 459 | */ |
| 460 | |
| 461 | if (importsList != null) { |
| 462 | insert(file, UtilConstants.NEW_LINE); |
| 463 | for (String imports : importsList) { |
| 464 | insert(file, imports); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Write data to the specific generated file. |
| 471 | * |
| 472 | * @param file generated file |
| 473 | * @param fileName file name |
| 474 | * @param genType generated file type |
| 475 | * @param javaDocType java doc type |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 476 | * @throws IOException when fails to write into a file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 477 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 478 | private static void write(File file, String fileName, int genType, JavaDocGen.JavaDocType javaDocType) |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 479 | throws IOException { |
| 480 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 481 | insert(file, JavaDocGen.getJavaDoc(javaDocType, fileName, false)); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 482 | insert(file, JavaCodeSnippetGen.getJavaClassDefStart(genType, fileName)); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Insert in the generated file. |
| 487 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 488 | * @param file file in which need to be inserted |
| 489 | * @param data data which need to be inserted |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 490 | * @throws IOException when fails to insert into file |
| 491 | */ |
| 492 | public static void insert(File file, String data) throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 493 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 494 | try { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 495 | FileSystemUtil.updateFileHandle(file, data, false); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 496 | } catch (IOException e) { |
| 497 | throw new IOException("Failed to insert in " + file + "file"); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 502 | * Closes the files handle for generate files. |
| 503 | * |
| 504 | * @param file generate files |
| 505 | * @throws IOException when failed to close the file handle |
| 506 | */ |
| 507 | public static void closeFileHandles(File file) throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 508 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 509 | try { |
| 510 | FileSystemUtil.updateFileHandle(file, null, true); |
| 511 | } catch (IOException e) { |
| 512 | throw new IOException("Failed to close file handle for " + file + "file"); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 517 | * Removes temp files. |
| 518 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 519 | * @param file file to be removed |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 520 | */ |
| 521 | public static void clean(File file) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 522 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 523 | if (file.exists()) { |
| 524 | file.delete(); |
| 525 | } |
| 526 | } |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 527 | |
| 528 | /** |
| 529 | * Removes temp files. |
| 530 | * |
| 531 | * @param tempDir temp directory |
| 532 | * @throws IOException when fails to delete the directory |
| 533 | */ |
| 534 | public static void cleanTempFiles(File tempDir) throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 535 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 536 | FileUtils.deleteDirectory(tempDir); |
| 537 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 538 | } |