blob: 8f0f145090df996d36f7209a74ca56eeafc0997b [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
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 Se4b9b0c2016-04-30 21:09:15 +053062 }
VinodKumarS-Huaweicb3a1f52016-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 /**
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 Se4b9b0c2016-04-30 21:09:15 +0530128}