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