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 | |
| 17 | package org.onosproject.yangutils.translator.tojava; |
| 18 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.utils.io.YangPluginConfig; |
| 20 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 21 | import java.io.File; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 22 | import java.io.IOException; |
| 23 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 24 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK; |
| 25 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 27 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 28 | /** |
| 29 | * Represents implementation of java bean code fragments temporary implementations. |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 30 | * Maintains the temp files required specific for bean java snippet generation. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 31 | */ |
| 32 | public class TempJavaBeanFragmentFiles |
| 33 | extends TempJavaFragmentFiles { |
| 34 | |
| 35 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 36 | * File name for constructor. |
| 37 | */ |
| 38 | private static final String CONSTRUCTOR_FILE_NAME = "Constructor"; |
| 39 | |
| 40 | /** |
| 41 | * Temporary file handle for constructor of class. |
| 42 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 43 | private final File constructorImplTempFileHandle; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 44 | |
| 45 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 46 | * Creates an instance of temporary java code fragment. |
| 47 | * |
| 48 | * @param javaFileInfo generated java file info |
| 49 | * @throws IOException when fails to create new file handle |
| 50 | */ |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 51 | TempJavaBeanFragmentFiles(JavaFileInfoTranslator javaFileInfo) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 52 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 53 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 54 | super(javaFileInfo); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 55 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 56 | /* |
| 57 | * Initialize getterImpl, attributes, constructor, hash code, equals and |
| 58 | * to strings when generation file type matches to impl class mask. |
| 59 | */ |
| 60 | addGeneratedTempFile(CONSTRUCTOR_IMPL_MASK); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 61 | constructorImplTempFileHandle = getTemporaryFileHandle(CONSTRUCTOR_FILE_NAME); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 62 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Returns constructor's temporary file handle. |
| 66 | * |
| 67 | * @return temporary file handle |
| 68 | */ |
| 69 | public File getConstructorImplTempFileHandle() { |
| 70 | return constructorImplTempFileHandle; |
| 71 | } |
| 72 | |
| 73 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 74 | * Adds constructor for class. |
| 75 | * |
| 76 | * @param attr attribute info |
| 77 | * @throws IOException when fails to append to temporary file |
| 78 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 79 | private void addConstructor(JavaAttributeInfo attr) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 80 | throws IOException { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 81 | appendToFile(constructorImplTempFileHandle, |
| 82 | getConstructor(attr, getGeneratedJavaFiles())); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Adds the new attribute info to the target generated temporary files. |
| 87 | * |
| 88 | * @param newAttrInfo the attribute info that needs to be added to temporary |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 89 | * files |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 90 | * @throws IOException IO operation fail |
| 91 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 92 | @Override |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 93 | void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo, |
| 94 | YangPluginConfig pluginConfig) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 95 | throws IOException { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 96 | super.addJavaSnippetInfoToApplicableTempFiles(newAttrInfo, pluginConfig); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 97 | addConstructor(newAttrInfo); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Removes all temporary file handles. |
| 102 | * |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 103 | * @param isErrorOccurred flag to tell translator that error has occurred |
| 104 | * while code generation |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 105 | * @throws IOException when failed to delete the temporary files |
| 106 | */ |
| 107 | @Override |
| 108 | public void freeTemporaryResources(boolean isErrorOccurred) |
| 109 | throws IOException { |
| 110 | |
| 111 | /* |
| 112 | * Close constructor temporary file handle and delete the file. |
| 113 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 114 | closeFile(constructorImplTempFileHandle, true); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 115 | super.freeTemporaryResources(isErrorOccurred); |
| 116 | } |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 117 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 118 | } |