Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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; |
| 18 | |
| 19 | import org.onosproject.yangutils.datamodel.YangNode; |
| 20 | import org.onosproject.yangutils.datamodel.YangType; |
| 21 | |
| 22 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getIsQualifiedAccessOrAddToImportList; |
| 23 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode; |
| 24 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfLeafAttribute; |
| 25 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 26 | |
| 27 | /** |
| 28 | * Maintains the attribute info corresponding to class/interface generated. |
| 29 | */ |
| 30 | public final class JavaAttributeInfo { |
| 31 | |
| 32 | /** |
| 33 | * The data type info of attribute. |
| 34 | */ |
| 35 | private YangType<?> attrType; |
| 36 | |
| 37 | /** |
| 38 | * Name of the attribute. |
| 39 | */ |
| 40 | private String name; |
| 41 | |
| 42 | /** |
| 43 | * If the added attribute is a list of info. |
| 44 | */ |
| 45 | private boolean isListAttr = false; |
| 46 | |
| 47 | /** |
| 48 | * If the added attribute has to be accessed in a fully qualified manner. |
| 49 | */ |
| 50 | private boolean isQualifiedName = false; |
| 51 | |
| 52 | /** |
| 53 | * The class info will be used to set the attribute type and package info |
| 54 | * will be use for qualified name. |
| 55 | */ |
| 56 | private JavaQualifiedTypeInfo importInfo; |
| 57 | |
| 58 | /** |
| 59 | * Default constructor. |
| 60 | */ |
| 61 | private JavaAttributeInfo() { |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get the data type info of attribute. |
| 66 | * |
| 67 | * @return the data type info of attribute |
| 68 | */ |
| 69 | public YangType<?> getAttributeType() { |
| 70 | |
| 71 | if (attrType == null) { |
| 72 | throw new RuntimeException("Expected java attribute type is null"); |
| 73 | } |
| 74 | return attrType; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Set the data type info of attribute. |
| 79 | * |
| 80 | * @param type the data type info of attribute |
| 81 | */ |
| 82 | public void setAttributeType(YangType<?> type) { |
| 83 | |
| 84 | attrType = type; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get name of the attribute. |
| 89 | * |
| 90 | * @return name of the attribute |
| 91 | */ |
| 92 | public String getAttributeName() { |
| 93 | |
| 94 | if (name == null) { |
| 95 | throw new RuntimeException("Expected java attribute name is null"); |
| 96 | } |
| 97 | return name; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Set name of the attribute. |
| 102 | * |
| 103 | * @param attrName name of the attribute |
| 104 | */ |
| 105 | public void setAttributeName(String attrName) { |
| 106 | |
| 107 | name = attrName; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get if the added attribute is a list of info. |
| 112 | * |
| 113 | * @return the if the added attribute is a list of info |
| 114 | */ |
| 115 | public boolean isListAttr() { |
| 116 | |
| 117 | return isListAttr; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Set if the added attribute is a list of info. |
| 122 | * |
| 123 | * @param isList if the added attribute is a list of info |
| 124 | */ |
| 125 | public void setListAttr(boolean isList) { |
| 126 | |
| 127 | isListAttr = isList; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get if the added attribute has to be accessed in a fully qualified |
| 132 | * manner. |
| 133 | * |
| 134 | * @return the if the added attribute has to be accessed in a fully |
| 135 | * qualified manner. |
| 136 | */ |
| 137 | public boolean isQualifiedName() { |
| 138 | |
| 139 | return isQualifiedName; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Set if the added attribute has to be accessed in a fully qualified |
| 144 | * manner. |
| 145 | * |
| 146 | * @param isQualified if the added attribute has to be accessed in a fully |
| 147 | * qualified manner |
| 148 | */ |
| 149 | public void setIsQualifiedAccess(boolean isQualified) { |
| 150 | |
| 151 | isQualifiedName = isQualified; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get the import info for the attribute type. It will be null, of the type |
| 156 | * is basic built-in java type. |
| 157 | * |
| 158 | * @return import info |
| 159 | */ |
| 160 | public JavaQualifiedTypeInfo getImportInfo() { |
| 161 | |
| 162 | return importInfo; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Set the import info for the attribute type. |
| 167 | * |
| 168 | * @param importInfo import info for the attribute type |
| 169 | */ |
| 170 | public void setImportInfo(JavaQualifiedTypeInfo importInfo) { |
| 171 | |
| 172 | this.importInfo = importInfo; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Create an attribute info object corresponding to the passed leaf |
| 177 | * information and return it. |
| 178 | * |
| 179 | * @param curNode current data model node for which the java file is being |
| 180 | * generated |
| 181 | * @param attributeType leaf data type |
| 182 | * @param attributeName leaf name |
| 183 | * @param isListAttribute is the current added attribute needs to be a list |
| 184 | * @return AttributeInfo attribute details required to add in temporary |
| 185 | * files |
| 186 | */ |
| 187 | public static JavaAttributeInfo getAttributeInfoOfLeaf(YangNode curNode, |
| 188 | YangType<?> attributeType, String attributeName, |
| 189 | boolean isListAttribute) { |
| 190 | |
| 191 | JavaAttributeInfo newAttr = new JavaAttributeInfo(); |
| 192 | |
| 193 | /* |
| 194 | * Get the import info corresponding to the attribute for import in |
| 195 | * generated java files or qualified access |
| 196 | */ |
| 197 | JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfLeafAttribute(curNode, |
| 198 | attributeType, attributeName, isListAttribute); |
| 199 | newAttr.setImportInfo(importInfo); |
| 200 | newAttr.setIsQualifiedAccess(getIsQualifiedAccessOrAddToImportList( |
| 201 | curNode, importInfo)); |
| 202 | newAttr.setAttributeName(getCamelCase(attributeName)); |
| 203 | newAttr.setListAttr(isListAttribute); |
| 204 | newAttr.setImportInfo(importInfo); |
| 205 | newAttr.setAttributeType(attributeType); |
| 206 | |
| 207 | return newAttr; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Create an attribute info object corresponding to a data model node and |
| 212 | * return it. |
| 213 | * |
| 214 | * @param curNode current data model node for which the java code generation |
| 215 | * is being handled |
| 216 | * @param parentNode parent node in which the current node is an attribute |
| 217 | * @param isListNode is the current added attribute needs to be a list |
| 218 | * @return AttributeInfo attribute details required to add in temporary |
| 219 | * files |
| 220 | */ |
| 221 | public static JavaAttributeInfo getCurNodeAsAttributeInParent( |
| 222 | YangNode curNode, YangNode parentNode, boolean isListNode) { |
| 223 | |
| 224 | JavaAttributeInfo newAttr = new JavaAttributeInfo(); |
| 225 | |
| 226 | // if (curNode instanceof HasJavaFileInfo) { |
| 227 | // throw new RuntimeException("translator data model node does not have java info"); |
| 228 | // } |
| 229 | |
| 230 | String curNodeName = ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName(); |
| 231 | |
| 232 | /* |
| 233 | * Get the import info corresponding to the attribute for import in |
| 234 | * generated java files or qualified access |
| 235 | */ |
| 236 | JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode, |
| 237 | curNodeName, isListNode); |
| 238 | newAttr.setImportInfo(qualifiedTypeInfo); |
| 239 | newAttr.setIsQualifiedAccess( |
| 240 | getIsQualifiedAccessOrAddToImportList(parentNode, |
| 241 | qualifiedTypeInfo)); |
| 242 | newAttr.setAttributeName(getCamelCase(curNodeName)); |
| 243 | newAttr.setListAttr(isListNode); |
| 244 | newAttr.setImportInfo(qualifiedTypeInfo); |
| 245 | newAttr.setAttributeType(null); |
| 246 | |
| 247 | return newAttr; |
| 248 | } |
| 249 | } |