blob: f44da543cfd7849ef65dcbe9f8f453c2e040a954 [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
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
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;
25
Bharat saraswal870c56f2016-02-20 21:57:16 +053026import org.onosproject.yangutils.utils.UtilConstants;
27
28/**
29 * Utility to handle file system operations.
30 */
31public final class FileSystemUtil {
Vinod Kumar S38046502016-03-23 15:30:27 +053032
Bharat saraswal870c56f2016-02-20 21:57:16 +053033 /**
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 Sc4216002016-03-03 19:55:30 +053042 * @param pkg Package to check if it is created
43 * @return existence status of package
Bharat saraswal870c56f2016-02-20 21:57:16 +053044 */
b.janani68c55e12016-02-24 12:23:03 +053045 public static boolean doesPackageExist(String pkg) {
Vinod Kumar S38046502016-03-23 15:30:27 +053046
b.janani68c55e12016-02-24 12:23:03 +053047 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 saraswal870c56f2016-02-20 21:57:16 +053050 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 S38046502016-03-23 15:30:27 +053063
b.janani68c55e12016-02-24 12:23:03 +053064 if (!doesPackageExist(pkg)) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053065 try {
Vinod Kumar S38046502016-03-23 15:30:27 +053066 File pack = YangIoUtils.createDirectories(pkg);
67 YangIoUtils.addPackageInfo(pack, pkgInfo, pkg.replace(UtilConstants.SLASH, UtilConstants.PERIOD));
Bharat saraswal870c56f2016-02-20 21:57:16 +053068 } catch (IOException e) {
69 throw new IOException("failed to create package-info file");
70 }
71 }
72 }
73
74 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053075 * 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 Sc4216002016-03-03 19:55:30 +053079 * appended
Bharat saraswal870c56f2016-02-20 21:57:16 +053080 * @param srcFile source file from which data is read and added to to append
Vinod Kumar Sc4216002016-03-03 19:55:30 +053081 * file
82 * @throws IOException any IO errors
Bharat saraswal870c56f2016-02-20 21:57:16 +053083 */
84 public static void appendFileContents(File toAppend, File srcFile) throws IOException {
85
Vinod Kumar S38046502016-03-23 15:30:27 +053086 updateFileHandle(srcFile,
87 UtilConstants.NEW_LINE + readAppendFile(toAppend.toString(), UtilConstants.FOUR_SPACE_INDENTATION),
88 false);
Bharat saraswal870c56f2016-02-20 21:57:16 +053089 return;
90 }
91
92 /**
93 * Reads file and convert it to string.
94 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053095 * @param toAppend file to be converted
Vinod Kumar S38046502016-03-23 15:30:27 +053096 * @param spaces spaces to be appended
Vinod Kumar Sc4216002016-03-03 19:55:30 +053097 * @return string of file
Bharat saraswal870c56f2016-02-20 21:57:16 +053098 * @throws IOException when fails to convert to string
99 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530100 public static String readAppendFile(String toAppend, String spaces) throws IOException {
101
102 FileReader fileReader = new FileReader(toAppend);
103 BufferedReader bufferReader = new BufferedReader(fileReader);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530104 try {
105 StringBuilder stringBuilder = new StringBuilder();
106 String line = bufferReader.readLine();
107
108 while (line != null) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530109 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 saraswal8f2a6c52016-03-09 18:34:56 +0530115 } else {
Vinod Kumar S38046502016-03-23 15:30:27 +0530116 stringBuilder.append(spaces + line);
117 stringBuilder.append(UtilConstants.NEW_LINE);
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530118 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530119 line = bufferReader.readLine();
120 }
121 return stringBuilder.toString();
122 } finally {
Vinod Kumar S38046502016-03-23 15:30:27 +0530123 fileReader.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530124 bufferReader.close();
125 }
126 }
127
128 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530129 * Update the generated file handle.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530130 *
131 * @param inputFile input file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530132 * @param contentTobeAdded content to be appended to the file
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530133 * @param isClose when close of file is called.
Vinod Kumar S38046502016-03-23 15:30:27 +0530134 * @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 saraswal870c56f2016-02-20 21:57:16 +0530137 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530138 public static void updateFileHandle(File inputFile, String contentTobeAdded, boolean isClose) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530139
Bharat saraswal870c56f2016-02-20 21:57:16 +0530140 FileWriter fileWriter = new FileWriter(inputFile, true);
Vinod Kumar S38046502016-03-23 15:30:27 +0530141 PrintWriter outputPrintWriter = new PrintWriter(fileWriter, true);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530142 if (!isClose) {
143 outputPrintWriter.write(contentTobeAdded);
144 outputPrintWriter.flush();
145 outputPrintWriter.close();
146 } else {
147 fileWriter.flush();
148 fileWriter.close();
149 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530150 }
151}