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