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