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