Bharat saraswal | 9745996 | 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.utils.io.impl; |
| 18 | |
| 19 | import java.io.BufferedReader; |
| 20 | import java.io.File; |
| 21 | import java.io.FileReader; |
| 22 | import java.io.FileWriter; |
| 23 | import java.io.IOException; |
| 24 | import java.io.PrintWriter; |
| 25 | |
| 26 | import org.onosproject.yangutils.translator.CachedFileHandle; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.translator.tojava.CachedJavaFileHandle; |
| 28 | import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax; |
| 29 | import org.onosproject.yangutils.utils.UtilConstants; |
| 30 | |
| 31 | /** |
| 32 | * Utility to handle file system operations. |
| 33 | */ |
| 34 | public final class FileSystemUtil { |
| 35 | /** |
| 36 | * Hiding constructor of a utility class. |
| 37 | */ |
| 38 | private FileSystemUtil() { |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Check if the package directory structure created. |
| 43 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 44 | * @param pkg Package to check if it is created |
| 45 | * @return existence status of package |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 46 | */ |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 47 | public static boolean doesPackageExist(String pkg) { |
| 48 | File pkgDir = new File(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH)); |
| 49 | File pkgWithFile = new File(pkgDir + File.separator + "package-info.java"); |
| 50 | if (pkgDir.exists() && pkgWithFile.isFile()) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Create a package structure with package info java file if not present. |
| 58 | * |
| 59 | * @param pkg java package string |
| 60 | * @param pkgInfo description of package |
| 61 | * @throws IOException any IO exception |
| 62 | */ |
| 63 | public static void createPackage(String pkg, String pkgInfo) throws IOException { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 64 | if (!doesPackageExist(pkg)) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 65 | try { |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 66 | File pack = YangIoUtils |
| 67 | .createDirectories(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH)); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 68 | YangIoUtils.addPackageInfo(pack, pkgInfo, pkg); |
| 69 | } catch (IOException e) { |
| 70 | throw new IOException("failed to create package-info file"); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Create a java source file in the specified package. |
| 77 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 78 | * @param pkg java package under which the interface file needs to be |
| 79 | * created |
| 80 | * @param yangName YANG name of the node for which java file needs to be |
| 81 | * created |
| 82 | * @param types types of files to be created |
| 83 | * @throws IOException when fails to create interface file |
| 84 | * @return the cached java file handle, which can be used to further add |
| 85 | * methods |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 86 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 87 | public static CachedFileHandle createSourceFiles(String pkg, String yangName, int types) |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 88 | throws IOException { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 89 | yangName = JavaIdentifierSyntax.getCamelCase(yangName); |
| 90 | CachedFileHandle handler = new CachedJavaFileHandle(pkg, yangName, types); |
| 91 | |
| 92 | return handler; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Read the contents from source file and append its contents to append |
| 97 | * file. |
| 98 | * |
| 99 | * @param toAppend destination file in which the contents of source file is |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 100 | * appended |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 101 | * @param srcFile source file from which data is read and added to to append |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 102 | * file |
| 103 | * @throws IOException any IO errors |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 104 | */ |
| 105 | public static void appendFileContents(File toAppend, File srcFile) throws IOException { |
| 106 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 107 | updateFileHandle(srcFile, UtilConstants.NEW_LINE + readAppendFile(toAppend.toString()), false); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Reads file and convert it to string. |
| 113 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 114 | * @param toAppend file to be converted |
| 115 | * @return string of file |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 116 | * @throws IOException when fails to convert to string |
| 117 | */ |
| 118 | private static String readAppendFile(String toAppend) throws IOException { |
| 119 | BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend)); |
| 120 | try { |
| 121 | StringBuilder stringBuilder = new StringBuilder(); |
| 122 | String line = bufferReader.readLine(); |
| 123 | |
| 124 | while (line != null) { |
| 125 | stringBuilder.append(UtilConstants.FOUR_SPACE_INDENTATION + line); |
| 126 | stringBuilder.append("\n"); |
| 127 | line = bufferReader.readLine(); |
| 128 | } |
| 129 | return stringBuilder.toString(); |
| 130 | } finally { |
| 131 | bufferReader.close(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 136 | * Update the generated file handle. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 137 | * |
| 138 | * @param inputFile input file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 139 | * @param contentTobeAdded content to be appended to the file |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 140 | * @param isClose when close of file is called. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 141 | * @throws IOException when fails to append content to the file |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 142 | */ |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 143 | public static void updateFileHandle(File inputFile, String contentTobeAdded, boolean isClose) throws IOException { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 144 | FileWriter fileWriter = new FileWriter(inputFile, true); |
| 145 | PrintWriter outputPrintWriter = new PrintWriter(fileWriter); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 146 | if (!isClose) { |
| 147 | outputPrintWriter.write(contentTobeAdded); |
| 148 | outputPrintWriter.flush(); |
| 149 | outputPrintWriter.close(); |
| 150 | } else { |
| 151 | fileWriter.flush(); |
| 152 | fileWriter.close(); |
| 153 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 154 | } |
| 155 | } |