blob: d3e7b0987fe0b74287d3a3969d3839d50f3dd2a7 [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Bharat saraswal870c56f2016-02-20 21:57:16 +05303 *
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
17package org.onosproject.yangutils.utils.io.impl;
18
19import java.io.BufferedReader;
20import java.io.File;
21import java.io.FileReader;
22import java.io.FileWriter;
23import java.io.IOException;
24import java.io.PrintWriter;
Bharat saraswal33dfa012016-05-17 19:59:16 +053025import java.util.ArrayList;
26import java.util.List;
Bharat saraswal870c56f2016-02-20 21:57:16 +053027
Bharat saraswale2d51d62016-03-23 19:40:35 +053028import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
29import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
30import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
31import static org.onosproject.yangutils.utils.UtilConstants.MULTIPLE_NEW_LINE;
32import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053033import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswal870c56f2016-02-20 21:57:16 +053034
35/**
Bharat saraswald9822e92016-04-05 15:13:44 +053036 * Represents utility to handle file system operations.
Bharat saraswal870c56f2016-02-20 21:57:16 +053037 */
38public final class FileSystemUtil {
Vinod Kumar S38046502016-03-23 15:30:27 +053039
Bharat saraswal870c56f2016-02-20 21:57:16 +053040 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053041 * Creates an instance of file system util.
Bharat saraswal870c56f2016-02-20 21:57:16 +053042 */
43 private FileSystemUtil() {
44 }
45
46 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053047 * Reads the contents from source file and append its contents to append
Bharat saraswal870c56f2016-02-20 21:57:16 +053048 * file.
49 *
50 * @param toAppend destination file in which the contents of source file is
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053051 * appended
52 * @param srcFile source file from which data is read and added to to append
53 * file
Vinod Kumar Sc4216002016-03-03 19:55:30 +053054 * @throws IOException any IO errors
Bharat saraswal870c56f2016-02-20 21:57:16 +053055 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053056 public static void appendFileContents(File toAppend, File srcFile)
57 throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +053058 updateFileHandle(srcFile, NEW_LINE + readAppendFile(toAppend.toString(), FOUR_SPACE_INDENTATION), false);
Bharat saraswal870c56f2016-02-20 21:57:16 +053059 }
60
61 /**
62 * Reads file and convert it to string.
63 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053064 * @param toAppend file to be converted
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053065 * @param spaces spaces to be appended
Vinod Kumar Sc4216002016-03-03 19:55:30 +053066 * @return string of file
Bharat saraswal870c56f2016-02-20 21:57:16 +053067 * @throws IOException when fails to convert to string
68 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053069 public static String readAppendFile(String toAppend, String spaces)
70 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +053071
Vinod Kumar S38046502016-03-23 15:30:27 +053072 FileReader fileReader = new FileReader(toAppend);
73 BufferedReader bufferReader = new BufferedReader(fileReader);
Bharat saraswal870c56f2016-02-20 21:57:16 +053074 try {
75 StringBuilder stringBuilder = new StringBuilder();
76 String line = bufferReader.readLine();
77
78 while (line != null) {
Vidyashree Rama74453712016-04-18 12:29:39 +053079 if (line.equals(SPACE) || line.equals(EMPTY_STRING) || line.equals(EIGHT_SPACE_INDENTATION)
80 || line.equals(MULTIPLE_NEW_LINE)) {
Bharat saraswale2d51d62016-03-23 19:40:35 +053081 stringBuilder.append(NEW_LINE);
82 } else if (line.equals(FOUR_SPACE_INDENTATION)) {
83 stringBuilder.append(EMPTY_STRING);
Bharat saraswal8f2a6c52016-03-09 18:34:56 +053084 } else {
Vinod Kumar S38046502016-03-23 15:30:27 +053085 stringBuilder.append(spaces + line);
Bharat saraswale2d51d62016-03-23 19:40:35 +053086 stringBuilder.append(NEW_LINE);
Bharat saraswal8f2a6c52016-03-09 18:34:56 +053087 }
Bharat saraswal870c56f2016-02-20 21:57:16 +053088 line = bufferReader.readLine();
89 }
90 return stringBuilder.toString();
91 } finally {
Vinod Kumar S38046502016-03-23 15:30:27 +053092 fileReader.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +053093 bufferReader.close();
94 }
95 }
96
97 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053098 * Updates the generated file handle.
Bharat saraswal870c56f2016-02-20 21:57:16 +053099 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530100 * @param inputFile input file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530101 * @param contentTobeAdded content to be appended to the file
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530102 * @param isClose when close of file is called.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530103 * @throws IOException if the named file exists but is a directory rather than a regular file,
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530104 * does not exist but cannot be created, or cannot be opened for any other reason
Bharat saraswal870c56f2016-02-20 21:57:16 +0530105 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530106 public static void updateFileHandle(File inputFile, String contentTobeAdded, boolean isClose)
107 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530108
109 List<FileWriter> fileWriterStore = new ArrayList<>();
110
Bharat saraswal870c56f2016-02-20 21:57:16 +0530111 FileWriter fileWriter = new FileWriter(inputFile, true);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530112 fileWriterStore.add(fileWriter);
Vinod Kumar S38046502016-03-23 15:30:27 +0530113 PrintWriter outputPrintWriter = new PrintWriter(fileWriter, true);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530114 if (!isClose) {
115 outputPrintWriter.write(contentTobeAdded);
116 outputPrintWriter.flush();
117 outputPrintWriter.close();
118 } else {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530119 for (FileWriter curWriter : fileWriterStore) {
120 curWriter.flush();
121 curWriter.close();
122 curWriter = null;
123 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530124 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530125 }
126}