Committed by
Gerrit Code Review
[ONOS-4144] ,[ONOS-4145] Bug fixes for YANG translator.
Change-Id: I6922c01269611b3aec61a9e46cda5166f8fe1ec0
Showing
11 changed files
with
102 additions
and
265 deletions
| ... | @@ -333,6 +333,7 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable { | ... | @@ -333,6 +333,7 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable { |
| 333 | throw new IOException("Failed to create the source files."); | 333 | throw new IOException("Failed to create the source files."); |
| 334 | } | 334 | } |
| 335 | setFileHandle(handle); | 335 | setFileHandle(handle); |
| 336 | + getDerivedType().getDataTypeExtendedInfo().getBaseType().setJavaPackage(getPackage()); | ||
| 336 | addAttributeInfo(); | 337 | addAttributeInfo(); |
| 337 | addAttributeInParent(); | 338 | addAttributeInParent(); |
| 338 | } | 339 | } | ... | ... |
| ... | @@ -17,7 +17,6 @@ | ... | @@ -17,7 +17,6 @@ |
| 17 | package org.onosproject.yangutils.plugin.manager; | 17 | package org.onosproject.yangutils.plugin.manager; |
| 18 | 18 | ||
| 19 | import java.io.File; | 19 | import java.io.File; |
| 20 | -import java.io.IOException; | ||
| 21 | import java.util.Iterator; | 20 | import java.util.Iterator; |
| 22 | import java.util.List; | 21 | import java.util.List; |
| 23 | 22 | ||
| ... | @@ -56,6 +55,12 @@ public class YangUtilManager extends AbstractMojo { | ... | @@ -56,6 +55,12 @@ public class YangUtilManager extends AbstractMojo { |
| 56 | private String yangFilesDir; | 55 | private String yangFilesDir; |
| 57 | 56 | ||
| 58 | /** | 57 | /** |
| 58 | + * Source directory for generated files. | ||
| 59 | + */ | ||
| 60 | + @Parameter(property = "genFilesDir", defaultValue = "src/main/java") | ||
| 61 | + private String genFilesDir; | ||
| 62 | + | ||
| 63 | + /** | ||
| 59 | * Base directory for project. | 64 | * Base directory for project. |
| 60 | */ | 65 | */ |
| 61 | @Parameter(property = "basedir", defaultValue = "${basedir}") | 66 | @Parameter(property = "basedir", defaultValue = "${basedir}") |
| ... | @@ -105,7 +110,7 @@ public class YangUtilManager extends AbstractMojo { | ... | @@ -105,7 +110,7 @@ public class YangUtilManager extends AbstractMojo { |
| 105 | YangIoUtils.clean(baseDir); | 110 | YangIoUtils.clean(baseDir); |
| 106 | 111 | ||
| 107 | searchDir = baseDir + File.separator + yangFilesDir; | 112 | searchDir = baseDir + File.separator + yangFilesDir; |
| 108 | - codeGenDir = baseDir + File.separator + UtilConstants.YANG_GEN_DIR; | 113 | + codeGenDir = baseDir + File.separator + genFilesDir + File.separator; |
| 109 | 114 | ||
| 110 | List<String> yangFiles = YangFileScanner.getYangFiles(searchDir); | 115 | List<String> yangFiles = YangFileScanner.getYangFiles(searchDir); |
| 111 | Iterator<String> yangFileIterator = yangFiles.iterator(); | 116 | Iterator<String> yangFileIterator = yangFiles.iterator(); |
| ... | @@ -129,8 +134,8 @@ public class YangUtilManager extends AbstractMojo { | ... | @@ -129,8 +134,8 @@ public class YangUtilManager extends AbstractMojo { |
| 129 | } | 134 | } |
| 130 | 135 | ||
| 131 | YangIoUtils.addToSource(baseDir + File.separator + UtilConstants.YANG_GEN_DIR, project, context); | 136 | YangIoUtils.addToSource(baseDir + File.separator + UtilConstants.YANG_GEN_DIR, project, context); |
| 132 | - } catch (final IOException e) { | 137 | + } catch (Exception e) { |
| 133 | - getLog().info("IOException occured"); | 138 | + getLog().info(e); |
| 134 | } | 139 | } |
| 135 | } | 140 | } |
| 136 | 141 | ... | ... |
| ... | @@ -305,8 +305,8 @@ public class CachedJavaFileHandle implements CachedFileHandle { | ... | @@ -305,8 +305,8 @@ public class CachedJavaFileHandle implements CachedFileHandle { |
| 305 | @Override | 305 | @Override |
| 306 | public void close() throws IOException { | 306 | public void close() throws IOException { |
| 307 | 307 | ||
| 308 | + List<AttributeInfo> attrList = getCachedAttributeList(); | ||
| 308 | flushCacheAttrToTempFile(); | 309 | flushCacheAttrToTempFile(); |
| 309 | - | ||
| 310 | String className = getYangName(); | 310 | String className = getYangName(); |
| 311 | className = JavaIdentifierSyntax.getCaptialCase(className); | 311 | className = JavaIdentifierSyntax.getCaptialCase(className); |
| 312 | String path = getRelativeFilePath(); | 312 | String path = getRelativeFilePath(); |
| ... | @@ -322,12 +322,15 @@ public class CachedJavaFileHandle implements CachedFileHandle { | ... | @@ -322,12 +322,15 @@ public class CachedJavaFileHandle implements CachedFileHandle { |
| 322 | 322 | ||
| 323 | for (ImportInfo importInfo : new ArrayList<ImportInfo>(getImportSet())) { | 323 | for (ImportInfo importInfo : new ArrayList<ImportInfo>(getImportSet())) { |
| 324 | importString = UtilConstants.IMPORT; | 324 | importString = UtilConstants.IMPORT; |
| 325 | - if (importInfo.getPkgInfo() != null) { | 325 | + if (importInfo.getPkgInfo() != "" && importInfo.getClassInfo() != null |
| 326 | + && importInfo.getPkgInfo() != UtilConstants.JAVA_LANG) { | ||
| 326 | importString = importString + importInfo.getPkgInfo() + "."; | 327 | importString = importString + importInfo.getPkgInfo() + "."; |
| 327 | - } | 328 | + importString = importString + importInfo.getClassInfo() + UtilConstants.SEMI_COLAN |
| 328 | - importString = importString + importInfo.getClassInfo() + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE; | 329 | + + UtilConstants.NEW_LINE; |
| 330 | + | ||
| 329 | imports.add(importString); | 331 | imports.add(importString); |
| 330 | } | 332 | } |
| 333 | + } | ||
| 331 | java.util.Collections.sort(imports); | 334 | java.util.Collections.sort(imports); |
| 332 | 335 | ||
| 333 | /** | 336 | /** |
| ... | @@ -342,7 +345,7 @@ public class CachedJavaFileHandle implements CachedFileHandle { | ... | @@ -342,7 +345,7 @@ public class CachedJavaFileHandle implements CachedFileHandle { |
| 342 | String interfaceFileName = className; | 345 | String interfaceFileName = className; |
| 343 | File interfaceFile = JavaFileGenerator.getFileObject(path, interfaceFileName, JAVA_FILE_EXTENSION, this); | 346 | File interfaceFile = JavaFileGenerator.getFileObject(path, interfaceFileName, JAVA_FILE_EXTENSION, this); |
| 344 | interfaceFile = JavaFileGenerator.generateInterfaceFile(interfaceFile, className, imports, | 347 | interfaceFile = JavaFileGenerator.generateInterfaceFile(interfaceFile, className, imports, |
| 345 | - getCachedAttributeList(), path.replace('/', '.'), this); | 348 | + attrList, path.replace('/', '.'), this); |
| 346 | /** | 349 | /** |
| 347 | * Create temp builder interface file. | 350 | * Create temp builder interface file. |
| 348 | */ | 351 | */ |
| ... | @@ -350,7 +353,7 @@ public class CachedJavaFileHandle implements CachedFileHandle { | ... | @@ -350,7 +353,7 @@ public class CachedJavaFileHandle implements CachedFileHandle { |
| 350 | File builderInterfaceFile = JavaFileGenerator.getFileObject(path, builderInterfaceFileName, | 353 | File builderInterfaceFile = JavaFileGenerator.getFileObject(path, builderInterfaceFileName, |
| 351 | TEMP_FILE_EXTENSION, this); | 354 | TEMP_FILE_EXTENSION, this); |
| 352 | builderInterfaceFile = JavaFileGenerator.generateBuilderInterfaceFile(builderInterfaceFile, className, | 355 | builderInterfaceFile = JavaFileGenerator.generateBuilderInterfaceFile(builderInterfaceFile, className, |
| 353 | - path.replace('/', '.'), getCachedAttributeList(), this); | 356 | + path.replace('/', '.'), attrList, this); |
| 354 | /** | 357 | /** |
| 355 | * Append builder interface file to interface file and close it. | 358 | * Append builder interface file to interface file and close it. |
| 356 | */ | 359 | */ |
| ... | @@ -369,9 +372,11 @@ public class CachedJavaFileHandle implements CachedFileHandle { | ... | @@ -369,9 +372,11 @@ public class CachedJavaFileHandle implements CachedFileHandle { |
| 369 | JavaFileGenerator.clean(builderInterfaceFile); | 372 | JavaFileGenerator.clean(builderInterfaceFile); |
| 370 | } | 373 | } |
| 371 | 374 | ||
| 375 | + if (!attrList.isEmpty()) { | ||
| 372 | imports.add(UtilConstants.MORE_OBJECT_IMPORT); | 376 | imports.add(UtilConstants.MORE_OBJECT_IMPORT); |
| 373 | imports.add(UtilConstants.JAVA_UTIL_OBJECTS_IMPORT); | 377 | imports.add(UtilConstants.JAVA_UTIL_OBJECTS_IMPORT); |
| 374 | java.util.Collections.sort(imports); | 378 | java.util.Collections.sort(imports); |
| 379 | + } | ||
| 375 | 380 | ||
| 376 | if ((fileType & GeneratedFileType.BUILDER_CLASS_MASK) != 0 | 381 | if ((fileType & GeneratedFileType.BUILDER_CLASS_MASK) != 0 |
| 377 | || fileType == GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER) { | 382 | || fileType == GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER) { |
| ... | @@ -382,7 +387,7 @@ public class CachedJavaFileHandle implements CachedFileHandle { | ... | @@ -382,7 +387,7 @@ public class CachedJavaFileHandle implements CachedFileHandle { |
| 382 | String builderFileName = className + UtilConstants.BUILDER; | 387 | String builderFileName = className + UtilConstants.BUILDER; |
| 383 | File builderFile = JavaFileGenerator.getFileObject(path, builderFileName, JAVA_FILE_EXTENSION, this); | 388 | File builderFile = JavaFileGenerator.getFileObject(path, builderFileName, JAVA_FILE_EXTENSION, this); |
| 384 | builderFile = JavaFileGenerator.generateBuilderClassFile(builderFile, className, imports, | 389 | builderFile = JavaFileGenerator.generateBuilderClassFile(builderFile, className, imports, |
| 385 | - path.replace('/', '.'), getCachedAttributeList(), this); | 390 | + path.replace('/', '.'), attrList, this); |
| 386 | /** | 391 | /** |
| 387 | * Create temp impl class file. | 392 | * Create temp impl class file. |
| 388 | */ | 393 | */ |
| ... | @@ -390,7 +395,7 @@ public class CachedJavaFileHandle implements CachedFileHandle { | ... | @@ -390,7 +395,7 @@ public class CachedJavaFileHandle implements CachedFileHandle { |
| 390 | String implFileName = className + UtilConstants.IMPL; | 395 | String implFileName = className + UtilConstants.IMPL; |
| 391 | File implTempFile = JavaFileGenerator.getFileObject(path, implFileName, TEMP_FILE_EXTENSION, this); | 396 | File implTempFile = JavaFileGenerator.getFileObject(path, implFileName, TEMP_FILE_EXTENSION, this); |
| 392 | implTempFile = JavaFileGenerator.generateImplClassFile(implTempFile, className, | 397 | implTempFile = JavaFileGenerator.generateImplClassFile(implTempFile, className, |
| 393 | - path.replace('/', '.'), getCachedAttributeList(), this); | 398 | + path.replace('/', '.'), attrList, this); |
| 394 | /** | 399 | /** |
| 395 | * Append impl class to builder class and close it. | 400 | * Append impl class to builder class and close it. |
| 396 | */ | 401 | */ |
| ... | @@ -418,7 +423,7 @@ public class CachedJavaFileHandle implements CachedFileHandle { | ... | @@ -418,7 +423,7 @@ public class CachedJavaFileHandle implements CachedFileHandle { |
| 418 | String typeDefFileName = className; | 423 | String typeDefFileName = className; |
| 419 | File typeDefFile = JavaFileGenerator.getFileObject(path, typeDefFileName, JAVA_FILE_EXTENSION, this); | 424 | File typeDefFile = JavaFileGenerator.getFileObject(path, typeDefFileName, JAVA_FILE_EXTENSION, this); |
| 420 | typeDefFile = JavaFileGenerator.generateTypeDefClassFile(typeDefFile, className, imports, | 425 | typeDefFile = JavaFileGenerator.generateTypeDefClassFile(typeDefFile, className, imports, |
| 421 | - path.replace('/', '.'), getCachedAttributeList(), this); | 426 | + path.replace('/', '.'), attrList, this); |
| 422 | JavaFileGenerator.insert(typeDefFile, | 427 | JavaFileGenerator.insert(typeDefFile, |
| 423 | JavaFileGenerator.closeFile(GeneratedFileType.GENERATE_TYPEDEF_CLASS, typeDefFileName)); | 428 | JavaFileGenerator.closeFile(GeneratedFileType.GENERATE_TYPEDEF_CLASS, typeDefFileName)); |
| 424 | 429 | ||
| ... | @@ -428,12 +433,19 @@ public class CachedJavaFileHandle implements CachedFileHandle { | ... | @@ -428,12 +433,19 @@ public class CachedJavaFileHandle implements CachedFileHandle { |
| 428 | JavaFileGenerator.closeFileHandles(typeDefFile); | 433 | JavaFileGenerator.closeFileHandles(typeDefFile); |
| 429 | } | 434 | } |
| 430 | 435 | ||
| 436 | + if (!getCachedAttributeList().isEmpty()) { | ||
| 431 | closeTempDataFileHandles(className, getCodeGenFilePath() + getRelativeFilePath()); | 437 | closeTempDataFileHandles(className, getCodeGenFilePath() + getRelativeFilePath()); |
| 432 | JavaFileGenerator | 438 | JavaFileGenerator |
| 433 | .cleanTempFiles(new File(getCodeGenFilePath() + getRelativeFilePath() + File.separator + className | 439 | .cleanTempFiles(new File(getCodeGenFilePath() + getRelativeFilePath() + File.separator + className |
| 434 | + TEMP_FOLDER_NAME_SUFIX)); | 440 | + TEMP_FOLDER_NAME_SUFIX)); |
| 435 | } | 441 | } |
| 436 | 442 | ||
| 443 | + /* | ||
| 444 | + * clear the contents from the cached attribute list. | ||
| 445 | + */ | ||
| 446 | + getCachedAttributeList().clear(); | ||
| 447 | + } | ||
| 448 | + | ||
| 437 | @Override | 449 | @Override |
| 438 | public void setTempData(String data, TempDataStoreTypes type, String className, String genDir) | 450 | public void setTempData(String data, TempDataStoreTypes type, String className, String genDir) |
| 439 | throws IOException { | 451 | throws IOException { |
| ... | @@ -512,7 +524,12 @@ public class CachedJavaFileHandle implements CachedFileHandle { | ... | @@ -512,7 +524,12 @@ public class CachedJavaFileHandle implements CachedFileHandle { |
| 512 | + File.separator + className + TEMP_FOLDER_NAME_SUFIX + File.separator; | 524 | + File.separator + className + TEMP_FOLDER_NAME_SUFIX + File.separator; |
| 513 | 525 | ||
| 514 | try { | 526 | try { |
| 527 | + String file = path + fileName + TEMP_FILE_EXTENSION; | ||
| 528 | + if (new File(file).exists()) { | ||
| 515 | return readFile(path + fileName + TEMP_FILE_EXTENSION); | 529 | return readFile(path + fileName + TEMP_FILE_EXTENSION); |
| 530 | + } else { | ||
| 531 | + return ""; | ||
| 532 | + } | ||
| 516 | 533 | ||
| 517 | } catch (FileNotFoundException e) { | 534 | } catch (FileNotFoundException e) { |
| 518 | throw new FileNotFoundException("No such file or directory."); | 535 | throw new FileNotFoundException("No such file or directory."); |
| ... | @@ -533,8 +550,14 @@ public class CachedJavaFileHandle implements CachedFileHandle { | ... | @@ -533,8 +550,14 @@ public class CachedJavaFileHandle implements CachedFileHandle { |
| 533 | String line = bufferReader.readLine(); | 550 | String line = bufferReader.readLine(); |
| 534 | 551 | ||
| 535 | while (line != null) { | 552 | while (line != null) { |
| 553 | + if (line.equals(UtilConstants.FOUR_SPACE_INDENTATION) | ||
| 554 | + || line.equals(UtilConstants.EIGHT_SPACE_INDENTATION) | ||
| 555 | + || line.equals(UtilConstants.SPACE) || line.equals("") || line.equals(UtilConstants.NEW_LINE)) { | ||
| 556 | + stringBuilder.append("\n"); | ||
| 557 | + } else { | ||
| 536 | stringBuilder.append(line); | 558 | stringBuilder.append(line); |
| 537 | stringBuilder.append("\n"); | 559 | stringBuilder.append("\n"); |
| 560 | + } | ||
| 538 | line = bufferReader.readLine(); | 561 | line = bufferReader.readLine(); |
| 539 | } | 562 | } |
| 540 | return stringBuilder.toString(); | 563 | return stringBuilder.toString(); | ... | ... |
| ... | @@ -84,7 +84,7 @@ public final class ClassDefinitionGenerator { | ... | @@ -84,7 +84,7 @@ public final class ClassDefinitionGenerator { |
| 84 | */ | 84 | */ |
| 85 | private static String getBuilderInterfaceDefinition(String yangName) { | 85 | private static String getBuilderInterfaceDefinition(String yangName) { |
| 86 | return UtilConstants.INTERFACE + UtilConstants.SPACE + yangName + UtilConstants.BUILDER + UtilConstants.SPACE | 86 | return UtilConstants.INTERFACE + UtilConstants.SPACE + yangName + UtilConstants.BUILDER + UtilConstants.SPACE |
| 87 | - + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE; | 87 | + + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.NEW_LINE; |
| 88 | } | 88 | } |
| 89 | 89 | ||
| 90 | /** | 90 | /** |
| ... | @@ -112,8 +112,7 @@ public final class ClassDefinitionGenerator { | ... | @@ -112,8 +112,7 @@ public final class ClassDefinitionGenerator { |
| 112 | return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.FINAL + UtilConstants.SPACE | 112 | return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.FINAL + UtilConstants.SPACE |
| 113 | + UtilConstants.CLASS + UtilConstants.SPACE + yangName + UtilConstants.IMPL + UtilConstants.SPACE | 113 | + UtilConstants.CLASS + UtilConstants.SPACE + yangName + UtilConstants.IMPL + UtilConstants.SPACE |
| 114 | + UtilConstants.IMPLEMENTS + UtilConstants.SPACE + yangName + UtilConstants.SPACE | 114 | + UtilConstants.IMPLEMENTS + UtilConstants.SPACE + yangName + UtilConstants.SPACE |
| 115 | - + UtilConstants.OPEN_CURLY_BRACKET | 115 | + + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.NEW_LINE; |
| 116 | - + UtilConstants.SPACE + UtilConstants.NEW_LINE; | ||
| 117 | } | 116 | } |
| 118 | 117 | ||
| 119 | /** | 118 | /** |
| ... | @@ -126,7 +125,7 @@ public final class ClassDefinitionGenerator { | ... | @@ -126,7 +125,7 @@ public final class ClassDefinitionGenerator { |
| 126 | 125 | ||
| 127 | return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.FINAL + UtilConstants.SPACE | 126 | return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.FINAL + UtilConstants.SPACE |
| 128 | + UtilConstants.CLASS + UtilConstants.SPACE + yangName + UtilConstants.SPACE | 127 | + UtilConstants.CLASS + UtilConstants.SPACE + yangName + UtilConstants.SPACE |
| 129 | - + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.SPACE + UtilConstants.NEW_LINE; | 128 | + + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE; |
| 130 | } | 129 | } |
| 131 | 130 | ||
| 132 | } | 131 | } | ... | ... |
| ... | @@ -36,6 +36,7 @@ import org.onosproject.yangutils.utils.io.impl.CopyrightHeader; | ... | @@ -36,6 +36,7 @@ import org.onosproject.yangutils.utils.io.impl.CopyrightHeader; |
| 36 | import org.onosproject.yangutils.utils.io.impl.FileSystemUtil; | 36 | import org.onosproject.yangutils.utils.io.impl.FileSystemUtil; |
| 37 | import org.onosproject.yangutils.utils.io.impl.JavaDocGen; | 37 | import org.onosproject.yangutils.utils.io.impl.JavaDocGen; |
| 38 | import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType; | 38 | import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType; |
| 39 | +import org.onosproject.yangutils.utils.io.impl.YangIoUtils; | ||
| 39 | import org.slf4j.Logger; | 40 | import org.slf4j.Logger; |
| 40 | 41 | ||
| 41 | /** | 42 | /** |
| ... | @@ -81,6 +82,7 @@ public final class JavaFileGenerator { | ... | @@ -81,6 +82,7 @@ public final class JavaFileGenerator { |
| 81 | String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH); | 82 | String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH); |
| 82 | initiateFile(file, className, INTERFACE_MASK, imports, pkg); | 83 | initiateFile(file, className, INTERFACE_MASK, imports, pkg); |
| 83 | 84 | ||
| 85 | + if (!attrList.isEmpty()) { | ||
| 84 | List<String> methods = new ArrayList<>(); | 86 | List<String> methods = new ArrayList<>(); |
| 85 | try { | 87 | try { |
| 86 | methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path)); | 88 | methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path)); |
| ... | @@ -95,6 +97,7 @@ public final class JavaFileGenerator { | ... | @@ -95,6 +97,7 @@ public final class JavaFileGenerator { |
| 95 | for (String method : methods) { | 97 | for (String method : methods) { |
| 96 | appendMethod(file, method); | 98 | appendMethod(file, method); |
| 97 | } | 99 | } |
| 100 | + } | ||
| 98 | return file; | 101 | return file; |
| 99 | } | 102 | } |
| 100 | 103 | ||
| ... | @@ -115,6 +118,8 @@ public final class JavaFileGenerator { | ... | @@ -115,6 +118,8 @@ public final class JavaFileGenerator { |
| 115 | initiateFile(file, className, BUILDER_INTERFACE_MASK, null, pkg); | 118 | initiateFile(file, className, BUILDER_INTERFACE_MASK, null, pkg); |
| 116 | List<String> methods = new ArrayList<>(); | 119 | List<String> methods = new ArrayList<>(); |
| 117 | 120 | ||
| 121 | + if (!attrList.isEmpty()) { | ||
| 122 | + | ||
| 118 | try { | 123 | try { |
| 119 | methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path)); | 124 | methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path)); |
| 120 | methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS, className, path)); | 125 | methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS, className, path)); |
| ... | @@ -122,7 +127,7 @@ public final class JavaFileGenerator { | ... | @@ -122,7 +127,7 @@ public final class JavaFileGenerator { |
| 122 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); | 127 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); |
| 123 | throw new IOException("Fail to read data from temp file."); | 128 | throw new IOException("Fail to read data from temp file."); |
| 124 | } | 129 | } |
| 125 | - | 130 | + } |
| 126 | /** | 131 | /** |
| 127 | * Add build method to builder interface file. | 132 | * Add build method to builder interface file. |
| 128 | */ | 133 | */ |
| ... | @@ -156,6 +161,8 @@ public final class JavaFileGenerator { | ... | @@ -156,6 +161,8 @@ public final class JavaFileGenerator { |
| 156 | String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH); | 161 | String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH); |
| 157 | initiateFile(file, className, BUILDER_CLASS_MASK, imports, pkg); | 162 | initiateFile(file, className, BUILDER_CLASS_MASK, imports, pkg); |
| 158 | 163 | ||
| 164 | + List<String> methods = new ArrayList<>(); | ||
| 165 | + if (!attrList.isEmpty()) { | ||
| 159 | /** | 166 | /** |
| 160 | * Add attribute strings. | 167 | * Add attribute strings. |
| 161 | */ | 168 | */ |
| ... | @@ -173,7 +180,6 @@ public final class JavaFileGenerator { | ... | @@ -173,7 +180,6 @@ public final class JavaFileGenerator { |
| 173 | insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute); | 180 | insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute); |
| 174 | } | 181 | } |
| 175 | 182 | ||
| 176 | - List<String> methods = new ArrayList<>(); | ||
| 177 | try { | 183 | try { |
| 178 | methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path)); | 184 | methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path)); |
| 179 | methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS_IMPL, className, path)); | 185 | methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS_IMPL, className, path)); |
| ... | @@ -182,10 +188,12 @@ public final class JavaFileGenerator { | ... | @@ -182,10 +188,12 @@ public final class JavaFileGenerator { |
| 182 | throw new IOException("Fail to read data from temp file."); | 188 | throw new IOException("Fail to read data from temp file."); |
| 183 | } | 189 | } |
| 184 | 190 | ||
| 191 | + } | ||
| 185 | /** | 192 | /** |
| 186 | * Add default constructor and build method impl. | 193 | * Add default constructor and build method impl. |
| 187 | */ | 194 | */ |
| 188 | - methods.add(UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE | 195 | + methods.add( |
| 196 | + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE | ||
| 189 | + MethodsGenerator.getDefaultConstructorString(className + UtilConstants.BUILDER, | 197 | + MethodsGenerator.getDefaultConstructorString(className + UtilConstants.BUILDER, |
| 190 | UtilConstants.PUBLIC)); | 198 | UtilConstants.PUBLIC)); |
| 191 | methods.add(MethodsGenerator.getBuildString(className)); | 199 | methods.add(MethodsGenerator.getBuildString(className)); |
| ... | @@ -216,6 +224,8 @@ public final class JavaFileGenerator { | ... | @@ -216,6 +224,8 @@ public final class JavaFileGenerator { |
| 216 | String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH); | 224 | String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH); |
| 217 | initiateFile(file, className, IMPL_CLASS_MASK, null, path); | 225 | initiateFile(file, className, IMPL_CLASS_MASK, null, path); |
| 218 | 226 | ||
| 227 | + List<String> methods = new ArrayList<>(); | ||
| 228 | + if (!attrList.isEmpty()) { | ||
| 219 | List<String> attributes = new ArrayList<>(); | 229 | List<String> attributes = new ArrayList<>(); |
| 220 | try { | 230 | try { |
| 221 | attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path)); | 231 | attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path)); |
| ... | @@ -231,18 +241,14 @@ public final class JavaFileGenerator { | ... | @@ -231,18 +241,14 @@ public final class JavaFileGenerator { |
| 231 | insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute); | 241 | insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute); |
| 232 | } | 242 | } |
| 233 | 243 | ||
| 234 | - List<String> methods = new ArrayList<>(); | ||
| 235 | try { | 244 | try { |
| 236 | 245 | ||
| 237 | methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path)); | 246 | methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path)); |
| 238 | 247 | ||
| 239 | - methods.add(getConstructorString(className) | ||
| 240 | - + handle.getTempData(TempDataStoreTypes.CONSTRUCTOR, className, path) | ||
| 241 | - + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET); | ||
| 242 | - | ||
| 243 | methods.add(MethodsGenerator.getHashCodeMethodClose(MethodsGenerator.getHashCodeMethodOpen() | 248 | methods.add(MethodsGenerator.getHashCodeMethodClose(MethodsGenerator.getHashCodeMethodOpen() |
| 244 | - + handle.getTempData(TempDataStoreTypes.HASH_CODE, className, path).replace(UtilConstants.NEW_LINE, | 249 | + + YangIoUtils |
| 245 | - ""))); | 250 | + .partString(handle.getTempData(TempDataStoreTypes.HASH_CODE, className, path).replace( |
| 251 | + UtilConstants.NEW_LINE, "")))); | ||
| 246 | 252 | ||
| 247 | methods.add(MethodsGenerator | 253 | methods.add(MethodsGenerator |
| 248 | .getEqualsMethodClose(MethodsGenerator.getEqualsMethodOpen(className + UtilConstants.IMPL) | 254 | .getEqualsMethodClose(MethodsGenerator.getEqualsMethodOpen(className + UtilConstants.IMPL) |
| ... | @@ -257,6 +263,16 @@ public final class JavaFileGenerator { | ... | @@ -257,6 +263,16 @@ public final class JavaFileGenerator { |
| 257 | throw new IOException("Fail to read data from temp file."); | 263 | throw new IOException("Fail to read data from temp file."); |
| 258 | } | 264 | } |
| 259 | 265 | ||
| 266 | + } | ||
| 267 | + | ||
| 268 | + try { | ||
| 269 | + methods.add(getConstructorString(className) | ||
| 270 | + + handle.getTempData(TempDataStoreTypes.CONSTRUCTOR, className, path) | ||
| 271 | + + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET); | ||
| 272 | + } catch (ClassNotFoundException | IOException e) { | ||
| 273 | + log.info("There is no attribute info of " + className + " YANG file in the temporary files."); | ||
| 274 | + throw new IOException("Fail to read data from temp file."); | ||
| 275 | + } | ||
| 260 | /** | 276 | /** |
| 261 | * Add methods in impl class. | 277 | * Add methods in impl class. |
| 262 | */ | 278 | */ |
| ... | @@ -315,8 +331,9 @@ public final class JavaFileGenerator { | ... | @@ -315,8 +331,9 @@ public final class JavaFileGenerator { |
| 315 | String javadoc = MethodsGenerator.getConstructorString(yangName); | 331 | String javadoc = MethodsGenerator.getConstructorString(yangName); |
| 316 | String constructor = UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE | 332 | String constructor = UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE |
| 317 | + yangName + UtilConstants.IMPL + UtilConstants.OPEN_PARENTHESIS + yangName + UtilConstants.BUILDER | 333 | + yangName + UtilConstants.IMPL + UtilConstants.OPEN_PARENTHESIS + yangName + UtilConstants.BUILDER |
| 318 | - + UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT + UtilConstants.CLOSE_PARENTHESIS | 334 | + + UtilConstants.SPACE + builderAttribute + UtilConstants.BUILDER + UtilConstants.OBJECT |
| 319 | - + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE; | 335 | + + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET |
| 336 | + + UtilConstants.NEW_LINE; | ||
| 320 | return javadoc + constructor; | 337 | return javadoc + constructor; |
| 321 | } | 338 | } |
| 322 | 339 | ||
| ... | @@ -514,15 +531,16 @@ public final class JavaFileGenerator { | ... | @@ -514,15 +531,16 @@ public final class JavaFileGenerator { |
| 514 | + MethodsGenerator.getSetterForTypeDefClass(attr) | 531 | + MethodsGenerator.getSetterForTypeDefClass(attr) |
| 515 | + UtilConstants.NEW_LINE; | 532 | + UtilConstants.NEW_LINE; |
| 516 | 533 | ||
| 517 | - hashCodeString = hashCodeString + MethodsGenerator.getHashCodeMethodOpen() | 534 | + hashCodeString = MethodsGenerator.getHashCodeMethodOpen() |
| 518 | - + MethodsGenerator.getHashCodeMethod(attr).replace(UtilConstants.NEW_LINE, ""); | 535 | + + YangIoUtils.partString( |
| 536 | + MethodsGenerator.getHashCodeMethod(attr).replace(UtilConstants.NEW_LINE, "")); | ||
| 519 | hashCodeString = MethodsGenerator.getHashCodeMethodClose(hashCodeString) + UtilConstants.NEW_LINE; | 537 | hashCodeString = MethodsGenerator.getHashCodeMethodClose(hashCodeString) + UtilConstants.NEW_LINE; |
| 520 | 538 | ||
| 521 | - equalsString = equalsString + MethodsGenerator.getEqualsMethodOpen(className) + UtilConstants.NEW_LINE | 539 | + equalsString = MethodsGenerator.getEqualsMethodOpen(className) + UtilConstants.NEW_LINE |
| 522 | + MethodsGenerator.getEqualsMethod(attr); | 540 | + MethodsGenerator.getEqualsMethod(attr); |
| 523 | equalsString = MethodsGenerator.getEqualsMethodClose(equalsString) + UtilConstants.NEW_LINE; | 541 | equalsString = MethodsGenerator.getEqualsMethodClose(equalsString) + UtilConstants.NEW_LINE; |
| 524 | 542 | ||
| 525 | - toString = toString + MethodsGenerator.getToStringMethodOpen() | 543 | + toString = MethodsGenerator.getToStringMethodOpen() |
| 526 | + MethodsGenerator.getToStringMethod(attr) + UtilConstants.NEW_LINE | 544 | + MethodsGenerator.getToStringMethod(attr) + UtilConstants.NEW_LINE |
| 527 | + MethodsGenerator.getToStringMethodClose() | 545 | + MethodsGenerator.getToStringMethodClose() |
| 528 | + UtilConstants.NEW_LINE; | 546 | + UtilConstants.NEW_LINE; |
| ... | @@ -568,7 +586,6 @@ public final class JavaFileGenerator { | ... | @@ -568,7 +586,6 @@ public final class JavaFileGenerator { |
| 568 | for (String imports : importsList) { | 586 | for (String imports : importsList) { |
| 569 | insert(file, imports); | 587 | insert(file, imports); |
| 570 | } | 588 | } |
| 571 | - insert(file, UtilConstants.NEW_LINE); | ||
| 572 | } | 589 | } |
| 573 | write(file, fileName, type, JavaDocType.IMPL_CLASS); | 590 | write(file, fileName, type, JavaDocType.IMPL_CLASS); |
| 574 | } else { | 591 | } else { |
| ... | @@ -581,7 +598,6 @@ public final class JavaFileGenerator { | ... | @@ -581,7 +598,6 @@ public final class JavaFileGenerator { |
| 581 | for (String imports : importsList) { | 598 | for (String imports : importsList) { |
| 582 | insert(file, imports); | 599 | insert(file, imports); |
| 583 | } | 600 | } |
| 584 | - insert(file, UtilConstants.NEW_LINE); | ||
| 585 | } | 601 | } |
| 586 | write(file, fileName, type, JavaDocType.INTERFACE); | 602 | write(file, fileName, type, JavaDocType.INTERFACE); |
| 587 | } else if ((type & BUILDER_CLASS_MASK) != 0) { | 603 | } else if ((type & BUILDER_CLASS_MASK) != 0) { | ... | ... |
| ... | @@ -434,7 +434,7 @@ public final class MethodsGenerator { | ... | @@ -434,7 +434,7 @@ public final class MethodsGenerator { |
| 434 | String constructor = UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS | 434 | String constructor = UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS |
| 435 | + UtilConstants.PERIOD + JavaIdentifierSyntax.getCamelCase(attributeName) | 435 | + UtilConstants.PERIOD + JavaIdentifierSyntax.getCamelCase(attributeName) |
| 436 | + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE + builderAttribute | 436 | + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE + builderAttribute |
| 437 | - + UtilConstants.OBJECT + UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX | 437 | + + UtilConstants.BUILDER + UtilConstants.OBJECT + UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX |
| 438 | + JavaIdentifierSyntax.getCaptialCase(JavaIdentifierSyntax.getCamelCase(attributeName)) | 438 | + JavaIdentifierSyntax.getCaptialCase(JavaIdentifierSyntax.getCamelCase(attributeName)) |
| 439 | + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN | 439 | + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN |
| 440 | + UtilConstants.NEW_LINE; | 440 | + UtilConstants.NEW_LINE; |
| ... | @@ -539,7 +539,6 @@ public final class MethodsGenerator { | ... | @@ -539,7 +539,6 @@ public final class MethodsGenerator { |
| 539 | public static String getHashCodeMethodClose(String hashcodeString) { | 539 | public static String getHashCodeMethodClose(String hashcodeString) { |
| 540 | hashcodeString = YangIoUtils.trimAtLast(hashcodeString, UtilConstants.COMMA); | 540 | hashcodeString = YangIoUtils.trimAtLast(hashcodeString, UtilConstants.COMMA); |
| 541 | hashcodeString = YangIoUtils.trimAtLast(hashcodeString, UtilConstants.SPACE); | 541 | hashcodeString = YangIoUtils.trimAtLast(hashcodeString, UtilConstants.SPACE); |
| 542 | - hashcodeString = YangIoUtils.partString(hashcodeString); | ||
| 543 | return hashcodeString + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE | 542 | return hashcodeString + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE |
| 544 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET; | 543 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET; |
| 545 | } | 544 | } | ... | ... |
| ... | @@ -119,7 +119,7 @@ public final class UtilConstants { | ... | @@ -119,7 +119,7 @@ public final class UtilConstants { |
| 119 | /** | 119 | /** |
| 120 | * For directories. | 120 | * For directories. |
| 121 | */ | 121 | */ |
| 122 | - public static final String YANG_GEN_DIR = "src/main/yangmodel/"; | 122 | + public static final String YANG_GEN_DIR = "src/main/java/"; |
| 123 | public static final String DEFAULT_BASE_PKG = "org.onosproject.yang.gen"; | 123 | public static final String DEFAULT_BASE_PKG = "org.onosproject.yang.gen"; |
| 124 | public static final String REVISION_PREFIX = "rev"; | 124 | public static final String REVISION_PREFIX = "rev"; |
| 125 | public static final String VERSION_PREFIX = "v"; | 125 | public static final String VERSION_PREFIX = "v"; | ... | ... |
| ... | @@ -122,8 +122,14 @@ public final class FileSystemUtil { | ... | @@ -122,8 +122,14 @@ public final class FileSystemUtil { |
| 122 | String line = bufferReader.readLine(); | 122 | String line = bufferReader.readLine(); |
| 123 | 123 | ||
| 124 | while (line != null) { | 124 | while (line != null) { |
| 125 | + if (line.equals(UtilConstants.FOUR_SPACE_INDENTATION) | ||
| 126 | + || line.equals(UtilConstants.EIGHT_SPACE_INDENTATION) | ||
| 127 | + || line.equals(UtilConstants.SPACE) || line.equals("") || line.equals(UtilConstants.NEW_LINE)) { | ||
| 128 | + stringBuilder.append("\n"); | ||
| 129 | + } else { | ||
| 125 | stringBuilder.append(UtilConstants.FOUR_SPACE_INDENTATION + line); | 130 | stringBuilder.append(UtilConstants.FOUR_SPACE_INDENTATION + line); |
| 126 | stringBuilder.append("\n"); | 131 | stringBuilder.append("\n"); |
| 132 | + } | ||
| 127 | line = bufferReader.readLine(); | 133 | line = bufferReader.readLine(); |
| 128 | } | 134 | } |
| 129 | return stringBuilder.toString(); | 135 | return stringBuilder.toString(); | ... | ... |
| ... | @@ -245,8 +245,8 @@ public final class JavaDocGen { | ... | @@ -245,8 +245,8 @@ public final class JavaDocGen { |
| 245 | * @return javaDocs | 245 | * @return javaDocs |
| 246 | */ | 246 | */ |
| 247 | private static String generateForImplClass(String className) { | 247 | private static String generateForImplClass(String className) { |
| 248 | - return UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.IMPL_CLASS_JAVA_DOC + className + UtilConstants.PERIOD | 248 | + return UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.IMPL_CLASS_JAVA_DOC |
| 249 | - + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE; | 249 | + + className + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE; |
| 250 | } | 250 | } |
| 251 | 251 | ||
| 252 | /** | 252 | /** |
| ... | @@ -310,13 +310,15 @@ public final class JavaDocGen { | ... | @@ -310,13 +310,15 @@ public final class JavaDocGen { |
| 310 | * @return javaDocs | 310 | * @return javaDocs |
| 311 | */ | 311 | */ |
| 312 | private static String generateForConstructors(String className) { | 312 | private static String generateForConstructors(String className) { |
| 313 | - return UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.FOUR_SPACE_INDENTATION | 313 | + return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE |
| 314 | - + UtilConstants.JAVA_DOC_CONSTRUCTOR + className + UtilConstants.IMPL + UtilConstants.PERIOD | 314 | + + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_CONSTRUCTOR + className |
| 315 | - + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.NEW_LINE_ESTRIC | 315 | + + UtilConstants.IMPL + UtilConstants.PERIOD + UtilConstants.NEW_LINE |
| 316 | + + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.NEW_LINE_ESTRIC | ||
| 316 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_PARAM | 317 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_PARAM |
| 317 | - + className.substring(0, 1).toLowerCase() + className.substring(1) + UtilConstants.OBJECT | 318 | + + className.substring(0, 1).toLowerCase() + className.substring(1) + UtilConstants.BUILDER |
| 318 | - + UtilConstants.SPACE + UtilConstants.BUILDER_OBJECT + UtilConstants.SPACE + className | 319 | + + UtilConstants.OBJECT + UtilConstants.SPACE + UtilConstants.BUILDER_OBJECT + UtilConstants.SPACE |
| 319 | - + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_END_LINE; | 320 | + + className + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION |
| 321 | + + UtilConstants.JAVA_DOC_END_LINE; | ||
| 320 | } | 322 | } |
| 321 | 323 | ||
| 322 | /** | 324 | /** | ... | ... |
utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/TempDataStore.java
deleted
100644 → 0
| 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.utils.io.impl; | ||
| 18 | - | ||
| 19 | -import java.io.BufferedInputStream; | ||
| 20 | -import java.io.BufferedOutputStream; | ||
| 21 | -import java.io.File; | ||
| 22 | -import java.io.FileInputStream; | ||
| 23 | -import java.io.FileNotFoundException; | ||
| 24 | -import java.io.FileOutputStream; | ||
| 25 | -import java.io.IOException; | ||
| 26 | -import java.io.InputStream; | ||
| 27 | -import java.io.ObjectInput; | ||
| 28 | -import java.io.ObjectInputStream; | ||
| 29 | -import java.io.ObjectOutput; | ||
| 30 | -import java.io.ObjectOutputStream; | ||
| 31 | -import java.io.OutputStream; | ||
| 32 | -import java.util.ArrayList; | ||
| 33 | -import java.util.List; | ||
| 34 | - | ||
| 35 | -/** | ||
| 36 | - * Provides storage for Temp data while traversing data model tree for code | ||
| 37 | - * generation. | ||
| 38 | - */ | ||
| 39 | -public final class TempDataStore { | ||
| 40 | - | ||
| 41 | - /** | ||
| 42 | - * Data Store types. | ||
| 43 | - */ | ||
| 44 | - public static enum TempDataStoreType { | ||
| 45 | - | ||
| 46 | - /** | ||
| 47 | - * Methods. | ||
| 48 | - */ | ||
| 49 | - GETTER_METHODS, | ||
| 50 | - | ||
| 51 | - /** | ||
| 52 | - * Methods. | ||
| 53 | - */ | ||
| 54 | - BUILDER_METHODS, | ||
| 55 | - | ||
| 56 | - /** | ||
| 57 | - * Methods. | ||
| 58 | - */ | ||
| 59 | - BUILDER_INTERFACE_METHODS, | ||
| 60 | - | ||
| 61 | - /** | ||
| 62 | - * Methods. | ||
| 63 | - */ | ||
| 64 | - IMPL_METHODS, | ||
| 65 | - | ||
| 66 | - /** | ||
| 67 | - * Attributes. | ||
| 68 | - */ | ||
| 69 | - ATTRIBUTE, | ||
| 70 | - | ||
| 71 | - /** | ||
| 72 | - * Imports. | ||
| 73 | - */ | ||
| 74 | - IMPORT | ||
| 75 | - } | ||
| 76 | - | ||
| 77 | - /** | ||
| 78 | - * File name string for Temp files of methods. | ||
| 79 | - */ | ||
| 80 | - private static final String GETTER_METHOD_FILE_NAME = "TempGetterMethodDataStore"; | ||
| 81 | - | ||
| 82 | - /** | ||
| 83 | - * File name string for Temp files of methods. | ||
| 84 | - */ | ||
| 85 | - private static final String BUILDER_METHOD_FILE_NAME = "TempBuilderMethodDataStore"; | ||
| 86 | - | ||
| 87 | - /** | ||
| 88 | - * File name string for Temp files of methods. | ||
| 89 | - */ | ||
| 90 | - private static final String BUILDER_INTERFACE_METHOD_FILE_NAME = "TempBuilderInterfaceMethodDataStore"; | ||
| 91 | - | ||
| 92 | - /** | ||
| 93 | - * File name string for Temp files of methods. | ||
| 94 | - */ | ||
| 95 | - private static final String IMPL_METHOD_FILE_NAME = "TempImplMethodDataStore"; | ||
| 96 | - | ||
| 97 | - /** | ||
| 98 | - * File name string for Temp files of attributes. | ||
| 99 | - */ | ||
| 100 | - private static final String ATTRIBUTE_FILE_NAME = "TempAttributeDataStore"; | ||
| 101 | - | ||
| 102 | - /** | ||
| 103 | - * File name string for Temp files of imports. | ||
| 104 | - */ | ||
| 105 | - private static final String IMPORT_FILE_NAME = "TempImportDataStore"; | ||
| 106 | - | ||
| 107 | - /** | ||
| 108 | - * File extension of Temp files. | ||
| 109 | - */ | ||
| 110 | - private static final String FILE_EXTENSION = ".tmp"; | ||
| 111 | - | ||
| 112 | - /** | ||
| 113 | - * Directory for generating Temp files. | ||
| 114 | - */ | ||
| 115 | - private static final String GEN_DIR = "target/"; | ||
| 116 | - | ||
| 117 | - /** | ||
| 118 | - * Buffer size. | ||
| 119 | - */ | ||
| 120 | - private static final int BUFFER_SIZE = 8 * 1024; | ||
| 121 | - | ||
| 122 | - /** | ||
| 123 | - * Default constructor. | ||
| 124 | - */ | ||
| 125 | - private TempDataStore() { | ||
| 126 | - } | ||
| 127 | - | ||
| 128 | - /** | ||
| 129 | - * Writes specific info to a Temp file. | ||
| 130 | - * | ||
| 131 | - * @param data data to be stored | ||
| 132 | - * @param type type of Temp data store | ||
| 133 | - * @param className class name | ||
| 134 | - * @throws IOException when fails to create a Temp data file | ||
| 135 | - */ | ||
| 136 | - public static void setTempData(String data, TempDataStoreType type, String className) throws IOException { | ||
| 137 | - | ||
| 138 | - String fileName = ""; | ||
| 139 | - if (type.equals(TempDataStoreType.ATTRIBUTE)) { | ||
| 140 | - fileName = ATTRIBUTE_FILE_NAME; | ||
| 141 | - } else if (type.equals(TempDataStoreType.GETTER_METHODS)) { | ||
| 142 | - fileName = GETTER_METHOD_FILE_NAME; | ||
| 143 | - } else if (type.equals(TempDataStoreType.BUILDER_INTERFACE_METHODS)) { | ||
| 144 | - fileName = BUILDER_INTERFACE_METHOD_FILE_NAME; | ||
| 145 | - } else if (type.equals(TempDataStoreType.BUILDER_METHODS)) { | ||
| 146 | - fileName = BUILDER_METHOD_FILE_NAME; | ||
| 147 | - } else if (type.equals(TempDataStoreType.IMPL_METHODS)) { | ||
| 148 | - fileName = IMPL_METHOD_FILE_NAME; | ||
| 149 | - } else { | ||
| 150 | - fileName = IMPORT_FILE_NAME; | ||
| 151 | - } | ||
| 152 | - | ||
| 153 | - File dir = new File(GEN_DIR + className + File.separator); | ||
| 154 | - dir.mkdirs(); | ||
| 155 | - try { | ||
| 156 | - OutputStream file = new FileOutputStream(GEN_DIR + className + File.separator + fileName + FILE_EXTENSION); | ||
| 157 | - OutputStream buffer = new BufferedOutputStream(file, BUFFER_SIZE); | ||
| 158 | - | ||
| 159 | - ObjectOutput output = new ObjectOutputStream(buffer); | ||
| 160 | - try { | ||
| 161 | - output.writeObject(data); | ||
| 162 | - } finally { | ||
| 163 | - output.close(); | ||
| 164 | - } | ||
| 165 | - } catch (IOException ex) { | ||
| 166 | - throw new IOException("failed to serialize data"); | ||
| 167 | - } | ||
| 168 | - } | ||
| 169 | - | ||
| 170 | - /** | ||
| 171 | - * Get the Temp data. | ||
| 172 | - * | ||
| 173 | - * @param type type of Temp data store | ||
| 174 | - * @param className name of the class | ||
| 175 | - * @return list of attribute info | ||
| 176 | - * @throws IOException when fails to read from the file | ||
| 177 | - * @throws ClassNotFoundException when class is missing | ||
| 178 | - * @throws FileNotFoundException when file is missing | ||
| 179 | - */ | ||
| 180 | - public static List<String> getTempData(TempDataStoreType type, String className) | ||
| 181 | - throws IOException, FileNotFoundException, ClassNotFoundException { | ||
| 182 | - | ||
| 183 | - String fileName = ""; | ||
| 184 | - if (type.equals(TempDataStoreType.ATTRIBUTE)) { | ||
| 185 | - fileName = ATTRIBUTE_FILE_NAME; | ||
| 186 | - } else if (type.equals(TempDataStoreType.GETTER_METHODS)) { | ||
| 187 | - fileName = GETTER_METHOD_FILE_NAME; | ||
| 188 | - } else if (type.equals(TempDataStoreType.BUILDER_INTERFACE_METHODS)) { | ||
| 189 | - fileName = BUILDER_INTERFACE_METHOD_FILE_NAME; | ||
| 190 | - } else if (type.equals(TempDataStoreType.BUILDER_METHODS)) { | ||
| 191 | - fileName = BUILDER_METHOD_FILE_NAME; | ||
| 192 | - } else if (type.equals(TempDataStoreType.IMPL_METHODS)) { | ||
| 193 | - fileName = IMPL_METHOD_FILE_NAME; | ||
| 194 | - } else { | ||
| 195 | - fileName = IMPORT_FILE_NAME; | ||
| 196 | - } | ||
| 197 | - try { | ||
| 198 | - InputStream file = new FileInputStream(GEN_DIR + className + File.separator + fileName + FILE_EXTENSION); | ||
| 199 | - InputStream buffer = new BufferedInputStream(file); | ||
| 200 | - ObjectInput input = new ObjectInputStream(buffer); | ||
| 201 | - try { | ||
| 202 | - String data = (String) input.readObject(); | ||
| 203 | - List<String> recoveredData = new ArrayList<>(); | ||
| 204 | - recoveredData.add(data); | ||
| 205 | - return recoveredData; | ||
| 206 | - } finally { | ||
| 207 | - input.close(); | ||
| 208 | - file.close(); | ||
| 209 | - } | ||
| 210 | - } catch (FileNotFoundException ex) { | ||
| 211 | - throw new FileNotFoundException("No such file or directory."); | ||
| 212 | - } catch (ClassNotFoundException ex) { | ||
| 213 | - throw new ClassNotFoundException("failed to fetch the Temp data file."); | ||
| 214 | - } | ||
| 215 | - } | ||
| 216 | -} |
| ... | @@ -94,7 +94,8 @@ public final class YangIoUtils { | ... | @@ -94,7 +94,8 @@ public final class YangIoUtils { |
| 94 | * @param baseDir generated directory in previous build | 94 | * @param baseDir generated directory in previous build |
| 95 | */ | 95 | */ |
| 96 | public static void clean(String baseDir) { | 96 | public static void clean(String baseDir) { |
| 97 | - File generatedDirectory = new File(baseDir + File.separator + UtilConstants.YANG_GEN_DIR); | 97 | + File generatedDirectory = new File(baseDir + File.separator + UtilConstants.YANG_GEN_DIR |
| 98 | + + UtilConstants.DEFAULT_BASE_PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH)); | ||
| 98 | if (generatedDirectory.exists()) { | 99 | if (generatedDirectory.exists()) { |
| 99 | List<String> javafiles; | 100 | List<String> javafiles; |
| 100 | try { | 101 | try { |
| ... | @@ -151,10 +152,11 @@ public final class YangIoUtils { | ... | @@ -151,10 +152,11 @@ public final class YangIoUtils { |
| 151 | String[] strArray = partString.split(UtilConstants.COMMA); | 152 | String[] strArray = partString.split(UtilConstants.COMMA); |
| 152 | String newString = ""; | 153 | String newString = ""; |
| 153 | for (int i = 0; i < strArray.length; i++) { | 154 | for (int i = 0; i < strArray.length; i++) { |
| 154 | - if (i % 4 != 0) { | 155 | + if (i % 4 != 0 || i == 0) { |
| 155 | newString = newString + strArray[i] + UtilConstants.COMMA; | 156 | newString = newString + strArray[i] + UtilConstants.COMMA; |
| 156 | } else { | 157 | } else { |
| 157 | - newString = newString + UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION + strArray[i] | 158 | + newString = newString + UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION |
| 159 | + + strArray[i] | ||
| 158 | + UtilConstants.COMMA; | 160 | + UtilConstants.COMMA; |
| 159 | } | 161 | } |
| 160 | } | 162 | } | ... | ... |
-
Please register or login to post a comment