Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
| 19 | import java.io.File; |
| 20 | import java.io.IOException; |
| 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.translator.exception.TranslatorException; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.translator.tojava.TempJavaBeanFragmentFiles; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.tojava.TempJavaEnumerationFragmentFiles; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.translator.tojava.TempJavaServiceFragmentFiles; |
| 29 | import org.onosproject.yangutils.translator.tojava.TempJavaTypeFragmentFiles; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.utils.io.impl.CopyrightHeader; |
| 31 | import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType; |
| 32 | |
| 33 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK; |
| 34 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS; |
| 37 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE; |
| 38 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS; |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK; |
| 42 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK; |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK; |
| 50 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK; |
| 51 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK; |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 52 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 53 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_IMPL_MASK; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 54 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_INTERFACE_MASK; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 55 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK; |
| 56 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK; |
| 57 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK; |
| 58 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefStart; |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 59 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getJavaPackageFromPackagePath; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 60 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase; |
| 61 | import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION; |
| 62 | import static org.onosproject.yangutils.utils.UtilConstants.INT; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 63 | import static org.onosproject.yangutils.utils.UtilConstants.MANAGER; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 64 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 65 | import static org.onosproject.yangutils.utils.UtilConstants.ORG; |
| 66 | import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 67 | import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 68 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 69 | import static org.onosproject.yangutils.utils.UtilConstants.SERVICE; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 70 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
| 71 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 72 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 73 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_CLASS; |
| 74 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_INTERFACE; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 75 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ENUM_CLASS; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 76 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.EVENT; |
| 77 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.EVENT_LISTENER; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 78 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.IMPL_CLASS; |
| 79 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.INTERFACE; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 80 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.RPC_INTERFACE; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 81 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.RPC_MANAGER; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 82 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile; |
| 83 | |
| 84 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 85 | * Represents utilities for java file generator. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 86 | */ |
| 87 | public final class JavaFileGeneratorUtils { |
| 88 | |
| 89 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 90 | * Creates an instance of java file generator util. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 91 | */ |
| 92 | private JavaFileGeneratorUtils() { |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns a file object for generated file. |
| 97 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 98 | * @param fileName file name |
| 99 | * @param filePath file package path |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 100 | * @param extension file extension |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 101 | * @param handle cached file handle |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 102 | * @return file object |
| 103 | */ |
| 104 | public static File getFileObject(String filePath, String fileName, String extension, JavaFileInfo handle) { |
| 105 | |
| 106 | return new File(handle.getBaseCodeGenPath() + filePath + SLASH + fileName + extension); |
| 107 | } |
| 108 | |
| 109 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 110 | * Returns data stored in temporary files. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 111 | * |
| 112 | * @param generatedTempFiles temporary file types |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 113 | * @param tempJavaFragmentFiles temp java fragment files |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 114 | * @return data stored in temporary files |
| 115 | * @throws IOException when failed to get the data from temporary file handle |
| 116 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 117 | public static String getDataFromTempFileHandle(int generatedTempFiles, |
| 118 | TempJavaFragmentFiles tempJavaFragmentFiles) |
| 119 | throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 120 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 121 | TempJavaTypeFragmentFiles typeFragmentFiles = null; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 122 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 123 | if (tempJavaFragmentFiles instanceof TempJavaTypeFragmentFiles) { |
| 124 | typeFragmentFiles = (TempJavaTypeFragmentFiles) tempJavaFragmentFiles; |
| 125 | } |
| 126 | |
| 127 | TempJavaBeanFragmentFiles beanFragmentFiles = null; |
| 128 | |
| 129 | if (tempJavaFragmentFiles instanceof TempJavaBeanFragmentFiles) { |
| 130 | beanFragmentFiles = (TempJavaBeanFragmentFiles) tempJavaFragmentFiles; |
| 131 | } |
| 132 | |
| 133 | TempJavaServiceFragmentFiles serviceFragmentFiles = null; |
| 134 | if (tempJavaFragmentFiles instanceof TempJavaServiceFragmentFiles) { |
| 135 | serviceFragmentFiles = (TempJavaServiceFragmentFiles) tempJavaFragmentFiles; |
| 136 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 137 | if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) { |
| 138 | return tempJavaFragmentFiles |
| 139 | .getTemporaryDataFromFileHandle(tempJavaFragmentFiles.getAttributesTempFileHandle()); |
| 140 | } else if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 141 | return tempJavaFragmentFiles |
| 142 | .getTemporaryDataFromFileHandle(tempJavaFragmentFiles.getGetterInterfaceTempFileHandle()); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 143 | } else if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 144 | return tempJavaFragmentFiles |
| 145 | .getTemporaryDataFromFileHandle(tempJavaFragmentFiles.getSetterInterfaceTempFileHandle()); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 146 | } else if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 147 | return tempJavaFragmentFiles |
| 148 | .getTemporaryDataFromFileHandle(tempJavaFragmentFiles.getGetterImplTempFileHandle()); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 149 | } else if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 150 | return tempJavaFragmentFiles |
| 151 | .getTemporaryDataFromFileHandle(tempJavaFragmentFiles.getSetterImplTempFileHandle()); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 152 | } else if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 153 | if (beanFragmentFiles == null) { |
| 154 | throw new TranslatorException("Required constructor info is missing."); |
| 155 | } |
| 156 | return beanFragmentFiles |
| 157 | .getTemporaryDataFromFileHandle(beanFragmentFiles.getConstructorImplTempFileHandle()); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 158 | } else if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 159 | return tempJavaFragmentFiles |
| 160 | .getTemporaryDataFromFileHandle(tempJavaFragmentFiles.getHashCodeImplTempFileHandle()); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 161 | } else if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 162 | return tempJavaFragmentFiles |
| 163 | .getTemporaryDataFromFileHandle(tempJavaFragmentFiles.getEqualsImplTempFileHandle()); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 164 | } else if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 165 | return tempJavaFragmentFiles |
| 166 | .getTemporaryDataFromFileHandle(tempJavaFragmentFiles.getToStringImplTempFileHandle()); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 167 | } else if ((generatedTempFiles & OF_STRING_IMPL_MASK) != 0) { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 168 | if (typeFragmentFiles == null) { |
| 169 | throw new TranslatorException("Required of string implementation info is missing."); |
| 170 | } |
| 171 | return typeFragmentFiles |
| 172 | .getTemporaryDataFromFileHandle(typeFragmentFiles.getOfStringImplTempFileHandle()); |
| 173 | } else if ((generatedTempFiles & CONSTRUCTOR_FOR_TYPE_MASK) != 0) { |
| 174 | if (typeFragmentFiles == null) { |
| 175 | throw new TranslatorException("Required constructor implementation info is missing."); |
| 176 | } |
| 177 | return typeFragmentFiles |
| 178 | .getTemporaryDataFromFileHandle(typeFragmentFiles.getConstructorForTypeTempFileHandle()); |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 179 | } else if ((generatedTempFiles & FROM_STRING_IMPL_MASK) != 0) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 180 | return tempJavaFragmentFiles |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 181 | .getTemporaryDataFromFileHandle(tempJavaFragmentFiles.getFromStringImplTempFileHandle()); |
| 182 | } else if ((generatedTempFiles & ENUM_IMPL_MASK) != 0) { |
| 183 | if (!(tempJavaFragmentFiles instanceof TempJavaEnumerationFragmentFiles)) { |
| 184 | throw new TranslatorException("Required enum info is missing."); |
| 185 | } |
| 186 | TempJavaEnumerationFragmentFiles enumFragmentFiles = |
| 187 | (TempJavaEnumerationFragmentFiles) tempJavaFragmentFiles; |
| 188 | return enumFragmentFiles |
| 189 | .getTemporaryDataFromFileHandle(enumFragmentFiles.getEnumClassTempFileHandle()); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 190 | } else if ((generatedTempFiles & RPC_INTERFACE_MASK) != 0) { |
| 191 | if (serviceFragmentFiles == null) { |
| 192 | throw new TranslatorException("Required rpc interface info is missing."); |
| 193 | } |
| 194 | return serviceFragmentFiles |
| 195 | .getTemporaryDataFromFileHandle(serviceFragmentFiles.getRpcInterfaceTempFileHandle()); |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 196 | } else if ((generatedTempFiles & RPC_IMPL_MASK) != 0) { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 197 | if (serviceFragmentFiles == null) { |
| 198 | throw new TranslatorException("Required rpc implementation info is missing."); |
| 199 | } |
| 200 | return serviceFragmentFiles |
| 201 | .getTemporaryDataFromFileHandle(serviceFragmentFiles.getRpcImplTempFileHandle()); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 202 | } |
| 203 | return null; |
| 204 | } |
| 205 | |
| 206 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 207 | * Initiates generation of file based on generated file type. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 208 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 209 | * @param file generated file |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 210 | * @param className generated file class name |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 211 | * @param type generated file type |
| 212 | * @param imports imports for the file |
| 213 | * @param pkg generated file package |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 214 | * @throws IOException when fails to generate a file |
| 215 | */ |
| 216 | public static void initiateJavaFileGeneration(File file, String className, int type, List<String> imports, |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 217 | String pkg) |
| 218 | throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 219 | |
| 220 | try { |
| 221 | file.createNewFile(); |
| 222 | appendContents(file, className, type, imports, pkg); |
| 223 | } catch (IOException e) { |
| 224 | throw new IOException("Failed to create " + file.getName() + " class file."); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Appends all the contents into a generated java file. |
| 230 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 231 | * @param file generated file |
| 232 | * @param fileName generated file name |
| 233 | * @param type generated file type |
| 234 | * @param pkg generated file package |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 235 | * @param importsList list of java imports. |
| 236 | * @throws IOException when fails to append contents |
| 237 | */ |
| 238 | private static void appendContents(File file, String fileName, int type, List<String> importsList, |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 239 | String pkg) |
| 240 | throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 241 | |
| 242 | String pkgString = parsePackageString(pkg, importsList); |
| 243 | |
| 244 | if ((type & IMPL_CLASS_MASK) != 0) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 245 | write(file, fileName, type, IMPL_CLASS); |
| 246 | } else if ((type & BUILDER_INTERFACE_MASK) != 0) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 247 | write(file, fileName, type, BUILDER_INTERFACE); |
| 248 | } else if ((type & GENERATE_TYPEDEF_CLASS) != 0) { |
| 249 | appendHeaderContents(file, pkgString, importsList); |
| 250 | write(file, fileName, type, IMPL_CLASS); |
| 251 | } else if ((type & INTERFACE_MASK) != 0) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 252 | appendHeaderContents(file, pkgString, importsList); |
| 253 | write(file, fileName, type, INTERFACE); |
| 254 | } else if ((type & BUILDER_CLASS_MASK) != 0) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 255 | appendHeaderContents(file, pkgString, importsList); |
| 256 | write(file, fileName, type, BUILDER_CLASS); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 257 | } else if ((type & GENERATE_UNION_CLASS) != 0) { |
| 258 | appendHeaderContents(file, pkgString, importsList); |
| 259 | write(file, fileName, type, IMPL_CLASS); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 260 | } else if ((type & GENERATE_ENUM_CLASS) != 0) { |
| 261 | appendHeaderContents(file, pkgString, importsList); |
| 262 | write(file, fileName, type, ENUM_CLASS); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 263 | } else if ((type & GENERATE_SERVICE_AND_MANAGER) != 0) { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 264 | appendHeaderContents(file, pkgString, importsList); |
| 265 | write(file, fileName, type, RPC_INTERFACE); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 266 | } else if ((type & GENERATE_EVENT_CLASS) != 0) { |
| 267 | appendHeaderContents(file, pkgString, importsList); |
| 268 | write(file, fileName, type, EVENT); |
| 269 | } else if ((type & GENERATE_EVENT_LISTENER_INTERFACE) != 0) { |
| 270 | appendHeaderContents(file, pkgString, importsList); |
| 271 | write(file, fileName, type, EVENT_LISTENER); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Removes base directory path from package and generates package string for file. |
| 277 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 278 | * @param javaPkg generated java package |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 279 | * @param importsList list of imports |
| 280 | * @return package string |
| 281 | */ |
| 282 | private static String parsePackageString(String javaPkg, List<String> importsList) { |
| 283 | |
| 284 | if (javaPkg.contains(ORG)) { |
| 285 | String[] strArray = javaPkg.split(ORG); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 286 | javaPkg = ORG + getJavaPackageFromPackagePath(strArray[1]); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 287 | } |
| 288 | if (importsList != null) { |
| 289 | if (!importsList.isEmpty()) { |
| 290 | return PACKAGE + SPACE + javaPkg + SEMI_COLAN + NEW_LINE; |
| 291 | } else { |
| 292 | return PACKAGE + SPACE + javaPkg + SEMI_COLAN; |
| 293 | } |
| 294 | } else { |
| 295 | return PACKAGE + SPACE + javaPkg + SEMI_COLAN; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Appends other contents to interface, builder and typedef classes. |
| 301 | * for example : ONOS copyright, imports and package. |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 302 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 303 | * @param file generated file |
| 304 | * @param pkg generated package |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 305 | * @param importsList list of imports |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 306 | * @throws IOException when fails to append contents |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 307 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 308 | private static void appendHeaderContents(File file, String pkg, List<String> importsList) |
| 309 | throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 310 | |
| 311 | insertDataIntoJavaFile(file, CopyrightHeader.getCopyrightHeader()); |
| 312 | insertDataIntoJavaFile(file, pkg); |
| 313 | |
| 314 | /* |
| 315 | * TODO: add the file header using |
| 316 | * JavaCodeSnippetGen.getFileHeaderComment |
| 317 | */ |
| 318 | |
| 319 | if (importsList != null) { |
| 320 | insertDataIntoJavaFile(file, NEW_LINE); |
| 321 | for (String imports : importsList) { |
| 322 | insertDataIntoJavaFile(file, imports); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 328 | * Writes data to the specific generated file. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 329 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 330 | * @param file generated file |
| 331 | * @param fileName file name |
| 332 | * @param genType generated file type |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 333 | * @param javaDocType java doc type |
| 334 | * @throws IOException when fails to write into a file |
| 335 | */ |
| 336 | private static void write(File file, String fileName, int genType, JavaDocType javaDocType) |
| 337 | throws IOException { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 338 | if ((genType & GENERATE_SERVICE_AND_MANAGER) != 0) { |
| 339 | if (!fileName.contains(SERVICE)) { |
| 340 | insertDataIntoJavaFile(file, getJavaDoc(RPC_MANAGER, fileName + MANAGER, false)); |
| 341 | } else { |
| 342 | insertDataIntoJavaFile(file, getJavaDoc(javaDocType, fileName, false)); |
| 343 | } |
| 344 | } else { |
| 345 | insertDataIntoJavaFile(file, getJavaDoc(javaDocType, fileName, false)); |
| 346 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 347 | insertDataIntoJavaFile(file, getJavaClassDefStart(genType, fileName)); |
| 348 | } |
| 349 | |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 350 | /** |
| 351 | * Returns integer attribute for enum's class to get the values. |
| 352 | * |
| 353 | * @param className enum's class name |
| 354 | * @return enum's attribute |
| 355 | */ |
| 356 | public static String getEnumsValueAttribute(String className) { |
| 357 | return NEW_LINE + FOUR_SPACE_INDENTATION + PRIVATE + SPACE + INT + SPACE + getSmallCase(className) + SEMI_COLAN |
| 358 | + NEW_LINE; |
| 359 | } |
| 360 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 361 | } |