001/**
002 * Copyright (c) 2011, 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.kohsuke.args4j.Argument;
038import org.kohsuke.args4j.CmdLineException;
039import org.kohsuke.args4j.Option;
040import org.openimaj.aop.classloader.ClassLoaderTransform;
041
042/**
043 * Options for the References Tool
044 * 
045 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
046 */
047public class ReferencesToolOpts {
048        @Option(
049                        name = "--writeBibtex",
050                        aliases = "-wb",
051                        usage = "Write BibTeX formatted references to a file.",
052                        required = false)
053        File bibtexFile;
054
055        @Option(
056                        name = "--printBibtex",
057                        aliases = "-pb",
058                        usage = "Print BibTeX formatted references to STDOUT.",
059                        required = false)
060        boolean printBibtex = false;
061
062        @Option(
063                        name = "--writeText",
064                        aliases = "-wt",
065                        usage = "Write text formatted references to a file.",
066                        required = false)
067        File textFile;
068
069        @Option(
070                        name = "--printText",
071                        aliases = "-pt",
072                        usage = "Print text formatted references to STDOUT.",
073                        required = false)
074        boolean printText = false;
075
076        @Option(
077                        name = "--writeHTML",
078                        aliases = "-wh",
079                        usage = "Write HTML formatted references to a file.",
080                        required = false)
081        File htmlFile;
082
083        @Option(
084                        name = "--printHTML",
085                        aliases = "-ph",
086                        usage = "Print HTML formatted references to STDOUT.",
087                        required = false)
088        boolean printHTML = false;
089
090        @Option(
091                        name = "-jar",
092                        usage = "Runnable jar containing the application to run",
093                        required = false)
094        File jarFile;
095
096        @Option(
097                        name = "-classpath",
098                        aliases = "-cp",
099                        usage = "Additional classpath string (separated by a colon on OSX/Linux [:] or semicolon [;] on windows)",
100                        required = false)
101        String classpath;
102
103        @Argument(required = false)
104        List<String> arguments = new ArrayList<String>();
105
106        String mainMethod;
107
108        protected ExtendedCmdLineParser parser;
109
110        /**
111         * Load the options from the arguments string
112         * 
113         * @param args
114         *            the arguments string
115         * 
116         * @throws CmdLineException
117         *             if an error occurs
118         */
119        public ReferencesToolOpts(String[] args) throws CmdLineException {
120                parser = new ExtendedCmdLineParser(this);
121                parser.parseArgument(args);
122        }
123
124        /**
125         * Validate the options, throwing a {@link CmdLineException} if the
126         * arguments are not valid.
127         * 
128         * @throws CmdLineException
129         */
130        public void validate() throws CmdLineException {
131                if (this.jarFile != null && this.classpath != null) {
132                        throw new CmdLineException(null, "-jar and -cp are independent and cannot both be set");
133                }
134
135                try {
136                        if (this.jarFile != null && ClassLoaderTransform.getMainClass(this.jarFile) == null) {
137                                throw new CmdLineException(null, "Failed to load Main-Class manifest attribute from " + jarFile);
138                        }
139                } catch (final IOException e) {
140                        throw new CmdLineException(null, "Unable to read jar file " + this.jarFile, e);
141                }
142
143                if (this.jarFile == null && this.classpath == null) {
144                        this.classpath = ".";
145                }
146
147                if (this.classpath != null) {
148                        if (this.arguments == null || this.arguments.size() < 1)
149                                throw new CmdLineException(null, "A main class must be specified.");
150
151                        this.mainMethod = this.arguments.remove(0);
152                }
153        }
154
155        /**
156         * @return true if jar mode; false if classpath mode
157         */
158        public boolean isJar() {
159                return this.jarFile != null;
160        }
161}