Bharat saraswal | e2bc60d | 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; |
| 22 | |
| 23 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.translator.tojava.JavaImportDataContainer; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo; |
| 27 | |
| 28 | import static java.util.Collections.sort; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 29 | |
| 30 | import static org.onosproject.yangutils.translator.tojava.JavaImportData.getAugmentedInfoImport; |
| 31 | import static org.onosproject.yangutils.translator.tojava.JavaImportData.getHasAugmentationImport; |
| 32 | import static org.onosproject.yangutils.translator.tojava.JavaImportData.getImportForArrayList; |
| 33 | import static org.onosproject.yangutils.translator.tojava.JavaImportData.getImportForList; |
| 34 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO; |
| 35 | import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION; |
| 36 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle; |
| 37 | |
| 38 | /** |
| 39 | * Represents utilities for temporary java code fragments. |
| 40 | */ |
| 41 | public final class TempJavaCodeFragmentFilesUtils { |
| 42 | |
| 43 | /** |
| 44 | * Creates a private instance of temporary java code fragment utils. |
| 45 | */ |
| 46 | private TempJavaCodeFragmentFilesUtils() { |
| 47 | } |
| 48 | |
| 49 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 50 | * Adds import for HasAugmentation class. |
| 51 | * |
| 52 | * @param curNode current YANG node |
| 53 | * @param imports list of imports |
| 54 | * @param operation add or delete import |
| 55 | * @return import for HasAugmentation class |
| 56 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 57 | public static List<String> addHasAugmentationImport(YangNode curNode, List<String> imports, |
| 58 | boolean operation) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 59 | if (curNode instanceof JavaImportDataContainer) { |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 60 | String thisImport = getHasAugmentationImport(); |
| 61 | performOperationOnImports(imports, thisImport, operation); |
| 62 | } |
| 63 | return imports; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Adds import for AugmentedInfo class. |
| 68 | * |
| 69 | * @param curNode current YANG node |
| 70 | * @param imports list of imports |
| 71 | * @param operation add or delete import |
| 72 | * @return import for AugmentedInfo class |
| 73 | */ |
| 74 | public static List<String> addAugmentedInfoImport(YangNode curNode, List<String> imports, boolean operation) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 75 | if (curNode instanceof JavaImportDataContainer) { |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 76 | String thisImport = getAugmentedInfoImport(); |
| 77 | performOperationOnImports(imports, thisImport, operation); |
| 78 | } |
| 79 | return imports; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Adds import for array list. |
| 84 | * |
| 85 | * @param curNode current YANG node |
| 86 | * @param imports list of imports |
| 87 | * @param operation add or delete import |
| 88 | * @return import for HasAugmentation class |
| 89 | */ |
| 90 | public static List<String> addArrayListImport(YangNode curNode, List<String> imports, boolean operation) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 91 | if (curNode instanceof JavaImportDataContainer) { |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 92 | String arrayListImport = getImportForArrayList(); |
| 93 | String listImport = getImportForList(); |
| 94 | performOperationOnImports(imports, arrayListImport, operation); |
| 95 | if (!imports.contains(listImport)) { |
| 96 | /** |
| 97 | * List can be there because of attribute also , so no need to remove it and operation will |
| 98 | * always be add(true). |
| 99 | */ |
| 100 | performOperationOnImports(imports, listImport, true); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return imports; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Performs given operations on import list. |
| 109 | * |
| 110 | * @param imports list of imports |
| 111 | * @param curImport current import |
| 112 | * @param operation add or remove |
| 113 | * @return import list |
| 114 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 115 | private static List<String> performOperationOnImports(List<String> imports, String curImport, |
| 116 | boolean operation) { |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 117 | if (operation) { |
| 118 | imports.add(curImport); |
| 119 | } else { |
| 120 | imports.remove(curImport); |
| 121 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 122 | sortImports(imports); |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 123 | return imports; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Prepares java file generator for extends list. |
| 128 | * |
| 129 | * @param extendsList list of classes need to be extended |
| 130 | */ |
| 131 | public static void prepareJavaFileGeneratorForExtendsList(List<String> extendsList) { |
| 132 | |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 133 | if (extendsList != null && !extendsList.isEmpty()) { |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 134 | JavaFileGenerator.setExtendsList(extendsList); |
| 135 | JavaFileGenerator.setIsExtendsList(true); |
| 136 | } else { |
| 137 | JavaFileGenerator.getExtendsList().clear(); |
| 138 | JavaFileGenerator.setIsExtendsList(false); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns true if HasAugmentation class needs to be extended. |
| 144 | * |
| 145 | * @param extendsList list of classes need to be extended |
| 146 | * @return true or false |
| 147 | */ |
| 148 | public static boolean isHasAugmentationExtended(List<String> extendsList) { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 149 | return extendsList != null && extendsList.contains(HAS_AUGMENTATION); |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns true if AugmentedInfo class needs to be extended. |
| 154 | * |
| 155 | * @param extendsList list of classes need to be extended |
| 156 | * @return true or false |
| 157 | */ |
| 158 | public static boolean isAugmentedInfoExtended(List<String> extendsList) { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 159 | return extendsList != null && extendsList.contains(AUGMENTED_INFO); |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Closes the file handle for temporary file. |
| 164 | * |
| 165 | * @param file file to be closed |
| 166 | * @param toBeDeleted flag to indicate if file needs to be deleted |
| 167 | * @throws IOException when failed to close the file handle |
| 168 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 169 | public static void closeFile(File file, boolean toBeDeleted) |
| 170 | throws IOException { |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 171 | |
| 172 | if (file != null) { |
| 173 | updateFileHandle(file, null, true); |
| 174 | if (toBeDeleted) { |
| 175 | file.delete(); |
| 176 | } |
| 177 | } |
| 178 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 179 | |
| 180 | /** |
| 181 | * Detects collision between parent and child node which have same name. |
| 182 | * When parent and child node both have the same name in that case child node should be used with |
| 183 | * qualified name. |
| 184 | * |
| 185 | * @param curNode current YANG node |
| 186 | * @param qualifiedTypeInfo current node's qualified info |
| 187 | * @return true if collision is detected |
| 188 | */ |
| 189 | public static boolean detectCollisionBwParentAndChildForImport(YangNode curNode, |
| 190 | JavaQualifiedTypeInfo qualifiedTypeInfo) { |
| 191 | |
| 192 | YangNode parent = curNode.getParent(); |
| 193 | String parentsClassInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName(); |
| 194 | String childsClassInfo = qualifiedTypeInfo.getClassInfo(); |
| 195 | if (childsClassInfo.equals(parentsClassInfo)) { |
| 196 | return true; |
| 197 | } |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Returns sorted import list. |
| 203 | * |
| 204 | * @param imports import list |
| 205 | * @return sorted import list |
| 206 | */ |
| 207 | public static List<String> sortImports(List<String> imports) { |
| 208 | sort(imports); |
| 209 | return imports; |
| 210 | } |
| 211 | |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 212 | } |