Path: blob/master/sourcetools/com.ibm.jpp.preprocessor/com/ibm/jpp/om/SimpleCopy.java
6004 views
/*******************************************************************************1* Copyright (c) 1999, 2021 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.jpp.om;2223import java.io.File;24import java.io.FileInputStream;25import java.io.FileOutputStream;26import java.io.IOException;27import java.io.InputStream;28import java.io.OutputStream;29import java.util.ArrayList;30import java.util.HashMap;31import java.util.List;32import java.util.Map;33import java.util.StringTokenizer;3435/**36* Class representing a preprocess copy simple copy (as opposed to a source that must be preprocessed). The SimpleCopy class is37* responsible for copying entire folder or single files to the appropriate destination.38*/39public class SimpleCopy {40private File sourcePath = null; /* The source directory or file to the copy. */41private File outputDir = null; /* The output directory for the copy. */42private int copyFileCount = 0; /* The number of copy files for each source directory. */43private final Map<File, String[]> copyFilesBySourceDir = new HashMap<>(); /* the relative paths of all of the files for a given sourceDir. */44private String baseDir = "";45private String simpleOutput = "";4647/**48* Returns the copy task's output directory.49*50* @return the output directory51*/52public File getOutputDir() {53return this.outputDir;54}5556/**57* Sets the copy task's output directory.58*59* @param outputDir the task's output directory60*/61public void setOutputDir(File outputDir) {62if (outputDir == null) {63throw new NullPointerException();64}65this.outputDir = outputDir;66}6768/**69* Returns the copy task's source path.70*71* @return the source path72*/73public File getSourcePath() {74return this.sourcePath;75}7677/**78* Sets the copy task's source path.79*80* @param sourcePath the source path81*/82public void setSourcePath(File sourcePath) {83if (sourcePath == null) {84throw new NullPointerException();85}8687this.sourcePath = sourcePath;88}8990/**91* Sets the simplecopy base directory.92*93* @param baseDir the base directory94*/95public void setBaseDir(String baseDir) {96this.baseDir = baseDir;97}9899/**100* Sets the output path of this simplecopy task. This can either be101* an absolute path or one relative to the configuration output path.102*103* @param simpleOutput the simplecopy output path104*/105public void setSimpleOutput(String simpleOutput) {106this.simpleOutput = simpleOutput;107}108109/**110* Performs the build.111*/112public boolean copy() {113System.out.println("\nCopying from " + sourcePath.getAbsolutePath());114115if (sourcePath.isDirectory()) {116computeBuildFiles();117copyFileCount = 0;118String[] copyFiles = copyFilesBySourceDir.get(sourcePath);119120for (String copyFile : copyFiles) {121File sourceFile = new File(sourcePath, copyFile);122File outputFile = new File(outputDir, copyFile);123124// Exclude CVS files so the generated project does not display as shared in a repository125if (outputFile.getParentFile().getName().indexOf("CVS") > -1) {126outputFile.delete();127continue;128}129130try {131copyFile(sourceFile, outputFile);132copyFileCount++;133} catch (IOException e) {134System.out.println("IOException occured in file " + sourceFile.getAbsolutePath() + ", copy failed.");135e.printStackTrace();136return false;137} catch (Exception e) {138System.out.println("Exception occured in file " + sourceFile.getAbsolutePath() + ", copy failed.");139e.printStackTrace();140return false;141}142}143} else {144File sourceFile = sourcePath;145File outputFile = null;146147if (simpleOutput.startsWith(File.separator) || simpleOutput.startsWith(":", 1)) {148outputFile = new File(simpleOutput, sourcePath.getName());149} else if (!simpleOutput.equals("")) {150outputFile = new File(new File(outputDir, simpleOutput), sourcePath.getName());151} else {152String parent = sourcePath.getParent();153parent = parent.substring(baseDir.length());154155StringBuffer strBuffer = new StringBuffer(outputDir.getAbsolutePath());156StringTokenizer st = new StringTokenizer(parent, File.separator);157158if (st.countTokens() > 2) {159st.nextToken();160String token = st.nextToken();161162if (!token.equals("src")) {163strBuffer.append(File.separator);164strBuffer.append(token);165}166167while (st.hasMoreTokens()) {168strBuffer.append(File.separator);169strBuffer.append(st.nextToken());170}171}172173strBuffer.append(File.separator);174strBuffer.append(sourcePath.getName());175176outputFile = new File(strBuffer.toString());177}178179try {180copyFile(sourceFile, outputFile);181copyFileCount++;182} catch (IOException e) {183System.out.println("IOException occured in file " + sourceFile.getAbsolutePath() + ", copy failed.");184e.printStackTrace();185return false;186}187}188189System.out.println(copyFileCount + " file(s) copied.\n");190return true;191}192193public static void copyFile(File inputFile, File outputFile) throws IOException {194outputFile.getParentFile().mkdirs();195outputFile.createNewFile();196197try (InputStream input = new FileInputStream(inputFile)) {198try (OutputStream output = new FileOutputStream(outputFile)) {199byte[] buffer = new byte[4096];200201for (int numBytes; (numBytes = input.read(buffer)) != -1;) {202output.write(buffer, 0, numBytes);203}204}205}206}207208/**209* Recursively searches the given root directory to find all files. The file210* paths are returned, relative to the root directory.211*/212private static List<String> getFiles(File root) {213List<String> fileList = new ArrayList<>();214File[] files = root.listFiles();215216if (files != null) {217getFiles(files, "", fileList);218} else {219System.out.print("Error reading the source directory \"");220System.out.print(root.getAbsolutePath());221System.out.println("\" - No Files copied");222}223224return fileList;225}226227/**228* This is a helper function to getFiles(File);229*/230private static void getFiles(File[] files, String relativePath, List<String> fileList) {231for (File file : files) {232String filePath = relativePath + file.getName();233234if (file.isFile()) {235fileList.add(filePath);236} else {237getFiles(file.listFiles(), filePath + File.separator, fileList);238}239}240}241242private void computeBuildFiles() {243List<String> copyFiles = getFiles(sourcePath);244String[] buildFilesArray = copyFiles.toArray(new String[copyFiles.size()]);245246copyFileCount += buildFilesArray.length;247copyFilesBySourceDir.put(sourcePath, buildFilesArray);248}249}250251252