Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 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; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 25 | import java.util.ArrayList; |
| 26 | import java.util.List; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 27 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION; |
| 29 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
| 30 | import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION; |
| 31 | import static org.onosproject.yangutils.utils.UtilConstants.MULTIPLE_NEW_LINE; |
| 32 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 34 | |
| 35 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 36 | * Represents utility to handle file system operations. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 37 | */ |
| 38 | public final class FileSystemUtil { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 39 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 40 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 41 | * Creates an instance of file system util. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 42 | */ |
| 43 | private FileSystemUtil() { |
| 44 | } |
| 45 | |
| 46 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 47 | * Reads the contents from source file and append its contents to append |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 48 | * file. |
| 49 | * |
| 50 | * @param toAppend destination file in which the contents of source file is |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 51 | * appended |
| 52 | * @param srcFile source file from which data is read and added to to append |
| 53 | * file |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 54 | * @throws IOException any IO errors |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 55 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 56 | public static void appendFileContents(File toAppend, File srcFile) |
| 57 | throws IOException { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 58 | updateFileHandle(srcFile, NEW_LINE + readAppendFile(toAppend.toString(), FOUR_SPACE_INDENTATION), false); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Reads file and convert it to string. |
| 63 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 64 | * @param toAppend file to be converted |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 65 | * @param spaces spaces to be appended |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 66 | * @return string of file |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 67 | * @throws IOException when fails to convert to string |
| 68 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 69 | public static String readAppendFile(String toAppend, String spaces) |
| 70 | throws IOException { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 71 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 72 | FileReader fileReader = new FileReader(toAppend); |
| 73 | BufferedReader bufferReader = new BufferedReader(fileReader); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 74 | try { |
| 75 | StringBuilder stringBuilder = new StringBuilder(); |
| 76 | String line = bufferReader.readLine(); |
| 77 | |
| 78 | while (line != null) { |
Vidyashree Rama | 7445371 | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 79 | if (line.equals(SPACE) || line.equals(EMPTY_STRING) || line.equals(EIGHT_SPACE_INDENTATION) |
| 80 | || line.equals(MULTIPLE_NEW_LINE)) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 81 | stringBuilder.append(NEW_LINE); |
| 82 | } else if (line.equals(FOUR_SPACE_INDENTATION)) { |
| 83 | stringBuilder.append(EMPTY_STRING); |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 84 | } else { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 85 | stringBuilder.append(spaces + line); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 86 | stringBuilder.append(NEW_LINE); |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 87 | } |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 88 | line = bufferReader.readLine(); |
| 89 | } |
| 90 | return stringBuilder.toString(); |
| 91 | } finally { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 92 | fileReader.close(); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 93 | bufferReader.close(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 98 | * Updates the generated file handle. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 99 | * |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 100 | * @param inputFile input file |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 101 | * @param contentTobeAdded content to be appended to the file |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 102 | * @param isClose when close of file is called. |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 103 | * @throws IOException if the named file exists but is a directory rather than a regular file, |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 104 | * does not exist but cannot be created, or cannot be opened for any other reason |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 105 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 106 | public static void updateFileHandle(File inputFile, String contentTobeAdded, boolean isClose) |
| 107 | throws IOException { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 108 | |
| 109 | List<FileWriter> fileWriterStore = new ArrayList<>(); |
| 110 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 111 | FileWriter fileWriter = new FileWriter(inputFile, true); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 112 | fileWriterStore.add(fileWriter); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 113 | PrintWriter outputPrintWriter = new PrintWriter(fileWriter, true); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 114 | if (!isClose) { |
| 115 | outputPrintWriter.write(contentTobeAdded); |
| 116 | outputPrintWriter.flush(); |
| 117 | outputPrintWriter.close(); |
| 118 | } else { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 119 | for (FileWriter curWriter : fileWriterStore) { |
| 120 | curWriter.flush(); |
| 121 | curWriter.close(); |
| 122 | curWriter = null; |
| 123 | } |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 124 | } |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 125 | } |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * Closes the file handle for temporary file. |
| 129 | * |
| 130 | * @param file file to be closed |
| 131 | * @param toBeDeleted flag to indicate if file needs to be deleted |
| 132 | * @throws IOException when failed to close the file handle |
| 133 | */ |
| 134 | public static void closeFile(File file, boolean toBeDeleted) |
| 135 | throws IOException { |
| 136 | |
| 137 | if (file != null) { |
| 138 | updateFileHandle(file, null, true); |
| 139 | if (toBeDeleted) { |
| 140 | file.delete(); |
| 141 | } |
| 142 | } |
| 143 | } |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 144 | } |