blob: 347877efe1ba65fb5cc578dc085c6041cf6ae29f [file] [log] [blame]
Vinod Kumar Se4b9b0c2016-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
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053019import java.io.File;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053020import java.io.IOException;
21
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053022import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
23import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Bharat saraswal33dfa012016-05-17 19:59:16 +053024
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053025import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK;
26import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053027import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053028
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053029/**
30 * Represents implementation of java bean code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053031 * Maintains the temp files required specific for bean java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053032 */
33public class TempJavaBeanFragmentFiles
34 extends TempJavaFragmentFiles {
35
36 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053037 * 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 Se4b9b0c2016-04-30 21:09:15 +053047 * 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 saraswale707f032016-07-14 23:33:55 +053052 TempJavaBeanFragmentFiles(JavaFileInfo javaFileInfo)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053053 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053054
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053055 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053056
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053057 /*
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 Se4b9b0c2016-04-30 21:09:15 +053064 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053065
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 saraswal33dfa012016-05-17 19:59:16 +053090 private void addConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053091 throws IOException {
Bharat saraswalb1170bd2016-07-14 13:26:18 +053092 appendToFile(getConstructorImplTempFileHandle(), getConstructor(attr,
Bharat saraswal33dfa012016-05-17 19:59:16 +053093 getGeneratedJavaFiles(), pluginConfig));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053094 }
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 saraswalc0e04842016-05-12 13:16:57 +0530103 @Override
Bharat saraswal33dfa012016-05-17 19:59:16 +0530104 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530105 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530106 super.addJavaSnippetInfoToApplicableTempFiles(newAttrInfo, pluginConfig);
107 addConstructor(newAttrInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530108 }
109
110 /**
111 * Removes all temporary file handles.
112 *
Bharat saraswale707f032016-07-14 23:33:55 +0530113 * @param isErrorOccurred flag to tell translator that error has occurred while code generation
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530114 * @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 Se4b9b0c2016-04-30 21:09:15 +0530128}