View Javadoc

1   /**
2    * Copyright (c) 2012, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.tools;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  import org.openimaj.io.FileUtils;
38  
39  /**
40   * Tools for dealing with #InOutTool instances that are local file
41   *
42   * @author Sina Samangooei (ss@ecs.soton.ac.uk)
43   *
44   */
45  public class FileToolsUtil {
46  
47  	/**
48  	 * Validate the (local) input from an {@link InOutToolOptions} and return
49  	 * the corresponding file.
50  	 *
51  	 * @param tool
52  	 *            the tool from which to get settings
53  	 * @return a none null input file location if it exists
54  	 * @throws IOException
55  	 *             if the file doesn't exist
56  	 */
57  	public static List<File> validateLocalInput(InOutToolOptions tool) throws IOException {
58  		if (tool.input == null && tool.getInputFile() == null) {
59  			throw new IOException("No valid input provided");
60  		}
61  		List<File> toret = null;
62  		toret = new ArrayList<File>();
63  		if (tool.input != null) {
64  			File f = new File(tool.input);
65  			if (!f.exists())
66  				throw new IOException("Couldn't file file");
67  			toret.add(f);
68  		} else {
69  			String[] allinputs = tool.getAllInputs();
70  			for (String floc : allinputs) {
71  				File f = new File(floc);
72  				if (f.exists())
73  					toret.add(f);
74  			}
75  		}
76  		return toret;
77  
78  	}
79  
80  	/**
81  	 * Test whether the input is the stdin
82  	 *
83  	 * @param tool
84  	 * @return true if input should be stdin, false otherwise.
85  	 */
86  	public static boolean isStdin(InOutToolOptions tool) {
87  		if (tool.getInput() == null && tool.getInputFile() == null)
88  			return true;
89  		return tool.getInput() != null && tool.getInput().equals("-");
90  	}
91  
92  	/**
93  	 * Test whether the requested output is stdout.
94  	 *
95  	 * @param tool
96  	 *            the tool from which to get settings
97  	 * @return true if output the stdout; false otherwise.
98  	 */
99  	public static boolean isStdout(InOutToolOptions tool) {
100 		if (tool.getOutput() == null)
101 			return true;
102 		return tool.getOutput().equals("-");
103 	}
104 
105 	/**
106 	 * Validate the (local) ouput from an {@link InOutToolOptions} and return
107 	 * the corresponding file.
108 	 *
109 	 * @param tool
110 	 *            the tool from which to get settings
111 	 * @return the output file location, deleted if it is allowed to be deleted
112 	 * @throws IOException
113 	 *             if the file exists, but can't be deleted
114 	 */
115 	public static File validateLocalOutput(InOutToolOptions tool) throws IOException {
116 		return validateLocalOutput(tool.output, tool.isForce(), tool.contin);
117 	}
118 
119 	/**
120 	 * Validate the (local) ouput from an String and return the corresponding
121 	 * file.
122 	 *
123 	 * @param out
124 	 *            where the file will go
125 	 * @param overwrite
126 	 *            whether to overwrite existing files
127 	 * @return the output file location, deleted if it is allowed to be deleted
128 	 * @throws IOException
129 	 *             if the file exists, but can't be deleted
130 	 */
131 	public static File validateLocalOutput(String out, boolean overwrite) throws IOException {
132 		return validateLocalOutput(out, overwrite, false);
133 	}
134 
135 	/**
136 	 * Validate the (local) ouput from an String and return the corresponding
137 	 * file.
138 	 *
139 	 * @param out
140 	 *            where the file will go
141 	 * @param overwrite
142 	 *            whether to overwrite existing files
143 	 * @param contin
144 	 *            whether an existing output should be continued (i.e. ignored
145 	 *            if it exists)
146 	 * @return the output file location, deleted if it is allowed to be deleted
147 	 * @throws IOException
148 	 *             if the file exists, but can't be deleted
149 	 */
150 	public static File validateLocalOutput(String out, boolean overwrite, boolean contin) throws IOException {
151 		if (out == null) {
152 			throw new IOException("No output specified");
153 		}
154 		File output = new File(out);
155 		if (output.exists()) {
156 			if (overwrite) {
157 				if (!FileUtils.deleteRecursive(output))
158 					throw new IOException("Couldn't delete existing output");
159 			} else if (!contin) {
160 				throw new IOException("Output already exists, didn't remove");
161 			}
162 		}
163 		return output;
164 	}
165 }