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.HashSet;
035import java.util.List;
036import java.util.Set;
037
038import org.kohsuke.args4j.Option;
039import org.openimaj.io.FileUtils;
040
041/**
042 * A file tool reads and writes files and knows whether existing outputs should
043 * be deleted
044 * 
045 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
046 * 
047 */
048public abstract class InOutToolOptions {
049
050        @Option(name = "--input", aliases = "-i", required = false, usage = "Input location", metaVar = "STRING")
051        String input = null;
052
053        @Option(name = "--output", aliases = "-o", required = false, usage = "output location", metaVar = "STRING")
054        protected String output = null;
055
056        @Option(
057                        name = "--remove-existing-output",
058                        aliases = "-rm",
059                        required = false,
060                        usage = "If existing output exists, remove it")
061        boolean force = false;
062
063        @Option(name = "--input-file", aliases = "-if", required = false, usage = "Get a set of inputs as listed in a file")
064        private String inputFile = null;
065
066        @Option(name = "--no-continue", aliases = "-nc", required = false, usage = "Do not continue an existing output")
067        boolean contin = false;
068
069        /**
070         * @return the input string option
071         */
072        public String getInput() {
073                return this.input;
074        }
075
076        /**
077         * @return the input string option
078         */
079        public String getOutput() {
080                return this.output;
081        }
082
083        /**
084         * When the input file is set, any existing input file LIST file is removed
085         * (anything from -if)
086         * 
087         * @param input
088         *            new input location
089         */
090        public void setInput(String input) {
091                this.inputFile = null;
092                this.input = input;
093        }
094
095        /**
096         * @param output
097         *            new input location
098         */
099        public void setOutput(String output) {
100                this.output = output;
101        }
102
103        /**
104         * @return the force option, whether the output should be overwritten if it
105         *         exists
106         */
107        public boolean overwriteOutput() {
108                return this.isForce();
109        }
110
111        /**
112         * Fixes a problem with args4j with multivalued arguments being preserved
113         * within the same JVM
114         * 
115         * @param <T>
116         * @param modeOptions
117         * @param defaults
118         *            optional default values if the modeoptions is empty
119         */
120        public static <T> void prepareMultivaluedArgument(List<T> modeOptions, T... defaults) {
121                final Set<T> modes = new HashSet<T>();
122                for (final T mode : modeOptions) {
123                        modes.add(mode);
124                }
125                modeOptions.clear();
126                modeOptions.addAll(modes);
127                if (modeOptions.isEmpty()) {
128                        for (final T t : defaults) {
129                                modeOptions.add(t);
130                        }
131                }
132        }
133
134        /**
135         * @return should files be forcefully removed
136         */
137        public boolean isForce() {
138                return force;
139        }
140
141        /**
142         * @return should existing files be continued from
143         */
144        public boolean isContinue() {
145                return this.contin;
146        }
147
148        /**
149         * @return all the inputs from the -if options file, or the single input if
150         *         -i was not defined
151         */
152        public String[] getAllInputs() {
153                boolean multifiles = this.getInputFile() != null;
154                File inputFileF = null;
155                if (multifiles) {
156                        inputFileF = new File(this.getInputFile());
157                        multifiles = inputFileF.exists() && inputFileF.canRead();
158                }
159                if (!multifiles) {
160                        if (this.input == null)
161                                return null;
162                        return new String[] { this.input };
163                } else {
164                        try {
165                                final String[] lines = FileUtils.readlines(inputFileF);
166                                return lines;
167                        } catch (final IOException e) {
168                                if (this.input == null)
169                                        return null;
170                                return new String[] { this.input };
171                        }
172                }
173        }
174
175        /**
176         * @return the input list file, each line is an input
177         */
178        public String getInputFile() {
179                return inputFile;
180        }
181
182        /**
183         * @param inputFile
184         *            the new input file
185         */
186        public void setInputFile(String inputFile) {
187                this.inputFile = inputFile;
188        }
189}