blob: 0eada3d3f5582cfbe06702eb84e3a9aa1f33e0ca [file] [log] [blame]
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +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
19import java.io.File;
20import java.io.IOException;
21import java.util.ArrayList;
22
23import org.onosproject.yangutils.datamodel.YangNode;
24
25import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventFile;
26import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
27import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
28import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
29
30/**
31 * Represents implementation of java bean code fragments temporary implementations.
32 * Maintains the temp files required specific for event java snippet generation.
33 */
34public class TempJavaEventFragmentFiles
35 extends TempJavaFragmentFiles {
36
37 /**
38 * File name for generated class file for special type like union, typedef
39 * suffix.
40 */
41 private static final String EVENT_FILE_NAME_SUFFIX = "Event";
42
43 /**
44 * Java file handle for event file.
45 */
46 private File eventJavaFileHandle;
47
48 /**
49 * Creates an instance of temporary java code fragment.
50 *
51 * @param javaFileInfo generated java file info
52 * @throws IOException when fails to create new file handle
53 */
54 public TempJavaEventFragmentFiles(JavaFileInfo javaFileInfo)
55 throws IOException {
56 setExtendsList(new ArrayList<>());
57 setJavaImportData(new JavaImportData());
58 setJavaFileInfo(javaFileInfo);
59 clearGeneratedTempFileMask();
60 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
61 getJavaFileInfo().getPackageFilePath()));
62
63 }
64
65 /**
66 * Returns event's java file handle.
67 *
68 * @return java file handle
69 */
70 private File getEventJavaFileHandle() {
71 return eventJavaFileHandle;
72 }
73
74 /**
75 * Sets event's java file handle.
76 *
77 * @param eventJavaFileHandle file handle for event
78 */
79 private void setEventJavaFileHandle(File eventJavaFileHandle) {
80 this.eventJavaFileHandle = eventJavaFileHandle;
81 }
82
83
84 /**
85 * Constructs java code exit.
86 *
87 * @param fileType generated file type
88 * @param curNode current YANG node
89 * @throws IOException when fails to generate java files
90 */
91 public void generateJavaFile(int fileType, YangNode curNode)
92 throws IOException {
93
94 createPackage(curNode);
95
96 /**
97 * Creates event interface file.
98 */
99 setEventJavaFileHandle(getJavaFileHandle(getJavaClassName(EVENT_FILE_NAME_SUFFIX)));
100 generateEventFile(getEventJavaFileHandle(), curNode, null);
101
102 /**
103 * Close all the file handles.
104 */
105 freeTemporaryResources(false);
106 }
107
108 /**
109 * Removes all temporary file handles.
110 *
111 * @param isErrorOccurred when translator fails to generate java files we
112 * need to close all open file handles include temporary files
113 * and java files.
114 * @throws IOException when failed to delete the temporary files
115 */
116 public void freeTemporaryResources(boolean isErrorOccurred)
117 throws IOException {
118 boolean isError = isErrorOccurred;
119 /**
120 * Close all java file handles and when error occurs delete the files.
121 */
122 closeFile(getEventJavaFileHandle(), isError);
123
124 super.freeTemporaryResources(isErrorOccurred);
125 }
126}