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 | |
| 17 | package org.onosproject.yangutils.translator.tojava; |
| 18 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangType; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 21 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 22 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 23 | * Represents the attribute info corresponding to class/interface generated. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 24 | */ |
| 25 | public final class JavaAttributeInfo { |
| 26 | |
| 27 | /** |
| 28 | * The data type info of attribute. |
| 29 | */ |
| 30 | private YangType<?> attrType; |
| 31 | |
| 32 | /** |
| 33 | * Name of the attribute. |
| 34 | */ |
| 35 | private String name; |
| 36 | |
| 37 | /** |
| 38 | * If the added attribute is a list of info. |
| 39 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 40 | private boolean isListAttr; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * If the added attribute has to be accessed in a fully qualified manner. |
| 44 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 45 | private boolean isQualifiedName; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * The class info will be used to set the attribute type and package info |
| 49 | * will be use for qualified name. |
| 50 | */ |
| 51 | private JavaQualifiedTypeInfo importInfo; |
| 52 | |
| 53 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 54 | * Creates a java attribute info object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 55 | */ |
| 56 | private JavaAttributeInfo() { |
| 57 | } |
| 58 | |
| 59 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 60 | * Creates object of java attribute info. |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 61 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 62 | * @param attrType YANG type |
| 63 | * @param name attribute name |
| 64 | * @param isListAttr is list attribute |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 65 | * @param isQualifiedName is qualified name |
| 66 | */ |
| 67 | public JavaAttributeInfo(YangType<?> attrType, String name, boolean isListAttr, boolean isQualifiedName) { |
| 68 | this.attrType = attrType; |
| 69 | this.name = name; |
| 70 | this.isListAttr = isListAttr; |
| 71 | this.isQualifiedName = isQualifiedName; |
| 72 | } |
| 73 | |
| 74 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 75 | * Returns the data type info of attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 76 | * |
| 77 | * @return the data type info of attribute |
| 78 | */ |
| 79 | public YangType<?> getAttributeType() { |
| 80 | |
| 81 | if (attrType == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 82 | throw new TranslatorException("Expected java attribute type is null"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 83 | } |
| 84 | return attrType; |
| 85 | } |
| 86 | |
| 87 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 88 | * Sets the data type info of attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 89 | * |
| 90 | * @param type the data type info of attribute |
| 91 | */ |
| 92 | public void setAttributeType(YangType<?> type) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 93 | attrType = type; |
| 94 | } |
| 95 | |
| 96 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 97 | * Returns name of the attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 98 | * |
| 99 | * @return name of the attribute |
| 100 | */ |
| 101 | public String getAttributeName() { |
| 102 | |
| 103 | if (name == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 104 | throw new TranslatorException("Expected java attribute name is null"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 105 | } |
| 106 | return name; |
| 107 | } |
| 108 | |
| 109 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 110 | * Sets name of the attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 111 | * |
| 112 | * @param attrName name of the attribute |
| 113 | */ |
| 114 | public void setAttributeName(String attrName) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 115 | name = attrName; |
| 116 | } |
| 117 | |
| 118 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 119 | * Returns if the added attribute is a list of info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 120 | * |
| 121 | * @return the if the added attribute is a list of info |
| 122 | */ |
| 123 | public boolean isListAttr() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 124 | return isListAttr; |
| 125 | } |
| 126 | |
| 127 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 128 | * Sets if the added attribute is a list of info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 129 | * |
| 130 | * @param isList if the added attribute is a list of info |
| 131 | */ |
| 132 | public void setListAttr(boolean isList) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 133 | isListAttr = isList; |
| 134 | } |
| 135 | |
| 136 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 137 | * Returns if the added attribute has to be accessed in a fully qualified |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 138 | * manner. |
| 139 | * |
| 140 | * @return the if the added attribute has to be accessed in a fully |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 141 | * qualified manner. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 142 | */ |
| 143 | public boolean isQualifiedName() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 144 | return isQualifiedName; |
| 145 | } |
| 146 | |
| 147 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 148 | * Sets if the added attribute has to be accessed in a fully qualified |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 149 | * manner. |
| 150 | * |
| 151 | * @param isQualified if the added attribute has to be accessed in a fully |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 152 | * qualified manner |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 153 | */ |
| 154 | public void setIsQualifiedAccess(boolean isQualified) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 155 | isQualifiedName = isQualified; |
| 156 | } |
| 157 | |
| 158 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 159 | * Returns the import info for the attribute type. It will be null, if the type |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 160 | * is basic built-in java type. |
| 161 | * |
| 162 | * @return import info |
| 163 | */ |
| 164 | public JavaQualifiedTypeInfo getImportInfo() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 165 | return importInfo; |
| 166 | } |
| 167 | |
| 168 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 169 | * Sets the import info for the attribute type. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 170 | * |
| 171 | * @param importInfo import info for the attribute type |
| 172 | */ |
| 173 | public void setImportInfo(JavaQualifiedTypeInfo importInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 174 | this.importInfo = importInfo; |
| 175 | } |
| 176 | |
| 177 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 178 | * Returns java attribute info. |
| 179 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 180 | * @param importInfo java qualified type info |
| 181 | * @param attributeName attribute name |
| 182 | * @param attributeType attribute type |
| 183 | * @param isQualifiedAccess is the attribute a qualified access |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 184 | * @param isListAttribute is list attribute |
| 185 | * @return java attribute info. |
| 186 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 187 | public static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfo importInfo, String attributeName, |
| 188 | YangType<?> attributeType, boolean isQualifiedAccess, |
| 189 | boolean isListAttribute) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 190 | |
| 191 | JavaAttributeInfo newAttr = new JavaAttributeInfo(); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 192 | newAttr.setImportInfo(importInfo); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 193 | newAttr.setAttributeName(attributeName); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 194 | newAttr.setAttributeType(attributeType); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 195 | newAttr.setIsQualifiedAccess(isQualifiedAccess); |
| 196 | newAttr.setListAttr(isListAttribute); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 197 | |
| 198 | return newAttr; |
| 199 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 200 | } |