blob: 1620bc92085d304fd755f9ff4e62eb6af9901c1d [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
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053022import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK;
23import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor;
24import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
25
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053026/**
27 * Represents implementation of java bean code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053028 * Maintains the temp files required specific for bean java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053029 */
30public class TempJavaBeanFragmentFiles
31 extends TempJavaFragmentFiles {
32
33 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053034 * 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 Se4b9b0c2016-04-30 21:09:15 +053044 * 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-Huaweicb3a1f52016-05-10 17:58:57 +053051
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053052 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053053
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053054 /*
55 * Initialize getterImpl, attributes, constructor, hash code, equals and
56 * to strings when generation file type matches to impl class mask.
57 */
58 addGeneratedTempFile(CONSTRUCTOR_IMPL_MASK);
59
60 setConstructorImplTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FILE_NAME));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053061 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053062
63 /**
64 * Returns constructor's temporary file handle.
65 *
66 * @return temporary file handle
67 */
68 public File getConstructorImplTempFileHandle() {
69 return constructorImplTempFileHandle;
70 }
71
72 /**
73 * Sets to constructor's temporary file handle.
74 *
75 * @param constructor file handle for to constructor
76 */
77 private void setConstructorImplTempFileHandle(File constructor) {
78 constructorImplTempFileHandle = constructor;
79 }
80
81 /**
82 * Adds constructor for class.
83 *
84 * @param attr attribute info
85 * @throws IOException when fails to append to temporary file
86 */
87 private void addConstructor(JavaAttributeInfo attr)
88 throws IOException {
89 appendToFile(getConstructorImplTempFileHandle(), getConstructor(getGeneratedJavaClassName(), attr,
90 getGeneratedJavaFiles()));
91 }
92
93 /**
94 * Adds the new attribute info to the target generated temporary files.
95 *
96 * @param newAttrInfo the attribute info that needs to be added to temporary
97 * files
98 * @throws IOException IO operation fail
99 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530100 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530101 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
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530127}