blob: 3765f71a8ecdd544223e30ad0cfd051e0caaf029 [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;
25
Bharat saraswal2f11f652016-03-25 18:19:46 +053026import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getJavaPackageFromPackagePath;
27import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
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;
33import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
34import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
35import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
Bharat saraswale2d51d62016-03-23 19:40:35 +053036import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
Bharat saraswal870c56f2016-02-20 21:57:16 +053037
38/**
Bharat saraswald9822e92016-04-05 15:13:44 +053039 * Represents utility to handle file system operations.
Bharat saraswal870c56f2016-02-20 21:57:16 +053040 */
41public final class FileSystemUtil {
Vinod Kumar S38046502016-03-23 15:30:27 +053042
Bharat saraswal870c56f2016-02-20 21:57:16 +053043 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053044 * Creates an instance of file system util.
Bharat saraswal870c56f2016-02-20 21:57:16 +053045 */
46 private FileSystemUtil() {
47 }
48
49 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053050 * Checks if the package directory structure created.
Bharat saraswal870c56f2016-02-20 21:57:16 +053051 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053052 * @param pkg Package to check if it is created
53 * @return existence status of package
Bharat saraswal870c56f2016-02-20 21:57:16 +053054 */
b.janani68c55e12016-02-24 12:23:03 +053055 public static boolean doesPackageExist(String pkg) {
Vinod Kumar S38046502016-03-23 15:30:27 +053056
Bharat saraswal2f11f652016-03-25 18:19:46 +053057 File pkgDir = new File(getPackageDirPathFromJavaJPackage(pkg));
Bharat saraswale2d51d62016-03-23 19:40:35 +053058 File pkgWithFile = new File(pkgDir + SLASH + "package-info.java");
b.janani68c55e12016-02-24 12:23:03 +053059 if (pkgDir.exists() && pkgWithFile.isFile()) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053060 return true;
61 }
62 return false;
63 }
64
65 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053066 * Creates a package structure with package info java file if not present.
Bharat saraswal870c56f2016-02-20 21:57:16 +053067 *
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 S38046502016-03-23 15:30:27 +053073
b.janani68c55e12016-02-24 12:23:03 +053074 if (!doesPackageExist(pkg)) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053075 try {
Bharat saraswale2d51d62016-03-23 19:40:35 +053076 File pack = createDirectories(pkg);
Bharat saraswal2f11f652016-03-25 18:19:46 +053077 addPackageInfo(pack, pkgInfo, getJavaPackageFromPackagePath(pkg));
Bharat saraswal870c56f2016-02-20 21:57:16 +053078 } catch (IOException e) {
79 throw new IOException("failed to create package-info file");
80 }
81 }
82 }
83
84 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053085 * Reads the contents from source file and append its contents to append
Bharat saraswal870c56f2016-02-20 21:57:16 +053086 * file.
87 *
88 * @param toAppend destination file in which the contents of source file is
Vinod Kumar Sc4216002016-03-03 19:55:30 +053089 * appended
Bharat saraswal870c56f2016-02-20 21:57:16 +053090 * @param srcFile source file from which data is read and added to to append
Vinod Kumar Sc4216002016-03-03 19:55:30 +053091 * file
92 * @throws IOException any IO errors
Bharat saraswal870c56f2016-02-20 21:57:16 +053093 */
94 public static void appendFileContents(File toAppend, File srcFile) throws IOException {
95
Bharat saraswale2d51d62016-03-23 19:40:35 +053096 updateFileHandle(srcFile, NEW_LINE + readAppendFile(toAppend.toString(), FOUR_SPACE_INDENTATION), false);
Bharat saraswal870c56f2016-02-20 21:57:16 +053097 return;
98 }
99
100 /**
101 * Reads file and convert it to string.
102 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530103 * @param toAppend file to be converted
Vinod Kumar S38046502016-03-23 15:30:27 +0530104 * @param spaces spaces to be appended
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530105 * @return string of file
Bharat saraswal870c56f2016-02-20 21:57:16 +0530106 * @throws IOException when fails to convert to string
107 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530108 public static String readAppendFile(String toAppend, String spaces) throws IOException {
109
110 FileReader fileReader = new FileReader(toAppend);
111 BufferedReader bufferReader = new BufferedReader(fileReader);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530112 try {
113 StringBuilder stringBuilder = new StringBuilder();
114 String line = bufferReader.readLine();
115
116 while (line != null) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530117 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 saraswal8f2a6c52016-03-09 18:34:56 +0530122 } else {
Vinod Kumar S38046502016-03-23 15:30:27 +0530123 stringBuilder.append(spaces + line);
Bharat saraswale2d51d62016-03-23 19:40:35 +0530124 stringBuilder.append(NEW_LINE);
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530125 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530126 line = bufferReader.readLine();
127 }
128 return stringBuilder.toString();
129 } finally {
Vinod Kumar S38046502016-03-23 15:30:27 +0530130 fileReader.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530131 bufferReader.close();
132 }
133 }
134
135 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530136 * Updates the generated file handle.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530137 *
138 * @param inputFile input file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530139 * @param contentTobeAdded content to be appended to the file
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530140 * @param isClose when close of file is called.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530141 * @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 saraswal870c56f2016-02-20 21:57:16 +0530143 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530144 public static void updateFileHandle(File inputFile, String contentTobeAdded, boolean isClose) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530145
Bharat saraswal870c56f2016-02-20 21:57:16 +0530146 FileWriter fileWriter = new FileWriter(inputFile, true);
Vinod Kumar S38046502016-03-23 15:30:27 +0530147 PrintWriter outputPrintWriter = new PrintWriter(fileWriter, true);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530148 if (!isClose) {
149 outputPrintWriter.write(contentTobeAdded);
150 outputPrintWriter.flush();
151 outputPrintWriter.close();
152 } else {
153 fileWriter.flush();
154 fileWriter.close();
155 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530156 }
157}