001/**
002 * Copyright (c) 2012, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.tools;
031
032import java.io.File;
033import java.io.IOException;
034import java.util.ArrayList;
035import java.util.List;
036
037import org.openimaj.io.FileUtils;
038
039/**
040 * Tools for dealing with #InOutTool instances that are local file
041 *
042 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
043 *
044 */
045public class FileToolsUtil {
046
047        /**
048         * Validate the (local) input from an {@link InOutToolOptions} and return
049         * the corresponding file.
050         *
051         * @param tool
052         *            the tool from which to get settings
053         * @return a none null input file location if it exists
054         * @throws IOException
055         *             if the file doesn't exist
056         */
057        public static List<File> validateLocalInput(InOutToolOptions tool) throws IOException {
058                if (tool.input == null && tool.getInputFile() == null) {
059                        throw new IOException("No valid input provided");
060                }
061                List<File> toret = null;
062                toret = new ArrayList<File>();
063                if (tool.input != null) {
064                        File f = new File(tool.input);
065                        if (!f.exists())
066                                throw new IOException("Couldn't file file");
067                        toret.add(f);
068                } else {
069                        String[] allinputs = tool.getAllInputs();
070                        for (String floc : allinputs) {
071                                File f = new File(floc);
072                                if (f.exists())
073                                        toret.add(f);
074                        }
075                }
076                return toret;
077
078        }
079
080        /**
081         * Test whether the input is the stdin
082         *
083         * @param tool
084         * @return true if input should be stdin, false otherwise.
085         */
086        public static boolean isStdin(InOutToolOptions tool) {
087                if (tool.getInput() == null && tool.getInputFile() == null)
088                        return true;
089                return tool.getInput() != null && tool.getInput().equals("-");
090        }
091
092        /**
093         * Test whether the requested output is stdout.
094         *
095         * @param tool
096         *            the tool from which to get settings
097         * @return true if output the stdout; false otherwise.
098         */
099        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}