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.javamodel; |
| 17 | |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 18 | import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaType; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.utils.io.YangToJavaNamingConflictUtil; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 22 | |
| 23 | import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaDataType; |
| 24 | import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaImportClass; |
| 25 | import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaImportPackage; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Represents java information corresponding to the YANG type. |
| 29 | */ |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 30 | public class YangJavaTypeTranslator |
| 31 | extends YangJavaType |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 32 | implements JavaQualifiedTypeResolver { |
| 33 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 34 | /** |
| 35 | * Create a YANG leaf object with java qualified access details. |
| 36 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 37 | public YangJavaTypeTranslator() { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 38 | super(); |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 39 | setJavaQualifiedInfo(new JavaQualifiedTypeInfoTranslator()); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | @Override |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 43 | public void updateJavaQualifiedInfo(YangToJavaNamingConflictUtil conflictResolver) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 44 | JavaQualifiedTypeInfoTranslator importInfo = (JavaQualifiedTypeInfoTranslator) getJavaQualifiedInfo(); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * Type is added as an attribute in the class. |
| 48 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 49 | String className = getJavaImportClass(this, false, conflictResolver); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 50 | if (className != null) { |
| 51 | /* |
| 52 | * Corresponding to the attribute type a class needs to be imported, |
| 53 | * since it can be a derived type or a usage of wrapper classes. |
| 54 | */ |
| 55 | importInfo.setClassInfo(className); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 56 | String classPkg = getJavaImportPackage(this, |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 57 | false, conflictResolver); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 58 | if (classPkg == null) { |
| 59 | throw new TranslatorException("import package cannot be null when the class is used"); |
| 60 | } |
| 61 | importInfo.setPkgInfo(classPkg); |
| 62 | } else { |
| 63 | /* |
| 64 | * The attribute does not need a class to be imported, for example |
| 65 | * built in java types. |
| 66 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 67 | String dataTypeName = getJavaDataType(this); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 68 | if (dataTypeName == null) { |
| 69 | throw new TranslatorException("not supported data type"); |
| 70 | } |
| 71 | importInfo.setClassInfo(dataTypeName); |
| 72 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 73 | setJavaQualifiedInfo(importInfo); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 74 | } |
| 75 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 76 | } |