blob: 338cba3c720c117fcda93eb1ad1419a9cbf0a6df [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
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530308 List<AttributeInfo> attrList = getCachedAttributeList();
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530309 flushCacheAttrToTempFile();
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;
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530325 if (importInfo.getPkgInfo() != "" && importInfo.getClassInfo() != null
326 && importInfo.getPkgInfo() != UtilConstants.JAVA_LANG) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530327 importString = importString + importInfo.getPkgInfo() + ".";
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530328 importString = importString + importInfo.getClassInfo() + UtilConstants.SEMI_COLAN
329 + UtilConstants.NEW_LINE;
330
331 imports.add(importString);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530332 }
333 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530334 java.util.Collections.sort(imports);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530335
Bharat saraswal870c56f2016-02-20 21:57:16 +0530336 /**
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530337 * Start generation of files.
338 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530339 if ((fileType & GeneratedFileType.INTERFACE_MASK) != 0
340 || fileType == GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530341
342 /**
343 * Create interface file.
344 */
345 String interfaceFileName = className;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530346 File interfaceFile = JavaFileGenerator.getFileObject(path, interfaceFileName, JAVA_FILE_EXTENSION, this);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530347 interfaceFile = JavaFileGenerator.generateInterfaceFile(interfaceFile, className, imports,
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530348 attrList, path.replace('/', '.'), this);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530349 /**
350 * Create temp builder interface file.
351 */
352 String builderInterfaceFileName = className + UtilConstants.BUILDER + UtilConstants.INTERFACE;
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530353 File builderInterfaceFile = JavaFileGenerator.getFileObject(path, builderInterfaceFileName,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530354 TEMP_FILE_EXTENSION, this);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530355 builderInterfaceFile = JavaFileGenerator.generateBuilderInterfaceFile(builderInterfaceFile, className,
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530356 path.replace('/', '.'), attrList, this);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530357 /**
358 * Append builder interface file to interface file and close it.
359 */
360 JavaFileGenerator.appendFileContents(builderInterfaceFile, interfaceFile);
361 JavaFileGenerator.insert(interfaceFile,
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530362 JavaFileGenerator.closeFile(GeneratedFileType.INTERFACE_MASK, interfaceFileName));
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530363 /**
364 * Close file handle for interface files.
365 */
366 JavaFileGenerator.closeFileHandles(builderInterfaceFile);
367 JavaFileGenerator.closeFileHandles(interfaceFile);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530368
369 /**
370 * Remove temp files.
371 */
372 JavaFileGenerator.clean(builderInterfaceFile);
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530373 }
374
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530375 if (!attrList.isEmpty()) {
376 imports.add(UtilConstants.MORE_OBJECT_IMPORT);
377 imports.add(UtilConstants.JAVA_UTIL_OBJECTS_IMPORT);
378 java.util.Collections.sort(imports);
379 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530380
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530381 if ((fileType & GeneratedFileType.BUILDER_CLASS_MASK) != 0
382 || fileType == GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER) {
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530383
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530384 /**
385 * Create builder class file.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530386 */
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530387 String builderFileName = className + UtilConstants.BUILDER;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530388 File builderFile = JavaFileGenerator.getFileObject(path, builderFileName, JAVA_FILE_EXTENSION, this);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530389 builderFile = JavaFileGenerator.generateBuilderClassFile(builderFile, className, imports,
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530390 path.replace('/', '.'), attrList, this);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530391 /**
392 * Create temp impl class file.
393 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530394
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530395 String implFileName = className + UtilConstants.IMPL;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530396 File implTempFile = JavaFileGenerator.getFileObject(path, implFileName, TEMP_FILE_EXTENSION, this);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530397 implTempFile = JavaFileGenerator.generateImplClassFile(implTempFile, className,
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530398 path.replace('/', '.'), attrList, this);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530399 /**
400 * Append impl class to builder class and close it.
401 */
402 JavaFileGenerator.appendFileContents(implTempFile, builderFile);
403 JavaFileGenerator.insert(builderFile,
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530404 JavaFileGenerator.closeFile(GeneratedFileType.BUILDER_CLASS_MASK, builderFileName));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530405
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530406 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530407 * Close file handle for classes files.
408 */
409 JavaFileGenerator.closeFileHandles(implTempFile);
410 JavaFileGenerator.closeFileHandles(builderFile);
411
412 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530413 * Remove temp files.
414 */
415 JavaFileGenerator.clean(implTempFile);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530416 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530417
418 if ((fileType & GeneratedFileType.GENERATE_TYPEDEF_CLASS) != 0) {
419
420 /**
421 * Create builder class file.
422 */
423 String typeDefFileName = className;
424 File typeDefFile = JavaFileGenerator.getFileObject(path, typeDefFileName, JAVA_FILE_EXTENSION, this);
425 typeDefFile = JavaFileGenerator.generateTypeDefClassFile(typeDefFile, className, imports,
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530426 path.replace('/', '.'), attrList, this);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530427 JavaFileGenerator.insert(typeDefFile,
428 JavaFileGenerator.closeFile(GeneratedFileType.GENERATE_TYPEDEF_CLASS, typeDefFileName));
429
430 /**
431 * Close file handle for classes files.
432 */
433 JavaFileGenerator.closeFileHandles(typeDefFile);
434 }
435
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530436 if (!getCachedAttributeList().isEmpty()) {
437 closeTempDataFileHandles(className, getCodeGenFilePath() + getRelativeFilePath());
438 JavaFileGenerator
439 .cleanTempFiles(new File(getCodeGenFilePath() + getRelativeFilePath() + File.separator + className
440 + TEMP_FOLDER_NAME_SUFIX));
441 }
442
443 /*
444 * clear the contents from the cached attribute list.
445 */
446 getCachedAttributeList().clear();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530447 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530448
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530449 @Override
450 public void setTempData(String data, TempDataStoreTypes type, String className, String genDir)
451 throws IOException {
452
453 String fileName = "";
454 if (type.equals(TempDataStoreTypes.ATTRIBUTE)) {
455 fileName = ATTRIBUTE_FILE_NAME;
456 } else if (type.equals(TempDataStoreTypes.GETTER_METHODS)) {
457 fileName = GETTER_METHOD_FILE_NAME;
458 } else if (type.equals(TempDataStoreTypes.GETTER_METHODS_IMPL)) {
459 fileName = GETTER_METHOD_IMPL_FILE_NAME;
460 } else if (type.equals(TempDataStoreTypes.SETTER_METHODS)) {
461 fileName = SETTER_METHOD_FILE_NAME;
462 } else if (type.equals(TempDataStoreTypes.SETTER_METHODS_IMPL)) {
463 fileName = SETTER_METHOD_IMPL_FILE_NAME;
464 } else if (type.equals(TempDataStoreTypes.TYPE_DEF)) {
465 fileName = TYPE_DEF_FILE_NAME;
466 } else if (type.equals(TempDataStoreTypes.TO_STRING)) {
467 fileName = TO_STRING_METHOD_FILE_NAME;
468 } else if (type.equals(TempDataStoreTypes.HASH_CODE)) {
469 fileName = HASH_CODE_METHOD_FILE_NAME;
470 } else if (type.equals(TempDataStoreTypes.EQUALS)) {
471 fileName = EQUALS_METHOD_FILE_NAME;
472 } else {
473 fileName = CONSTRUCTOR_FILE_NAME;
474 }
475
476 String path = genDir.replace(UtilConstants.PERIOD, UtilConstants.SLASH)
477 + File.separator + className
478 + TEMP_FOLDER_NAME_SUFIX + File.separator;
479 File dir = new File(path);
480 if (!dir.exists()) {
481 dir.mkdirs();
482 }
483 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
484 try {
485 if (!file.exists()) {
486 file.createNewFile();
487 JavaFileGenerator.insert(file, data);
488 } else {
489 JavaFileGenerator.insert(file, data);
490 }
491 } catch (IOException ex) {
492 throw new IOException("failed to write in temp file.");
493 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530494 }
495
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530496 @Override
497 public String getTempData(TempDataStoreTypes type, String className, String genDir)
498 throws IOException, FileNotFoundException, ClassNotFoundException {
499
500 String fileName = "";
501 if (type.equals(TempDataStoreTypes.ATTRIBUTE)) {
502 fileName = ATTRIBUTE_FILE_NAME;
503 } else if (type.equals(TempDataStoreTypes.GETTER_METHODS)) {
504 fileName = GETTER_METHOD_FILE_NAME;
505 } else if (type.equals(TempDataStoreTypes.GETTER_METHODS_IMPL)) {
506 fileName = GETTER_METHOD_IMPL_FILE_NAME;
507 } else if (type.equals(TempDataStoreTypes.SETTER_METHODS)) {
508 fileName = SETTER_METHOD_FILE_NAME;
509 } else if (type.equals(TempDataStoreTypes.SETTER_METHODS_IMPL)) {
510 fileName = SETTER_METHOD_IMPL_FILE_NAME;
511 } else if (type.equals(TempDataStoreTypes.TYPE_DEF)) {
512 fileName = TYPE_DEF_FILE_NAME;
513 } else if (type.equals(TempDataStoreTypes.TO_STRING)) {
514 fileName = TO_STRING_METHOD_FILE_NAME;
515 } else if (type.equals(TempDataStoreTypes.HASH_CODE)) {
516 fileName = HASH_CODE_METHOD_FILE_NAME;
517 } else if (type.equals(TempDataStoreTypes.EQUALS)) {
518 fileName = EQUALS_METHOD_FILE_NAME;
519 } else {
520 fileName = CONSTRUCTOR_FILE_NAME;
521 }
522
523 String path = genDir.replace(UtilConstants.PERIOD, UtilConstants.SLASH)
524 + File.separator + className + TEMP_FOLDER_NAME_SUFIX + File.separator;
525
526 try {
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530527 String file = path + fileName + TEMP_FILE_EXTENSION;
528 if (new File(file).exists()) {
529 return readFile(path + fileName + TEMP_FILE_EXTENSION);
530 } else {
531 return "";
532 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530533
534 } catch (FileNotFoundException e) {
535 throw new FileNotFoundException("No such file or directory.");
536 }
537 }
538
539 /**
540 * Reads file and convert it to string.
541 *
542 * @param toAppend file to be converted
543 * @return string of file
544 * @throws IOException when fails to convert to string
545 */
546 private static String readFile(String toAppend) throws IOException {
547 BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend));
548 try {
549 StringBuilder stringBuilder = new StringBuilder();
550 String line = bufferReader.readLine();
551
552 while (line != null) {
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530553 if (line.equals(UtilConstants.FOUR_SPACE_INDENTATION)
554 || line.equals(UtilConstants.EIGHT_SPACE_INDENTATION)
555 || line.equals(UtilConstants.SPACE) || line.equals("") || line.equals(UtilConstants.NEW_LINE)) {
556 stringBuilder.append("\n");
557 } else {
558 stringBuilder.append(line);
559 stringBuilder.append("\n");
560 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530561 line = bufferReader.readLine();
562 }
563 return stringBuilder.toString();
564 } finally {
565 bufferReader.close();
566 }
567 }
568
569 /**
570 * Closes the temp file handles.
571 *
572 * @param className class name
573 * @param genDir generated directory
574 * @throws IOException when failes to close file handle
575 */
576 private void closeTempDataFileHandles(String className, String genDir)
577 throws IOException {
578
579 String path = genDir.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + className
580 + TEMP_FOLDER_NAME_SUFIX + File.separator;
581
582 String fileName = "";
583 fileName = ATTRIBUTE_FILE_NAME;
584 closeTempFile(fileName, path);
585
586 fileName = GETTER_METHOD_FILE_NAME;
587 closeTempFile(fileName, path);
588
589 fileName = GETTER_METHOD_IMPL_FILE_NAME;
590 closeTempFile(fileName, path);
591
592 fileName = SETTER_METHOD_FILE_NAME;
593 closeTempFile(fileName, path);
594
595 fileName = SETTER_METHOD_IMPL_FILE_NAME;
596 closeTempFile(fileName, path);
597
598 fileName = TYPE_DEF_FILE_NAME;
599 closeTempFile(fileName, path);
600
601 fileName = TO_STRING_METHOD_FILE_NAME;
602 closeTempFile(fileName, path);
603
604 fileName = HASH_CODE_METHOD_FILE_NAME;
605 closeTempFile(fileName, path);
606
607 fileName = EQUALS_METHOD_FILE_NAME;
608 closeTempFile(fileName, path);
609
610 fileName = CONSTRUCTOR_FILE_NAME;
611 closeTempFile(fileName, path);
612 }
613
614 /**
615 * Closes the specific temp file.
616 *
617 * @param fileName temp file name
618 * @param path path
619 * @throws IOException when failed to close file handle
620 */
621 private void closeTempFile(String fileName, String path) throws IOException {
622 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
623 try {
624 if (!file.exists()) {
625 FileSystemUtil.updateFileHandle(file, null, true);
626 }
627 } catch (IOException ex) {
628 throw new IOException("failed to close the temp file handle.");
629 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530630 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530631}