blob: c6a297e770e25524b5f0f3029ab7cd8e8790b043 [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.generateEventListenerFile;
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 listener java snippet generation.
33 */
34public class TempJavaEventListenerFragmentFiles
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_LISTENER_FILE_NAME_SUFFIX = "Listener";
42
43 /**
44 * Java file handle for event listener file.
45 */
46 private File eventListenerJavaFileHandle;
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 TempJavaEventListenerFragmentFiles(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 * Returns event listeners's java file handle.
66 *
67 * @return java file handle
68 */
69 private File getEventListenerJavaFileHandle() {
70 return eventListenerJavaFileHandle;
71 }
72
73 /**
74 * Sets event's java file handle.
75 *
76 * @param eventListenerJavaFileHandle file handle for event
77 */
78 private void setEventListenerJavaFileHandle(File eventListenerJavaFileHandle) {
79 this.eventListenerJavaFileHandle = eventListenerJavaFileHandle;
80 }
81
82
83 /**
84 * Constructs java code exit.
85 *
86 * @param fileType generated file type
87 * @param curNode current YANG node
88 * @throws IOException when fails to generate java files
89 */
90 public void generateJavaFile(int fileType, YangNode curNode)
91 throws IOException {
92
93 createPackage(curNode);
94
95 /**
96 * Creates event listener interface file.
97 */
98 setEventListenerJavaFileHandle(getJavaFileHandle(getJavaClassName(EVENT_LISTENER_FILE_NAME_SUFFIX)));
99 generateEventListenerFile(getEventListenerJavaFileHandle(), curNode, null);
100
101 /**
102 * Close all the file handles.
103 */
104 freeTemporaryResources(false);
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 public void freeTemporaryResources(boolean isErrorOccurred)
116 throws IOException {
117 boolean isError = isErrorOccurred;
118 /**
119 * Close all java file handles and when error occurs delete the files.
120 */
121 closeFile(getEventListenerJavaFileHandle(), isError);
122
123 super.freeTemporaryResources(isErrorOccurred);
124 }
125
126}