Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava; |
| 18 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 19 | import java.io.BufferedReader; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 20 | import java.io.File; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 21 | import java.io.FileNotFoundException; |
| 22 | import java.io.FileReader; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 23 | import java.io.IOException; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 24 | import java.util.ArrayList; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 25 | import java.util.LinkedList; |
| 26 | import java.util.List; |
| 27 | import java.util.SortedSet; |
| 28 | import java.util.TreeSet; |
| 29 | |
| 30 | import org.onosproject.yangutils.datamodel.YangType; |
| 31 | import org.onosproject.yangutils.translator.CachedFileHandle; |
| 32 | import org.onosproject.yangutils.translator.GeneratedFileType; |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType; |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 36 | import org.onosproject.yangutils.translator.tojava.utils.TempDataStoreTypes; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.utils.UtilConstants; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 38 | import org.onosproject.yangutils.utils.io.impl.FileSystemUtil; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Maintain the information about the java file to be generated. |
| 42 | */ |
| 43 | public class CachedJavaFileHandle implements CachedFileHandle { |
| 44 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 45 | 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 saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 48 | 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 saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * The type(s) of java source file(s) to be generated when the cached file |
| 62 | * handle is closed. |
| 63 | */ |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 64 | private int genFileTypes; |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 65 | |
| 66 | /** |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 67 | * 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 S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 75 | private SortedSet<ImportInfo> importSet; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Cached list of attribute info. |
| 79 | */ |
| 80 | private List<AttributeInfo> attributeList; |
| 81 | |
| 82 | /** |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 83 | * File generation directory path. |
| 84 | */ |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 85 | private String relativeFilePath; |
| 86 | |
| 87 | /** |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 88 | * File generation base directory path. |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 89 | */ |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 90 | private String codeGenDirFilePath; |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 91 | |
| 92 | /** |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 93 | * Prevent invoking default constructor. |
| 94 | */ |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 95 | public CachedJavaFileHandle() { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 96 | 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 S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 103 | * @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 saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 107 | */ |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 108 | 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 saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 112 | setGeneratedFileTypes(types); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 113 | setYangName(yangName); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 114 | } |
| 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 S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 121 | * definition |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 122 | */ |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 123 | public int getGeneratedFileTypes() { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 124 | 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 S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 132 | * YANG definition |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 133 | */ |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 134 | public void setGeneratedFileTypes(int fileTypes) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 135 | genFileTypes = fileTypes; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Get the corresponding name defined in YANG. |
| 140 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 141 | * @return the corresponding name defined in YANG |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 142 | */ |
| 143 | public String getYangName() { |
| 144 | return yangName; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Set the corresponding name defined in YANG. |
| 149 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 150 | * @param yangName the corresponding name defined in YANG |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 151 | */ |
| 152 | public void setYangName(String yangName) { |
| 153 | this.yangName = yangName; |
| 154 | } |
| 155 | |
| 156 | /** |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 157 | * Get the set containing the imported class/interface info. |
| 158 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 159 | * @return the set containing the imported class/interface info |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 160 | */ |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 161 | public SortedSet<ImportInfo> getImportSet() { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 162 | return importSet; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Assign the set containing the imported class/interface info. |
| 167 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 168 | * @param importSet the set containing the imported class/interface info |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 169 | */ |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 170 | private void setImportSet(SortedSet<ImportInfo> importSet) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 171 | 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 S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 179 | * @param importInfo class/interface info being imported |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 180 | * @return status of new addition of class/interface to the import set |
| 181 | */ |
| 182 | public boolean addImportInfo(ImportInfo importInfo) { |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 183 | return getImportSet().add(importInfo); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Get the list of cached attribute list. |
| 188 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 189 | * @return the set containing the imported class/interface info |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 190 | */ |
| 191 | public List<AttributeInfo> getCachedAttributeList() { |
| 192 | return attributeList; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Set the cached attribute list. |
| 197 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 198 | * @param attrList attribute list |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 199 | */ |
| 200 | private void setCachedAttributeList(List<AttributeInfo> attrList) { |
| 201 | attributeList = attrList; |
| 202 | } |
| 203 | |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 204 | @Override |
| 205 | public void setRelativeFilePath(String path) { |
| 206 | relativeFilePath = path; |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 207 | } |
| 208 | |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 209 | @Override |
| 210 | public String getRelativeFilePath() { |
| 211 | return relativeFilePath; |
| 212 | } |
| 213 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 214 | @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 S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 224 | /** |
| 225 | * Flush the cached attribute list to the corresponding temporary file. |
| 226 | */ |
| 227 | private void flushCacheAttrToTempFile() { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 228 | |
| 229 | for (AttributeInfo attr : getCachedAttributeList()) { |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 230 | JavaFileGenerator.parseAttributeInfo(attr, getGeneratedFileTypes(), getYangName(), getCodeGenFilePath() + |
| 231 | getRelativeFilePath().replace(UtilConstants.PERIOD, UtilConstants.SLASH), this); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* |
| 235 | * clear the contents from the cached attribute list. |
| 236 | */ |
| 237 | getCachedAttributeList().clear(); |
| 238 | } |
| 239 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 240 | @Override |
| 241 | public void addAttributeInfo(YangType<?> attrType, String name, boolean isListAttr) { |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 242 | /* YANG name is mapped to java name */ |
| 243 | name = JavaIdentifierSyntax.getCamelCase(name); |
| 244 | |
| 245 | ImportInfo importInfo = new ImportInfo(); |
| 246 | boolean isImport = false; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 247 | |
| 248 | AttributeInfo newAttr = new AttributeInfo(); |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 249 | if (attrType != null) { |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 250 | newAttr.setAttributeType(attrType); |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 251 | 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 saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 257 | } else { |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 258 | 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 saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 264 | } |
| 265 | |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 266 | } else { |
| 267 | importInfo.setClassInfo(JavaIdentifierSyntax.getCaptialCase(name)); |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 268 | importInfo.setPkgInfo(getRelativeFilePath().replace('/', '.') |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 269 | + "." + getYangName().toLowerCase()); |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 270 | isImport = true; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 271 | } |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 272 | |
| 273 | newAttr.setQualifiedName(false); |
| 274 | if (isImport) { |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 275 | 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 S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 291 | newAttr.setQualifiedName(true); |
| 292 | } |
| 293 | } |
| 294 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 295 | newAttr.setAttributeName(name); |
| 296 | newAttr.setListAttr(isListAttr); |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 297 | newAttr.setImportInfo(importInfo); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 298 | |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 299 | if (getCachedAttributeList().size() == MAX_CACHABLE_ATTR) { |
| 300 | flushCacheAttrToTempFile(); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 301 | } |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 302 | getCachedAttributeList().add(newAttr); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 303 | } |
| 304 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 305 | @Override |
| 306 | public void close() throws IOException { |
| 307 | |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 308 | List<AttributeInfo> attrList = getCachedAttributeList(); |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 309 | flushCacheAttrToTempFile(); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 310 | String className = getYangName(); |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 311 | className = JavaIdentifierSyntax.getCaptialCase(className); |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 312 | String path = getRelativeFilePath(); |
| 313 | int fileType = getGeneratedFileTypes(); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 314 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 315 | /* |
| 316 | * TODO: add the file header using |
| 317 | * JavaCodeSnippetGen.getFileHeaderComment |
| 318 | */ |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 319 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 320 | List<String> imports = new ArrayList<>(); |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 321 | String importString; |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 322 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 323 | for (ImportInfo importInfo : new ArrayList<ImportInfo>(getImportSet())) { |
| 324 | importString = UtilConstants.IMPORT; |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 325 | if (importInfo.getPkgInfo() != "" && importInfo.getClassInfo() != null |
| 326 | && importInfo.getPkgInfo() != UtilConstants.JAVA_LANG) { |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 327 | importString = importString + importInfo.getPkgInfo() + "."; |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 328 | importString = importString + importInfo.getClassInfo() + UtilConstants.SEMI_COLAN |
| 329 | + UtilConstants.NEW_LINE; |
| 330 | |
| 331 | imports.add(importString); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 332 | } |
| 333 | } |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 334 | java.util.Collections.sort(imports); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 335 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 336 | /** |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 337 | * Start generation of files. |
| 338 | */ |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 339 | if ((fileType & GeneratedFileType.INTERFACE_MASK) != 0 |
| 340 | || fileType == GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER) { |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 341 | |
| 342 | /** |
| 343 | * Create interface file. |
| 344 | */ |
| 345 | String interfaceFileName = className; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 346 | File interfaceFile = JavaFileGenerator.getFileObject(path, interfaceFileName, JAVA_FILE_EXTENSION, this); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 347 | interfaceFile = JavaFileGenerator.generateInterfaceFile(interfaceFile, className, imports, |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 348 | attrList, path.replace('/', '.'), this); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 349 | /** |
| 350 | * Create temp builder interface file. |
| 351 | */ |
| 352 | String builderInterfaceFileName = className + UtilConstants.BUILDER + UtilConstants.INTERFACE; |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 353 | File builderInterfaceFile = JavaFileGenerator.getFileObject(path, builderInterfaceFileName, |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 354 | TEMP_FILE_EXTENSION, this); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 355 | builderInterfaceFile = JavaFileGenerator.generateBuilderInterfaceFile(builderInterfaceFile, className, |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 356 | path.replace('/', '.'), attrList, this); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 357 | /** |
| 358 | * Append builder interface file to interface file and close it. |
| 359 | */ |
| 360 | JavaFileGenerator.appendFileContents(builderInterfaceFile, interfaceFile); |
| 361 | JavaFileGenerator.insert(interfaceFile, |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 362 | JavaFileGenerator.closeFile(GeneratedFileType.INTERFACE_MASK, interfaceFileName)); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 363 | /** |
| 364 | * Close file handle for interface files. |
| 365 | */ |
| 366 | JavaFileGenerator.closeFileHandles(builderInterfaceFile); |
| 367 | JavaFileGenerator.closeFileHandles(interfaceFile); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 368 | |
| 369 | /** |
| 370 | * Remove temp files. |
| 371 | */ |
| 372 | JavaFileGenerator.clean(builderInterfaceFile); |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 373 | } |
| 374 | |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 375 | 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 saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 380 | |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 381 | if ((fileType & GeneratedFileType.BUILDER_CLASS_MASK) != 0 |
| 382 | || fileType == GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER) { |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 383 | |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 384 | /** |
| 385 | * Create builder class file. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 386 | */ |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 387 | String builderFileName = className + UtilConstants.BUILDER; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 388 | File builderFile = JavaFileGenerator.getFileObject(path, builderFileName, JAVA_FILE_EXTENSION, this); |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 389 | builderFile = JavaFileGenerator.generateBuilderClassFile(builderFile, className, imports, |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 390 | path.replace('/', '.'), attrList, this); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 391 | /** |
| 392 | * Create temp impl class file. |
| 393 | */ |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 394 | |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 395 | String implFileName = className + UtilConstants.IMPL; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 396 | File implTempFile = JavaFileGenerator.getFileObject(path, implFileName, TEMP_FILE_EXTENSION, this); |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 397 | implTempFile = JavaFileGenerator.generateImplClassFile(implTempFile, className, |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 398 | path.replace('/', '.'), attrList, this); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 399 | /** |
| 400 | * Append impl class to builder class and close it. |
| 401 | */ |
| 402 | JavaFileGenerator.appendFileContents(implTempFile, builderFile); |
| 403 | JavaFileGenerator.insert(builderFile, |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 404 | JavaFileGenerator.closeFile(GeneratedFileType.BUILDER_CLASS_MASK, builderFileName)); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 405 | |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 406 | /** |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 407 | * Close file handle for classes files. |
| 408 | */ |
| 409 | JavaFileGenerator.closeFileHandles(implTempFile); |
| 410 | JavaFileGenerator.closeFileHandles(builderFile); |
| 411 | |
| 412 | /** |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 413 | * Remove temp files. |
| 414 | */ |
| 415 | JavaFileGenerator.clean(implTempFile); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 416 | } |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 417 | |
| 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 saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 426 | path.replace('/', '.'), attrList, this); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 427 | 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 saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 436 | 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 saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 447 | } |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 448 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 449 | @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 S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 494 | } |
| 495 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 496 | @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 saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 527 | 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 saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 533 | |
| 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 saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 553 | 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 saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 561 | 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 S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 630 | } |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 631 | } |