blob: 39ce34fe0c2df23a1585d2f7404c2999a963ed59 [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
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053022import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Bharat saraswal33dfa012016-05-17 19:59:16 +053023
VinodKumarS-Huaweicb3a1f52016-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 saraswalb1170bd2016-07-14 13:26:18 +053026import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053027
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053028/**
29 * Represents implementation of java bean code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053030 * Maintains the temp files required specific for bean java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053031 */
32public class TempJavaBeanFragmentFiles
33 extends TempJavaFragmentFiles {
34
35 /**
VinodKumarS-Huaweicb3a1f52016-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 */
43 private File constructorImplTempFileHandle;
44
45 /**
Vinod Kumar Se4b9b0c2016-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 */
51 public TempJavaBeanFragmentFiles(JavaFileInfo javaFileInfo)
52 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053053
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053054 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053055
VinodKumarS-Huaweicb3a1f52016-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);
61
62 setConstructorImplTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FILE_NAME));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053063 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053064
65 /**
66 * Returns constructor's temporary file handle.
67 *
68 * @return temporary file handle
69 */
70 public File getConstructorImplTempFileHandle() {
71 return constructorImplTempFileHandle;
72 }
73
74 /**
75 * Sets to constructor's temporary file handle.
76 *
77 * @param constructor file handle for to constructor
78 */
79 private void setConstructorImplTempFileHandle(File constructor) {
80 constructorImplTempFileHandle = constructor;
81 }
82
83 /**
84 * Adds constructor for class.
85 *
86 * @param attr attribute info
87 * @throws IOException when fails to append to temporary file
88 */
Bharat saraswal33dfa012016-05-17 19:59:16 +053089 private void addConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053090 throws IOException {
Bharat saraswalb1170bd2016-07-14 13:26:18 +053091 appendToFile(getConstructorImplTempFileHandle(), getConstructor(attr,
Bharat saraswal33dfa012016-05-17 19:59:16 +053092 getGeneratedJavaFiles(), pluginConfig));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053093 }
94
95 /**
96 * Adds the new attribute info to the target generated temporary files.
97 *
98 * @param newAttrInfo the attribute info that needs to be added to temporary
99 * files
100 * @throws IOException IO operation fail
101 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530102 @Override
Bharat saraswal33dfa012016-05-17 19:59:16 +0530103 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530104 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530105 super.addJavaSnippetInfoToApplicableTempFiles(newAttrInfo, pluginConfig);
106 addConstructor(newAttrInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530107 }
108
109 /**
110 * Removes all temporary file handles.
111 *
112 * @param isErrorOccurred when translator fails to generate java files we
113 * need to close all open file handles include temporary files
114 * and java files.
115 * @throws IOException when failed to delete the temporary files
116 */
117 @Override
118 public void freeTemporaryResources(boolean isErrorOccurred)
119 throws IOException {
120
121 /*
122 * Close constructor temporary file handle and delete the file.
123 */
124 closeFile(getConstructorImplTempFileHandle(), true);
125
126 super.freeTemporaryResources(isErrorOccurred);
127 }
128
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530129}