| /******************************************************************************* |
| * Copyright (c) 2020 DFKI. |
| * |
| * This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * DFKI - Volkan Gezer <volkan.gezer@dfki.de> |
| * |
| *******************************************************************************/ |
| package org.eclipse.aas.basyx.codegen.util; |
| |
| /** |
| * |
| */ |
| |
| import java.io.BufferedWriter; |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.FileNotFoundException; |
| import java.io.FileWriter; |
| import java.io.IOException; |
| import java.io.PrintWriter; |
| import java.io.RandomAccessFile; |
| import java.nio.file.FileAlreadyExistsException; |
| import java.nio.file.Files; |
| |
| /** |
| * Provides static methods to perform file operations. |
| * |
| * @author Volkan |
| * |
| */ |
| public class FileUtils { |
| |
| /** |
| * Removes file contents. |
| * |
| * @param fileName to clear |
| */ |
| public static void clearFile(final String fileName) { |
| PrintWriter writer; |
| try { |
| writer = new PrintWriter(fileName); |
| writer.close(); |
| } catch (final FileNotFoundException e) { |
| System.err.println("File not found!"); |
| e.printStackTrace(); |
| } |
| |
| } |
| |
| /** |
| * Reads file contents. |
| * |
| * @param fileName to read |
| */ |
| public static String readFile(final String fileName) { |
| |
| try { |
| File file = new File(fileName); |
| FileInputStream fis = new FileInputStream(file); |
| byte[] data = new byte[(int) file.length()]; |
| fis.read(data); |
| fis.close(); |
| |
| return new String(data, "UTF-8"); |
| } catch (Exception e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } |
| return "File Error!"; |
| |
| } |
| |
| |
| /** |
| * Removes a line from the given file. |
| * |
| * @param fileName to open for writing |
| * @param lineToRemove line to search for. |
| * @return {@code true if successful}, {@code false} if not. |
| */ |
| public static boolean removeLine(final String fileName, final String lineToRemove) { |
| RandomAccessFile file; |
| boolean success = false; |
| try { |
| file = new RandomAccessFile(fileName, "rw"); |
| String delete; |
| String task = ""; |
| while ((delete = file.readLine()) != null) { |
| if (delete.startsWith(lineToRemove)) { |
| success = true; |
| continue; |
| } |
| task += delete + "\n"; |
| } |
| final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); |
| writer.write(task); |
| file.close(); |
| writer.close(); |
| System.out.println("Line removed!"); |
| } catch (final Exception e) { |
| System.err.println("Error during line removal!"); |
| e.printStackTrace(); |
| } |
| return success; |
| |
| } |
| |
| /** |
| * Writes the given text into the given file. Creates if file not exists. Closes |
| * after writing. It does not clear the existing data. |
| * |
| * @param fileName to open for writing. |
| * @param data to write as a new line. |
| * |
| */ |
| public static void writeData(final String fileName, final String data) { |
| try { |
| clearFile(fileName); |
| final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true)); |
| writer.write(data); |
| writer.newLine(); |
| writer.close(); |
| } catch (final IOException e) { |
| System.err.println("Error while opening the file!"); |
| } |
| } |
| |
| /** |
| * Creates the given {@code fullPath}, if some of the folders do not exist, it |
| * creates them. |
| * |
| * @param fullPath |
| * @return {@code true} if created, {@code false} if not |
| */ |
| public static boolean createDirectories(String fullPath) { |
| File directory = new File(fullPath); |
| |
| System.out.println("Creating folder: " + directory.getAbsolutePath()); |
| if (!directory.exists()) { |
| |
| directory.mkdirs(); |
| System.out.println(directory.getAbsolutePath()+ " successfully created"); |
| return true; |
| } |
| return false; |
| } |
| |
| public static String getCurrentWorkingDirectory() { |
| File directory = new File(""); |
| return directory.getAbsolutePath(); |
| } |
| |
| public static String getFullPathFromNamespace(String namespace) { |
| // convert each dot into a folder |
| return namespace.replace(".", "/"); |
| } |
| |
| public static void copyFile(String fromFullPath, String toFullPath) { |
| File source = new File(fromFullPath); |
| File dest = new File(toFullPath); |
| try { |
| Files.copy(source.toPath(), dest.toPath()); |
| } catch(FileAlreadyExistsException e) { |
| System.out.println("The project with the same name was found. Skipping external class copy!"); |
| } catch (IOException e) { |
| System.out.println("Error during copy!"); |
| e.printStackTrace(); |
| } |
| } |
| } |