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