VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava; |
| 18 | |
| 19 | import java.io.File; |
| 20 | import java.io.IOException; |
| 21 | import java.util.ArrayList; |
| 22 | |
| 23 | import org.onosproject.yangutils.datamodel.YangNode; |
| 24 | |
| 25 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventFile; |
| 26 | import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile; |
| 27 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage; |
| 28 | import 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 | */ |
| 34 | public 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); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 59 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 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 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 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 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 90 | @Override |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 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 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 116 | @Override |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 117 | 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(getEventJavaFileHandle(), isError); |
| 124 | |
| 125 | super.freeTemporaryResources(isErrorOccurred); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 126 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 127 | } |
| 128 | } |