blob: 7718f71361aea63f599d4355d25c4dc8fbfe3e34 [file] [log] [blame]
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +05303 *
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
17package org.onosproject.yangutils.translator.tojava.javamodel;
18
19import java.io.IOException;
20import org.onosproject.yangutils.datamodel.YangOutput;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053021import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053022import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
23import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
24import org.onosproject.yangutils.translator.tojava.JavaImportData;
25import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
26
27import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053028import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfNode;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053029
30/**
Bharat saraswald9822e92016-04-05 15:13:44 +053031 * Represents output information extended to support java code generation.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053032 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053033public class YangJavaOutput extends YangOutput implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053034
35 /**
36 * Contains information of the java file being generated.
37 */
38 private JavaFileInfo javaFileInfo;
39
40 /**
41 * Contains information of the imports to be inserted in the java file
42 * generated.
43 */
44 private JavaImportData javaImportData;
45
46 /**
47 * File handle to maintain temporary java code fragments as per the code
48 * snippet types.
49 */
50 private TempJavaCodeFragmentFiles tempFileHandle;
51
52 /**
53 * Creates an instance of java output.
54 */
55 public YangJavaOutput() {
56 super();
57 setJavaFileInfo(new JavaFileInfo());
58 setJavaImportData(new JavaImportData());
59 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
60 }
61
62 /**
63 * Returns the generated java file information.
64 *
65 * @return generated java file information
66 */
67 @Override
68 public JavaFileInfo getJavaFileInfo() {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053069 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053070 throw new TranslatorException("Missing java info in java datamodel node");
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053071 }
72 return javaFileInfo;
73 }
74
75 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053076 * Sets the java file info object.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053077 *
78 * @param javaInfo java file info object
79 */
80 @Override
81 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053082 javaFileInfo = javaInfo;
83 }
84
85 /**
86 * Returns the data of java imports to be included in generated file.
87 *
88 * @return data of java imports to be included in generated file
89 */
90 @Override
91 public JavaImportData getJavaImportData() {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053092 return javaImportData;
93 }
94
95 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053096 * Sets the data of java imports to be included in generated file.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053097 *
98 * @param javaImportData data of java imports to be included in generated
99 * file
100 */
101 @Override
102 public void setJavaImportData(JavaImportData javaImportData) {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530103 this.javaImportData = javaImportData;
104 }
105
106 /**
107 * Returns the temporary file handle.
108 *
109 * @return temporary file handle
110 */
111 @Override
112 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530113 return tempFileHandle;
114 }
115
116 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530117 * Sets temporary file handle.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530118 *
119 * @param fileHandle temporary file handle
120 */
121 @Override
122 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530123 tempFileHandle = fileHandle;
124 }
125
126 /**
127 * Prepare the information for java code generation corresponding to YANG
128 * container info.
129 *
130 * @param codeGenDir code generation directory
131 * @throws IOException IO operation fail
132 */
133 @Override
134 public void generateCodeEntry(String codeGenDir) throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530135 generateCodeOfNode(this, codeGenDir, false);
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530136 }
137
138 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530139 * Creates a java file using the YANG grouping info.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530140 *
141 * @throws IOException IO operation fail
142 */
143 @Override
144 public void generateCodeExit() throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530145 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530146 }
147}