blob: 7f15bc389e490919c4376b50b69f8d233a47391b [file] [log] [blame]
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301/*
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
17package org.onosproject.yangutils.translator.tojava;
18
Bharat saraswal9fab16b2016-09-23 23:27:24 +053019import org.onosproject.yangutils.utils.io.YangPluginConfig;
20
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053021import java.io.File;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053022import java.io.IOException;
23
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053024import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK;
25import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor;
Bharat saraswald14cbe82016-07-14 13:26:18 +053026import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053027
Vinod Kumar S79a374b2016-04-30 21:09:15 +053028/**
29 * Represents implementation of java bean code fragments temporary implementations.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053030 * Maintains the temp files required specific for bean java snippet generation.
Vinod Kumar S79a374b2016-04-30 21:09:15 +053031 */
32public class TempJavaBeanFragmentFiles
33 extends TempJavaFragmentFiles {
34
35 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053036 * 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 saraswal9fab16b2016-09-23 23:27:24 +053043 private final File constructorImplTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053044
45 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053046 * 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 saraswale50edca2016-08-05 01:58:25 +053051 TempJavaBeanFragmentFiles(JavaFileInfoTranslator javaFileInfo)
Vinod Kumar S79a374b2016-04-30 21:09:15 +053052 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053053
Vinod Kumar S79a374b2016-04-30 21:09:15 +053054 super(javaFileInfo);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053055
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053056 /*
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 saraswal9fab16b2016-09-23 23:27:24 +053061 constructorImplTempFileHandle = getTemporaryFileHandle(CONSTRUCTOR_FILE_NAME);
Vinod Kumar S79a374b2016-04-30 21:09:15 +053062 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053063
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-Huawei6266db32016-05-10 17:58:57 +053074 * Adds constructor for class.
75 *
76 * @param attr attribute info
77 * @throws IOException when fails to append to temporary file
78 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +053079 private void addConstructor(JavaAttributeInfo attr)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053080 throws IOException {
Bharat saraswal9fab16b2016-09-23 23:27:24 +053081 appendToFile(constructorImplTempFileHandle,
82 getConstructor(attr, getGeneratedJavaFiles()));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053083 }
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 saraswal9fab16b2016-09-23 23:27:24 +053089 * files
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053090 * @throws IOException IO operation fail
91 */
Bharat saraswal250a7472016-05-12 13:16:57 +053092 @Override
Bharat saraswal9fab16b2016-09-23 23:27:24 +053093 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo,
94 YangPluginConfig pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053095 throws IOException {
Bharat saraswal715d3fc2016-05-17 19:59:16 +053096 super.addJavaSnippetInfoToApplicableTempFiles(newAttrInfo, pluginConfig);
Bharat saraswal9fab16b2016-09-23 23:27:24 +053097 addConstructor(newAttrInfo);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053098 }
99
100 /**
101 * Removes all temporary file handles.
102 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530103 * @param isErrorOccurred flag to tell translator that error has occurred
104 * while code generation
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530105 * @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 saraswal9fab16b2016-09-23 23:27:24 +0530114 closeFile(constructorImplTempFileHandle, true);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530115 super.freeTemporaryResources(isErrorOccurred);
116 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530117}