blob: dabac6b1af61ff8ff46b9c634710465f7ebac201 [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-03-23 15:30:27 +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 */
16package org.onosproject.yangutils.translator.tojava.javamodel;
17
18import java.io.IOException;
Vinod Kumar S38046502016-03-23 15:30:27 +053019import org.onosproject.yangutils.datamodel.YangContainer;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053020import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053021import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
22import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
23import org.onosproject.yangutils.translator.tojava.JavaImportData;
24import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
25
26import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053027import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfNode;
Vinod Kumar S38046502016-03-23 15:30:27 +053028
29/**
Bharat saraswald9822e92016-04-05 15:13:44 +053030 * Represents container information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053031 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053032public class YangJavaContainer extends YangContainer implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S38046502016-03-23 15:30:27 +053033
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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053052 * Creates YANG java container object.
Vinod Kumar S38046502016-03-23 15:30:27 +053053 */
54 public YangJavaContainer() {
55 super();
56 setJavaFileInfo(new JavaFileInfo());
57 setJavaImportData(new JavaImportData());
58 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
59 }
60
61 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053062 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053063 *
64 * @return generated java file information
65 */
66 @Override
67 public JavaFileInfo getJavaFileInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053068 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053069 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053070 }
71 return javaFileInfo;
72 }
73
74 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053075 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053076 *
77 * @param javaInfo java file info object
78 */
79 @Override
80 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053081 javaFileInfo = javaInfo;
82 }
83
84 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053085 * Returns the data of java imports to be included in generated file.
Vinod Kumar S38046502016-03-23 15:30:27 +053086 *
87 * @return data of java imports to be included in generated file
88 */
89 @Override
90 public JavaImportData getJavaImportData() {
Vinod Kumar S38046502016-03-23 15:30:27 +053091 return javaImportData;
92 }
93
94 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053095 * Sets the data of java imports to be included in generated file.
Vinod Kumar S38046502016-03-23 15:30:27 +053096 *
97 * @param javaImportData data of java imports to be included in generated
98 * file
99 */
100 @Override
101 public void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530102 this.javaImportData = javaImportData;
103 }
104
105 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530106 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530107 *
108 * @return temporary file handle
109 */
110 @Override
111 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530112 return tempFileHandle;
113 }
114
115 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530116 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530117 *
118 * @param fileHandle temporary file handle
119 */
120 @Override
121 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530122 tempFileHandle = fileHandle;
123 }
124
125 /**
126 * Prepare the information for java code generation corresponding to YANG
127 * container info.
128 *
129 * @param codeGenDir code generation directory
130 * @throws IOException IO operation fail
131 */
132 @Override
133 public void generateCodeEntry(String codeGenDir) throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530134 generateCodeOfNode(this, codeGenDir, false);
Vinod Kumar S38046502016-03-23 15:30:27 +0530135 }
136
137 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530138 * Create a java file using the YANG container info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530139 *
140 * @throws IOException IO operation fail
141 */
142 @Override
143 public void generateCodeExit() throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530144 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
Vinod Kumar S38046502016-03-23 15:30:27 +0530145 }
146
147}