blob: f7e3556a9a49800c7f86fce7b743760dc24aa6c5 [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
26import org.onosproject.yangutils.translator.CachedFileHandle;
27import org.onosproject.yangutils.translator.GeneratedFileType;
28import org.onosproject.yangutils.translator.tojava.CachedJavaFileHandle;
29import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
30import org.onosproject.yangutils.utils.UtilConstants;
31
32/**
33 * Utility to handle file system operations.
34 */
35public final class FileSystemUtil {
36 /**
37 * Hiding constructor of a utility class.
38 */
39 private FileSystemUtil() {
40 }
41
42 /**
43 * Check if the package directory structure created.
44 *
45 * @param pkg Package to check if it is created.
46 * @return existence status of package.
47 */
48 public static boolean doesPackageExist(File pkg) {
49 if (pkg.exists()) {
50 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 {
63 if (!doesPackageExist(new File(pkg))) {
64 try {
65 File pack = YangIoUtils
66 .createDirectories(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
67 YangIoUtils.addPackageInfo(pack, pkgInfo, pkg);
68 } catch (IOException e) {
69 throw new IOException("failed to create package-info file");
70 }
71 }
72 }
73
74 /**
75 * Create a java source file in the specified package.
76 *
77 * @param pkg java package under which the interface file needs to be created.
78 * @param yangName YANG name of the node for which java file needs to be created.
79 * @param types types of files to be created.
80 * @throws IOException when fails to create interface file.
81 * @return the cached java file handle, which can be used to further add methods.
82 */
83 public static CachedFileHandle createSourceFiles(String pkg, String yangName, GeneratedFileType types)
84 throws IOException {
85 //if (!doesPackageExist(new File(pkg))) {
86 // throw new IOException("package does not exist.");
87 //}
88 yangName = JavaIdentifierSyntax.getCamelCase(yangName);
89 CachedFileHandle handler = new CachedJavaFileHandle(pkg, yangName, types);
90
91 return handler;
92 }
93
94 /**
95 * Read the contents from source file and append its contents to append
96 * file.
97 *
98 * @param toAppend destination file in which the contents of source file is
99 * appended.
100 * @param srcFile source file from which data is read and added to to append
101 * file.
102 * @throws IOException any IO errors.
103 */
104 public static void appendFileContents(File toAppend, File srcFile) throws IOException {
105
106 insertStringInFile(srcFile, UtilConstants.NEW_LINE + readAppendFile(toAppend.toString()));
107 //TODO: read the contents from src file and append its contents to append file.
108 return;
109 }
110
111 /**
112 * Reads file and convert it to string.
113 *
114 * @param toAppend file to be converted.
115 * @return string of file.
116 * @throws IOException when fails to convert to string
117 */
118 private static String readAppendFile(String toAppend) throws IOException {
119 BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend));
120 try {
121 StringBuilder stringBuilder = new StringBuilder();
122 String line = bufferReader.readLine();
123
124 while (line != null) {
125 stringBuilder.append(UtilConstants.FOUR_SPACE_INDENTATION + line);
126 stringBuilder.append("\n");
127 line = bufferReader.readLine();
128 }
129 return stringBuilder.toString();
130 } finally {
131 bufferReader.close();
132 }
133 }
134
135 /**
136 * Insert content to the generated file.
137 *
138 * @param inputFile input file
139 * @param contentTobeAdded content to be appended to the file.
140 * @throws IOException when fails to append content to the file.
141 */
142 public static void insertStringInFile(File inputFile, String contentTobeAdded) throws IOException {
143 FileWriter fileWriter = new FileWriter(inputFile, true);
144 PrintWriter outputPrintWriter = new PrintWriter(fileWriter);
145 outputPrintWriter.write(contentTobeAdded);
146 outputPrintWriter.flush();
147 outputPrintWriter.close();
148
149 }
150}