Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +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 | package org.onosproject.yangutils.translator.tojava; |
| 17 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 20 | import java.util.SortedSet; |
| 21 | import java.util.TreeSet; |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 22 | import static java.util.Collections.sort; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 23 | |
| 24 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 26 | |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST; |
| 28 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_CLASS; |
| 29 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_PKG; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 30 | import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS; |
| 31 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
| 32 | import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS; |
| 33 | import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION_CLASS_IMPORT_CLASS; |
| 35 | import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION_CLASS_IMPORT_PKG; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.utils.UtilConstants.IMPORT; |
| 37 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG; |
| 38 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS; |
| 39 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG; |
| 40 | import static org.onosproject.yangutils.utils.UtilConstants.LIST; |
| 41 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 42 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 43 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
| 44 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 45 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 46 | * Represents that generated Java file can contain imports. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 47 | */ |
| 48 | public class JavaImportData { |
| 49 | |
| 50 | /** |
| 51 | * Flag to denote if any list in imported. |
| 52 | */ |
| 53 | private boolean isListToImport; |
| 54 | |
| 55 | /** |
| 56 | * Sorted set of import info, to be used to maintain the set of classes to |
| 57 | * be imported in the generated class. |
| 58 | */ |
| 59 | private SortedSet<JavaQualifiedTypeInfo> importSet; |
| 60 | |
| 61 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 62 | * Creates java import data object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 63 | */ |
| 64 | public JavaImportData() { |
| 65 | setImportSet(new TreeSet<JavaQualifiedTypeInfo>()); |
| 66 | } |
| 67 | |
| 68 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 69 | * Returns if the list needs to be imported. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 70 | * |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 71 | * @return true if any of the attribute needs to be maintained as a list |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 72 | */ |
| 73 | public boolean getIfListImported() { |
| 74 | return isListToImport; |
| 75 | } |
| 76 | |
| 77 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 78 | * Sets the status of importing list. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 79 | * |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 80 | * @param isList status to mention list is bing imported |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 81 | */ |
| 82 | public void setIfListImported(boolean isList) { |
| 83 | isListToImport = isList; |
| 84 | } |
| 85 | |
| 86 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 87 | * Returns the set containing the imported class/interface info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 88 | * |
| 89 | * @return the set containing the imported class/interface info |
| 90 | */ |
| 91 | public SortedSet<JavaQualifiedTypeInfo> getImportSet() { |
| 92 | return importSet; |
| 93 | } |
| 94 | |
| 95 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 96 | * Assigns the set containing the imported class/interface info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 97 | * |
| 98 | * @param importSet the set containing the imported class/interface info |
| 99 | */ |
| 100 | private void setImportSet(SortedSet<JavaQualifiedTypeInfo> importSet) { |
| 101 | this.importSet = importSet; |
| 102 | } |
| 103 | |
| 104 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 105 | * Adds an imported class/interface info if it is not already part of the |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 106 | * collection. |
| 107 | * |
| 108 | * If already part of the collection, check if the packages are same, if so |
| 109 | * then return true, to denote it is already in the import collection, and |
| 110 | * it can be accessed without qualified access. If the packages do not |
| 111 | * match, then do not add to the import collection, and return false to |
| 112 | * denote, it is not added to import collection and needs to be accessed in |
| 113 | * a qualified manner. |
| 114 | * |
| 115 | * @param curNode current data model node |
| 116 | * @param newImportInfo class/interface info being imported |
| 117 | * @return status of new addition of class/interface to the import set |
| 118 | */ |
| 119 | public boolean addImportInfo(YangNode curNode, JavaQualifiedTypeInfo newImportInfo) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 120 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 121 | if (!(curNode instanceof HasJavaImportData)) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 122 | throw new TranslatorException("missing import info in data model node"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 123 | } |
| 124 | for (JavaQualifiedTypeInfo curImportInfo : ((HasJavaImportData) curNode).getJavaImportData().getImportSet()) { |
| 125 | if (curImportInfo.getClassInfo() |
| 126 | .contentEquals(newImportInfo.getClassInfo())) { |
| 127 | return curImportInfo.getPkgInfo() |
| 128 | .contentEquals(newImportInfo.getPkgInfo()); |
| 129 | } |
| 130 | } |
| 131 | ((HasJavaImportData) curNode).getJavaImportData().getImportSet().add(newImportInfo); |
| 132 | return true; |
| 133 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * Returns import for class. |
| 137 | * |
| 138 | * @param attr java attribute info |
| 139 | * @return imports for class |
| 140 | */ |
| 141 | public List<String> getImports(JavaAttributeInfo attr) { |
| 142 | |
| 143 | String importString; |
| 144 | List<String> imports = new ArrayList<>(); |
| 145 | |
| 146 | for (JavaQualifiedTypeInfo importInfo : getImportSet()) { |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 147 | if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null |
| 148 | && !importInfo.getPkgInfo().equals(JAVA_LANG)) { |
| 149 | importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 150 | + NEW_LINE; |
| 151 | |
| 152 | imports.add(importString); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (attr.isListAttr()) { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 157 | imports.add(getImportForList()); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 158 | } |
| 159 | |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 160 | sort(imports); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 161 | return imports; |
| 162 | } |
| 163 | |
| 164 | /** |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 165 | * Returns import for class. |
| 166 | * |
| 167 | * @return imports for class |
| 168 | */ |
| 169 | public List<String> getImports() { |
| 170 | String importString; |
| 171 | List<String> imports = new ArrayList<>(); |
| 172 | |
| 173 | for (JavaQualifiedTypeInfo importInfo : getImportSet()) { |
| 174 | if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null |
| 175 | && !importInfo.getPkgInfo().equals(JAVA_LANG)) { |
| 176 | importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN |
| 177 | + NEW_LINE; |
| 178 | |
| 179 | imports.add(importString); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | sort(imports); |
| 184 | return imports; |
| 185 | } |
| 186 | |
| 187 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 188 | * Returns import for hash and equals method. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 189 | * |
| 190 | * @return import for hash and equals method |
| 191 | */ |
| 192 | public String getImportForHashAndEquals() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 193 | return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS; |
| 194 | } |
| 195 | |
| 196 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 197 | * Returns import for to string method. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 198 | * |
| 199 | * @return import for to string method |
| 200 | */ |
| 201 | public String getImportForToString() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 202 | return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS; |
| 203 | } |
| 204 | |
| 205 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 206 | * Returns import for list attribute. |
| 207 | * |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 208 | * @return import for list attribute |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 209 | */ |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 210 | public static String getImportForList() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 211 | return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE; |
| 212 | } |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 213 | |
| 214 | /** |
| 215 | * Returns import for array list attribute. |
| 216 | * |
| 217 | * @return import for array list attribute |
| 218 | */ |
| 219 | public static String getImportForArrayList() { |
| 220 | return IMPORT + COLLECTION_IMPORTS + PERIOD + ARRAY_LIST + SEMI_COLAN + NEW_LINE; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Returns import string for HasAugmentation class. |
| 225 | * |
| 226 | * @return import string for HasAugmentation class |
| 227 | */ |
| 228 | public static String getHasAugmentationImport() { |
| 229 | return IMPORT + HAS_AUGMENTATION_CLASS_IMPORT_PKG + PERIOD + HAS_AUGMENTATION_CLASS_IMPORT_CLASS; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Returns import string for AugmentedInfo class. |
| 234 | * |
| 235 | * @return import string for AugmentedInfo class |
| 236 | */ |
| 237 | public static String getAugmentedInfoImport() { |
| 238 | return IMPORT + AUGMENTED_INFO_CLASS_IMPORT_PKG + PERIOD + AUGMENTED_INFO_CLASS_IMPORT_CLASS; |
| 239 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 240 | } |