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