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; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 21 | |
| 22 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame^] | 23 | import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 24 | |
| 25 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventFile; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame^] | 26 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile; |
| 28 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage; |
| 29 | import 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 | */ |
| 35 | public 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 saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame^] | 57 | setJavaExtendsListHolder(new JavaExtendsListHolder()); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 58 | setJavaImportData(new JavaImportData()); |
| 59 | setJavaFileInfo(javaFileInfo); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 60 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 61 | 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-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 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 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 91 | @Override |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 92 | public void generateJavaFile(int fileType, YangNode curNode) |
| 93 | throws IOException { |
| 94 | |
| 95 | createPackage(curNode); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame^] | 96 | String parentInfo = getCapitalCase(((JavaFileInfoContainer) curNode.getParent()) |
| 97 | .getJavaFileInfo().getJavaName()); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Creates event interface file. |
| 101 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame^] | 102 | setEventJavaFileHandle(getJavaFileHandle(parentInfo + EVENT_FILE_NAME_SUFFIX)); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 103 | 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 saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 119 | @Override |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 120 | 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 saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 129 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 130 | } |
| 131 | } |