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