blob: b925d899d61f9a9f177ee5bfb6780655b9fdf182 [file] [log] [blame]
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301/*
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
17package org.onosproject.yangutils.translator.tojava;
18
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053019import java.io.File;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053020import java.io.IOException;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053021import java.util.ArrayList;
22import java.util.List;
23
24import org.onosproject.yangutils.datamodel.YangNode;
25import org.onosproject.yangutils.datamodel.YangType;
26import org.onosproject.yangutils.datamodel.YangTypeHolder;
27import org.onosproject.yangutils.translator.exception.TranslatorException;
28import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaType;
29
30import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
31import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
32import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
33import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
34import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
35import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
36import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
37import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
38import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile;
39import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
40import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc;
41import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
42import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
43import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
44import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
45import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053046
47/**
48 * Represents implementation of java data type code fragments temporary implementations.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053049 * Maintains the temp files required specific for user defined data type java snippet generation.
Vinod Kumar S79a374b2016-04-30 21:09:15 +053050 */
51public class TempJavaTypeFragmentFiles
52 extends TempJavaFragmentFiles {
53
54 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053055 * File name for of string method.
56 */
57 private static final String OF_STRING_METHOD_FILE_NAME = "OfString";
58
59 /**
60 * File name for construction for special type like union, typedef.
61 */
62 private static final String CONSTRUCTOR_FOR_TYPE_FILE_NAME = "ConstructorForType";
VinodKumarS-Huaweibb2c27e2016-05-11 09:34:21 +053063
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053064 /**
65 * File name for from string method.
66 */
67 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
68
69 /**
70 * File name for typedef class file name suffix.
71 */
72 private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
73
74 /**
75 * File name for generated class file for special type like union, typedef
76 * suffix.
77 */
78 private static final String UNION_TYPE_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
79
80 /**
81 * Temporary file handle for of string method of class.
82 */
83 private File ofStringImplTempFileHandle;
VinodKumarS-Huaweibb2c27e2016-05-11 09:34:21 +053084
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053085 /**
86 * Temporary file handle for constructor for type class.
87 */
88 private File constructorForTypeTempFileHandle;
89
90 /**
91 * Temporary file handle for from string method of class.
92 */
93 private File fromStringImplTempFileHandle;
94
95 /**
96 * Java file handle for typedef class file.
97 */
98 private File typedefClassJavaFileHandle;
VinodKumarS-Huaweibb2c27e2016-05-11 09:34:21 +053099
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530100 /**
101 * Java file handle for type class like union, typedef file.
102 */
103 private File typeClassJavaFileHandle;
104
105 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530106 * Creates an instance of temporary java code fragment.
107 *
108 * @param javaFileInfo generated java file info
109 * @throws IOException when fails to create new file handle
110 */
111 public TempJavaTypeFragmentFiles(JavaFileInfo javaFileInfo)
112 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530113
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530114 super(javaFileInfo);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530115
116 /*
117 * Initialize getterImpl, attributes, hash code, equals and to strings
118 * when generation file type matches to typeDef class mask.
119 */
120 addGeneratedTempFile(OF_STRING_IMPL_MASK);
121 addGeneratedTempFile(CONSTRUCTOR_FOR_TYPE_MASK);
122 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
123
124
125 setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
126 setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME));
127 setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
128
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530129 }
130
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530131 /**
132 * Returns type class constructor method's temporary file handle.
133 *
134 * @return type class constructor method's temporary file handle
135 */
136
137 public File getConstructorForTypeTempFileHandle() {
138 return constructorForTypeTempFileHandle;
139 }
140
141 /**
142 * Sets type class constructor method's temporary file handle.
143 *
144 * @param constructorForTypeTempFileHandle type class constructor method's
145 * temporary file handle
146 */
147 private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) {
148 this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle;
149 }
150
151 /**
152 * Returns from string method's temporary file handle.
153 *
154 * @return from string method's temporary file handle
155 */
156 public File getFromStringImplTempFileHandle() {
157 return fromStringImplTempFileHandle;
158 }
159
160 /**
161 * Sets from string method's temporary file handle.
162 *
163 * @param fromStringImplTempFileHandle from string method's temporary file
164 * handle
165 */
166 private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
167 this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
168 }
169
170 /**
171 * Returns java file handle for typedef class file.
172 *
173 * @return java file handle for typedef class file
174 */
175 File getTypedefClassJavaFileHandle() {
176 return typedefClassJavaFileHandle;
177 }
178
179 /**
180 * Sets the java file handle for typedef class file.
181 *
182 * @param typedefClassJavaFileHandle java file handle
183 */
184 private void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
185 this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
186 }
187
188 /**
189 * Returns java file handle for type class file.
190 *
191 * @return java file handle for type class file
192 */
193 File getTypeClassJavaFileHandle() {
194 return typeClassJavaFileHandle;
195 }
196
197 /**
198 * Sets the java file handle for type class file.
199 *
200 * @param typeClassJavaFileHandle type file handle
201 */
202 private void setTypeClassJavaFileHandle(File typeClassJavaFileHandle) {
203 this.typeClassJavaFileHandle = typeClassJavaFileHandle;
204 }
205
206 /**
207 * Returns of string method's temporary file handle.
208 *
209 * @return of string method's temporary file handle
210 */
211
212 public File getOfStringImplTempFileHandle() {
213 return ofStringImplTempFileHandle;
214 }
215
216 /**
217 * Set of string method's temporary file handle.
218 *
219 * @param ofStringImplTempFileHandle of string method's temporary file
220 * handle
221 */
222 private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) {
223 this.ofStringImplTempFileHandle = ofStringImplTempFileHandle;
224 }
225
226 /**
227 * Adds all the type in the current data model node as part of the generated
228 * temporary file.
229 *
230 * @param yangTypeHolder YANG java data model node which has type info, eg union /
231 * typedef
232 * @throws IOException IO operation fail
233 */
234 public void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder)
235 throws IOException {
236
237 List<YangType<?>> typeList = yangTypeHolder.getTypeList();
238 if (typeList != null) {
239 for (YangType<?> yangType : typeList) {
240 if (!(yangType instanceof YangJavaType)) {
241 throw new TranslatorException("Type does not have Java info");
242 }
243 YangJavaType<?> javaType = (YangJavaType<?>) yangType;
244 javaType.updateJavaQualifiedInfo();
245 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
246 javaType.getJavaQualifiedInfo(),
247 javaType.getDataTypeName(), javaType,
248 getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
249 false);
250 addJavaSnippetInfoToApplicableTempFiles((YangNode) yangTypeHolder, javaAttributeInfo);
251 }
252 }
253 }
254
255 /**
256 * Adds the new attribute info to the target generated temporary files for
257 * union class.
258 *
259 * @param hasType the node for which the type is being added as an attribute
260 * @param javaAttributeInfo the attribute info that needs to be added to
261 * temporary files
262 * @throws IOException IO operation fail
263 */
264 private void addJavaSnippetInfoToApplicableTempFiles(YangNode hasType, JavaAttributeInfo javaAttributeInfo)
265 throws IOException {
266
267 super.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
268
269 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
270 addOfStringMethod(javaAttributeInfo);
271 }
272 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
273 addTypeConstructor(javaAttributeInfo);
274 }
275
276 JavaQualifiedTypeInfo qualifiedInfoOfFromString = getQualifiedInfoOfFromString(javaAttributeInfo);
277 /*
278 * Create a new java attribute info with qualified information of
279 * wrapper classes.
280 */
281 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
282 javaAttributeInfo.getAttributeName(),
283 javaAttributeInfo.getAttributeType(),
284 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
285 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
286 addFromStringMethod(javaAttributeInfo, fromStringAttributeInfo);
287 }
288 }
289
290
291 /**
292 * Adds from string method for union class.
293 *
294 * @param javaAttributeInfo type attribute info
295 * @param fromStringAttributeInfo from string attribute info
296 * @throws IOException when fails to append to temporary file
297 */
298 private void addFromStringMethod(JavaAttributeInfo javaAttributeInfo,
299 JavaAttributeInfo fromStringAttributeInfo)
300 throws IOException {
301 appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
302 fromStringAttributeInfo) + NEW_LINE);
303 }
304
305 /**
306 * Adds type constructor.
307 *
308 * @param attr attribute info
309 * @throws IOException when fails to append to temporary file
310 */
311 private void addTypeConstructor(JavaAttributeInfo attr)
312 throws IOException {
313 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr,
314 getGeneratedJavaClassName()) + NEW_LINE);
315 }
316
317 /**
318 * Adds of string for type.
319 *
320 * @param attr attribute info
321 * @throws IOException when fails to append to temporary file
322 */
323 private void addOfStringMethod(JavaAttributeInfo attr)
324 throws IOException {
325 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr,
326 getGeneratedJavaClassName())
327 + NEW_LINE);
328 }
329
330
331 /**
332 * Removes all temporary file handles.
333 *
334 * @param isErrorOccurred when translator fails to generate java files we
335 * need to close all open file handles include temporary files
336 * and java files.
337 * @throws IOException when failed to delete the temporary files
338 */
339 public void freeTemporaryResources(boolean isErrorOccurred)
340 throws IOException {
341 boolean isError = isErrorOccurred;
342
343 if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0) {
344 closeFile(getTypedefClassJavaFileHandle(), isError);
345 }
346
347 if ((getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) {
348 closeFile(getTypeClassJavaFileHandle(), isError);
349 }
350
351 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
352 closeFile(getConstructorForTypeTempFileHandle(), true);
353 }
354 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
355 closeFile(getOfStringImplTempFileHandle(), true);
356 }
357 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
358 closeFile(getFromStringImplTempFileHandle(), true);
359 }
360
361 super.freeTemporaryResources(isErrorOccurred);
362 }
363
364 /**
365 * Constructs java code exit.
366 *
367 * @param fileType generated file type
368 * @param curNode current YANG node
369 * @throws IOException when fails to generate java files
370 */
371 public void generateJavaFile(int fileType, YangNode curNode)
372 throws IOException {
373 List<String> imports = new ArrayList<>();
374 if (isAttributePresent()) {
375 imports = getJavaImportData().getImports();
376 }
377
378 createPackage(curNode);
379
380 /*
381 * Creates type def class file.
382 */
383 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
384 addImportsToStringAndHasCodeMethods(curNode, imports);
385 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
386 generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports);
387 }
388 /*
389 * Creates type class file.
390 */
391 if ((fileType & GENERATE_UNION_CLASS) != 0) {
392 addImportsToStringAndHasCodeMethods(curNode, imports);
393 setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX)));
394 generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports);
395 }
396
397 /*
398 * Close all the file handles.
399 */
400 freeTemporaryResources(false);
401 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530402}