Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.translator.tojava; |
| 18 | |
| 19 | import java.io.File; |
| 20 | import java.io.IOException; |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 21 | import java.util.ArrayList; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 22 | import java.util.LinkedList; |
| 23 | import java.util.List; |
| 24 | import java.util.SortedSet; |
| 25 | import java.util.TreeSet; |
| 26 | |
| 27 | import org.onosproject.yangutils.datamodel.YangType; |
| 28 | import org.onosproject.yangutils.translator.CachedFileHandle; |
| 29 | import org.onosproject.yangutils.translator.GeneratedFileType; |
| 30 | import org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen; |
| 31 | import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax; |
| 32 | import org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator; |
| 33 | import org.onosproject.yangutils.utils.UtilConstants; |
| 34 | import org.onosproject.yangutils.utils.io.impl.FileSystemUtil; |
| 35 | import org.onosproject.yangutils.utils.io.impl.JavaDocGen; |
| 36 | import org.onosproject.yangutils.utils.io.impl.SerializedDataStore; |
| 37 | import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType; |
| 38 | import org.onosproject.yangutils.utils.io.impl.CopyrightHeader; |
| 39 | |
| 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | import org.slf4j.Logger; |
| 42 | |
| 43 | /** |
| 44 | * Maintain the information about the java file to be generated. |
| 45 | */ |
| 46 | public class CachedJavaFileHandle implements CachedFileHandle { |
| 47 | |
| 48 | private static final Logger log = getLogger(CachedJavaFileHandle.class); |
| 49 | |
| 50 | private static final int MAX_CACHABLE_ATTR = 64; |
| 51 | private static final String JAVA_FILE_EXTENSION = ".java"; |
| 52 | private static final String TEMP_FILE_EXTENSION = ".tmp"; |
| 53 | |
| 54 | /** |
| 55 | * The type(s) of java source file(s) to be generated when the cached file |
| 56 | * handle is closed. |
| 57 | */ |
| 58 | private GeneratedFileType genFileTypes; |
| 59 | |
| 60 | /** |
| 61 | * The type(s) of java method to be generated when the cached file handle is |
| 62 | * closed. |
| 63 | */ |
| 64 | private GeneratedMethodTypes genMethodTypes; |
| 65 | |
| 66 | /** |
| 67 | * Java package in which the class/interface needs to be generated. |
| 68 | */ |
| 69 | private String pkg; |
| 70 | |
| 71 | /** |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 72 | * Java package in which the child class/interface needs to be generated. |
| 73 | */ |
| 74 | private String childsPkg; |
| 75 | |
| 76 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 77 | * Name of the object in YANG file. |
| 78 | */ |
| 79 | private String yangName; |
| 80 | |
| 81 | /** |
| 82 | * Sorted set of import info, to be used to maintain the set of classes to |
| 83 | * be imported in the generated class. |
| 84 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 85 | private SortedSet<String> importSet; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Cached list of attribute info. |
| 89 | */ |
| 90 | private List<AttributeInfo> attributeList; |
| 91 | |
| 92 | /** |
| 93 | * Prevent invoking default constructor. |
| 94 | */ |
| 95 | private CachedJavaFileHandle() { |
| 96 | setCachedAttributeList(new LinkedList<AttributeInfo>()); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Create a cached file handle which takes care of adding attributes to the |
| 101 | * generated java file. |
| 102 | * |
| 103 | * @param pcg package in which class/interface need to be generated. |
| 104 | * @param yangName name of the attribute in YANG file. |
| 105 | * @param types the types of files that needs to be generated. |
| 106 | * @throws IOException file IO exception. |
| 107 | */ |
| 108 | public CachedJavaFileHandle(String pcg, String yangName, GeneratedFileType types) throws IOException { |
| 109 | if ((new File(pcg).exists())) { |
| 110 | setGeneratedFileTypes(types); |
| 111 | setPackage(pcg); |
| 112 | setYangName(yangName); |
| 113 | } else { |
| 114 | FileSystemUtil.createPackage(pcg, yangName); |
| 115 | setGeneratedFileTypes(types); |
| 116 | setPackage(pcg); |
| 117 | setYangName(yangName); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get the types of files being generated corresponding to the YANG |
| 123 | * definition. |
| 124 | * |
| 125 | * @return the types of files being generated corresponding to the YANG |
| 126 | * definition. |
| 127 | */ |
| 128 | public GeneratedFileType getGeneratedFileTypes() { |
| 129 | return genFileTypes; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Set the types of files being generated corresponding to the YANG |
| 134 | * definition. |
| 135 | * |
| 136 | * @param fileTypes the types of files being generated corresponding to the |
| 137 | * YANG definition. |
| 138 | */ |
| 139 | public void setGeneratedFileTypes(GeneratedFileType fileTypes) { |
| 140 | genFileTypes = fileTypes; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get the corresponding name defined in YANG. |
| 145 | * |
| 146 | * @return the corresponding name defined in YANG. |
| 147 | */ |
| 148 | public String getYangName() { |
| 149 | return yangName; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Set the corresponding name defined in YANG. |
| 154 | * |
| 155 | * @param yangName the corresponding name defined in YANG. |
| 156 | */ |
| 157 | public void setYangName(String yangName) { |
| 158 | this.yangName = yangName; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get the java package. |
| 163 | * |
| 164 | * @return the java package. |
| 165 | */ |
| 166 | public String getPackage() { |
| 167 | return pkg; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Set the java package. |
| 172 | * |
| 173 | * @param pcg the package to set |
| 174 | */ |
| 175 | public void setPackage(String pcg) { |
| 176 | pkg = pcg; |
| 177 | } |
| 178 | |
| 179 | /** |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 180 | * Get the java package. |
| 181 | * |
| 182 | * @return the java package. |
| 183 | */ |
| 184 | public String getChildsPackage() { |
| 185 | return childsPkg; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Set the java package. |
| 190 | * |
| 191 | * @param pcg the package to set |
| 192 | */ |
| 193 | @Override |
| 194 | public void setChildsPackage(String pcg) { |
| 195 | childsPkg = pcg; |
| 196 | } |
| 197 | |
| 198 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 199 | * Get the set containing the imported class/interface info. |
| 200 | * |
| 201 | * @return the set containing the imported class/interface info. |
| 202 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 203 | public SortedSet<String> getImportSet() { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 204 | return importSet; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Assign the set containing the imported class/interface info. |
| 209 | * |
| 210 | * @param importSet the set containing the imported class/interface info. |
| 211 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 212 | private void setImportSet(SortedSet<String> importSet) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 213 | this.importSet = importSet; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Add an imported class/interface info is it is not already part of the |
| 218 | * set. If already part of the set, return false, else add to set and return |
| 219 | * true. |
| 220 | * |
| 221 | * @param importInfo class/interface info being imported. |
| 222 | * @return status of new addition of class/interface to the import set |
| 223 | */ |
| 224 | public boolean addImportInfo(ImportInfo importInfo) { |
| 225 | /* |
| 226 | * implement the import info adding. The return value will be used to |
| 227 | * check if the qualified name will be used or class/interface name will |
| 228 | * be used in the generated class. |
| 229 | */ |
| 230 | if (getImportSet() == null) { |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 231 | setImportSet(new TreeSet<String>()); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 232 | } |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 233 | return getImportSet().add(JavaCodeSnippetGen.getImportText(importInfo)); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Get the list of cached attribute list. |
| 238 | * |
| 239 | * @return the set containing the imported class/interface info. |
| 240 | */ |
| 241 | public List<AttributeInfo> getCachedAttributeList() { |
| 242 | return attributeList; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Set the cached attribute list. |
| 247 | * |
| 248 | * @param attrList attribute list. |
| 249 | */ |
| 250 | private void setCachedAttributeList(List<AttributeInfo> attrList) { |
| 251 | attributeList = attrList; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Flush the cached attribute list to the serialized file. |
| 256 | */ |
| 257 | private void flushCacheAttrToSerFile() { |
| 258 | |
| 259 | for (AttributeInfo attr : getCachedAttributeList()) { |
| 260 | parseAttributeInfo(attr); |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * clear the contents from the cached attribute list. |
| 265 | */ |
| 266 | getCachedAttributeList().clear(); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Add a new attribute to the file(s). |
| 271 | * |
| 272 | * @param attrType data type of the added attribute. |
| 273 | * @param name name of the attribute. |
| 274 | * @param isListAttr if the current added attribute needs to be maintained |
| 275 | * in a list. |
| 276 | */ |
| 277 | @Override |
| 278 | public void addAttributeInfo(YangType<?> attrType, String name, boolean isListAttr) { |
| 279 | |
| 280 | AttributeInfo newAttr = new AttributeInfo(); |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 281 | if (attrType != null) { |
| 282 | attrType.setDataTypeName(attrType.getDataTypeName().replace("\"", "")); |
| 283 | if (attrType.getDataTypeName().equals("string")) { |
| 284 | attrType.setDataTypeName(JavaIdentifierSyntax.getCaptialCase(attrType.getDataTypeName())); |
| 285 | } |
| 286 | newAttr.setAttributeType(attrType); |
| 287 | } else { |
| 288 | ImportInfo importInfo = new ImportInfo(); |
| 289 | importInfo.setPkgInfo(getChildsPackage()); |
| 290 | importInfo.setClassInfo(JavaIdentifierSyntax.getCaptialCase(name)); |
| 291 | if (getImportSet() != null) { |
| 292 | getImportSet().add(JavaCodeSnippetGen.getImportText(importInfo)); |
| 293 | } else { |
| 294 | SortedSet<String> newImportInfo = new TreeSet<>(); |
| 295 | newImportInfo.add(JavaCodeSnippetGen.getImportText(importInfo)); |
| 296 | setImportSet(newImportInfo); |
| 297 | } |
| 298 | |
| 299 | newAttr.setQualifiedName(getQualifiedFlag(JavaCodeSnippetGen.getImportText(importInfo))); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 300 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 301 | newAttr.setAttributeName(name); |
| 302 | newAttr.setListAttr(isListAttr); |
| 303 | |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 304 | if (isListAttr) { |
| 305 | String listImport = UtilConstants.COLLECTION_IMPORTS + UtilConstants.LIST + UtilConstants.SEMI_COLAN |
| 306 | + UtilConstants.NEW_LINE + UtilConstants.NEW_LINE; |
| 307 | if (getImportSet() != null) { |
| 308 | getImportSet().add(listImport); |
| 309 | } else { |
| 310 | SortedSet<String> newImportInfo = new TreeSet<>(); |
| 311 | newImportInfo.add(listImport); |
| 312 | setImportSet(newImportInfo); |
| 313 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 314 | |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 315 | newAttr.setQualifiedName(getQualifiedFlag(listImport)); |
| 316 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 317 | |
| 318 | if (getCachedAttributeList() != null) { |
| 319 | if (getCachedAttributeList().size() == MAX_CACHABLE_ATTR) { |
| 320 | flushCacheAttrToSerFile(); |
| 321 | } |
| 322 | getCachedAttributeList().add(newAttr); |
| 323 | } else { |
| 324 | List<AttributeInfo> newAttributeInfo = new LinkedList<>(); |
| 325 | newAttributeInfo.add(newAttr); |
| 326 | setCachedAttributeList(newAttributeInfo); |
| 327 | } |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Check if the import set does not have a class info same as the new class |
| 332 | * info, if so the new class info be added to the import set. Otherwise |
| 333 | * check if the corresponding package info is same as the new package info, |
| 334 | * if so no need to qualified access, otherwise, it needs qualified access. |
| 335 | * |
| 336 | * @param newImportInfo new import info to be check for qualified access or |
| 337 | * not and updated in the import set accordingly. |
| 338 | * @return if the new attribute needs to be accessed in a qualified manner. |
| 339 | */ |
| 340 | private boolean getQualifiedFlag(String newImportInfo) { |
| 341 | for (String curImportInfo : getImportSet()) { |
| 342 | if (curImportInfo.equals(newImportInfo)) { |
| 343 | /* |
| 344 | * If import is already existing import with same package, we |
| 345 | * don't need qualified access, otherwise it needs to be |
| 346 | * qualified access. |
| 347 | */ |
| 348 | return !curImportInfo.equals(newImportInfo); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | getImportSet().add(newImportInfo); |
| 353 | return false; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Flushes the cached contents to the target file, frees used resources. |
| 358 | */ |
| 359 | @Override |
| 360 | public void close() throws IOException { |
| 361 | |
| 362 | String className = getYangName(); |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 363 | className = JavaIdentifierSyntax.getCaptialCase(className); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 364 | String packagePath = getPackage(); |
| 365 | String filePath = UtilConstants.YANG_GEN_DIR + packagePath.replace(".", "/"); |
| 366 | GeneratedFileType fileType = getGeneratedFileTypes(); |
| 367 | |
| 368 | /** |
| 369 | * Create interface file. |
| 370 | */ |
| 371 | String interfaceFileName = className + JAVA_FILE_EXTENSION; |
| 372 | File interfaceFile = new File(filePath + File.separator + interfaceFileName); |
| 373 | |
| 374 | /** |
| 375 | * Create temp builder interface file. |
| 376 | */ |
| 377 | String builderInterfaceFileName = interfaceFileName + TEMP_FILE_EXTENSION; |
| 378 | File builderInterfaceFile = new File(filePath + File.separator + builderInterfaceFileName); |
| 379 | |
| 380 | /** |
| 381 | * Create builder class file. |
| 382 | */ |
| 383 | String builderFileName = className + UtilConstants.BUILDER + JAVA_FILE_EXTENSION; |
| 384 | File builderFile = new File(filePath + File.separator + builderFileName); |
| 385 | MethodsGenerator.setBuilderClassName(className + UtilConstants.BUILDER); |
| 386 | |
| 387 | /** |
| 388 | * Create temp impl class file. |
| 389 | */ |
| 390 | |
| 391 | String implFileName = className + UtilConstants.IMPL + TEMP_FILE_EXTENSION; |
| 392 | File implTempFile = new File(filePath + File.separator + implFileName); |
| 393 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 394 | /* |
| 395 | * TODO: add the file header using |
| 396 | * JavaCodeSnippetGen.getFileHeaderComment |
| 397 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 398 | |
| 399 | List<String> attributes = new LinkedList<>(); |
| 400 | List<String> interfaceMethods = new LinkedList<>(); |
| 401 | List<String> builderInterfaceMethods = new LinkedList<>(); |
| 402 | List<String> builderClassMethods = new LinkedList<>(); |
| 403 | List<String> implClassMethods = new LinkedList<>(); |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 404 | List<String> imports = new LinkedList<>(); |
| 405 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 406 | try { |
| 407 | attributes = SerializedDataStore.getSerializeData(SerializedDataStore.SerializedDataStoreType.ATTRIBUTE); |
| 408 | |
| 409 | interfaceMethods = SerializedDataStore |
| 410 | .getSerializeData(SerializedDataStore.SerializedDataStoreType.INTERFACE_METHODS); |
| 411 | |
| 412 | builderInterfaceMethods = SerializedDataStore |
| 413 | .getSerializeData(SerializedDataStore.SerializedDataStoreType.BUILDER_INTERFACE_METHODS); |
| 414 | |
| 415 | builderClassMethods = SerializedDataStore |
| 416 | .getSerializeData(SerializedDataStore.SerializedDataStoreType.BUILDER_METHODS); |
| 417 | |
| 418 | implClassMethods = SerializedDataStore |
| 419 | .getSerializeData(SerializedDataStore.SerializedDataStoreType.IMPL_METHODS); |
| 420 | |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 421 | imports = SerializedDataStore.getSerializeData(SerializedDataStore.SerializedDataStoreType.IMPORT); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 422 | } catch (ClassNotFoundException | IOException e) { |
| 423 | log.info("There is no attribute info of " + className + " YANG file in the serialized files."); |
| 424 | } |
| 425 | |
| 426 | if (getCachedAttributeList() != null) { |
| 427 | MethodsGenerator.setAttrInfo(getCachedAttributeList()); |
| 428 | for (AttributeInfo attr : getCachedAttributeList()) { |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 429 | if (attr.isListAttr()) { |
| 430 | String listString = JavaCodeSnippetGen.getListAttribute(attr.getAttributeType().getDataTypeName()); |
| 431 | @SuppressWarnings("rawtypes") |
| 432 | YangType<?> type = new YangType(); |
| 433 | type.setDataTypeName(listString); |
| 434 | attr.setAttributeType(type); |
| 435 | } |
| 436 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 437 | attributes.add(getAttributeString(attr)); |
| 438 | |
| 439 | interfaceMethods.add(MethodsGenerator.getMethodString(attr, GeneratedFileType.INTERFACE)); |
| 440 | |
| 441 | builderClassMethods.add(MethodsGenerator.getMethodString(attr, GeneratedFileType.BUILDER_CLASS)); |
| 442 | |
| 443 | builderInterfaceMethods |
| 444 | .add(MethodsGenerator.getMethodString(attr, GeneratedFileType.BUILDER_INTERFACE)); |
| 445 | |
| 446 | implClassMethods.add(MethodsGenerator.getMethodString(attr, GeneratedFileType.IMPL)); |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 447 | |
| 448 | if (getImportSet() != null) { |
| 449 | imports = new ArrayList<>(getImportSet()); |
| 450 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | builderInterfaceMethods.add(MethodsGenerator.parseBuilderInterfaceBuildMethodString(className)); |
| 455 | builderClassMethods.add(UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE |
| 456 | + MethodsGenerator.getDefaultConstructorString(GeneratedFileType.BUILDER_CLASS, className)); |
| 457 | builderClassMethods.add(MethodsGenerator.getBuildString(className)); |
| 458 | |
| 459 | implClassMethods.add(UtilConstants.JAVA_DOC_FIRST_LINE |
| 460 | + MethodsGenerator.getDefaultConstructorString(GeneratedFileType.IMPL, className)); |
| 461 | implClassMethods.add(MethodsGenerator.getConstructorString(className)); |
| 462 | |
| 463 | /** |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 464 | * Start generation of files. |
| 465 | */ |
| 466 | if (fileType.equals(GeneratedFileType.INTERFACE) || fileType.equals(GeneratedFileType.ALL)) { |
| 467 | initiateFile(interfaceFile, className, GeneratedFileType.INTERFACE, imports); |
| 468 | } |
| 469 | |
| 470 | if (fileType.equals(GeneratedFileType.BUILDER_CLASS) || fileType.equals(GeneratedFileType.ALL)) { |
| 471 | initiateFile(builderFile, className, GeneratedFileType.BUILDER_CLASS, imports); |
| 472 | } |
| 473 | |
| 474 | if (fileType.equals(GeneratedFileType.IMPL) || fileType.equals(GeneratedFileType.ALL)) { |
| 475 | initiateFile(implTempFile, className, GeneratedFileType.IMPL, imports); |
| 476 | } |
| 477 | |
| 478 | if (fileType.equals(GeneratedFileType.BUILDER_INTERFACE) || fileType.equals(GeneratedFileType.ALL)) { |
| 479 | initiateFile(builderInterfaceFile, className, GeneratedFileType.BUILDER_INTERFACE, imports); |
| 480 | } |
| 481 | |
| 482 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 483 | * Add attributes to the file. |
| 484 | */ |
| 485 | for (String attribute : attributes) { |
| 486 | insert(builderFile, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute); |
| 487 | insert(implTempFile, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute); |
| 488 | } |
| 489 | |
| 490 | insert(builderFile, UtilConstants.NEW_LINE); |
| 491 | insert(implTempFile, UtilConstants.NEW_LINE); |
| 492 | |
| 493 | /** |
| 494 | * Add getter methods to interface file. |
| 495 | */ |
| 496 | for (String method : interfaceMethods) { |
| 497 | appendMethod(interfaceFile, method + UtilConstants.NEW_LINE); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Add getters and setters in builder interface. |
| 502 | */ |
| 503 | for (String method : builderInterfaceMethods) { |
| 504 | appendMethod(builderInterfaceFile, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE); |
| 505 | } |
| 506 | |
| 507 | insert(builderInterfaceFile, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE); |
| 508 | /** |
| 509 | * Add methods in builder class. |
| 510 | */ |
| 511 | for (String method : builderClassMethods) { |
| 512 | appendMethod(builderFile, method + UtilConstants.NEW_LINE); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Add methods in impl class. |
| 517 | */ |
| 518 | for (String method : implClassMethods) { |
| 519 | appendMethod(implTempFile, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE); |
| 520 | } |
| 521 | |
| 522 | insert(implTempFile, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE); |
| 523 | |
| 524 | /** |
| 525 | * Append builder interface file to interface file and close it. |
| 526 | */ |
| 527 | appendFileContents(builderInterfaceFile, interfaceFile); |
| 528 | insert(interfaceFile, closeFile(GeneratedFileType.INTERFACE, interfaceFileName)); |
| 529 | |
| 530 | /** |
| 531 | * Append impl class to builder class and close it. |
| 532 | */ |
| 533 | appendFileContents(implTempFile, builderFile); |
| 534 | insert(builderFile, closeFile(GeneratedFileType.BUILDER_CLASS, builderFileName)); |
| 535 | |
| 536 | /** |
| 537 | * Remove temp files. |
| 538 | */ |
| 539 | clean(implTempFile); |
| 540 | clean(builderInterfaceFile); |
| 541 | } |
| 542 | |
| 543 | /** |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 544 | * Initiate generation of file based on generated file type. |
| 545 | * |
| 546 | * @param file generated file |
| 547 | * @param className generated file class name |
| 548 | * @param type generated file type |
| 549 | * @param imports imports for the file |
| 550 | * @throws IOException when fails to generate a file |
| 551 | */ |
| 552 | private void initiateFile(File file, String className, GeneratedFileType type, List<String> imports) |
| 553 | throws IOException { |
| 554 | try { |
| 555 | file.createNewFile(); |
| 556 | appendContents(file, className, type, imports); |
| 557 | } catch (IOException e) { |
| 558 | throw new IOException("Failed to create " + file.getName() + " class file."); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 563 | * Appends the temp files to main files. |
| 564 | * |
| 565 | * @param appendFile temp file |
| 566 | * @param srcFile main file |
| 567 | */ |
| 568 | private static void appendFileContents(File appendFile, File srcFile) throws IOException { |
| 569 | try { |
| 570 | FileSystemUtil.appendFileContents(appendFile, srcFile); |
| 571 | } catch (IOException e) { |
| 572 | throw new IOException("Failed to append " + appendFile + " in " + srcFile); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * Append methods to the generated files. |
| 578 | * |
| 579 | * @param file file in which method needs to be appended. |
| 580 | * @param method method which needs to be appended. |
| 581 | */ |
| 582 | private static void appendMethod(File file, String method) throws IOException { |
| 583 | insert(file, method); |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Closes the current generated file. |
| 588 | * |
| 589 | * @param fileType generate file type |
| 590 | * @param yangName file name |
| 591 | * @return end of class definition string. |
| 592 | */ |
| 593 | private static String closeFile(GeneratedFileType fileType, String yangName) { |
| 594 | return JavaCodeSnippetGen.getJavaClassDefClose(fileType, yangName); |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Parses attribute info and fetch specific data and creates serialized |
| 599 | * files of it. |
| 600 | * |
| 601 | * @param attr attribute info. |
| 602 | */ |
| 603 | private void parseAttributeInfo(AttributeInfo attr) { |
| 604 | |
| 605 | String attrString = ""; |
| 606 | String methodString = ""; |
| 607 | String getterString = ""; |
| 608 | |
| 609 | try { |
| 610 | /* |
| 611 | * Serialize attributes. |
| 612 | */ |
| 613 | attrString = getAttributeString(attr); |
| 614 | attrString = attrString.replace("\"", ""); |
| 615 | SerializedDataStore.setSerializeData(attrString, SerializedDataStore.SerializedDataStoreType.ATTRIBUTE); |
| 616 | |
| 617 | if (getGeneratedFileTypes().equals(GeneratedFileType.ALL)) { |
| 618 | |
| 619 | methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.INTERFACE); |
| 620 | SerializedDataStore.setSerializeData(methodString, |
| 621 | SerializedDataStore.SerializedDataStoreType.INTERFACE_METHODS); |
| 622 | |
| 623 | methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.BUILDER_CLASS); |
| 624 | SerializedDataStore.setSerializeData(methodString, |
| 625 | SerializedDataStore.SerializedDataStoreType.BUILDER_METHODS); |
| 626 | |
| 627 | methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.BUILDER_INTERFACE); |
| 628 | SerializedDataStore.setSerializeData(methodString, |
| 629 | SerializedDataStore.SerializedDataStoreType.BUILDER_INTERFACE_METHODS); |
| 630 | |
| 631 | methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.IMPL); |
| 632 | SerializedDataStore.setSerializeData(methodString, |
| 633 | SerializedDataStore.SerializedDataStoreType.IMPL_METHODS); |
| 634 | |
| 635 | } else if (getGeneratedFileTypes().equals(GeneratedFileType.INTERFACE)) { |
| 636 | |
| 637 | getterString = MethodsGenerator.getGetterString(attr); |
| 638 | SerializedDataStore.setSerializeData(methodString, |
| 639 | SerializedDataStore.SerializedDataStoreType.INTERFACE_METHODS); |
| 640 | } |
| 641 | } catch (IOException e) { |
| 642 | log.info("Failed to get data for " + attr.getAttributeName() + " from serialized files."); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * Returns attribute string. |
| 648 | * |
| 649 | * @param attr attribute info |
| 650 | * @return attribute string |
| 651 | */ |
| 652 | private String getAttributeString(AttributeInfo attr) { |
| 653 | return JavaCodeSnippetGen.getJavaAttributeInfo(getGeneratedFileTypes(), attr.getAttributeName(), |
| 654 | attr.getAttributeType()); |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Appends all the contents into a generated java file. |
| 659 | * |
| 660 | * @param file generated file |
| 661 | * @param fileName generated file name |
| 662 | * @param type generated file type |
| 663 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 664 | private void appendContents(File file, String fileName, GeneratedFileType type, List<String> importsList) |
| 665 | throws IOException { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 666 | |
| 667 | if (type.equals(GeneratedFileType.IMPL)) { |
| 668 | |
| 669 | write(file, fileName, type, JavaDocType.IMPL_CLASS); |
| 670 | } else if (type.equals(GeneratedFileType.BUILDER_INTERFACE)) { |
| 671 | |
| 672 | write(file, fileName, type, JavaDocType.BUILDER_INTERFACE); |
| 673 | } else { |
| 674 | |
| 675 | // TODO: handle imports for attributes. |
| 676 | |
| 677 | if (type.equals(GeneratedFileType.INTERFACE)) { |
| 678 | insert(file, CopyrightHeader.getCopyrightHeader()); |
| 679 | insert(file, "package" + UtilConstants.SPACE + getPackage() + UtilConstants.SEMI_COLAN |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 680 | + UtilConstants.NEW_LINE + UtilConstants.NEW_LINE); |
| 681 | for (String imports : importsList) { |
| 682 | insert(file, imports); |
| 683 | } |
| 684 | insert(file, UtilConstants.NEW_LINE); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 685 | write(file, fileName, type, JavaDocType.INTERFACE); |
| 686 | } else if (type.equals(GeneratedFileType.BUILDER_CLASS)) { |
| 687 | insert(file, CopyrightHeader.getCopyrightHeader()); |
| 688 | insert(file, "package" + UtilConstants.SPACE + getPackage() + UtilConstants.SEMI_COLAN |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 689 | + UtilConstants.NEW_LINE + UtilConstants.NEW_LINE); |
| 690 | for (String imports : importsList) { |
| 691 | insert(file, imports); |
| 692 | } |
| 693 | insert(file, UtilConstants.NEW_LINE); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 694 | write(file, fileName, type, JavaDocType.BUILDER_CLASS); |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * Write data to the specific generated file. |
| 701 | * |
| 702 | * @param file generated file |
| 703 | * @param fileName file name |
| 704 | * @param genType generated file type |
| 705 | * @param javaDocType java doc type |
| 706 | */ |
| 707 | private static void write(File file, String fileName, GeneratedFileType genType, JavaDocGen.JavaDocType javaDocType) |
| 708 | throws IOException { |
| 709 | |
| 710 | insert(file, JavaDocGen.getJavaDoc(javaDocType, fileName)); |
| 711 | insert(file, JavaCodeSnippetGen.getJavaClassDefStart(genType, fileName)); |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * Insert in the generated file. |
| 716 | * |
| 717 | * @param file file in which need to be inserted. |
| 718 | * @param data data which need to be inserted. |
| 719 | */ |
| 720 | private static void insert(File file, String data) throws IOException { |
| 721 | try { |
| 722 | FileSystemUtil.insertStringInFile(file, data); |
| 723 | } catch (IOException e) { |
| 724 | throw new IOException("Failed to insert in " + file + "file"); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * Removes temp files. |
| 730 | * |
| 731 | * @param file file to be removed. |
| 732 | */ |
| 733 | private static void clean(File file) { |
| 734 | if (file.exists()) { |
| 735 | file.delete(); |
| 736 | } |
| 737 | } |
| 738 | } |