Gaurav Agrawal | 97a5e1c | 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; |
| 19 | import org.onosproject.yangutils.datamodel.YangUnion; |
| 20 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
| 21 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
| 22 | import org.onosproject.yangutils.translator.tojava.JavaImportData; |
| 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; |
| 27 | import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfType; |
| 28 | |
| 29 | /** |
| 30 | * Represents union information extended to support java code generation. |
| 31 | */ |
| 32 | public class YangJavaUnion extends YangUnion implements JavaCodeGeneratorInfo, JavaCodeGenerator { |
| 33 | |
| 34 | /** |
| 35 | * Contains the information of the java file being generated. |
| 36 | */ |
| 37 | private JavaFileInfo javaFileInfo; |
| 38 | |
| 39 | /** |
| 40 | * Contains information of the imports to be inserted in the java file |
| 41 | * generated. |
| 42 | */ |
| 43 | private JavaImportData javaImportData; |
| 44 | |
| 45 | /** |
| 46 | * File handle to maintain temporary java code fragments as per the code |
| 47 | * snippet types. |
| 48 | */ |
| 49 | private TempJavaCodeFragmentFiles tempFileHandle; |
| 50 | |
| 51 | /** |
| 52 | * Creates an instance of YANG java union. |
| 53 | */ |
| 54 | public YangJavaUnion() { |
| 55 | super(); |
| 56 | setJavaFileInfo(new JavaFileInfo()); |
| 57 | setJavaImportData(new JavaImportData()); |
| 58 | getJavaFileInfo().setGeneratedFileTypes(GENERATE_UNION_CLASS); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns the generated java file information. |
| 63 | * |
| 64 | * @return generated java file information |
| 65 | */ |
| 66 | @Override |
| 67 | public JavaFileInfo getJavaFileInfo() { |
| 68 | if (javaFileInfo == null) { |
| 69 | throw new RuntimeException("Missing java info in java datamodel node"); |
| 70 | } |
| 71 | return javaFileInfo; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Sets the java file info object. |
| 76 | * |
| 77 | * @param javaInfo java file info object |
| 78 | */ |
| 79 | @Override |
| 80 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
| 81 | javaFileInfo = javaInfo; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the data of java imports to be included in generated file. |
| 86 | * |
| 87 | * @return data of java imports to be included in generated file |
| 88 | */ |
| 89 | @Override |
| 90 | public JavaImportData getJavaImportData() { |
| 91 | return javaImportData; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Sets the data of java imports to be included in generated file. |
| 96 | * |
| 97 | * @param javaImportData data of java imports to be included in generated |
| 98 | * file |
| 99 | */ |
| 100 | @Override |
| 101 | public void setJavaImportData(JavaImportData javaImportData) { |
| 102 | this.javaImportData = javaImportData; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns the temporary file handle. |
| 107 | * |
| 108 | * @return temporary file handle |
| 109 | */ |
| 110 | @Override |
| 111 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
| 112 | if (tempFileHandle == null) { |
| 113 | throw new RuntimeException("Missing temp file hand for current node " |
| 114 | + getJavaFileInfo().getJavaName()); |
| 115 | } |
| 116 | return tempFileHandle; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Sets temporary file handle. |
| 121 | * |
| 122 | * @param fileHandle temporary file handle |
| 123 | */ |
| 124 | @Override |
| 125 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
| 126 | tempFileHandle = fileHandle; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Prepare the information for java code generation corresponding to YANG |
| 131 | * union info. |
| 132 | * |
| 133 | * @param yangPlugin YANG plugin config |
| 134 | * @throws IOException IO operations fails |
| 135 | */ |
| 136 | @Override |
| 137 | public void generateCodeEntry(YangPluginConfig yangPlugin) throws IOException { |
| 138 | generateCodeOfType(this, yangPlugin, false); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Creates a java file using the YANG union info. |
| 143 | * |
| 144 | * @throws IOException IO operations fails |
| 145 | */ |
| 146 | @Override |
| 147 | public void generateCodeExit() throws IOException { |
| 148 | getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_UNION_CLASS, this); |
| 149 | } |
| 150 | } |