blob: 5e2cbe33f02f0b12a7ff4fc1720bbc5b6455ce09 [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
2 * Copyright 2016 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
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053019import java.io.BufferedReader;
Bharat saraswal870c56f2016-02-20 21:57:16 +053020import java.io.File;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053021import java.io.FileNotFoundException;
22import java.io.FileReader;
Bharat saraswal870c56f2016-02-20 21:57:16 +053023import java.io.IOException;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053024import java.util.ArrayList;
Bharat saraswal870c56f2016-02-20 21:57:16 +053025import java.util.LinkedList;
26import java.util.List;
27import java.util.SortedSet;
28import java.util.TreeSet;
29
30import org.onosproject.yangutils.datamodel.YangType;
31import org.onosproject.yangutils.translator.CachedFileHandle;
32import org.onosproject.yangutils.translator.GeneratedFileType;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053033import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053034import org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator;
Bharat saraswal870c56f2016-02-20 21:57:16 +053035import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053036import org.onosproject.yangutils.translator.tojava.utils.TempDataStoreTypes;
Bharat saraswal870c56f2016-02-20 21:57:16 +053037import org.onosproject.yangutils.utils.UtilConstants;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053038import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
Bharat saraswal870c56f2016-02-20 21:57:16 +053039
40/**
41 * Maintain the information about the java file to be generated.
42 */
43public class CachedJavaFileHandle implements CachedFileHandle {
44
Bharat saraswal870c56f2016-02-20 21:57:16 +053045 private static final int MAX_CACHABLE_ATTR = 64;
46 private static final String JAVA_FILE_EXTENSION = ".java";
47 private static final String TEMP_FILE_EXTENSION = ".tmp";
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053048 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
49 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
50 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
51 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
52 private static final String CONSTRUCTOR_FILE_NAME = "Constructor";
53 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
54 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
55 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
56 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
57 private static final String TYPE_DEF_FILE_NAME = "TypeDef";
58 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
Bharat saraswal870c56f2016-02-20 21:57:16 +053059
60 /**
61 * The type(s) of java source file(s) to be generated when the cached file
62 * handle is closed.
63 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053064 private int genFileTypes;
Bharat saraswal594bc6d2016-02-22 22:15:21 +053065
66 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053067 * Name of the object in YANG file.
68 */
69 private String yangName;
70
71 /**
72 * Sorted set of import info, to be used to maintain the set of classes to
73 * be imported in the generated class.
74 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053075 private SortedSet<ImportInfo> importSet;
Bharat saraswal870c56f2016-02-20 21:57:16 +053076
77 /**
78 * Cached list of attribute info.
79 */
80 private List<AttributeInfo> attributeList;
81
82 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +053083 * File generation directory path.
84 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053085 private String relativeFilePath;
86
87 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053088 * File generation base directory path.
Vinod Kumar Sc4216002016-03-03 19:55:30 +053089 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053090 private String codeGenDirFilePath;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053091
92 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053093 * Prevent invoking default constructor.
94 */
Bharat saraswal4bf8b152016-02-25 02:26:43 +053095 public CachedJavaFileHandle() {
Bharat saraswal870c56f2016-02-20 21:57:16 +053096 setCachedAttributeList(new LinkedList<AttributeInfo>());
97 }
98
99 /**
100 * Create a cached file handle which takes care of adding attributes to the
101 * generated java file.
102 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530103 * @param pcg package in which class/interface need to be generated
104 * @param yangName name of the attribute in YANG file
105 * @param types the types of files that needs to be generated
106 * @throws IOException file IO exception
Bharat saraswal870c56f2016-02-20 21:57:16 +0530107 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530108 public CachedJavaFileHandle(String pcg, String yangName, int types) throws IOException {
109 setCachedAttributeList(new LinkedList<AttributeInfo>());
110 setImportSet(new TreeSet<ImportInfo>());
111 setRelativeFilePath(pcg.replace(".", "/"));
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530112 setGeneratedFileTypes(types);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530113 setYangName(yangName);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530114 }
115
116 /**
117 * Get the types of files being generated corresponding to the YANG
118 * definition.
119 *
120 * @return the types of files being generated corresponding to the YANG
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530121 * definition
Bharat saraswal870c56f2016-02-20 21:57:16 +0530122 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530123 public int getGeneratedFileTypes() {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530124 return genFileTypes;
125 }
126
127 /**
128 * Set the types of files being generated corresponding to the YANG
129 * definition.
130 *
131 * @param fileTypes the types of files being generated corresponding to the
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530132 * YANG definition
Bharat saraswal870c56f2016-02-20 21:57:16 +0530133 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530134 public void setGeneratedFileTypes(int fileTypes) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530135 genFileTypes = fileTypes;
136 }
137
138 /**
139 * Get the corresponding name defined in YANG.
140 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530141 * @return the corresponding name defined in YANG
Bharat saraswal870c56f2016-02-20 21:57:16 +0530142 */
143 public String getYangName() {
144 return yangName;
145 }
146
147 /**
148 * Set the corresponding name defined in YANG.
149 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530150 * @param yangName the corresponding name defined in YANG
Bharat saraswal870c56f2016-02-20 21:57:16 +0530151 */
152 public void setYangName(String yangName) {
153 this.yangName = yangName;
154 }
155
156 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530157 * Get the set containing the imported class/interface info.
158 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530159 * @return the set containing the imported class/interface info
Bharat saraswal870c56f2016-02-20 21:57:16 +0530160 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530161 public SortedSet<ImportInfo> getImportSet() {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530162 return importSet;
163 }
164
165 /**
166 * Assign the set containing the imported class/interface info.
167 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530168 * @param importSet the set containing the imported class/interface info
Bharat saraswal870c56f2016-02-20 21:57:16 +0530169 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530170 private void setImportSet(SortedSet<ImportInfo> importSet) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530171 this.importSet = importSet;
172 }
173
174 /**
175 * Add an imported class/interface info is it is not already part of the
176 * set. If already part of the set, return false, else add to set and return
177 * true.
178 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530179 * @param importInfo class/interface info being imported
Bharat saraswal870c56f2016-02-20 21:57:16 +0530180 * @return status of new addition of class/interface to the import set
181 */
182 public boolean addImportInfo(ImportInfo importInfo) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530183 return getImportSet().add(importInfo);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530184 }
185
186 /**
187 * Get the list of cached attribute list.
188 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530189 * @return the set containing the imported class/interface info
Bharat saraswal870c56f2016-02-20 21:57:16 +0530190 */
191 public List<AttributeInfo> getCachedAttributeList() {
192 return attributeList;
193 }
194
195 /**
196 * Set the cached attribute list.
197 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530198 * @param attrList attribute list
Bharat saraswal870c56f2016-02-20 21:57:16 +0530199 */
200 private void setCachedAttributeList(List<AttributeInfo> attrList) {
201 attributeList = attrList;
202 }
203
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530204 @Override
205 public void setRelativeFilePath(String path) {
206 relativeFilePath = path;
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530207 }
208
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530209 @Override
210 public String getRelativeFilePath() {
211 return relativeFilePath;
212 }
213
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530214 @Override
215 public String getCodeGenFilePath() {
216 return codeGenDirFilePath;
217 }
218
219 @Override
220 public void setCodeGenFilePath(String path) {
221 codeGenDirFilePath = path;
222 }
223
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530224 /**
225 * Flush the cached attribute list to the corresponding temporary file.
226 */
227 private void flushCacheAttrToTempFile() {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530228
229 for (AttributeInfo attr : getCachedAttributeList()) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530230 JavaFileGenerator.parseAttributeInfo(attr, getGeneratedFileTypes(), getYangName(), getCodeGenFilePath() +
231 getRelativeFilePath().replace(UtilConstants.PERIOD, UtilConstants.SLASH), this);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530232 }
233
234 /*
235 * clear the contents from the cached attribute list.
236 */
237 getCachedAttributeList().clear();
238 }
239
Bharat saraswal870c56f2016-02-20 21:57:16 +0530240 @Override
241 public void addAttributeInfo(YangType<?> attrType, String name, boolean isListAttr) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530242 /* YANG name is mapped to java name */
243 name = JavaIdentifierSyntax.getCamelCase(name);
244
245 ImportInfo importInfo = new ImportInfo();
246 boolean isImport = false;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530247
248 AttributeInfo newAttr = new AttributeInfo();
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530249 if (attrType != null) {
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530250 newAttr.setAttributeType(attrType);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530251 String importStr = AttributesJavaDataType.getJavaImportClass(attrType, isListAttr);
252 if (importStr != null) {
253 importInfo.setClassInfo(importStr);
254 importStr = AttributesJavaDataType.getJavaImportPackage(attrType, isListAttr);
255 importInfo.setPkgInfo(importStr);
256 isImport = true;
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530257 } else {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530258 importStr = AttributesJavaDataType.getJavaDataType(attrType);
259 if (importStr == null) {
260 throw new RuntimeException("not supported data type");
261 //TODO: need to change to translator exception.
262 }
263 importInfo.setClassInfo(importStr);
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530264 }
265
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530266 } else {
267 importInfo.setClassInfo(JavaIdentifierSyntax.getCaptialCase(name));
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530268 importInfo.setPkgInfo(getRelativeFilePath().replace('/', '.')
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530269 + "." + getYangName().toLowerCase());
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530270 isImport = true;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530271 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530272
273 newAttr.setQualifiedName(false);
274 if (isImport) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530275 addImportInfo(importInfo);
276 }
277
278 if (isListAttr) {
279 ImportInfo listImportInfo = new ImportInfo();
280 listImportInfo.setPkgInfo(UtilConstants.COLLECTION_IMPORTS);
281 listImportInfo.setClassInfo(UtilConstants.LIST);
282 addImportInfo(listImportInfo);
283 }
284
285 /**
286 * If two classes with different packages have same class info for import than use qualified name.
287 */
288 for (ImportInfo imports : getImportSet()) {
289 if (imports.getClassInfo().equals(importInfo.getClassInfo())
290 && !imports.getPkgInfo().equals(importInfo.getPkgInfo())) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530291 newAttr.setQualifiedName(true);
292 }
293 }
294
Bharat saraswal870c56f2016-02-20 21:57:16 +0530295 newAttr.setAttributeName(name);
296 newAttr.setListAttr(isListAttr);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530297 newAttr.setImportInfo(importInfo);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530298
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530299 if (getCachedAttributeList().size() == MAX_CACHABLE_ATTR) {
300 flushCacheAttrToTempFile();
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530301 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530302 getCachedAttributeList().add(newAttr);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530303 }
304
Bharat saraswal870c56f2016-02-20 21:57:16 +0530305 @Override
306 public void close() throws IOException {
307
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530308 flushCacheAttrToTempFile();
309
Bharat saraswal870c56f2016-02-20 21:57:16 +0530310 String className = getYangName();
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530311 className = JavaIdentifierSyntax.getCaptialCase(className);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530312 String path = getRelativeFilePath();
313 int fileType = getGeneratedFileTypes();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530314
Bharat saraswal870c56f2016-02-20 21:57:16 +0530315 /*
316 * TODO: add the file header using
317 * JavaCodeSnippetGen.getFileHeaderComment
318 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530319
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530320 List<String> imports = new ArrayList<>();
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530321 String importString;
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530322
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530323 for (ImportInfo importInfo : new ArrayList<ImportInfo>(getImportSet())) {
324 importString = UtilConstants.IMPORT;
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530325 if (importInfo.getPkgInfo() != null) {
326 importString = importString + importInfo.getPkgInfo() + ".";
Bharat saraswal870c56f2016-02-20 21:57:16 +0530327 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530328 importString = importString + importInfo.getClassInfo() + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530329 imports.add(importString);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530330 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530331 java.util.Collections.sort(imports);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530332
Bharat saraswal870c56f2016-02-20 21:57:16 +0530333 /**
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530334 * Start generation of files.
335 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530336 if ((fileType & GeneratedFileType.INTERFACE_MASK) != 0
337 || fileType == GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530338
339 /**
340 * Create interface file.
341 */
342 String interfaceFileName = className;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530343 File interfaceFile = JavaFileGenerator.getFileObject(path, interfaceFileName, JAVA_FILE_EXTENSION, this);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530344 interfaceFile = JavaFileGenerator.generateInterfaceFile(interfaceFile, className, imports,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530345 getCachedAttributeList(), path.replace('/', '.'), this);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530346 /**
347 * Create temp builder interface file.
348 */
349 String builderInterfaceFileName = className + UtilConstants.BUILDER + UtilConstants.INTERFACE;
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530350 File builderInterfaceFile = JavaFileGenerator.getFileObject(path, builderInterfaceFileName,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530351 TEMP_FILE_EXTENSION, this);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530352 builderInterfaceFile = JavaFileGenerator.generateBuilderInterfaceFile(builderInterfaceFile, className,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530353 path.replace('/', '.'), getCachedAttributeList(), this);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530354 /**
355 * Append builder interface file to interface file and close it.
356 */
357 JavaFileGenerator.appendFileContents(builderInterfaceFile, interfaceFile);
358 JavaFileGenerator.insert(interfaceFile,
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530359 JavaFileGenerator.closeFile(GeneratedFileType.INTERFACE_MASK, interfaceFileName));
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530360 /**
361 * Close file handle for interface files.
362 */
363 JavaFileGenerator.closeFileHandles(builderInterfaceFile);
364 JavaFileGenerator.closeFileHandles(interfaceFile);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530365
366 /**
367 * Remove temp files.
368 */
369 JavaFileGenerator.clean(builderInterfaceFile);
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530370 }
371
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530372 imports.add(UtilConstants.MORE_OBJECT_IMPORT);
373 imports.add(UtilConstants.JAVA_UTIL_OBJECTS_IMPORT);
374 java.util.Collections.sort(imports);
375
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530376 if ((fileType & GeneratedFileType.BUILDER_CLASS_MASK) != 0
377 || fileType == GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER) {
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530378
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530379 /**
380 * Create builder class file.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530381 */
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530382 String builderFileName = className + UtilConstants.BUILDER;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530383 File builderFile = JavaFileGenerator.getFileObject(path, builderFileName, JAVA_FILE_EXTENSION, this);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530384 builderFile = JavaFileGenerator.generateBuilderClassFile(builderFile, className, imports,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530385 path.replace('/', '.'), getCachedAttributeList(), this);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530386 /**
387 * Create temp impl class file.
388 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530389
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530390 String implFileName = className + UtilConstants.IMPL;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530391 File implTempFile = JavaFileGenerator.getFileObject(path, implFileName, TEMP_FILE_EXTENSION, this);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530392 implTempFile = JavaFileGenerator.generateImplClassFile(implTempFile, className,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530393 path.replace('/', '.'), getCachedAttributeList(), this);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530394 /**
395 * Append impl class to builder class and close it.
396 */
397 JavaFileGenerator.appendFileContents(implTempFile, builderFile);
398 JavaFileGenerator.insert(builderFile,
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530399 JavaFileGenerator.closeFile(GeneratedFileType.BUILDER_CLASS_MASK, builderFileName));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530400
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530401 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530402 * Close file handle for classes files.
403 */
404 JavaFileGenerator.closeFileHandles(implTempFile);
405 JavaFileGenerator.closeFileHandles(builderFile);
406
407 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530408 * Remove temp files.
409 */
410 JavaFileGenerator.clean(implTempFile);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530411 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530412
413 if ((fileType & GeneratedFileType.GENERATE_TYPEDEF_CLASS) != 0) {
414
415 /**
416 * Create builder class file.
417 */
418 String typeDefFileName = className;
419 File typeDefFile = JavaFileGenerator.getFileObject(path, typeDefFileName, JAVA_FILE_EXTENSION, this);
420 typeDefFile = JavaFileGenerator.generateTypeDefClassFile(typeDefFile, className, imports,
421 path.replace('/', '.'), getCachedAttributeList(), this);
422 JavaFileGenerator.insert(typeDefFile,
423 JavaFileGenerator.closeFile(GeneratedFileType.GENERATE_TYPEDEF_CLASS, typeDefFileName));
424
425 /**
426 * Close file handle for classes files.
427 */
428 JavaFileGenerator.closeFileHandles(typeDefFile);
429 }
430
431 closeTempDataFileHandles(className, getCodeGenFilePath() + getRelativeFilePath());
432 JavaFileGenerator
433 .cleanTempFiles(new File(getCodeGenFilePath() + getRelativeFilePath() + File.separator + className
434 + TEMP_FOLDER_NAME_SUFIX));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530435 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530436
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530437 @Override
438 public void setTempData(String data, TempDataStoreTypes type, String className, String genDir)
439 throws IOException {
440
441 String fileName = "";
442 if (type.equals(TempDataStoreTypes.ATTRIBUTE)) {
443 fileName = ATTRIBUTE_FILE_NAME;
444 } else if (type.equals(TempDataStoreTypes.GETTER_METHODS)) {
445 fileName = GETTER_METHOD_FILE_NAME;
446 } else if (type.equals(TempDataStoreTypes.GETTER_METHODS_IMPL)) {
447 fileName = GETTER_METHOD_IMPL_FILE_NAME;
448 } else if (type.equals(TempDataStoreTypes.SETTER_METHODS)) {
449 fileName = SETTER_METHOD_FILE_NAME;
450 } else if (type.equals(TempDataStoreTypes.SETTER_METHODS_IMPL)) {
451 fileName = SETTER_METHOD_IMPL_FILE_NAME;
452 } else if (type.equals(TempDataStoreTypes.TYPE_DEF)) {
453 fileName = TYPE_DEF_FILE_NAME;
454 } else if (type.equals(TempDataStoreTypes.TO_STRING)) {
455 fileName = TO_STRING_METHOD_FILE_NAME;
456 } else if (type.equals(TempDataStoreTypes.HASH_CODE)) {
457 fileName = HASH_CODE_METHOD_FILE_NAME;
458 } else if (type.equals(TempDataStoreTypes.EQUALS)) {
459 fileName = EQUALS_METHOD_FILE_NAME;
460 } else {
461 fileName = CONSTRUCTOR_FILE_NAME;
462 }
463
464 String path = genDir.replace(UtilConstants.PERIOD, UtilConstants.SLASH)
465 + File.separator + className
466 + TEMP_FOLDER_NAME_SUFIX + File.separator;
467 File dir = new File(path);
468 if (!dir.exists()) {
469 dir.mkdirs();
470 }
471 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
472 try {
473 if (!file.exists()) {
474 file.createNewFile();
475 JavaFileGenerator.insert(file, data);
476 } else {
477 JavaFileGenerator.insert(file, data);
478 }
479 } catch (IOException ex) {
480 throw new IOException("failed to write in temp file.");
481 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530482 }
483
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530484 @Override
485 public String getTempData(TempDataStoreTypes type, String className, String genDir)
486 throws IOException, FileNotFoundException, ClassNotFoundException {
487
488 String fileName = "";
489 if (type.equals(TempDataStoreTypes.ATTRIBUTE)) {
490 fileName = ATTRIBUTE_FILE_NAME;
491 } else if (type.equals(TempDataStoreTypes.GETTER_METHODS)) {
492 fileName = GETTER_METHOD_FILE_NAME;
493 } else if (type.equals(TempDataStoreTypes.GETTER_METHODS_IMPL)) {
494 fileName = GETTER_METHOD_IMPL_FILE_NAME;
495 } else if (type.equals(TempDataStoreTypes.SETTER_METHODS)) {
496 fileName = SETTER_METHOD_FILE_NAME;
497 } else if (type.equals(TempDataStoreTypes.SETTER_METHODS_IMPL)) {
498 fileName = SETTER_METHOD_IMPL_FILE_NAME;
499 } else if (type.equals(TempDataStoreTypes.TYPE_DEF)) {
500 fileName = TYPE_DEF_FILE_NAME;
501 } else if (type.equals(TempDataStoreTypes.TO_STRING)) {
502 fileName = TO_STRING_METHOD_FILE_NAME;
503 } else if (type.equals(TempDataStoreTypes.HASH_CODE)) {
504 fileName = HASH_CODE_METHOD_FILE_NAME;
505 } else if (type.equals(TempDataStoreTypes.EQUALS)) {
506 fileName = EQUALS_METHOD_FILE_NAME;
507 } else {
508 fileName = CONSTRUCTOR_FILE_NAME;
509 }
510
511 String path = genDir.replace(UtilConstants.PERIOD, UtilConstants.SLASH)
512 + File.separator + className + TEMP_FOLDER_NAME_SUFIX + File.separator;
513
514 try {
515 return readFile(path + fileName + TEMP_FILE_EXTENSION);
516
517 } catch (FileNotFoundException e) {
518 throw new FileNotFoundException("No such file or directory.");
519 }
520 }
521
522 /**
523 * Reads file and convert it to string.
524 *
525 * @param toAppend file to be converted
526 * @return string of file
527 * @throws IOException when fails to convert to string
528 */
529 private static String readFile(String toAppend) throws IOException {
530 BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend));
531 try {
532 StringBuilder stringBuilder = new StringBuilder();
533 String line = bufferReader.readLine();
534
535 while (line != null) {
536 stringBuilder.append(line);
537 stringBuilder.append("\n");
538 line = bufferReader.readLine();
539 }
540 return stringBuilder.toString();
541 } finally {
542 bufferReader.close();
543 }
544 }
545
546 /**
547 * Closes the temp file handles.
548 *
549 * @param className class name
550 * @param genDir generated directory
551 * @throws IOException when failes to close file handle
552 */
553 private void closeTempDataFileHandles(String className, String genDir)
554 throws IOException {
555
556 String path = genDir.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + className
557 + TEMP_FOLDER_NAME_SUFIX + File.separator;
558
559 String fileName = "";
560 fileName = ATTRIBUTE_FILE_NAME;
561 closeTempFile(fileName, path);
562
563 fileName = GETTER_METHOD_FILE_NAME;
564 closeTempFile(fileName, path);
565
566 fileName = GETTER_METHOD_IMPL_FILE_NAME;
567 closeTempFile(fileName, path);
568
569 fileName = SETTER_METHOD_FILE_NAME;
570 closeTempFile(fileName, path);
571
572 fileName = SETTER_METHOD_IMPL_FILE_NAME;
573 closeTempFile(fileName, path);
574
575 fileName = TYPE_DEF_FILE_NAME;
576 closeTempFile(fileName, path);
577
578 fileName = TO_STRING_METHOD_FILE_NAME;
579 closeTempFile(fileName, path);
580
581 fileName = HASH_CODE_METHOD_FILE_NAME;
582 closeTempFile(fileName, path);
583
584 fileName = EQUALS_METHOD_FILE_NAME;
585 closeTempFile(fileName, path);
586
587 fileName = CONSTRUCTOR_FILE_NAME;
588 closeTempFile(fileName, path);
589 }
590
591 /**
592 * Closes the specific temp file.
593 *
594 * @param fileName temp file name
595 * @param path path
596 * @throws IOException when failed to close file handle
597 */
598 private void closeTempFile(String fileName, String path) throws IOException {
599 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
600 try {
601 if (!file.exists()) {
602 FileSystemUtil.updateFileHandle(file, null, true);
603 }
604 } catch (IOException ex) {
605 throw new IOException("failed to close the temp file handle.");
606 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530607 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530608}