blob: bd2742ffc8e0a0b8931fd27241fc9f33d53ef139 [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.generateEventFile;
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 java snippet generation.
34 */
35public class TempJavaEventFragmentFiles
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_FILE_NAME_SUFFIX = "Event";
43
44 /**
45 * Java file handle for event file.
46 */
47 private File eventJavaFileHandle;
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 TempJavaEventFragmentFiles(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);
Bharat saraswalc0e04842016-05-12 13:16:57 +053060
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053061 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
62 getJavaFileInfo().getPackageFilePath()));
63
64 }
65
66 /**
67 * Returns event's java file handle.
68 *
69 * @return java file handle
70 */
71 private File getEventJavaFileHandle() {
72 return eventJavaFileHandle;
73 }
74
75 /**
76 * Sets event's java file handle.
77 *
78 * @param eventJavaFileHandle file handle for event
79 */
80 private void setEventJavaFileHandle(File eventJavaFileHandle) {
81 this.eventJavaFileHandle = eventJavaFileHandle;
82 }
83
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053084 /**
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 */
Bharat saraswalc0e04842016-05-12 13:16:57 +053091 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053092 public void generateJavaFile(int fileType, YangNode curNode)
93 throws IOException {
94
95 createPackage(curNode);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053096 String parentInfo = getCapitalCase(((JavaFileInfoContainer) curNode.getParent())
97 .getJavaFileInfo().getJavaName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053098
99 /**
100 * Creates event interface file.
101 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530102 setEventJavaFileHandle(getJavaFileHandle(parentInfo + EVENT_FILE_NAME_SUFFIX));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530103 generateEventFile(getEventJavaFileHandle(), curNode, null);
104
105 /**
106 * Close all the file handles.
107 */
108 freeTemporaryResources(false);
109 }
110
111 /**
112 * Removes all temporary file handles.
113 *
114 * @param isErrorOccurred when translator fails to generate java files we
115 * need to close all open file handles include temporary files
116 * and java files.
117 * @throws IOException when failed to delete the temporary files
118 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530119 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530120 public void freeTemporaryResources(boolean isErrorOccurred)
121 throws IOException {
122 boolean isError = isErrorOccurred;
123 /**
124 * Close all java file handles and when error occurs delete the files.
125 */
126 closeFile(getEventJavaFileHandle(), isError);
127
128 super.freeTemporaryResources(isErrorOccurred);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530129
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530130 }
131}