Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +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.javamodel; |
| 18 | |
| 19 | import java.io.IOException; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 20 | |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangEnumeration; |
| 22 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
| 23 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
| 24 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
| 26 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
| 27 | |
| 28 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfNode; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Represents YANG java enumeration information extended to support java code generation. |
| 33 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 34 | public class YangJavaEnumeration |
| 35 | extends YangEnumeration |
| 36 | implements JavaCodeGenerator, JavaCodeGeneratorInfo { |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Contains the information of the java file being generated. |
| 40 | */ |
| 41 | private JavaFileInfo javaFileInfo; |
| 42 | |
| 43 | /** |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 44 | * File handle to maintain temporary java code fragments as per the code |
| 45 | * snippet types. |
| 46 | */ |
| 47 | private TempJavaCodeFragmentFiles tempFileHandle; |
| 48 | |
| 49 | /** |
| 50 | * Creates YANG java enumeration object. |
| 51 | */ |
| 52 | public YangJavaEnumeration() { |
| 53 | super(); |
| 54 | setJavaFileInfo(new JavaFileInfo()); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 55 | getJavaFileInfo().setGeneratedFileTypes(GENERATE_ENUM_CLASS); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns the generated java file information. |
| 60 | * |
| 61 | * @return generated java file information |
| 62 | */ |
| 63 | @Override |
| 64 | public JavaFileInfo getJavaFileInfo() { |
| 65 | |
| 66 | if (javaFileInfo == null) { |
| 67 | throw new TranslatorException("Missing java info in java datamodel node"); |
| 68 | } |
| 69 | return javaFileInfo; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Sets the java file info object. |
| 74 | * |
| 75 | * @param javaInfo java file info object |
| 76 | */ |
| 77 | @Override |
| 78 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 79 | javaFileInfo = javaInfo; |
| 80 | } |
| 81 | |
| 82 | /** |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 83 | * Returns the temporary file handle. |
| 84 | * |
| 85 | * @return temporary file handle |
| 86 | */ |
| 87 | @Override |
| 88 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 89 | return tempFileHandle; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Sets temporary file handle. |
| 94 | * |
| 95 | * @param fileHandle temporary file handle |
| 96 | */ |
| 97 | @Override |
| 98 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 99 | tempFileHandle = fileHandle; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Prepare the information for java code generation corresponding to YANG |
| 104 | * enumeration info. |
| 105 | * |
| 106 | * @param yangPlugin YANG plugin config |
| 107 | * @throws IOException IO operations fails |
| 108 | */ |
| 109 | @Override |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 110 | public void generateCodeEntry(YangPluginConfig yangPlugin) |
| 111 | throws IOException { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 112 | generateCodeOfNode(this, yangPlugin); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Creates a java file using the YANG enumeration info. |
| 117 | * |
| 118 | * @throws IOException IO operation fail |
| 119 | */ |
| 120 | @Override |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 121 | public void generateCodeExit() |
| 122 | throws IOException { |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 123 | getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_ENUM_CLASS, this); |
| 124 | } |
| 125 | |
| 126 | } |