Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | package org.onosproject.yangutils.translator.tojava; |
| 17 | |
| 18 | import java.io.File; |
| 19 | import java.io.IOException; |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.List; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 22 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.RpcNotificationContainer; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangCase; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 26 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 27 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangList; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangNode; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
| 31 | import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugment; |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGrouping; |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule; |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 36 | import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.utils.io.impl.YangPluginConfig; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 38 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK; |
| 41 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK; |
| 42 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER; |
| 44 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK; |
| 46 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK; |
| 47 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.AUGMENTE_CLASS_CONSTRUCTOR_MASK; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 51 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK; |
| 52 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK; |
| 53 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 54 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK; |
| 55 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK; |
| 56 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK; |
| 57 | import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 58 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 59 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 60 | import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.updateJavaFileInfo; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 61 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination; |
| 62 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose; |
| 63 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile; |
| 64 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 65 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile; |
| 66 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 67 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 68 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage; |
| 69 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAugmentedConstructor; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 70 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 71 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString; |
| 72 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 73 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 74 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass; |
| 75 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString; |
| 76 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod; |
| 77 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 78 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 79 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass; |
| 80 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString; |
| 81 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 82 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 83 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile; |
| 84 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.sortImports; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 85 | import static org.onosproject.yangutils.utils.UtilConstants.ACTIVATE; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 86 | import static org.onosproject.yangutils.utils.UtilConstants.BUILDER; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 87 | import static org.onosproject.yangutils.utils.UtilConstants.COMPONENT; |
| 88 | import static org.onosproject.yangutils.utils.UtilConstants.DEACTIVATE; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 89 | import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 90 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
| 91 | import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION; |
| 92 | import static org.onosproject.yangutils.utils.UtilConstants.IMPL; |
| 93 | import static org.onosproject.yangutils.utils.UtilConstants.IMPORT; |
| 94 | import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 95 | import static org.onosproject.yangutils.utils.UtilConstants.MANAGER; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 96 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 97 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 98 | import static org.onosproject.yangutils.utils.UtilConstants.REFERENCE; |
| 99 | import static org.onosproject.yangutils.utils.UtilConstants.REFERENCE_CARDINALITY; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 100 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 101 | import static org.onosproject.yangutils.utils.UtilConstants.SERVICE; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 102 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 103 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile; |
| 104 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD; |
| 105 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 106 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 107 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 108 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase; |
| 109 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
| 110 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 111 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile; |
| 112 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 113 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.validateLineLength; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Represents implementation of java code fragments temporary implementations. |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 117 | * Manages the common temp file required for Java file(s) generated. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 118 | */ |
| 119 | public class TempJavaFragmentFiles { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 120 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 121 | /** |
| 122 | * Information about the java files being generated. |
| 123 | */ |
| 124 | private JavaFileInfo javaFileInfo; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 125 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 126 | /** |
| 127 | * Imported class info. |
| 128 | */ |
| 129 | private JavaImportData javaImportData; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 130 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 131 | /** |
| 132 | * The variable which guides the types of temporary files generated using |
| 133 | * the temporary generated file types mask. |
| 134 | */ |
| 135 | private int generatedTempFiles; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 136 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 137 | /** |
| 138 | * Absolute path where the target java file needs to be generated. |
| 139 | */ |
| 140 | private String absoluteDirPath; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 141 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 142 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 143 | * Contains all the interface(s)/class name which will be extended by generated files. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 144 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 145 | private JavaExtendsListHolder javaExtendsListHolder; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 146 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 147 | /** |
| 148 | * File type extension for java classes. |
| 149 | */ |
| 150 | private static final String JAVA_FILE_EXTENSION = ".java"; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 151 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 152 | /** |
| 153 | * File type extension for temporary classes. |
| 154 | */ |
| 155 | private static final String TEMP_FILE_EXTENSION = ".tmp"; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 156 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 157 | /** |
| 158 | * Folder suffix for temporary files folder. |
| 159 | */ |
| 160 | private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp"; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 161 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 162 | /** |
| 163 | * File name for getter method. |
| 164 | */ |
| 165 | private static final String GETTER_METHOD_FILE_NAME = "GetterMethod"; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 166 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 167 | /** |
| 168 | * File name for setter method. |
| 169 | */ |
| 170 | private static final String SETTER_METHOD_FILE_NAME = "SetterMethod"; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * File name for getter method implementation. |
| 174 | */ |
| 175 | private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl"; |
| 176 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 177 | /** |
| 178 | * File name for setter method implementation. |
| 179 | */ |
| 180 | private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl"; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 181 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 182 | /** |
| 183 | * File name for attributes. |
| 184 | */ |
| 185 | private static final String ATTRIBUTE_FILE_NAME = "Attributes"; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 186 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 187 | /** |
| 188 | * File name for to string method. |
| 189 | */ |
| 190 | private static final String TO_STRING_METHOD_FILE_NAME = "ToString"; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 191 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 192 | /** |
| 193 | * File name for hash code method. |
| 194 | */ |
| 195 | private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode"; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 196 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 197 | /** |
| 198 | * File name for equals method. |
| 199 | */ |
| 200 | private static final String EQUALS_METHOD_FILE_NAME = "Equals"; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 201 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 202 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 203 | * File name for from string method. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 204 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 205 | private static final String FROM_STRING_METHOD_FILE_NAME = "FromString"; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 206 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 207 | /** |
| 208 | * File name for interface java file name suffix. |
| 209 | */ |
| 210 | private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 211 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 212 | /** |
| 213 | * File name for builder interface file name suffix. |
| 214 | */ |
| 215 | private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 216 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 217 | /** |
| 218 | * File name for builder class file name suffix. |
| 219 | */ |
| 220 | private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 221 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 222 | /** |
| 223 | * File name for impl class file name suffix. |
| 224 | */ |
| 225 | private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 226 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 227 | /** |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 228 | * File name for augment copy constructor method. |
| 229 | */ |
| 230 | private static final String AUGMENT_CONSTRUCTOR_FILE_NAME = "AugmentConstructor"; |
| 231 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 232 | * Java file handle for interface file. |
| 233 | */ |
| 234 | private File interfaceJavaFileHandle; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 235 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 236 | /** |
| 237 | * Java file handle for builder interface file. |
| 238 | */ |
| 239 | private File builderInterfaceJavaFileHandle; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 240 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 241 | /** |
| 242 | * Java file handle for builder class file. |
| 243 | */ |
| 244 | private File builderClassJavaFileHandle; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 245 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 246 | /** |
| 247 | * Java file handle for impl class file. |
| 248 | */ |
| 249 | private File implClassJavaFileHandle; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 250 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 251 | /** |
| 252 | * Temporary file handle for attribute. |
| 253 | */ |
| 254 | private File attributesTempFileHandle; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 255 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 256 | /** |
| 257 | * Temporary file handle for getter of interface. |
| 258 | */ |
| 259 | private File getterInterfaceTempFileHandle; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 260 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 261 | /** |
| 262 | * Temporary file handle for setter of interface. |
| 263 | */ |
| 264 | private File setterInterfaceTempFileHandle; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * Temporary file handle for getter of class. |
| 268 | */ |
| 269 | private File getterImplTempFileHandle; |
| 270 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 271 | /** |
| 272 | * Temporary file handle for setter of class. |
| 273 | */ |
| 274 | private File setterImplTempFileHandle; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 275 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 276 | /** |
| 277 | * Temporary file handle for hash code method of class. |
| 278 | */ |
| 279 | private File hashCodeImplTempFileHandle; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 280 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 281 | /** |
| 282 | * Temporary file handle for equals method of class. |
| 283 | */ |
| 284 | private File equalsImplTempFileHandle; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 285 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 286 | /** |
| 287 | * Temporary file handle for to string method of class. |
| 288 | */ |
| 289 | private File toStringImplTempFileHandle; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 290 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 291 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 292 | * Temporary file handle for from string method of class. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 293 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 294 | private File fromStringImplTempFileHandle; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 295 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 296 | /** |
| 297 | * Import info for case. |
| 298 | */ |
| 299 | private JavaQualifiedTypeInfo caseImportInfo; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 300 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 301 | /** |
| 302 | * Is attribute added. |
| 303 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 304 | private boolean isAttributePresent; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 305 | |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 306 | /** |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 307 | * Temporary file handle for augments copy constructor method of class. |
| 308 | */ |
| 309 | private File augmentConstructorImplTempFileHandle; |
| 310 | |
| 311 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 312 | * Retrieves the absolute path where the file needs to be generated. |
| 313 | * |
| 314 | * @return absolute path where the file needs to be generated |
| 315 | */ |
| 316 | private String getAbsoluteDirPath() { |
| 317 | return absoluteDirPath; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Sets absolute path where the file needs to be generated. |
| 322 | * |
| 323 | * @param absoluteDirPath absolute path where the file needs to be |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 324 | * generated. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 325 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 326 | void setAbsoluteDirPath(String absoluteDirPath) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 327 | this.absoluteDirPath = absoluteDirPath; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Sets the generated java file information. |
| 332 | * |
| 333 | * @param javaFileInfo generated java file information |
| 334 | */ |
| 335 | public void setJavaFileInfo(JavaFileInfo javaFileInfo) { |
| 336 | this.javaFileInfo = javaFileInfo; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Retrieves the generated java file information. |
| 341 | * |
| 342 | * @return generated java file information |
| 343 | */ |
| 344 | public JavaFileInfo getJavaFileInfo() { |
| 345 | return javaFileInfo; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Retrieves the generated temp files. |
| 350 | * |
| 351 | * @return generated temp files |
| 352 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 353 | int getGeneratedTempFiles() { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 354 | return generatedTempFiles; |
| 355 | } |
| 356 | |
| 357 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 358 | * Clears the generated file mask. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 359 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 360 | void clearGeneratedTempFileMask() { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 361 | generatedTempFiles = 0; |
| 362 | } |
| 363 | |
| 364 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 365 | * Adds to generated temporary files. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 366 | * |
| 367 | * @param generatedTempFile generated file |
| 368 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 369 | void addGeneratedTempFile(int generatedTempFile) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 370 | generatedTempFiles |= generatedTempFile; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 371 | setGeneratedTempFiles(generatedTempFiles); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Sets generated file files. |
| 376 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 377 | * @param fileType generated file type |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 378 | */ |
| 379 | void setGeneratedTempFiles(int fileType) { |
| 380 | generatedTempFiles = fileType; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Retrieves the generated Java files. |
| 385 | * |
| 386 | * @return generated Java files |
| 387 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 388 | int getGeneratedJavaFiles() { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 389 | return getJavaFileInfo().getGeneratedFileTypes(); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Retrieves the mapped Java class name. |
| 394 | * |
| 395 | * @return mapped Java class name |
| 396 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 397 | String getGeneratedJavaClassName() { |
janani b | 703cfe4 | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 398 | return getCapitalCase(getJavaFileInfo().getJavaName()); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Retrieves the import data for the generated Java file. |
| 403 | * |
| 404 | * @return import data for the generated Java file |
| 405 | */ |
| 406 | public JavaImportData getJavaImportData() { |
| 407 | return javaImportData; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Sets import data for the generated Java file. |
| 412 | * |
| 413 | * @param javaImportData import data for the generated Java file |
| 414 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 415 | void setJavaImportData(JavaImportData javaImportData) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 416 | this.javaImportData = javaImportData; |
| 417 | } |
| 418 | |
| 419 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 420 | * Retrieves the status of any attributes added. |
| 421 | * |
| 422 | * @return status of any attributes added |
| 423 | */ |
| 424 | public boolean isAttributePresent() { |
| 425 | return isAttributePresent; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Sets status of any attributes added. |
| 430 | * |
| 431 | * @param attributePresent status of any attributes added |
| 432 | */ |
| 433 | public void setAttributePresent(boolean attributePresent) { |
| 434 | isAttributePresent = attributePresent; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Returns getter methods's temporary file handle. |
| 439 | * |
| 440 | * @return temporary file handle |
| 441 | */ |
| 442 | public File getGetterInterfaceTempFileHandle() { |
| 443 | return getterInterfaceTempFileHandle; |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Sets to getter method's temporary file handle. |
| 448 | * |
| 449 | * @param getterForInterface file handle for to getter method |
| 450 | */ |
| 451 | private void setGetterInterfaceTempFileHandle(File getterForInterface) { |
| 452 | getterInterfaceTempFileHandle = getterForInterface; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Returns setter method's temporary file handle. |
| 457 | * |
| 458 | * @return temporary file handle |
| 459 | */ |
| 460 | public File getSetterInterfaceTempFileHandle() { |
| 461 | return setterInterfaceTempFileHandle; |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Sets to setter method's temporary file handle. |
| 466 | * |
| 467 | * @param setterForInterface file handle for to setter method |
| 468 | */ |
| 469 | private void setSetterInterfaceTempFileHandle(File setterForInterface) { |
| 470 | setterInterfaceTempFileHandle = setterForInterface; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Returns setter method's impl's temporary file handle. |
| 475 | * |
| 476 | * @return temporary file handle |
| 477 | */ |
| 478 | public File getSetterImplTempFileHandle() { |
| 479 | return setterImplTempFileHandle; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Sets to setter method's impl's temporary file handle. |
| 484 | * |
| 485 | * @param setterImpl file handle for to setter method's implementation class |
| 486 | */ |
| 487 | private void setSetterImplTempFileHandle(File setterImpl) { |
| 488 | setterImplTempFileHandle = setterImpl; |
| 489 | } |
| 490 | |
| 491 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 492 | * Returns from string method's temporary file handle. |
| 493 | * |
| 494 | * @return from string method's temporary file handle |
| 495 | */ |
| 496 | public File getFromStringImplTempFileHandle() { |
| 497 | return fromStringImplTempFileHandle; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Sets from string method's temporary file handle. |
| 502 | * |
| 503 | * @param fromStringImplTempFileHandle from string method's temporary file |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 504 | * handle |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 505 | */ |
| 506 | private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) { |
| 507 | this.fromStringImplTempFileHandle = fromStringImplTempFileHandle; |
| 508 | } |
| 509 | |
| 510 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 511 | * Creates an instance of temporary java code fragment. |
| 512 | * |
| 513 | * @param javaFileInfo generated java file information |
| 514 | * @throws IOException when fails to create new file handle |
| 515 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 516 | TempJavaFragmentFiles(JavaFileInfo javaFileInfo) |
| 517 | throws IOException { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 518 | setJavaExtendsListHolder(new JavaExtendsListHolder()); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 519 | setJavaImportData(new JavaImportData()); |
| 520 | setJavaFileInfo(javaFileInfo); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 521 | setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(), |
| 522 | getJavaFileInfo().getPackageFilePath())); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 523 | |
| 524 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 525 | * Initialize getter when generation file type matches to interface |
| 526 | * mask. |
| 527 | */ |
| 528 | if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) { |
| 529 | addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK); |
| 530 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 531 | |
| 532 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 533 | * Initialize getter and setter when generation file type matches to |
| 534 | * builder interface mask. |
| 535 | */ |
| 536 | if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) { |
| 537 | addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK); |
| 538 | addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK); |
| 539 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 540 | |
| 541 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 542 | * Initialize getterImpl, setterImpl and attributes when generation file |
| 543 | * type matches to builder class mask. |
| 544 | */ |
| 545 | if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) { |
| 546 | addGeneratedTempFile(ATTRIBUTES_MASK); |
| 547 | addGeneratedTempFile(GETTER_FOR_CLASS_MASK); |
| 548 | addGeneratedTempFile(SETTER_FOR_CLASS_MASK); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 549 | addGeneratedTempFile(AUGMENTE_CLASS_CONSTRUCTOR_MASK); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 550 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 551 | |
| 552 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 553 | * Initialize getterImpl, attributes, constructor, hash code, equals and |
| 554 | * to strings when generation file type matches to impl class mask. |
| 555 | */ |
| 556 | if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) { |
| 557 | addGeneratedTempFile(ATTRIBUTES_MASK); |
| 558 | addGeneratedTempFile(GETTER_FOR_CLASS_MASK); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 559 | addGeneratedTempFile(HASH_CODE_IMPL_MASK); |
| 560 | addGeneratedTempFile(EQUALS_IMPL_MASK); |
| 561 | addGeneratedTempFile(TO_STRING_IMPL_MASK); |
| 562 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 563 | |
| 564 | /* |
| 565 | * Initialize temp files to generate type class. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 566 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 567 | if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 568 | addGeneratedTempFile(ATTRIBUTES_MASK); |
| 569 | addGeneratedTempFile(GETTER_FOR_CLASS_MASK); |
| 570 | addGeneratedTempFile(HASH_CODE_IMPL_MASK); |
| 571 | addGeneratedTempFile(EQUALS_IMPL_MASK); |
| 572 | addGeneratedTempFile(TO_STRING_IMPL_MASK); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 573 | addGeneratedTempFile(FROM_STRING_IMPL_MASK); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 574 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 575 | |
| 576 | /* |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 577 | * Initialize temp files to generate enum class. |
| 578 | */ |
| 579 | if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) { |
| 580 | addGeneratedTempFile(FROM_STRING_IMPL_MASK); |
| 581 | } |
| 582 | /* |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 583 | * Initialize getter and setter when generation file type matches to |
| 584 | * builder interface mask. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 585 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 586 | if ((getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) { |
| 587 | addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK); |
| 588 | addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 589 | addGeneratedTempFile(GETTER_FOR_CLASS_MASK); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 590 | addGeneratedTempFile(SETTER_FOR_CLASS_MASK); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 591 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 592 | |
| 593 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 594 | * Set temporary file handles. |
| 595 | */ |
| 596 | if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) { |
| 597 | setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME)); |
| 598 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 599 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 600 | if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) { |
| 601 | setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME)); |
| 602 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 603 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 604 | if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) { |
| 605 | setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME)); |
| 606 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 607 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 608 | if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) { |
| 609 | setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME)); |
| 610 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 611 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 612 | if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) { |
| 613 | setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME)); |
| 614 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 615 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 616 | if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) { |
| 617 | setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME)); |
| 618 | } |
| 619 | if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) { |
| 620 | setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME)); |
| 621 | } |
| 622 | if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) { |
| 623 | setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME)); |
| 624 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 625 | if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) { |
| 626 | setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME)); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 627 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 628 | if ((getGeneratedTempFiles() & AUGMENTE_CLASS_CONSTRUCTOR_MASK) != 0) { |
| 629 | setAugmentConstructorImplTempFileHandle(getTemporaryFileHandle(AUGMENT_CONSTRUCTOR_FILE_NAME)); |
| 630 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 631 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /** |
| 635 | * Returns java file handle for interface file. |
| 636 | * |
| 637 | * @return java file handle for interface file |
| 638 | */ |
| 639 | private File getInterfaceJavaFileHandle() { |
| 640 | return interfaceJavaFileHandle; |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Sets the java file handle for interface file. |
| 645 | * |
| 646 | * @param interfaceJavaFileHandle java file handle |
| 647 | */ |
| 648 | private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) { |
| 649 | this.interfaceJavaFileHandle = interfaceJavaFileHandle; |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Returns java file handle for builder interface file. |
| 654 | * |
| 655 | * @return java file handle for builder interface file |
| 656 | */ |
| 657 | private File getBuilderInterfaceJavaFileHandle() { |
| 658 | return builderInterfaceJavaFileHandle; |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Sets the java file handle for builder interface file. |
| 663 | * |
| 664 | * @param builderInterfaceJavaFileHandle java file handle |
| 665 | */ |
| 666 | private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) { |
| 667 | this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle; |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Returns java file handle for builder class file. |
| 672 | * |
| 673 | * @return java file handle for builder class file |
| 674 | */ |
| 675 | private File getBuilderClassJavaFileHandle() { |
| 676 | return builderClassJavaFileHandle; |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * Sets the java file handle for builder class file. |
| 681 | * |
| 682 | * @param builderClassJavaFileHandle java file handle |
| 683 | */ |
| 684 | private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) { |
| 685 | this.builderClassJavaFileHandle = builderClassJavaFileHandle; |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Returns java file handle for impl class file. |
| 690 | * |
| 691 | * @return java file handle for impl class file |
| 692 | */ |
| 693 | private File getImplClassJavaFileHandle() { |
| 694 | return implClassJavaFileHandle; |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * Sets the java file handle for impl class file. |
| 699 | * |
| 700 | * @param implClassJavaFileHandle java file handle |
| 701 | */ |
| 702 | private void setImplClassJavaFileHandle(File implClassJavaFileHandle) { |
| 703 | this.implClassJavaFileHandle = implClassJavaFileHandle; |
| 704 | } |
| 705 | |
| 706 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 707 | * Returns attribute's temporary file handle. |
| 708 | * |
| 709 | * @return temporary file handle |
| 710 | */ |
| 711 | public File getAttributesTempFileHandle() { |
| 712 | return attributesTempFileHandle; |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * Sets attribute's temporary file handle. |
| 717 | * |
| 718 | * @param attributeForClass file handle for attribute |
| 719 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 720 | void setAttributesTempFileHandle(File attributeForClass) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 721 | attributesTempFileHandle = attributeForClass; |
| 722 | } |
| 723 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 724 | /** |
| 725 | * Returns getter method's impl's temporary file handle. |
| 726 | * |
| 727 | * @return temporary file handle |
| 728 | */ |
| 729 | public File getGetterImplTempFileHandle() { |
| 730 | return getterImplTempFileHandle; |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Sets to getter method's impl's temporary file handle. |
| 735 | * |
| 736 | * @param getterImpl file handle for to getter method's impl |
| 737 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 738 | void setGetterImplTempFileHandle(File getterImpl) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 739 | getterImplTempFileHandle = getterImpl; |
| 740 | } |
| 741 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 742 | /** |
| 743 | * Returns hash code method's temporary file handle. |
| 744 | * |
| 745 | * @return temporary file handle |
| 746 | */ |
| 747 | public File getHashCodeImplTempFileHandle() { |
| 748 | return hashCodeImplTempFileHandle; |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * Sets hash code method's temporary file handle. |
| 753 | * |
| 754 | * @param hashCodeMethod file handle for hash code method |
| 755 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 756 | void setHashCodeImplTempFileHandle(File hashCodeMethod) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 757 | hashCodeImplTempFileHandle = hashCodeMethod; |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Returns equals mehtod's temporary file handle. |
| 762 | * |
| 763 | * @return temporary file handle |
| 764 | */ |
| 765 | public File getEqualsImplTempFileHandle() { |
| 766 | return equalsImplTempFileHandle; |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * Sets equals method's temporary file handle. |
| 771 | * |
| 772 | * @param equalsMethod file handle for to equals method |
| 773 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 774 | void setEqualsImplTempFileHandle(File equalsMethod) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 775 | equalsImplTempFileHandle = equalsMethod; |
| 776 | } |
| 777 | |
| 778 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 779 | * Returns to string method's temporary file handle. |
| 780 | * |
| 781 | * @return temporary file handle |
| 782 | */ |
| 783 | public File getToStringImplTempFileHandle() { |
| 784 | return toStringImplTempFileHandle; |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * Sets to string method's temporary file handle. |
| 789 | * |
| 790 | * @param toStringMethod file handle for to string method |
| 791 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 792 | void setToStringImplTempFileHandle(File toStringMethod) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 793 | toStringImplTempFileHandle = toStringMethod; |
| 794 | } |
| 795 | |
| 796 | /** |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 797 | * Returns java extends list holder. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 798 | * |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 799 | * @return java extends list holder |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 800 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 801 | public JavaExtendsListHolder getJavaExtendsListHolder() { |
| 802 | return javaExtendsListHolder; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /** |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 806 | * Sets java extends list holder. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 807 | * |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 808 | * @param javaExtendsListHolder java extends list holder |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 809 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 810 | public void setJavaExtendsListHolder(JavaExtendsListHolder javaExtendsListHolder) { |
| 811 | this.javaExtendsListHolder = javaExtendsListHolder; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /** |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 815 | * Returns augments copy constructor method impl class file. |
| 816 | * |
| 817 | * @return augments copy constructor method impl class file |
| 818 | */ |
| 819 | public File getAugmentConstructorImplTempFileHandle() { |
| 820 | return augmentConstructorImplTempFileHandle; |
| 821 | } |
| 822 | |
| 823 | /** |
| 824 | * Sets augments copy constructor method impl class. |
| 825 | * |
| 826 | * @param augmentConstructorImplTempFileHandle augments copy constructor method impl class file |
| 827 | */ |
| 828 | public void setAugmentConstructorImplTempFileHandle(File augmentConstructorImplTempFileHandle) { |
| 829 | this.augmentConstructorImplTempFileHandle = augmentConstructorImplTempFileHandle; |
| 830 | } |
| 831 | |
| 832 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 833 | * Adds attribute for class. |
| 834 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 835 | * @param attr attribute info |
| 836 | * @param yangPluginConfig plugin configurations |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 837 | * @throws IOException when fails to append to temporary file |
| 838 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 839 | private void addAttribute(JavaAttributeInfo attr, YangPluginConfig yangPluginConfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 840 | throws IOException { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 841 | appendToFile(getAttributesTempFileHandle(), parseAttribute(attr, yangPluginConfig) |
| 842 | + FOUR_SPACE_INDENTATION); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /** |
| 846 | * Adds getter for interface. |
| 847 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 848 | * @param attr attribute info |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 849 | * @param pluginConfig plugin configurations |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 850 | * @throws IOException when fails to append to temporary file |
| 851 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 852 | private void addGetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 853 | throws IOException { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 854 | appendToFile(getGetterInterfaceTempFileHandle(), |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 855 | getGetterString(attr, getGeneratedJavaFiles(), pluginConfig) + NEW_LINE); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /** |
| 859 | * Adds setter for interface. |
| 860 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 861 | * @param attr attribute info |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 862 | * @param pluginConfig plugin configurations |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 863 | * @throws IOException when fails to append to temporary file |
| 864 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 865 | private void addSetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 866 | throws IOException { |
| 867 | appendToFile(getSetterInterfaceTempFileHandle(), |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 868 | getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles(), pluginConfig) |
| 869 | + NEW_LINE); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | /** |
| 873 | * Adds setter's implementation for class. |
| 874 | * |
| 875 | * @param attr attribute info |
| 876 | * @throws IOException when fails to append to temporary file |
| 877 | */ |
| 878 | private void addSetterImpl(JavaAttributeInfo attr) |
| 879 | throws IOException { |
| 880 | appendToFile(getSetterImplTempFileHandle(), |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 881 | getOverRideString() + getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles()) |
| 882 | + |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 883 | NEW_LINE); |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * Adds getter method's impl for class. |
| 888 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 889 | * @param attr attribute info |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 890 | * @param pluginConfig plugin configurations |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 891 | * @throws IOException when fails to append to temporary file |
| 892 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 893 | private void addGetterImpl(JavaAttributeInfo attr, YangPluginConfig pluginConfig) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 894 | throws IOException { |
| 895 | if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0 |
| 896 | || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) { |
| 897 | appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr, |
| 898 | getGeneratedJavaFiles()) + NEW_LINE); |
| 899 | } else { |
janani b | 703cfe4 | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 900 | appendToFile(getGetterImplTempFileHandle(), |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 901 | getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig) |
janani b | 703cfe4 | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 902 | + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 903 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Adds build method for interface. |
| 908 | * |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 909 | * @param pluginConfig plugin configurations |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 910 | * @return build method for interface |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 911 | * @throws IOException when fails to append to temporary file |
| 912 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 913 | String addBuildMethodForInterface(YangPluginConfig pluginConfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 914 | throws IOException { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 915 | return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName(), pluginConfig); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | /** |
| 919 | * Adds build method's implementation for class. |
| 920 | * |
| 921 | * @return build method implementation for class |
| 922 | * @throws IOException when fails to append to temporary file |
| 923 | */ |
| 924 | String addBuildMethodImpl() |
| 925 | throws IOException { |
| 926 | return getBuildString(getGeneratedJavaClassName()) + NEW_LINE; |
| 927 | } |
| 928 | |
| 929 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 930 | * Adds default constructor for class. |
| 931 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 932 | * @param modifier modifier for constructor. |
| 933 | * @param toAppend string which need to be appended with the class name |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 934 | * @param pluginConfig plugin configurations |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 935 | * @return default constructor for class |
| 936 | * @throws IOException when fails to append to file |
| 937 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 938 | String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 939 | throws IOException { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 940 | return NEW_LINE |
| 941 | + getDefaultConstructorString(getGeneratedJavaClassName() + toAppend, modifier, pluginConfig); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /** |
| 945 | * Adds default constructor for class. |
| 946 | * |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 947 | * @param pluginCnfig plugin configurations |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 948 | * @return default constructor for class |
| 949 | * @throws IOException when fails to append to file |
| 950 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 951 | public String addOfMethod(YangPluginConfig pluginCnfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 952 | throws IOException { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 953 | return getJavaDoc(OF_METHOD, getGeneratedJavaClassName(), false, pluginCnfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 954 | + getOfMethod(getGeneratedJavaClassName(), null); |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * Adds hash code method for class. |
| 959 | * |
| 960 | * @param attr attribute info |
| 961 | * @throws IOException when fails to append to temporary file |
| 962 | */ |
| 963 | private void addHashCodeMethod(JavaAttributeInfo attr) |
| 964 | throws IOException { |
| 965 | appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE); |
| 966 | } |
| 967 | |
| 968 | /** |
| 969 | * Adds equals method for class. |
| 970 | * |
| 971 | * @param attr attribute info |
| 972 | * @throws IOException when fails to append to temporary file |
| 973 | */ |
| 974 | private void addEqualsMethod(JavaAttributeInfo attr) |
| 975 | throws IOException { |
| 976 | appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE); |
| 977 | } |
| 978 | |
| 979 | /** |
| 980 | * Adds ToString method for class. |
| 981 | * |
| 982 | * @param attr attribute info |
| 983 | * @throws IOException when fails to append to temporary file |
| 984 | */ |
| 985 | private void addToStringMethod(JavaAttributeInfo attr) |
| 986 | throws IOException { |
| 987 | appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE); |
| 988 | } |
| 989 | |
| 990 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 991 | * Adds from string method for union class. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 992 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 993 | * @param javaAttributeInfo type attribute info |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 994 | * @param fromStringAttributeInfo from string attribute info |
| 995 | * @throws IOException when fails to append to temporary file |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 996 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 997 | private void addFromStringMethod(JavaAttributeInfo javaAttributeInfo, |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 998 | JavaAttributeInfo fromStringAttributeInfo) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 999 | throws IOException { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 1000 | appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo, |
| 1001 | fromStringAttributeInfo) + NEW_LINE); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1002 | } |
| 1003 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1004 | /** |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1005 | * Adds constructor for augment class. |
| 1006 | * |
| 1007 | * @param attr attribute info |
| 1008 | * @throws IOException when fails to append to temporary file |
| 1009 | */ |
| 1010 | private void addAugmentConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig) |
| 1011 | throws IOException { |
| 1012 | appendToFile(getAugmentConstructorImplTempFileHandle(), getAugmentedConstructor(attr, pluginConfig)); |
| 1013 | } |
| 1014 | |
| 1015 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1016 | * Returns a temporary file handle for the specific file type. |
| 1017 | * |
| 1018 | * @param fileName file name |
| 1019 | * @return temporary file handle |
| 1020 | * @throws IOException when fails to create new file handle |
| 1021 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1022 | File getTemporaryFileHandle(String fileName) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1023 | throws IOException { |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1024 | String path = getTempDirPath(getAbsoluteDirPath()); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1025 | File dir = new File(path); |
| 1026 | if (!dir.exists()) { |
| 1027 | dir.mkdirs(); |
| 1028 | } |
| 1029 | File file = new File(path + fileName + TEMP_FILE_EXTENSION); |
| 1030 | if (!file.exists()) { |
| 1031 | file.createNewFile(); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1032 | } else { |
| 1033 | throw new IOException(fileName + " is reused due to YANG naming"); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1034 | } |
| 1035 | return file; |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * Returns a temporary file handle for the specific file type. |
| 1040 | * |
| 1041 | * @param fileName file name |
| 1042 | * @return temporary file handle |
| 1043 | * @throws IOException when fails to create new file handle |
| 1044 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1045 | File getJavaFileHandle(String fileName) |
| 1046 | throws IOException { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1047 | return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo()); |
| 1048 | } |
| 1049 | |
| 1050 | /** |
| 1051 | * Returns data from the temporary files. |
| 1052 | * |
| 1053 | * @param file temporary file handle |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1054 | * @param absolutePath absolute path |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1055 | * @return stored data from temporary files |
| 1056 | * @throws IOException when failed to get data from the given file |
| 1057 | */ |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1058 | public String getTemporaryDataFromFileHandle(File file, String absolutePath) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1059 | throws IOException { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1060 | |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1061 | String path = getTempDirPath(absolutePath); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1062 | if (new File(path + file.getName()).exists()) { |
| 1063 | return readAppendFile(path + file.getName(), EMPTY_STRING); |
| 1064 | } else { |
| 1065 | throw new IOException("Unable to get data from the given " |
| 1066 | + file.getName() + " file for " + getGeneratedJavaClassName() + PERIOD); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | /** |
| 1071 | * Returns temporary directory path. |
| 1072 | * |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1073 | * @param absolutePath absolute path |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1074 | * @return directory path |
| 1075 | */ |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1076 | String getTempDirPath(String absolutePath) { |
| 1077 | return getPackageDirPathFromJavaJPackage(absolutePath) + SLASH + getGeneratedJavaClassName() |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1078 | + TEMP_FOLDER_NAME_SUFIX + SLASH; |
| 1079 | } |
| 1080 | |
| 1081 | /** |
| 1082 | * Parses attribute to get the attribute string. |
| 1083 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1084 | * @param attr attribute info |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1085 | * @param pluginConfig plugin configurations |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1086 | * @return attribute string |
| 1087 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1088 | public String parseAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1089 | /* |
| 1090 | * TODO: check if this utility needs to be called or move to the caller |
| 1091 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1092 | String attributeName = getCamelCase(attr.getAttributeName(), pluginConfig.getConflictResolver()); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1093 | if (attr.isQualifiedName()) { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 1094 | return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), |
| 1095 | attr.getImportInfo().getClassInfo(), |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1096 | attributeName, attr.isListAttr()); |
| 1097 | } else { |
| 1098 | return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName, |
| 1099 | attr.isListAttr()); |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | /** |
| 1104 | * Appends content to temporary file. |
| 1105 | * |
| 1106 | * @param file temporary file |
| 1107 | * @param data data to be appended |
| 1108 | * @throws IOException when fails to append to file |
| 1109 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1110 | void appendToFile(File file, String data) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1111 | throws IOException { |
| 1112 | try { |
| 1113 | insertDataIntoJavaFile(file, data); |
| 1114 | } catch (IOException ex) { |
| 1115 | throw new IOException("failed to write in temp file."); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | /** |
| 1120 | * Adds current node info as and attribute to the parent generated file. |
| 1121 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1122 | * @param curNode current node which needs to be added as an attribute in |
| 1123 | * the parent generated code |
| 1124 | * @param isList is list construct |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1125 | * @param pluginConfig plugin configurations |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1126 | * @throws IOException IO operation exception |
| 1127 | */ |
| 1128 | public static void addCurNodeInfoInParentTempFile(YangNode curNode, |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1129 | boolean isList, YangPluginConfig pluginConfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1130 | throws IOException { |
| 1131 | YangNode parent = getParentNodeInGenCode(curNode); |
| 1132 | if (!(parent instanceof JavaCodeGenerator)) { |
| 1133 | throw new TranslatorException("missing parent node to contain current node info in generated file"); |
| 1134 | } |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1135 | |
| 1136 | if (parent instanceof YangJavaGrouping) { |
| 1137 | /* |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 1138 | * In case of grouping, there is no need to add the information, it |
| 1139 | * will be taken care in uses |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1140 | */ |
| 1141 | return; |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 1142 | } |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1143 | |
| 1144 | JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInTarget(curNode, |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1145 | parent, isList); |
| 1146 | if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) { |
| 1147 | throw new TranslatorException("missing parent temp file handle"); |
| 1148 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1149 | getNodesInterfaceFragmentFiles(parent) |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1150 | .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1151 | } |
| 1152 | |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1153 | /** |
| 1154 | * Adds current node info as and attribute to the parent generated file. |
| 1155 | * |
| 1156 | * @param curNode current node which needs to be added as an attribute in |
| 1157 | * the parent generated code |
| 1158 | * @param pluginConfig plugin configurations |
| 1159 | * @param targetNode target node to add the attribute |
| 1160 | * @throws IOException IO operation exception |
| 1161 | */ |
| 1162 | public static void addCurNodeAsAttributeInTargetTempFile(YangNode curNode, |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1163 | YangPluginConfig pluginConfig, YangNode targetNode) |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1164 | throws IOException { |
| 1165 | |
| 1166 | if (!(targetNode instanceof JavaCodeGenerator)) { |
| 1167 | throw new TranslatorException("invalid target node to generated file"); |
| 1168 | } |
| 1169 | |
| 1170 | if (targetNode instanceof YangJavaGrouping) { |
| 1171 | /* |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 1172 | * In case of grouping, there is no need to add the information, it |
| 1173 | * will be taken care in uses |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1174 | */ |
| 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | boolean isList = curNode instanceof YangList; |
| 1179 | |
| 1180 | JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInTarget(curNode, |
| 1181 | targetNode, isList); |
| 1182 | if (!(targetNode instanceof TempJavaCodeFragmentFilesContainer)) { |
| 1183 | throw new TranslatorException("missing target node's temp file handle"); |
| 1184 | } |
| 1185 | getNodesInterfaceFragmentFiles(targetNode) |
| 1186 | .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig); |
| 1187 | } |
| 1188 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1189 | /** |
| 1190 | * Creates an attribute info object corresponding to a data model node and |
| 1191 | * return it. |
| 1192 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1193 | * @param curNode current data model node for which the java code generation |
| 1194 | * is being handled |
| 1195 | * @param targetNode target node in which the current node is an attribute |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1196 | * @param isListNode is the current added attribute needs to be a list |
| 1197 | * @return AttributeInfo attribute details required to add in temporary |
| 1198 | * files |
| 1199 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1200 | public static JavaAttributeInfo getCurNodeAsAttributeInTarget(YangNode curNode, |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1201 | YangNode targetNode, boolean isListNode) { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1202 | String curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName(); |
| 1203 | if (curNodeName == null) { |
| 1204 | updateJavaFileInfo(curNode, null); |
| 1205 | curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName(); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 1206 | } |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 1207 | |
| 1208 | /* |
| 1209 | * Get the import info corresponding to the attribute for import in |
| 1210 | * generated java files or qualified access |
| 1211 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1212 | JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode, |
| 1213 | getCapitalCase(curNodeName)); |
| 1214 | if (!(targetNode instanceof TempJavaCodeFragmentFilesContainer)) { |
| 1215 | throw new TranslatorException("Parent node does not have file info"); |
| 1216 | } |
| 1217 | TempJavaFragmentFiles tempJavaFragmentFiles = getNodesInterfaceFragmentFiles(targetNode); |
| 1218 | JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData(); |
| 1219 | JavaFileInfo fileInfo = ((JavaFileInfoContainer) targetNode).getJavaFileInfo(); |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 1220 | |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1221 | boolean isQualified; |
| 1222 | if ((targetNode instanceof YangJavaModule || targetNode instanceof YangJavaSubModule) |
| 1223 | && (qualifiedTypeInfo.getClassInfo().contentEquals(SERVICE) |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1224 | || qualifiedTypeInfo.getClassInfo().contentEquals(COMPONENT) |
| 1225 | || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(ACTIVATE)) |
| 1226 | || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(DEACTIVATE)) |
| 1227 | || qualifiedTypeInfo.getClassInfo().contentEquals(REFERENCE_CARDINALITY) |
| 1228 | || qualifiedTypeInfo.getClassInfo().contentEquals(REFERENCE)) |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1229 | || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + SERVICE)) |
| 1230 | || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + MANAGER))) { |
| 1231 | |
| 1232 | isQualified = true; |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 1233 | } else { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1234 | String className; |
| 1235 | if (targetNode instanceof YangJavaModule || targetNode instanceof YangJavaSubModule) { |
| 1236 | className = getCapitalCase(fileInfo.getJavaName()) + "Service"; |
| 1237 | } else { |
| 1238 | className = getCapitalCase(fileInfo.getJavaName()); |
| 1239 | } |
| 1240 | |
| 1241 | isQualified = parentImportData.addImportInfo(qualifiedTypeInfo, |
| 1242 | className, fileInfo.getPackage()); |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 1243 | } |
| 1244 | |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1245 | if (isListNode) { |
| 1246 | parentImportData.setIfListImported(true); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 1247 | } |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1248 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1249 | return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode); |
| 1250 | } |
| 1251 | |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 1252 | /** |
| 1253 | * Returns interface fragment files for node. |
| 1254 | * |
| 1255 | * @param node YANG node |
| 1256 | * @return interface fragment files for node |
| 1257 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1258 | public static TempJavaFragmentFiles getNodesInterfaceFragmentFiles(YangNode node) { |
| 1259 | TempJavaFragmentFiles tempJavaFragmentFiles; |
| 1260 | if (node instanceof RpcNotificationContainer) { |
| 1261 | tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node) |
| 1262 | .getTempJavaCodeFragmentFiles() |
| 1263 | .getServiceTempFiles(); |
| 1264 | } else { |
| 1265 | tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node) |
| 1266 | .getTempJavaCodeFragmentFiles() |
| 1267 | .getBeanTempFiles(); |
| 1268 | } |
| 1269 | return tempJavaFragmentFiles; |
| 1270 | } |
| 1271 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1272 | /** |
| 1273 | * Adds parent's info to current node import list. |
| 1274 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1275 | * @param curNode current node for which import list needs to be updated |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1276 | * @param pluginConfig plugin configurations |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1277 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1278 | public void addParentInfoInCurNodeTempFile(YangNode curNode, YangPluginConfig pluginConfig) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1279 | caseImportInfo = new JavaQualifiedTypeInfo(); |
| 1280 | YangNode parent = getParentNodeInGenCode(curNode); |
| 1281 | if (!(parent instanceof JavaCodeGenerator)) { |
| 1282 | throw new TranslatorException("missing parent node to contain current node info in generated file"); |
| 1283 | } |
| 1284 | if (!(curNode instanceof JavaFileInfoContainer)) { |
| 1285 | throw new TranslatorException("missing java file information to get the package details " |
| 1286 | + "of attribute corresponding to child node"); |
| 1287 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1288 | caseImportInfo.setClassInfo(getCapitalCase(getCamelCase(parent.getName(), |
| 1289 | pluginConfig.getConflictResolver()))); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1290 | caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage()); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1291 | |
| 1292 | JavaFileInfo fileInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo(); |
| 1293 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1294 | ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles() |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1295 | .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo, |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1296 | getCapitalCase(fileInfo.getJavaName()), fileInfo.getPackage()); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | /** |
| 1300 | * Adds leaf attributes in generated files. |
| 1301 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1302 | * @param listOfLeaves list of YANG leaf |
| 1303 | * @param yangPluginConfig plugin config |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1304 | * @throws IOException IO operation fail |
| 1305 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1306 | public void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves, |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1307 | YangPluginConfig yangPluginConfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1308 | throws IOException { |
| 1309 | if (listOfLeaves != null) { |
| 1310 | for (YangLeaf leaf : listOfLeaves) { |
| 1311 | if (!(leaf instanceof JavaLeafInfoContainer)) { |
| 1312 | throw new TranslatorException("Leaf does not have java information"); |
| 1313 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1314 | addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeaf(leaf, yangPluginConfig), |
| 1315 | yangPluginConfig); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | /** |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1321 | * Returns java attribute for leaf. |
| 1322 | * |
| 1323 | * @param leaf YANG leaf |
| 1324 | * @param yangPluginConfig plugin configurations |
| 1325 | * @return java attribute for leaf |
| 1326 | */ |
| 1327 | private JavaAttributeInfo getJavaAttributeOfLeaf(YangLeaf leaf, |
| 1328 | YangPluginConfig yangPluginConfig) { |
| 1329 | JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf; |
| 1330 | javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver()); |
| 1331 | javaLeaf.updateJavaQualifiedInfo(); |
| 1332 | return getAttributeInfoForTheData( |
| 1333 | javaLeaf.getJavaQualifiedInfo(), |
| 1334 | javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()), |
| 1335 | javaLeaf.getDataType(), |
| 1336 | getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()), |
| 1337 | false); |
| 1338 | } |
| 1339 | |
| 1340 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1341 | * Adds leaf list's attributes in generated files. |
| 1342 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1343 | * @param listOfLeafList list of YANG leaves |
| 1344 | * @param yangPluginConfig plugin config |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1345 | * @throws IOException IO operation fail |
| 1346 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1347 | public void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1348 | throws IOException { |
| 1349 | if (listOfLeafList != null) { |
| 1350 | for (YangLeafList leafList : listOfLeafList) { |
| 1351 | if (!(leafList instanceof JavaLeafInfoContainer)) { |
| 1352 | throw new TranslatorException("Leaf-list does not have java information"); |
| 1353 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1354 | addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeafList(leafList, yangPluginConfig), |
| 1355 | yangPluginConfig); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | /** |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1361 | * Returns java attribute for leaf-list. |
| 1362 | * |
| 1363 | * @param leafList YANG leaf-list |
| 1364 | * @param yangPluginConfig plugin configurations |
| 1365 | * @return java attribute for leaf-list |
| 1366 | */ |
| 1367 | private JavaAttributeInfo getJavaAttributeOfLeafList(YangLeafList leafList, YangPluginConfig yangPluginConfig) { |
| 1368 | JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList; |
| 1369 | javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver()); |
| 1370 | javaLeaf.updateJavaQualifiedInfo(); |
| 1371 | getJavaImportData().setIfListImported(true); |
| 1372 | return getAttributeInfoForTheData( |
| 1373 | javaLeaf.getJavaQualifiedInfo(), |
| 1374 | javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()), |
| 1375 | javaLeaf.getDataType(), |
| 1376 | getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()), |
| 1377 | true); |
| 1378 | } |
| 1379 | |
| 1380 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1381 | * Adds all the leaves in the current data model node as part of the |
| 1382 | * generated temporary file. |
| 1383 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1384 | * @param curNode java file info of the generated file |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1385 | * @param yangPluginConfig plugin config |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1386 | * @throws IOException IO operation fail |
| 1387 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1388 | public void addCurNodeLeavesInfoToTempFiles(YangNode curNode, |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1389 | YangPluginConfig yangPluginConfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1390 | throws IOException { |
| 1391 | if (!(curNode instanceof YangLeavesHolder)) { |
| 1392 | throw new TranslatorException("Data model node does not have any leaves"); |
| 1393 | } |
| 1394 | YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1395 | addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig); |
| 1396 | addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1400 | * Adds the new attribute info to the target generated temporary files. |
| 1401 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1402 | * @param newAttrInfo the attribute info that needs to be added to temporary |
| 1403 | * files |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1404 | * @param pluginConfig plugin configurations |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1405 | * @throws IOException IO operation fail |
| 1406 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1407 | void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1408 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1409 | setAttributePresent(true); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1410 | if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1411 | addAttribute(newAttrInfo, pluginConfig); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1412 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1413 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1414 | if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1415 | addGetterForInterface(newAttrInfo, pluginConfig); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1416 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1417 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1418 | if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1419 | addSetterForInterface(newAttrInfo, pluginConfig); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1420 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1421 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1422 | if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) { |
| 1423 | addSetterImpl(newAttrInfo); |
| 1424 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1425 | |
| 1426 | if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1427 | addGetterImpl(newAttrInfo, pluginConfig); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1428 | } |
| 1429 | if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) { |
| 1430 | addHashCodeMethod(newAttrInfo); |
| 1431 | } |
| 1432 | if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) { |
| 1433 | addEqualsMethod(newAttrInfo); |
| 1434 | } |
| 1435 | if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) { |
| 1436 | addToStringMethod(newAttrInfo); |
| 1437 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 1438 | |
| 1439 | if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 1440 | JavaQualifiedTypeInfo qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo, |
| 1441 | pluginConfig.getConflictResolver()); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 1442 | /* |
| 1443 | * Create a new java attribute info with qualified information of |
| 1444 | * wrapper classes. |
| 1445 | */ |
| 1446 | JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString, |
| 1447 | newAttrInfo.getAttributeName(), |
| 1448 | newAttrInfo.getAttributeType(), |
| 1449 | getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false); |
| 1450 | |
| 1451 | addFromStringMethod(newAttrInfo, fromStringAttributeInfo); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1452 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | /** |
| 1456 | * Returns java class name. |
| 1457 | * |
| 1458 | * @param suffix for the class name based on the file type |
| 1459 | * @return java class name |
| 1460 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1461 | String getJavaClassName(String suffix) { |
janani b | 703cfe4 | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 1462 | return getCapitalCase(getJavaFileInfo().getJavaName()) + suffix; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | /** |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1466 | * Returns java class name. |
| 1467 | * |
| 1468 | * @return java class name |
| 1469 | */ |
| 1470 | String getImplClassName() { |
| 1471 | return getCapitalCase(DEFAULT) + getCapitalCase(getJavaFileInfo().getJavaName()); |
| 1472 | } |
| 1473 | |
| 1474 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1475 | * Returns the directory path. |
| 1476 | * |
| 1477 | * @return directory path |
| 1478 | */ |
| 1479 | private String getDirPath() { |
| 1480 | return getJavaFileInfo().getPackageFilePath(); |
| 1481 | } |
| 1482 | |
| 1483 | /** |
| 1484 | * Constructs java code exit. |
| 1485 | * |
| 1486 | * @param fileType generated file type |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1487 | * @param curNode current YANG node |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1488 | * @throws IOException when fails to generate java files |
| 1489 | */ |
| 1490 | public void generateJavaFile(int fileType, YangNode curNode) |
| 1491 | throws IOException { |
| 1492 | List<String> imports = new ArrayList<>(); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1493 | imports = getJavaImportData().getImports(); |
| 1494 | |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1495 | JavaFileInfo curInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo(); |
| 1496 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1497 | createPackage(curNode); |
| 1498 | |
| 1499 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1500 | * Generate java code. |
| 1501 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1502 | if ((fileType & INTERFACE_MASK) != 0 || (fileType & |
| 1503 | BUILDER_INTERFACE_MASK) != 0) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 1504 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1505 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1506 | * Create interface file. |
| 1507 | */ |
| 1508 | setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX))); |
| 1509 | setInterfaceJavaFileHandle( |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1510 | generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent())); |
| 1511 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1512 | * Create builder interface file. |
| 1513 | */ |
| 1514 | if ((fileType & BUILDER_INTERFACE_MASK) != 0) { |
| 1515 | setBuilderInterfaceJavaFileHandle( |
| 1516 | getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX))); |
| 1517 | setBuilderInterfaceJavaFileHandle( |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1518 | generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode, |
| 1519 | isAttributePresent())); |
| 1520 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1521 | * Append builder interface file to interface file and close it. |
| 1522 | */ |
| 1523 | mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle()); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 1524 | validateLineLength(getInterfaceJavaFileHandle()); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1525 | } |
| 1526 | insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose()); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1527 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1528 | if (curNode instanceof YangCase) { |
| 1529 | removeCaseImport(imports); |
| 1530 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1531 | if (curNode instanceof YangJavaAugment) { |
| 1532 | updateAugmentConstructorTempFile(curNode, curInfo.getPluginConfig()); |
| 1533 | YangJavaAugment augment = (YangJavaAugment) curNode; |
| 1534 | List<JavaQualifiedTypeInfo> infoList = augment.getExtendedClassInfo(); |
| 1535 | for (JavaQualifiedTypeInfo info : infoList) { |
| 1536 | if (info.getClassInfo() |
| 1537 | .equals(getCapitalCase(getCamelCase(augment.getAugmentedNode().getName(), null)))) { |
| 1538 | removeAugmentedImport(imports, info); |
| 1539 | } |
| 1540 | |
| 1541 | } |
| 1542 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1543 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 1544 | if ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & IMPL_CLASS_MASK) != 0) { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1545 | if (isAttributePresent()) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1546 | addImportsToStringAndHasCodeMethods(curNode, imports); |
| 1547 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 1548 | sortImports(imports); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1549 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1550 | * Create impl class file. |
| 1551 | */ |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1552 | setImplClassJavaFileHandle(getJavaFileHandle(getImplClassName())); |
| 1553 | setImplClassJavaFileHandle( |
| 1554 | generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent(), imports)); |
| 1555 | |
| 1556 | /* |
| 1557 | * Create builder class file. |
| 1558 | */ |
| 1559 | if ((fileType & BUILDER_CLASS_MASK) != 0) { |
| 1560 | setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX))); |
| 1561 | setBuilderClassJavaFileHandle( |
| 1562 | generateBuilderClassFile(getBuilderClassJavaFileHandle(), curNode, |
| 1563 | isAttributePresent())); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1564 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1565 | * Append impl class to builder class and close it. |
| 1566 | */ |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1567 | mergeJavaFiles(getBuilderClassJavaFileHandle(), getImplClassJavaFileHandle()); |
| 1568 | validateLineLength(getImplClassJavaFileHandle()); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1569 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1570 | insertDataIntoJavaFile(getImplClassJavaFileHandle(), getJavaClassDefClose()); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1571 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1572 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1573 | * Close all the file handles. |
| 1574 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1575 | freeTemporaryResources(false); |
| 1576 | } |
| 1577 | |
| 1578 | /** |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1579 | * Updates augment constructor temp file. |
| 1580 | * |
| 1581 | * @param curNode current augment node |
| 1582 | * @param pluginConfig plugin configurations |
| 1583 | * @throws IOException when fails to do IO operations |
| 1584 | */ |
| 1585 | private void updateAugmentConstructorTempFile(YangNode curNode, YangPluginConfig pluginConfig) |
| 1586 | throws IOException { |
| 1587 | YangJavaAugment augment = (YangJavaAugment) curNode; |
| 1588 | |
| 1589 | YangNode augmentedNode = augment.getAugmentedNode(); |
| 1590 | if (augmentedNode instanceof YangLeavesHolder) { |
| 1591 | YangLeavesHolder holder = (YangLeavesHolder) augmentedNode; |
| 1592 | if (holder.getListOfLeaf() != null) { |
| 1593 | for (YangLeaf leaf : holder.getListOfLeaf()) { |
| 1594 | addAugmentConstructor(getJavaAttributeOfLeaf(leaf, |
| 1595 | pluginConfig), pluginConfig); |
| 1596 | } |
| 1597 | |
| 1598 | } |
| 1599 | if (holder.getListOfLeafList() != null) { |
| 1600 | for (YangLeafList leafList : holder.getListOfLeafList()) { |
| 1601 | addAugmentConstructor(getJavaAttributeOfLeafList(leafList, |
| 1602 | pluginConfig), pluginConfig); |
| 1603 | } |
| 1604 | |
| 1605 | } |
| 1606 | } |
| 1607 | augmentedNode = augmentedNode.getChild(); |
| 1608 | boolean isList = false; |
| 1609 | while (augmentedNode != null) { |
| 1610 | if (augmentedNode instanceof YangList) { |
| 1611 | isList = true; |
| 1612 | } |
| 1613 | addAugmentConstructor(getCurNodeAsAttributeInTarget(augmentedNode, augment, isList), pluginConfig); |
| 1614 | augmentedNode = augmentedNode.getNextSibling(); |
| 1615 | } |
| 1616 | |
| 1617 | } |
| 1618 | |
| 1619 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1620 | * Adds imports for ToString and HashCodeMethod. |
| 1621 | * |
| 1622 | * @param curNode current YANG node |
| 1623 | * @param imports import list |
| 1624 | * @return import list |
| 1625 | */ |
| 1626 | public List<String> addImportsToStringAndHasCodeMethods(YangNode curNode, List<String> imports) { |
| 1627 | imports.add(getJavaImportData().getImportForHashAndEquals()); |
| 1628 | imports.add(getJavaImportData().getImportForToString()); |
| 1629 | return imports; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | /** |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1633 | * Removes augmented node import info from import list. |
| 1634 | * |
| 1635 | * @param imports list of imports |
| 1636 | * @return import for class |
| 1637 | */ |
| 1638 | private List<String> removeAugmentedImport(List<String> imports, JavaQualifiedTypeInfo augmentedInfo) { |
| 1639 | String augmentedNodeImport = null; |
| 1640 | if (imports != null && augmentedInfo != null) { |
| 1641 | augmentedNodeImport = IMPORT + augmentedInfo.getPkgInfo() + PERIOD + getCapitalCase(DEFAULT) |
| 1642 | + augmentedInfo.getClassInfo() + PERIOD + augmentedInfo.getClassInfo() |
| 1643 | + BUILDER + SEMI_COLAN + NEW_LINE; |
| 1644 | imports.remove(augmentedNodeImport); |
| 1645 | augmentedNodeImport = IMPORT + augmentedInfo.getPkgInfo() + PERIOD |
| 1646 | + augmentedInfo.getClassInfo() + PERIOD + augmentedInfo.getClassInfo() |
| 1647 | + BUILDER + SEMI_COLAN + NEW_LINE; |
| 1648 | imports.remove(augmentedNodeImport); |
| 1649 | } |
| 1650 | return imports; |
| 1651 | } |
| 1652 | |
| 1653 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1654 | * Removes case import info from import list. |
| 1655 | * |
| 1656 | * @param imports list of imports |
| 1657 | * @return import for class |
| 1658 | */ |
| 1659 | private List<String> removeCaseImport(List<String> imports) { |
| 1660 | if (imports != null && caseImportInfo != null) { |
| 1661 | String caseImport = IMPORT + caseImportInfo.getPkgInfo() + PERIOD + caseImportInfo.getClassInfo() + |
| 1662 | SEMI_COLAN + NEW_LINE; |
| 1663 | imports.remove(caseImport); |
| 1664 | } |
| 1665 | return imports; |
| 1666 | } |
| 1667 | |
| 1668 | /** |
| 1669 | * Removes all temporary file handles. |
| 1670 | * |
| 1671 | * @param isErrorOccurred when translator fails to generate java files we |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1672 | * need to close all open file handles include temporary files |
| 1673 | * and java files. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1674 | * @throws IOException when failed to delete the temporary files |
| 1675 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1676 | public void freeTemporaryResources(boolean isErrorOccurred) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1677 | throws IOException { |
| 1678 | boolean isError = isErrorOccurred; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1679 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1680 | * Close all java file handles and when error occurs delete the files. |
| 1681 | */ |
| 1682 | if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) { |
| 1683 | closeFile(getInterfaceJavaFileHandle(), isError); |
| 1684 | } |
| 1685 | if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) { |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1686 | closeFile(getBuilderClassJavaFileHandle(), true); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1687 | } |
| 1688 | if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) { |
| 1689 | closeFile(getBuilderInterfaceJavaFileHandle(), true); |
| 1690 | } |
| 1691 | if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) { |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1692 | closeFile(getImplClassJavaFileHandle(), isError); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1693 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1694 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1695 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1696 | * Close all temporary file handles and delete the files. |
| 1697 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1698 | if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) { |
| 1699 | closeFile(getGetterImplTempFileHandle(), true); |
| 1700 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1701 | if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) { |
| 1702 | closeFile(getAttributesTempFileHandle(), true); |
| 1703 | } |
| 1704 | if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) { |
| 1705 | closeFile(getHashCodeImplTempFileHandle(), true); |
| 1706 | } |
| 1707 | if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) { |
| 1708 | closeFile(getToStringImplTempFileHandle(), true); |
| 1709 | } |
| 1710 | if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) { |
| 1711 | closeFile(getEqualsImplTempFileHandle(), true); |
| 1712 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 1713 | if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) { |
| 1714 | closeFile(getFromStringImplTempFileHandle(), true); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1715 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1716 | if ((getGeneratedTempFiles() & AUGMENTE_CLASS_CONSTRUCTOR_MASK) != 0) { |
| 1717 | closeFile(getAugmentConstructorImplTempFileHandle(), true); |
| 1718 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | /** |
| 1722 | * Returns if the attribute needs to be accessed in a qualified manner or |
| 1723 | * not, if it needs to be imported, then the same needs to be done. |
| 1724 | * |
| 1725 | * @param importInfo import info for the current attribute being added |
| 1726 | * @return status of the qualified access to the attribute |
| 1727 | */ |
| 1728 | public boolean getIsQualifiedAccessOrAddToImportList( |
| 1729 | JavaQualifiedTypeInfo importInfo) { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1730 | |
| 1731 | return getJavaImportData().addImportInfo(importInfo, getGeneratedJavaClassName(), |
| 1732 | getJavaFileInfo().getPackage()); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | /** |
| 1736 | * Checks if the import info is same as the package of the current generated |
| 1737 | * java file. |
| 1738 | * |
| 1739 | * @param importInfo import info for an attribute |
| 1740 | * @return true if the import info is same as the current nodes package |
| 1741 | * false otherwise |
| 1742 | */ |
| 1743 | public boolean isImportPkgEqualCurNodePkg(JavaQualifiedTypeInfo importInfo) { |
| 1744 | return getJavaFileInfo().getPackage() |
| 1745 | .contentEquals(importInfo.getPkgInfo()); |
| 1746 | } |
| 1747 | } |