Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +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 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
| 19 | import java.io.File; |
| 20 | import java.io.IOException; |
| 21 | import java.util.List; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo; |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
| 25 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer; |
| 26 | import org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 27 | |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.utils.UtilConstants.ACTIVATE_ANNOTATION_IMPORT; |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTATION_HOLDER; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 30 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.utils.UtilConstants.COMPONENT_ANNOTATION_IMPORT; |
| 32 | import static org.onosproject.yangutils.utils.UtilConstants.DEACTIVATE_ANNOTATION_IMPORT; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.utils.UtilConstants.ENUM; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION; |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.utils.UtilConstants.LOGGER_FACTORY_IMPORT; |
| 37 | import static org.onosproject.yangutils.utils.UtilConstants.LOGGER_IMPORT; |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.utils.UtilConstants.SERVICE_ANNOTATION_IMPORT; |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 42 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.utils.UtilConstants.TYPE; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle; |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 45 | import static java.util.Collections.sort; |
| 46 | |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 47 | /** |
| 48 | * Represents utilities for temporary java code fragments. |
| 49 | */ |
| 50 | public final class TempJavaCodeFragmentFilesUtils { |
| 51 | |
| 52 | /** |
| 53 | * Creates a private instance of temporary java code fragment utils. |
| 54 | */ |
| 55 | private TempJavaCodeFragmentFilesUtils() { |
| 56 | } |
| 57 | |
| 58 | /** |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 59 | * Adds import for AugmentationHolders class. |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 60 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 61 | * @param curNode current YANG node |
| 62 | * @param imports list of imports |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 63 | * @param operation add or delete import |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 64 | */ |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 65 | public static void addAugmentationHoldersImport(YangNode curNode, List<String> imports, boolean operation) { |
| 66 | String thisImport = getTempJavaFragement(curNode).getJavaImportData().getAugmentationHolderImport(); |
| 67 | performOperationOnImports(imports, thisImport, operation); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Adds import for AugmentedInfo class. |
| 72 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 73 | * @param curNode current YANG node |
| 74 | * @param imports list of imports |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 75 | * @param operation add or delete import |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 76 | */ |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 77 | public static void addAugmentedInfoImport(YangNode curNode, List<String> imports, boolean operation) { |
| 78 | String thisImport = getTempJavaFragement(curNode).getJavaImportData().getAugmentedInfoImport(); |
| 79 | performOperationOnImports(imports, thisImport, operation); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns temp java fragment. |
| 84 | * |
| 85 | * @param curNode current YANG node |
| 86 | * @return temp java fragments |
| 87 | */ |
| 88 | public static TempJavaFragmentFiles getTempJavaFragement(YangNode curNode) { |
| 89 | TempJavaCodeFragmentFiles container = ((TempJavaCodeFragmentFilesContainer) curNode) |
| 90 | .getTempJavaCodeFragmentFiles(); |
| 91 | if (container.getBeanTempFiles() != null) { |
| 92 | return container.getBeanTempFiles(); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 93 | } |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 94 | if (container.getServiceTempFiles() != null) { |
| 95 | return container.getServiceTempFiles(); |
| 96 | } |
| 97 | |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | /** |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 102 | * Adds import for array list. |
| 103 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 104 | * @param curNode current YANG node |
| 105 | * @param imports list of imports |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 106 | * @param operation add or delete import |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 107 | */ |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 108 | public static void addArrayListImport(YangNode curNode, List<String> imports, boolean operation) { |
| 109 | String arrayListImport = getTempJavaFragement(curNode).getJavaImportData().getImportForArrayList(); |
| 110 | String listImport = getTempJavaFragement(curNode).getJavaImportData().getImportForList(); |
| 111 | performOperationOnImports(imports, arrayListImport, operation); |
| 112 | if (!imports.contains(listImport)) { |
| 113 | /** |
| 114 | * List can be there because of attribute also , so no need to remove it and operation will |
| 115 | * always be add(true). |
| 116 | */ |
| 117 | performOperationOnImports(imports, listImport, true); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 118 | } |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 119 | } |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 120 | |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 121 | /** |
| 122 | * Adds listener's imports. |
| 123 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 124 | * @param curNode currentYangNode. |
| 125 | * @param imports import list |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 126 | * @param operation add or remove |
| 127 | * @param classInfo class info to be added to import list |
| 128 | */ |
| 129 | public static void addListnersImport(YangNode curNode, List<String> imports, boolean operation, |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 130 | String classInfo) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 131 | String thisImport = ""; |
| 132 | if (classInfo.equals(LISTENER_SERVICE)) { |
| 133 | thisImport = getTempJavaFragement(curNode).getJavaImportData().getListenerServiceImport(); |
| 134 | performOperationOnImports(imports, thisImport, operation); |
| 135 | } else { |
| 136 | thisImport = getTempJavaFragement(curNode).getJavaImportData().getListenerRegistryImport(); |
| 137 | performOperationOnImports(imports, thisImport, operation); |
| 138 | } |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 142 | * Adds annotations imports. |
| 143 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 144 | * @param imports list if imports |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 145 | * @param operation to add or to delete |
| 146 | */ |
| 147 | public static void addAnnotationsImports(List<String> imports, boolean operation) { |
| 148 | if (operation) { |
| 149 | imports.add(ACTIVATE_ANNOTATION_IMPORT); |
| 150 | imports.add(DEACTIVATE_ANNOTATION_IMPORT); |
| 151 | imports.add(COMPONENT_ANNOTATION_IMPORT); |
| 152 | imports.add(SERVICE_ANNOTATION_IMPORT); |
| 153 | imports.add(LOGGER_FACTORY_IMPORT); |
| 154 | imports.add(LOGGER_IMPORT); |
| 155 | } else { |
| 156 | imports.remove(ACTIVATE_ANNOTATION_IMPORT); |
| 157 | imports.remove(DEACTIVATE_ANNOTATION_IMPORT); |
| 158 | imports.remove(COMPONENT_ANNOTATION_IMPORT); |
| 159 | imports.remove(SERVICE_ANNOTATION_IMPORT); |
| 160 | imports.remove(LOGGER_FACTORY_IMPORT); |
| 161 | imports.remove(LOGGER_IMPORT); |
| 162 | } |
| 163 | sortImports(imports); |
| 164 | } |
| 165 | |
| 166 | /** |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 167 | * Performs given operations on import list. |
| 168 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 169 | * @param imports list of imports |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 170 | * @param curImport current import |
| 171 | * @param operation add or remove |
| 172 | * @return import list |
| 173 | */ |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 174 | private static List<String> performOperationOnImports(List<String> imports, String curImport, |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 175 | boolean operation) { |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 176 | if (operation) { |
| 177 | imports.add(curImport); |
| 178 | } else { |
| 179 | imports.remove(curImport); |
| 180 | } |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 181 | sortImports(imports); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 182 | return imports; |
| 183 | } |
| 184 | |
| 185 | /** |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 186 | * Returns true if AugmentationHolder class needs to be extended. |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 187 | * |
| 188 | * @param extendsList list of classes need to be extended |
| 189 | * @return true or false |
| 190 | */ |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 191 | public static boolean isAugmentationHolderExtended(List<JavaQualifiedTypeInfo> extendsList) { |
| 192 | for (JavaQualifiedTypeInfo info : extendsList) { |
| 193 | return info.getClassInfo().equals(AUGMENTATION_HOLDER); |
| 194 | } |
| 195 | return false; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Returns true if AugmentedInfo class needs to be extended. |
| 200 | * |
| 201 | * @param extendsList list of classes need to be extended |
| 202 | * @return true or false |
| 203 | */ |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 204 | public static boolean isAugmentedInfoExtended(List<JavaQualifiedTypeInfo> extendsList) { |
| 205 | for (JavaQualifiedTypeInfo info : extendsList) { |
| 206 | return info.getClassInfo().equals(AUGMENTED_INFO); |
| 207 | } |
| 208 | return false; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Closes the file handle for temporary file. |
| 213 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 214 | * @param file file to be closed |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 215 | * @param toBeDeleted flag to indicate if file needs to be deleted |
| 216 | * @throws IOException when failed to close the file handle |
| 217 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 218 | public static void closeFile(File file, boolean toBeDeleted) |
| 219 | throws IOException { |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 220 | |
| 221 | if (file != null) { |
| 222 | updateFileHandle(file, null, true); |
| 223 | if (toBeDeleted) { |
| 224 | file.delete(); |
| 225 | } |
| 226 | } |
| 227 | } |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 228 | |
| 229 | /** |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 230 | * Returns sorted import list. |
| 231 | * |
| 232 | * @param imports import list |
| 233 | * @return sorted import list |
| 234 | */ |
| 235 | public static List<String> sortImports(List<String> imports) { |
| 236 | sort(imports); |
| 237 | return imports; |
| 238 | } |
| 239 | |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 240 | /** |
| 241 | * Returns event enum start. |
| 242 | * |
| 243 | * @return event enum start |
| 244 | */ |
| 245 | public static String getEventEnumTypeStart() { |
| 246 | return FOUR_SPACE_INDENTATION + PUBLIC + SPACE + ENUM + SPACE + TYPE + SPACE + OPEN_CURLY_BRACKET |
| 247 | + NEW_LINE; |
| 248 | } |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 249 | } |