blob: 8009e45b11a240745f00f6b37019aaa293f2cddd [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;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053021
22import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053023import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053024
25import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventListenerFile;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053026import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053027import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
28import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
29import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
30
31/**
32 * Represents implementation of java bean code fragments temporary implementations.
33 * Maintains the temp files required specific for event listener java snippet generation.
34 */
35public class TempJavaEventListenerFragmentFiles
36 extends TempJavaFragmentFiles {
37
38 /**
39 * File name for generated class file for special type like union, typedef
40 * suffix.
41 */
42 private static final String EVENT_LISTENER_FILE_NAME_SUFFIX = "Listener";
43
44 /**
45 * Java file handle for event listener file.
46 */
47 private File eventListenerJavaFileHandle;
48
49 /**
50 * Creates an instance of temporary java code fragment.
51 *
52 * @param javaFileInfo generated java file info
53 * @throws IOException when fails to create new file handle
54 */
55 public TempJavaEventListenerFragmentFiles(JavaFileInfo javaFileInfo)
56 throws IOException {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053057 setJavaExtendsListHolder(new JavaExtendsListHolder());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053058 setJavaImportData(new JavaImportData());
59 setJavaFileInfo(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053060 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
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053082 /**
83 * Constructs java code exit.
84 *
85 * @param fileType generated file type
86 * @param curNode current YANG node
87 * @throws IOException when fails to generate java files
88 */
Bharat saraswalc0e04842016-05-12 13:16:57 +053089 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053090 public void generateJavaFile(int fileType, YangNode curNode)
91 throws IOException {
92
93 createPackage(curNode);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053094 String parentInfo = getCapitalCase(((JavaFileInfoContainer) curNode.getParent())
95 .getJavaFileInfo().getJavaName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053096 /**
97 * Creates event listener interface file.
98 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053099 setEventListenerJavaFileHandle(getJavaFileHandle(parentInfo + EVENT_LISTENER_FILE_NAME_SUFFIX));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530100 generateEventListenerFile(getEventListenerJavaFileHandle(), 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 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530116 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530117 public void freeTemporaryResources(boolean isErrorOccurred)
118 throws IOException {
119 boolean isError = isErrorOccurred;
120 /**
121 * Close all java file handles and when error occurs delete the files.
122 */
123 closeFile(getEventListenerJavaFileHandle(), isError);
124
125 super.freeTemporaryResources(isErrorOccurred);
126 }
127
128}