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; |
| 17 | |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 18 | import java.io.File; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 19 | import java.io.IOException; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 20 | |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangEnum; |
| 22 | import org.onosproject.yangutils.datamodel.YangEnumeration; |
| 23 | import org.onosproject.yangutils.datamodel.YangNode; |
| 24 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeTranslator; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.utils.io.YangPluginConfig; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 27 | |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK; |
| 30 | import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame^] | 31 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen |
| 32 | .generateEnumAttributeStringWithSchemaName; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEnumClassFile; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.utils.UtilConstants.INT; |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_FIRST_DIGIT; |
| 38 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUTO_PREFIX; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile; |
| 40 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPrefixForIdentifier; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 41 | |
| 42 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 43 | * Represents implementation of java code fragments temporary implementations. Maintains the temp files required |
| 44 | * specific for enumeration java snippet generation. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 45 | */ |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 46 | public class TempJavaEnumerationFragmentFiles |
| 47 | extends TempJavaFragmentFiles { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * File name for temporary enum class. |
| 51 | */ |
| 52 | private static final String ENUM_CLASS_TEMP_FILE_NAME = "EnumClass"; |
| 53 | |
| 54 | /** |
| 55 | * File name for enum class file name suffix. |
| 56 | */ |
| 57 | private static final String ENUM_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING; |
| 58 | |
| 59 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 60 | * Temporary file handle for enum class file. |
| 61 | */ |
| 62 | private File enumClassTempFileHandle; |
| 63 | |
| 64 | /** |
| 65 | * Java file handle for enum class. |
| 66 | */ |
| 67 | private File enumClassJavaFileHandle; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Creates an instance of temporary java code fragment. |
| 71 | * |
| 72 | * @param javaFileInfo generated java file info |
| 73 | * @throws IOException when fails to create new file handle |
| 74 | */ |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 75 | TempJavaEnumerationFragmentFiles(JavaFileInfoTranslator javaFileInfo) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 76 | throws IOException { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 77 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 78 | super(javaFileInfo); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 79 | /* |
| 80 | * Initialize enum when generation file type matches to enum class mask. |
| 81 | */ |
| 82 | addGeneratedTempFile(ENUM_IMPL_MASK); |
| 83 | setEnumClassTempFileHandle(getTemporaryFileHandle(ENUM_CLASS_TEMP_FILE_NAME)); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns enum class java file handle. |
| 88 | * |
| 89 | * @return enum class java file handle |
| 90 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 91 | private File getEnumClassJavaFileHandle() { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 92 | return enumClassJavaFileHandle; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Sets enum class java file handle. |
| 97 | * |
| 98 | * @param enumClassJavaFileHandle enum class java file handle |
| 99 | */ |
| 100 | private void setEnumClassJavaFileHandle(File enumClassJavaFileHandle) { |
| 101 | this.enumClassJavaFileHandle = enumClassJavaFileHandle; |
| 102 | } |
| 103 | |
| 104 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 105 | * Returns temporary file handle for enum class file. |
| 106 | * |
| 107 | * @return temporary file handle for enum class file |
| 108 | */ |
| 109 | public File getEnumClassTempFileHandle() { |
| 110 | return enumClassTempFileHandle; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Sets temporary file handle for enum class file. |
| 115 | * |
| 116 | * @param enumClassTempFileHandle temporary file handle for enum class file |
| 117 | */ |
| 118 | private void setEnumClassTempFileHandle(File enumClassTempFileHandle) { |
| 119 | this.enumClassTempFileHandle = enumClassTempFileHandle; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Adds enum class attributes to temporary file. |
| 124 | * |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame^] | 125 | * @param yangEnum YANG enum |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 126 | * @throws IOException when fails to do IO operations. |
| 127 | */ |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame^] | 128 | private void addAttributesForEnumClass(YangPluginConfig pluginConfig, YangEnum yangEnum) |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 129 | throws IOException { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 130 | appendToFile(getEnumClassTempFileHandle(), |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame^] | 131 | generateEnumAttributeStringWithSchemaName(yangEnum.getNamedValue(), |
| 132 | yangEnum.getValue(), pluginConfig)); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Adds enum attributes to temporary files. |
| 137 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 138 | * @param curNode current YANG node |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 139 | * @param pluginConfig plugin configurations |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 140 | * @throws IOException when fails to do IO operations |
| 141 | */ |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 142 | void addEnumAttributeToTempFiles(YangNode curNode, YangPluginConfig pluginConfig) |
| 143 | throws IOException { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 144 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 145 | super.addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeForEnum(pluginConfig), pluginConfig); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 146 | if (curNode instanceof YangEnumeration) { |
| 147 | YangEnumeration enumeration = (YangEnumeration) curNode; |
| 148 | for (YangEnum curEnum : enumeration.getEnumSet()) { |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 149 | String enumName = curEnum.getNamedValue(); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 150 | String prefixForIdentifier; |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 151 | if (enumName.matches(REGEX_FOR_FIRST_DIGIT)) { |
| 152 | prefixForIdentifier = getPrefixForIdentifier(pluginConfig.getConflictResolver()); |
| 153 | if (prefixForIdentifier != null) { |
| 154 | curEnum.setNamedValue(prefixForIdentifier + enumName); |
| 155 | } else { |
| 156 | curEnum.setNamedValue(YANG_AUTO_PREFIX + enumName); |
| 157 | } |
| 158 | } |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame^] | 159 | addJavaSnippetInfoToApplicableTempFiles(pluginConfig, curEnum); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 160 | } |
| 161 | } else { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 162 | throw new TranslatorException("current node should be of enumeration type. " + |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame^] | 163 | curNode.getName() + " in " + curNode.getLineNumber() + " at " + |
| 164 | curNode.getCharPosition() |
| 165 | + " in " + curNode.getFileName()); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 170 | * Returns java attribute for enum class. |
| 171 | * |
| 172 | * @param pluginConfig plugin configurations |
| 173 | * @return java attribute |
| 174 | */ |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame^] | 175 | private JavaAttributeInfo getJavaAttributeForEnum(YangPluginConfig pluginConfig) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 176 | YangJavaTypeTranslator javaType = new YangJavaTypeTranslator(); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 177 | javaType.setDataType(INT32); |
| 178 | javaType.setDataTypeName(INT); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 179 | javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver()); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 180 | return getAttributeInfoForTheData( |
| 181 | javaType.getJavaQualifiedInfo(), |
| 182 | javaType.getDataTypeName(), javaType, |
| 183 | getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()), |
| 184 | false); |
| 185 | } |
| 186 | |
| 187 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 188 | * Adds the new attribute info to the target generated temporary files. |
| 189 | * |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame^] | 190 | * @param yangEnum@throws IOException IO operation fail |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 191 | */ |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame^] | 192 | private void addJavaSnippetInfoToApplicableTempFiles(YangPluginConfig pluginConfig, YangEnum yangEnum) |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 193 | throws IOException { |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame^] | 194 | addAttributesForEnumClass(pluginConfig, yangEnum); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Constructs java code exit. |
| 199 | * |
| 200 | * @param fileType generated file type |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 201 | * @param curNode current YANG node |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 202 | * @throws IOException when fails to generate java files |
| 203 | */ |
| 204 | @Override |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 205 | public void generateJavaFile(int fileType, YangNode curNode) |
| 206 | throws IOException { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 207 | createPackage(curNode); |
| 208 | setEnumClassJavaFileHandle(getJavaFileHandle(getJavaClassName(ENUM_CLASS_FILE_NAME_SUFFIX))); |
| 209 | setEnumClassJavaFileHandle(generateEnumClassFile(getEnumClassJavaFileHandle(), curNode)); |
| 210 | freeTemporaryResources(false); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Removes all temporary file handles. |
| 215 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 216 | * @param isErrorOccurred flag to tell translator that error has occurred while file generation |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 217 | * @throws IOException when failed to delete the temporary files |
| 218 | */ |
| 219 | @Override |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 220 | public void freeTemporaryResources(boolean isErrorOccurred) |
| 221 | throws IOException { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 222 | closeFile(getEnumClassJavaFileHandle(), isErrorOccurred); |
| 223 | closeFile(getEnumClassTempFileHandle(), true); |
| 224 | super.freeTemporaryResources(isErrorOccurred); |
| 225 | } |
| 226 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 227 | } |