Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +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.utils; |
| 18 | |
| 19 | import java.io.File; |
| 20 | import java.io.IOException; |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import org.onosproject.yangutils.datamodel.YangNode; |
| 25 | import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo; |
| 26 | import org.onosproject.yangutils.translator.tojava.HasJavaImportData; |
| 27 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
| 28 | import org.onosproject.yangutils.translator.tojava.JavaImportData; |
| 29 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo; |
| 30 | import org.onosproject.yangutils.utils.UtilConstants; |
| 31 | |
| 32 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK; |
| 33 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK; |
| 34 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER; |
| 35 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK; |
| 36 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK; |
| 37 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.appendFileContents; |
| 38 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.clean; |
| 39 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.closeFileHandles; |
| 40 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile; |
| 41 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile; |
| 42 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile; |
| 43 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile; |
| 44 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.getFileObject; |
| 45 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.insert; |
| 46 | |
| 47 | /** |
| 48 | * Provides generate java code exit. |
| 49 | */ |
| 50 | public final class GenerateJavaCodeExitBuilder { |
| 51 | |
| 52 | private static final String JAVA_FILE_EXTENSION = ".java"; |
| 53 | |
| 54 | /** |
| 55 | * Default constructor. |
| 56 | */ |
| 57 | private GenerateJavaCodeExitBuilder() { |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Sets import for hash and equals method. |
| 62 | * |
| 63 | * @return import string for implementing hash and equals |
| 64 | */ |
| 65 | private static String setImportForHashAndEquals() { |
| 66 | |
| 67 | return UtilConstants.IMPORT + UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG + UtilConstants.PERIOD |
| 68 | + UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Sets import for to string method. |
| 73 | * |
| 74 | * @return import string for implementing to string |
| 75 | */ |
| 76 | private static String setImportForToString() { |
| 77 | |
| 78 | return UtilConstants.IMPORT + UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG + UtilConstants.PERIOD |
| 79 | + UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Sets import for to list. |
| 84 | * |
| 85 | * @return import string for list collection |
| 86 | */ |
| 87 | private static String setImportForList() { |
| 88 | |
| 89 | return UtilConstants.IMPORT + UtilConstants.COLLECTION_IMPORTS + UtilConstants.PERIOD |
| 90 | + UtilConstants.LIST + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Construct java code exit. |
| 95 | * |
| 96 | * @param fileType generated file type |
| 97 | * @param curNode current YANG node |
| 98 | * @throws IOException when fails to generate java files |
| 99 | */ |
| 100 | public static void generateJavaFile(int fileType, YangNode curNode) throws IOException { |
| 101 | |
| 102 | JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo(); |
| 103 | String className = JavaIdentifierSyntax.getCaptialCase(javaFileInfo.getJavaName()); |
| 104 | String pkg = javaFileInfo.getPackageFilePath(); |
| 105 | List<String> imports = getImports(((HasJavaImportData) curNode).getJavaImportData()); |
| 106 | |
| 107 | /** |
| 108 | * Start generation of files. |
| 109 | */ |
| 110 | if ((fileType & INTERFACE_MASK) != 0 | (fileType & BUILDER_INTERFACE_MASK) != 0 |
| 111 | | fileType == GENERATE_INTERFACE_WITH_BUILDER) { |
| 112 | |
| 113 | /** |
| 114 | * Create interface file. |
| 115 | */ |
| 116 | String interfaceFileName = className; |
| 117 | File interfaceFile = getFileObject(pkg, interfaceFileName, JAVA_FILE_EXTENSION, javaFileInfo); |
| 118 | interfaceFile = generateInterfaceFile(interfaceFile, imports, curNode); |
| 119 | /** |
| 120 | * Create temp builder interface file. |
| 121 | */ |
| 122 | String builderInterfaceFileName = className |
| 123 | + UtilConstants.BUILDER + UtilConstants.INTERFACE; |
| 124 | File builderInterfaceFile = getFileObject(pkg, builderInterfaceFileName, JAVA_FILE_EXTENSION, javaFileInfo); |
| 125 | builderInterfaceFile = generateBuilderInterfaceFile(builderInterfaceFile, curNode); |
| 126 | /** |
| 127 | * Append builder interface file to interface file and close it. |
| 128 | */ |
| 129 | appendFileContents(builderInterfaceFile, interfaceFile); |
| 130 | insert(interfaceFile, JavaCodeSnippetGen.getJavaClassDefClose()); |
| 131 | /** |
| 132 | * Close file handle for interface files. |
| 133 | */ |
| 134 | closeFileHandles(builderInterfaceFile); |
| 135 | closeFileHandles(interfaceFile); |
| 136 | |
| 137 | /** |
| 138 | * Remove temp files. |
| 139 | */ |
| 140 | clean(builderInterfaceFile); |
| 141 | } |
| 142 | |
| 143 | imports.add(setImportForHashAndEquals()); |
| 144 | imports.add(setImportForToString()); |
| 145 | java.util.Collections.sort(imports); |
| 146 | |
| 147 | if ((fileType & BUILDER_CLASS_MASK) != 0 | (fileType & IMPL_CLASS_MASK) != 0 |
| 148 | | fileType == GENERATE_INTERFACE_WITH_BUILDER) { |
| 149 | |
| 150 | /** |
| 151 | * Create builder class file. |
| 152 | */ |
| 153 | String builderFileName = className |
| 154 | + UtilConstants.BUILDER; |
| 155 | File builderFile = getFileObject(pkg, builderFileName, JAVA_FILE_EXTENSION, javaFileInfo); |
| 156 | builderFile = generateBuilderClassFile(builderFile, imports, curNode); |
| 157 | /** |
| 158 | * Create temp impl class file. |
| 159 | */ |
| 160 | |
| 161 | String implFileName = className + UtilConstants.IMPL; |
| 162 | File implTempFile = getFileObject(pkg, implFileName, JAVA_FILE_EXTENSION, javaFileInfo); |
| 163 | implTempFile = generateImplClassFile(implTempFile, curNode); |
| 164 | /** |
| 165 | * Append impl class to builder class and close it. |
| 166 | */ |
| 167 | appendFileContents(implTempFile, builderFile); |
| 168 | insert(builderFile, JavaCodeSnippetGen.getJavaClassDefClose()); |
| 169 | |
| 170 | /** |
| 171 | * Close file handle for classes files. |
| 172 | */ |
| 173 | closeFileHandles(implTempFile); |
| 174 | closeFileHandles(builderFile); |
| 175 | |
| 176 | /** |
| 177 | * Remove temp files. |
| 178 | */ |
| 179 | clean(implTempFile); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) { |
| 184 | * |
| 185 | * /** Create builder class file. // |
| 186 | */ |
| 187 | //String typeDefFileName = className; |
| 188 | //File typeDefFile = JavaFileGenerator.getFileObject(path, typeDefFileName, JAVA_FILE_EXTENSION, |
| 189 | // ((HasJavaFileInfo) curNode).getJavaFileInfo()); |
| 190 | //typeDefFile = JavaFileGenerator.generateTypeDefClassFile(typeDefFile, className, imports, |
| 191 | // path.replace('/', '.'), attrList, ((HasJavaFileInfo) curNode).getJavaFileInfo()); |
| 192 | // JavaFileGenerator.insert(typeDefFile, JavaCodeSnippetGen.getJavaClassDefClose()); |
| 193 | |
| 194 | // /** |
| 195 | // * Close file handle for classes files. |
| 196 | // */ |
| 197 | // JavaFileGenerator.closeFileHandles(typeDefFile); |
| 198 | // } |
| 199 | // |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Returns import for class. |
| 204 | * |
| 205 | * @param javaImportData import data |
| 206 | * @return imports for class |
| 207 | */ |
| 208 | private static List<String> getImports(JavaImportData javaImportData) { |
| 209 | |
| 210 | String importString; |
| 211 | List<String> imports = new ArrayList<>(); |
| 212 | |
| 213 | for (JavaQualifiedTypeInfo importInfo : javaImportData.getImportSet()) { |
| 214 | importString = UtilConstants.IMPORT; |
| 215 | if (importInfo.getPkgInfo() != "" && importInfo.getClassInfo() != null |
| 216 | && importInfo.getPkgInfo() != UtilConstants.JAVA_LANG) { |
| 217 | importString = importString + importInfo.getPkgInfo() + "."; |
| 218 | importString = importString + importInfo.getClassInfo() + UtilConstants.SEMI_COLAN |
| 219 | + UtilConstants.NEW_LINE; |
| 220 | |
| 221 | imports.add(importString); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (javaImportData.getIfListImported()) { |
| 226 | imports.add(setImportForList()); |
| 227 | } |
| 228 | |
| 229 | java.util.Collections.sort(imports); |
| 230 | return imports; |
| 231 | } |
| 232 | } |