You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oodt.apache.org by bf...@apache.org on 2011/10/10 10:07:09 UTC
svn commit: r1180819 [1/2] - in /oodt/branches/cas-cl: ./ src/ src/main/
src/main/java/ src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/oodt/ src/main/java/org/apache/oodt/cas/
src/main/java/org/apache/oodt/cas/cl/ src/main/java/o...
Author: bfoster
Date: Mon Oct 10 08:07:07 2011
New Revision: 1180819
URL: http://svn.apache.org/viewvc?rev=1180819&view=rev
Log:
Initial check for factoring out CAS Command Line interface
- will include speed improvements over existing version in oodt-commons
- will include support for grouping command line options
- will include support for writing your own command line help print-out look/format
- will include factoring out of Spring based usages such that they are still highly supported, but also optional
Added:
oodt/branches/cas-cl/pom.xml (with props)
oodt/branches/cas-cl/src/
oodt/branches/cas-cl/src/main/
oodt/branches/cas-cl/src/main/java/
oodt/branches/cas-cl/src/main/java/org/
oodt/branches/cas-cl/src/main/java/org/apache/
oodt/branches/cas-cl/src/main/java/org/apache/oodt/
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineOptionParser.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/OptionHelpException.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/formatter/
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/formatter/CmdLineOptionHelpFormatter.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/formatter/StdCmdLineOptionHelpFormatter.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/CmdLineOptionHelpPresenter.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/StdCmdLineOptionHelpPresenter.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionHelpPrinter.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionSpecificHelpPrinter.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionHelpPrinter.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionSpecificHelpPrinter.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineAction.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineOption.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineOptionInstance.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/RequirementRule.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/BeanInfo.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionHandler.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/StdCmdLineOptionHandler.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/CmdLineOptionStore.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/CmdLineOptionStoreFactory.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringCmdLineOptionStore.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringCmdLineOptionStoreFactory.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringSetContextInjectionType.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/AllowedArgsCmdLineOptionValidator.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/ArgRegExpCmdLineOptionValidator.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/ClassExistsCmdLineOptionValidator.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/CmdLineOptionValidator.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/FileExistCmdLineOptionValidator.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/NoRestrictionsCmdLineOptionValidator.java (with props)
Added: oodt/branches/cas-cl/pom.xml
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/pom.xml?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/pom.xml (added)
+++ oodt/branches/cas-cl/pom.xml Mon Oct 10 08:07:07 2011
@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
+ license agreements. See the NOTICE file distributed with this work for additional
+ information regarding copyright ownership. The ASF licenses this file to
+ You under the Apache License, Version 2.0 (the "License"); you may not use
+ this file except in compliance with the License. You may obtain a copy of
+ the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
+ by applicable law or agreed to in writing, software distributed under the
+ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
+ OF ANY KIND, either express or implied. See the License for the specific
+ language governing permissions and limitations under the License. -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.oodt</groupId>
+ <artifactId>oodt-core</artifactId>
+ <version>0.3-SNAPSHOT</version>
+ <relativePath>../core/pom.xml</relativePath>
+ </parent>
+ <artifactId>cas-cl</artifactId>
+ <name>CAS Command Line</name>
+ <profiles>
+ <profile>
+ <id>audit</id>
+ <activation>
+ <activeByDefault>false</activeByDefault>
+ </activation>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>rat-maven-plugin</artifactId>
+ <configuration>
+ <excludes>
+ <exclude>**/resources/examples/**/*</exclude>
+ </excludes>
+ </configuration>
+ <executions>
+ <execution>
+ <phase>verify</phase>
+ <goals>
+ <goal>check</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ </profiles>
+ <dependencies>
+<!-- <dependency>
+ <groupId>org.apache.oodt</groupId>
+ <artifactId>oodt-commons</artifactId>
+ <version>${project.parent.version}</version>
+ </dependency> -->
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.3</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-core</artifactId>
+ <version>2.5.4</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-hibernate3</artifactId>
+ <version>2.0.8</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.2</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
Propchange: oodt/branches/cas-cl/pom.xml
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineOptionParser.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineOptionParser.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineOptionParser.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineOptionParser.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.oodt.cas.cl;
+
+//JDK imports
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+//OODT imports
+import org.apache.oodt.cas.cl.help.OptionHelpException;
+import org.apache.oodt.cas.cl.help.presenter.CmdLineOptionHelpPresenter;
+import org.apache.oodt.cas.cl.help.presenter.StdCmdLineOptionHelpPresenter;
+import org.apache.oodt.cas.cl.help.formatter.CmdLineOptionHelpFormatter;
+import org.apache.oodt.cas.cl.help.formatter.StdCmdLineOptionHelpFormatter;
+import org.apache.oodt.cas.cl.help.printer.CmdLineOptionHelpPrinter;
+import org.apache.oodt.cas.cl.help.printer.CmdLineOptionSpecificHelpPrinter;
+import org.apache.oodt.cas.cl.help.printer.StdCmdLineOptionHelpPrinter;
+import org.apache.oodt.cas.cl.help.printer.StdCmdLineOptionSpecificHelpPrinter;
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+import org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils;
+
+/**
+ * @author bfoster
+ * @version $Revision$
+ */
+public class CmdLineOptionParser {
+
+ private Set<CmdLineOption> validOptions;
+ private CmdLineOptionHelpPrinter helpPrinter;
+ private CmdLineOptionSpecificHelpPrinter specificHelpPrinter;
+ private CmdLineOptionHelpFormatter helpFormatter;
+ private CmdLineOptionHelpPresenter helpPresenter;
+
+ public CmdLineOptionParser() {
+ validOptions = new HashSet<CmdLineOption>();
+ helpPrinter = new StdCmdLineOptionHelpPrinter();
+ specificHelpPrinter = new StdCmdLineOptionSpecificHelpPrinter();
+ helpFormatter = new StdCmdLineOptionHelpFormatter();
+ helpPresenter = new StdCmdLineOptionHelpPresenter();
+ }
+
+ public Set<CmdLineOption> getValidOptions() {
+ return validOptions;
+ }
+
+ public void setValidOptions(Set<CmdLineOption> validOptions) {
+ this.validOptions = validOptions;
+ }
+
+ public CmdLineOptionHelpPrinter getHelpPrinter() {
+ return helpPrinter;
+ }
+
+ public void setHelpPrinter(CmdLineOptionHelpPrinter helpPrinter) {
+ this.helpPrinter = helpPrinter;
+ }
+
+ public CmdLineOptionSpecificHelpPrinter getSpecificHelpPrinter() {
+ return specificHelpPrinter;
+ }
+
+ public void setSpecificHelpPrinter(
+ CmdLineOptionSpecificHelpPrinter specificHelpPrinter) {
+ this.specificHelpPrinter = specificHelpPrinter;
+ }
+
+ public CmdLineOptionHelpFormatter getHelpFormatter() {
+ return helpFormatter;
+ }
+
+ public void setHelpFormatter(CmdLineOptionHelpFormatter helpFormatter) {
+ this.helpFormatter = helpFormatter;
+ }
+
+ public CmdLineOptionHelpPresenter getHelpPresenter() {
+ return helpPresenter;
+ }
+
+ public void setHelpPresenter(CmdLineOptionHelpPresenter helpPresenter) {
+ this.helpPresenter = helpPresenter;
+ }
+
+ public Set<CmdLineOptionInstance> parse(String[] args) throws IOException {
+ HashSet<CmdLineOptionInstance> optionInstances = new HashSet<CmdLineOptionInstance>();
+ if (args.length < 1)
+ throw new OptionHelpException(
+ "Must specify options : type -h or --help for info");
+
+ for (int j = 0; j < args.length; j++) {
+ String curArg = args[j];
+
+ // print usage and quit if -h or --help is given
+ if (curArg.equals("-h") || curArg.equals("--help")) {
+ if (args.length > j + 1) {
+ String[] helpArgs = new String[args.length - j - 1];
+ System.arraycopy(args, j + 1, helpArgs, 0, helpArgs.length);
+ helpPresenter.presentSpecificHelp(helpFormatter.format(
+ specificHelpPrinter, validOptions, parse(helpArgs).iterator()
+ .next()));
+ } else {
+ helpPresenter.presentHelp(helpFormatter.format(helpPrinter,
+ validOptions));
+ }
+ System.exit(0);
+ }
+
+ if (curArg.startsWith("-")) {
+ // check if long or short version was used
+ if (curArg.startsWith("--"))
+ curArg = curArg.substring(2);
+ else
+ curArg = curArg.substring(1);
+
+ // check if option is a valid one
+ CmdLineOption curOption = CmdLineOptionUtils.getOptionByName(curArg,
+ validOptions);
+ if (curOption == null)
+ throw new IOException("Invalid option '" + curArg + "'");
+
+ // check if option has arguments
+ List<String> values = new LinkedList<String>();
+ if (curOption.hasArgs()) {
+ while (j + 1 < args.length && !args[j + 1].startsWith("-"))
+ values.add(args[++j]);
+ if (values.size() < 1)
+ throw new IOException("Option " + curArg
+ + " should have at least one argument");
+ }
+
+ // check if is a perform and quit option
+ if (curOption.isPerformAndQuit()) {
+ curOption.getHandler().handleOption(curOption, values);
+ System.exit(0);
+ }
+
+ // add to list of option instances
+ optionInstances.add(new CmdLineOptionInstance(curOption, values));
+
+ } else {
+ throw new IOException("Argument with no option flag '" + curArg + "'");
+ }
+ }
+ return optionInstances;
+ }
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineOptionParser.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineOptionParser.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/OptionHelpException.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/OptionHelpException.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/OptionHelpException.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/OptionHelpException.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.oodt.cas.cl.help;
+
+//JDK imports
+import java.io.IOException;
+
+/**
+ *
+ * @author bfoster
+ * Exception thrown to express that -h should be given to see help
+ *
+ */
+public class OptionHelpException extends IOException {
+
+ private static final long serialVersionUID = -8198106641155733222L;
+
+ public OptionHelpException() {
+ super();
+ }
+
+ public OptionHelpException(String message) {
+ super(message);
+ }
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/OptionHelpException.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/formatter/CmdLineOptionHelpFormatter.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/formatter/CmdLineOptionHelpFormatter.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/formatter/CmdLineOptionHelpFormatter.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/formatter/CmdLineOptionHelpFormatter.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,18 @@
+package org.apache.oodt.cas.cl.help.formatter;
+
+import java.util.Set;
+
+import org.apache.oodt.cas.cl.help.printer.CmdLineOptionHelpPrinter;
+import org.apache.oodt.cas.cl.help.printer.CmdLineOptionSpecificHelpPrinter;
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+
+public interface CmdLineOptionHelpFormatter {
+
+ public String format(CmdLineOptionHelpPrinter helpPrinter,
+ Set<CmdLineOption> options);
+
+ public String format(CmdLineOptionSpecificHelpPrinter specificHelpPrinter,
+ Set<CmdLineOption> options, CmdLineOptionInstance specifiedOption);
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/formatter/CmdLineOptionHelpFormatter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/formatter/StdCmdLineOptionHelpFormatter.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/formatter/StdCmdLineOptionHelpFormatter.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/formatter/StdCmdLineOptionHelpFormatter.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/formatter/StdCmdLineOptionHelpFormatter.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,54 @@
+package org.apache.oodt.cas.cl.help.formatter;
+
+import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.getAlwaysRequiredOptions;
+import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.getConditionallyRequiredOptions;
+import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.getOptionalOptions;
+import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.sortOptionsByRequiredStatus;
+
+import java.util.List;
+import java.util.Set;
+
+import org.apache.oodt.cas.cl.help.printer.CmdLineOptionHelpPrinter;
+import org.apache.oodt.cas.cl.help.printer.CmdLineOptionSpecificHelpPrinter;
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+
+
+public class StdCmdLineOptionHelpFormatter implements CmdLineOptionHelpFormatter {
+
+ public String format(CmdLineOptionHelpPrinter helpPrinter,
+ Set<CmdLineOption> options) {
+ StringBuffer sb = new StringBuffer("");
+ List<CmdLineOption> sortedOptions = sortOptionsByRequiredStatus(options);
+ sb.append(helpPrinter.getHeader()).append("\n");
+ for (CmdLineOption option : sortedOptions) {
+ sb.append(helpPrinter.getOptionHelp(option)).append("\n");
+ }
+ sb.append(helpPrinter.getFooter()).append("\n");
+ return sb.toString();
+ }
+
+ public String format(CmdLineOptionSpecificHelpPrinter specificHelpPrinter,
+ Set<CmdLineOption> options, CmdLineOptionInstance specifiedOption) {
+ StringBuffer sb = new StringBuffer("");
+ sb.append(specificHelpPrinter.getHeader(specifiedOption)).append("\n");
+
+ sb.append(specificHelpPrinter.getRequiredSubHeader(specifiedOption)).append("\n");
+ Set<CmdLineOption> requiredOptions = getAlwaysRequiredOptions(options);
+ requiredOptions.addAll(getConditionallyRequiredOptions(options, specifiedOption));
+ List<CmdLineOption> sortedRequiredOptions = sortOptionsByRequiredStatus(requiredOptions);
+ for (CmdLineOption option : sortedRequiredOptions) {
+ sb.append(specificHelpPrinter.getRequiredOptionHelp(option, specifiedOption)).append("\n");
+ }
+
+ sb.append(specificHelpPrinter.getOptionalSubHeader(specifiedOption)).append("\n");
+ Set<CmdLineOption> optionalOptions = getOptionalOptions(options, specifiedOption);
+ List<CmdLineOption> sortedOptionalOptions = sortOptionsByRequiredStatus(optionalOptions);
+ for (CmdLineOption option : sortedOptionalOptions) {
+ sb.append(specificHelpPrinter.getOptionalOptionHelp(option, specifiedOption)).append("\n");
+ }
+
+ sb.append(specificHelpPrinter.getFooter(specifiedOption)).append("\n");
+ return sb.toString();
+ }
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/formatter/StdCmdLineOptionHelpFormatter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/CmdLineOptionHelpPresenter.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/CmdLineOptionHelpPresenter.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/CmdLineOptionHelpPresenter.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/CmdLineOptionHelpPresenter.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,9 @@
+package org.apache.oodt.cas.cl.help.presenter;
+
+public interface CmdLineOptionHelpPresenter {
+
+ public void presentHelp(String helpMessage);
+
+ public void presentSpecificHelp(String specificHelpMessage);
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/CmdLineOptionHelpPresenter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/StdCmdLineOptionHelpPresenter.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/StdCmdLineOptionHelpPresenter.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/StdCmdLineOptionHelpPresenter.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/StdCmdLineOptionHelpPresenter.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,21 @@
+package org.apache.oodt.cas.cl.help.presenter;
+
+import java.io.PrintStream;
+
+public class StdCmdLineOptionHelpPresenter implements CmdLineOptionHelpPresenter {
+
+ private PrintStream ps;
+
+ public StdCmdLineOptionHelpPresenter() {
+ ps = new PrintStream(System.out);
+ }
+
+ public void presentHelp(String helpMessage) {
+ ps.println(helpMessage);
+ }
+
+ public void presentSpecificHelp(String specificHelpMessage) {
+ ps.println(specificHelpMessage);
+ }
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/StdCmdLineOptionHelpPresenter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionHelpPrinter.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionHelpPrinter.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionHelpPrinter.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionHelpPrinter.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,13 @@
+package org.apache.oodt.cas.cl.help.printer;
+
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+
+public interface CmdLineOptionHelpPrinter {
+
+ public String getHeader();
+
+ public String getOptionHelp(CmdLineOption option);
+
+ public String getFooter();
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionHelpPrinter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionSpecificHelpPrinter.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionSpecificHelpPrinter.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionSpecificHelpPrinter.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionSpecificHelpPrinter.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,20 @@
+package org.apache.oodt.cas.cl.help.printer;
+
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+
+public interface CmdLineOptionSpecificHelpPrinter {
+
+ public String getHeader(CmdLineOptionInstance specifiedOption);
+
+ public String getRequiredSubHeader(CmdLineOptionInstance specifiedOption);
+
+ public String getRequiredOptionHelp(CmdLineOption option, CmdLineOptionInstance specifiedOption);
+
+ public String getOptionalSubHeader(CmdLineOptionInstance specifiedOption);
+
+ public String getOptionalOptionHelp(CmdLineOption option, CmdLineOptionInstance specifiedOption);
+
+ public String getFooter(CmdLineOptionInstance specifiedOption);
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionSpecificHelpPrinter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionHelpPrinter.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionHelpPrinter.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionHelpPrinter.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionHelpPrinter.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,44 @@
+package org.apache.oodt.cas.cl.help.printer;
+
+import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.getFormattedString;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+
+public class StdCmdLineOptionHelpPrinter implements CmdLineOptionHelpPrinter {
+
+ public String getHeader() {
+ StringBuffer sb = new StringBuffer("");
+ sb.append("-----------------------------------------------------------------------------------------------------------------\n");
+ sb.append("|" + StringUtils.rightPad(" Short", 7) + "|"
+ + StringUtils.rightPad(" Long", 50) + "| Description\n");
+ sb.append("-----------------------------------------------------------------------------------------------------------------\n");
+ return sb.toString();
+ }
+
+ public String getOptionHelp(CmdLineOption option) {
+ String argName = option.hasArgs() ? " <" + option.getOptionArgName() + ">" : "";
+ String optionUsage = "-"
+ + StringUtils.rightPad(option.getShortOption() + ",", 7) + "--"
+ + StringUtils.rightPad((option.getLongOption() + argName), 49)
+ + option.getDescription();
+ optionUsage += getFormattedString(option.getHandler().getCustomOptionHelp(option),
+ 62, 113);
+
+ if (option.isRequired()) {
+ optionUsage = " " + optionUsage;
+ } else if (!option.getRequirementRules().isEmpty()) {
+ optionUsage = "{" + optionUsage + "}";
+ optionUsage += "\n" + getFormattedString("RequiredOptions: "
+ + option.getRequirementRules(), 62, 113);
+ } else {
+ optionUsage = "[" + optionUsage + "]";
+ }
+
+ return optionUsage;
+ }
+
+ public String getFooter() {
+ return "-----------------------------------------------------------------------------------------------------------------";
+ }
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionHelpPrinter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionSpecificHelpPrinter.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionSpecificHelpPrinter.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionSpecificHelpPrinter.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionSpecificHelpPrinter.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,40 @@
+package org.apache.oodt.cas.cl.help.printer;
+
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+
+public class StdCmdLineOptionSpecificHelpPrinter implements CmdLineOptionSpecificHelpPrinter {
+
+ public String getHeader(CmdLineOptionInstance specifiedOption) {
+ return "Option Help when option '--"
+ + specifiedOption.getOption().getLongOption()
+ + "' is specified with values " + specifiedOption.getValues();
+ }
+
+ public String getRequiredSubHeader(CmdLineOptionInstance specifiedOption) {
+ return " - Required:";
+ }
+
+ public String getRequiredOptionHelp(CmdLineOption option, CmdLineOptionInstance specifiedOption) {
+ return getOptionHelp(option, specifiedOption);
+ }
+
+ public String getOptionalSubHeader(CmdLineOptionInstance specifiedOption) {
+ return " - Optional:";
+ }
+
+ public String getOptionalOptionHelp(CmdLineOption option, CmdLineOptionInstance specifiedOption) {
+ return getOptionHelp(option, specifiedOption);
+ }
+
+ public String getFooter(CmdLineOptionInstance specifiedOption) {
+ return "";
+ }
+
+ private String getOptionHelp(CmdLineOption option,
+ CmdLineOptionInstance specifiedOption) {
+ return " -" + option.getShortOption() + " [--"
+ + option.getLongOption() + "] "
+ + (option.hasArgs() ? "<" + option.getOptionArgName() + ">" : "");
+ }
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionSpecificHelpPrinter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineAction.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineAction.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineAction.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineAction.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,26 @@
+package org.apache.oodt.cas.cl.option;
+
+public abstract class CmdLineAction {
+
+ private String name;
+ private String description;
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public abstract void execute();
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineAction.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineOption.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineOption.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineOption.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineOption.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,189 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.oodt.cas.cl.option;
+
+//JDK imports
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+//OODT imports
+import org.apache.oodt.cas.cl.option.handler.CmdLineOptionHandler;
+import org.apache.oodt.cas.cl.option.handler.StdCmdLineOptionHandler;
+import org.apache.oodt.cas.cl.option.validator.CmdLineOptionValidator;
+
+/**
+ *
+ * @author bfoster
+ * @version $Revision$
+ */
+public class CmdLineOption implements Comparable<CmdLineOption> {
+
+ private String shortOption;
+
+ private String longOption;
+
+ private String description;
+
+ private String optionArgName;
+
+ private boolean required;
+
+ private List<RequirementRule> requirementRules;
+
+ private boolean hasArgs;
+
+ private boolean performAndQuit;
+
+ private Class<?> type;
+
+ private CmdLineOptionHandler handler;
+
+ private List<CmdLineOptionValidator> validators;
+
+ public CmdLineOption() {
+ optionArgName = "arg";
+ required = false;
+ hasArgs = false;
+ performAndQuit = false;
+ type = String.class;
+ requirementRules = new ArrayList<RequirementRule>();
+ handler = new StdCmdLineOptionHandler();
+ validators = new LinkedList<CmdLineOptionValidator>();
+ }
+
+ public CmdLineOption(String shortOption, String longOption,
+ String description, boolean hasArgs) {
+ this();
+ this.shortOption = shortOption;
+ this.longOption = longOption;
+ this.description = description;
+ this.hasArgs = hasArgs;
+ }
+
+ public Class<?> getType() {
+ return type;
+ }
+
+ public void setType(Class<?> type) {
+ this.type = type;
+ }
+
+ public void setHandler(CmdLineOptionHandler handler) {
+ this.handler = handler;
+ }
+
+ public CmdLineOptionHandler getHandler() {
+ return this.handler;
+ }
+
+ public List<CmdLineOptionValidator> getValidators() {
+ return this.validators;
+ }
+
+ public void setValidators(List<CmdLineOptionValidator> validators) {
+ this.validators = validators;
+ }
+
+ public String getShortOption() {
+ return shortOption;
+ }
+
+ public void setShortOption(String shortOption) {
+ this.shortOption = shortOption;
+ }
+
+ public String getLongOption() {
+ return longOption;
+ }
+
+ public void setLongOption(String longOption) {
+ this.longOption = longOption;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+ public boolean hasArgs() {
+ return hasArgs;
+ }
+
+ public void setHasArgs(boolean hasArgs) {
+ this.hasArgs = hasArgs;
+ }
+
+ public void setOptionArgName(String optionArgName) {
+ this.optionArgName = optionArgName;
+ }
+
+ public String getOptionArgName() {
+ return optionArgName;
+ }
+
+ public void setRequired(boolean required) {
+ this.required = required;
+ }
+
+ public boolean isRequired() {
+ return required;
+ }
+
+ public List<RequirementRule> getRequirementRules() {
+ return this.requirementRules;
+ }
+
+ public void setRequirementRules(List<RequirementRule> requirementRules) {
+ this.requirementRules = requirementRules;
+ }
+
+ public boolean isPerformAndQuit() {
+ return performAndQuit;
+ }
+
+ public void setPerformAndQuit(boolean performAndQuit) {
+ this.performAndQuit = performAndQuit;
+ }
+
+ public int compareTo(CmdLineOption cmdLineOption) {
+ int thisScore = (this.required ? 2 : 0)
+ + (!requirementRules.isEmpty() ? 1 : 0);
+ int compareScore = (cmdLineOption.required ? 2 : 0)
+ + (!cmdLineOption.requirementRules.isEmpty() ? 1 : 0);
+ return new Integer(thisScore).compareTo(compareScore);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj instanceof CmdLineOption) {
+ CmdLineOption compareObj = (CmdLineOption) obj;
+ return compareObj.shortOption.equals(this.shortOption)
+ || compareObj.longOption.equals(this.longOption);
+ } else
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return shortOption.hashCode();
+ }
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineOption.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineOption.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineOptionInstance.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineOptionInstance.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineOptionInstance.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineOptionInstance.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.oodt.cas.cl.option;
+
+//JDK imports
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ *
+ * @author bfoster
+ * @version $Revision$
+ *
+ * <p>Describe your class here</p>.
+ */
+public class CmdLineOptionInstance {
+
+ private CmdLineOption option;
+ private List<String> values;
+
+ public CmdLineOptionInstance() {
+ this.option = null;
+ this.values = new LinkedList<String>();
+ }
+
+ public CmdLineOptionInstance(CmdLineOption option, List<String> values) {
+ this.option = option;
+ this.values = values;
+ }
+
+ public CmdLineOption getOption() {
+ return option;
+ }
+
+ public void setOption(CmdLineOption option) {
+ this.option = option;
+ }
+
+ public List<String> getValues() {
+ return values;
+ }
+
+ public void setValues(List<String> values) {
+ this.values = values;
+ }
+
+ public boolean equals(Object obj) {
+ if (obj instanceof CmdLineOptionInstance) {
+ CmdLineOptionInstance compareObj = (CmdLineOptionInstance) obj;
+ return compareObj.option.equals(this.option)
+ && compareObj.values.equals(this.values);
+ } else
+ return false;
+ }
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineOptionInstance.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/CmdLineOptionInstance.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/RequirementRule.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/RequirementRule.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/RequirementRule.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/RequirementRule.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.oodt.cas.cl.option;
+
+//OODT imports
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ *
+ * @author bfoster
+ * @version $Revision$
+ *
+ * <p>Describe your class here</p>.
+ */
+public class RequirementRule {
+
+ private List<String> optionValues;
+ private String optionLongName;
+ private boolean requireAllValues;
+
+ public RequirementRule() {
+ this.optionValues = new LinkedList<String>();
+ this.requireAllValues = false;
+ }
+
+ public List<String> getOptionValues() {
+ return optionValues;
+ }
+
+ public void setOptionValues(List<String> optionValues) {
+ this.optionValues = optionValues;
+ }
+
+ public boolean isRequireAllValues() {
+ return requireAllValues;
+ }
+
+ public void setRequireAllValues(boolean requireAllValues) {
+ this.requireAllValues = requireAllValues;
+ }
+
+ public String getOptionLongName() {
+ return optionLongName;
+ }
+
+ public void setOptionLongName(String optionLongName) {
+ this.optionLongName = optionLongName;
+ }
+
+ public String toString() {
+ return (this.requireAllValues ? "All" : "At least one")
+ + " of the following values must be specified for option --"
+ + this.optionLongName + " " + this.optionValues;
+ }
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/RequirementRule.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/RequirementRule.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/BeanInfo.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/BeanInfo.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/BeanInfo.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/BeanInfo.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.cl.option.handler;
+
+/**
+ * Wraps information about how to invoke a property value into a given bean.
+ *
+ * @author bfoster
+ * @version $Revision$
+ */
+public class BeanInfo {
+
+ private String beanId;
+ private String methodName;
+
+ public String getBeanId() {
+ return beanId;
+ }
+
+ public void setBeanId(String beanId) {
+ this.beanId = beanId;
+ }
+
+ public String getMethodName() {
+ return methodName;
+ }
+
+ public void setMethodName(String methodName) {
+ this.methodName = methodName;
+ }
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/BeanInfo.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/BeanInfo.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.cl.option.handler;
+
+//JDK imports
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+//OODT imports
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+import org.apache.oodt.cas.cl.option.store.spring.SpringSetContextInjectionType;
+
+//Spring imports
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.Validate;
+import org.springframework.context.ApplicationContext;
+
+/**
+ *
+ * @author bfoster
+ * @version $Revision$
+ */
+public class CmdLineOptionBeanHandler extends CmdLineOptionHandler implements
+ SpringSetContextInjectionType {
+
+ private List<BeanInfo> applyToBeans;
+ private ApplicationContext appContext;
+
+ public void setContext(ApplicationContext appContext) {
+ this.appContext = appContext;
+ }
+
+ public ApplicationContext getContext() {
+ return appContext;
+ }
+
+ public void setApplyToBeans(List<BeanInfo> applyToBeans) {
+ this.applyToBeans = applyToBeans;
+ }
+
+ public void handleOption(CmdLineOption option, List<String> values) {
+ Validate.notNull(appContext, "Spring ApplicationContext must be set!");
+ Validate.notNull(applyToBeans, "Apply to beans must be set!");
+
+ for (BeanInfo beanInfo : applyToBeans) {
+ try {
+ Class<?> type = option.getType();
+ Object[] vals = (values.isEmpty()) ? convertToType(
+ Arrays.asList(new String[] { "true" }), type = Boolean.TYPE)
+ : convertToType(values, type);
+ Object applyToBean = appContext.getBean(beanInfo.getBeanId());
+ if (beanInfo.getMethodName() != null) {
+ applyToBean.getClass()
+ .getMethod(beanInfo.getMethodName(), type)
+ .invoke(applyToBean, vals);
+ } else {
+ applyToBean
+ .getClass()
+ .getMethod(
+ "set" + StringUtils.capitalize(option.getLongOption()), type)
+ .invoke(applyToBean, vals);
+ }
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ public String getCustomOptionHelp(CmdLineOption option) {
+ Validate.notNull(applyToBeans, "Apply to beans must be set!");
+
+ HashSet<String> affectedClasses = new HashSet<String>();
+ for (BeanInfo beanInfo : applyToBeans) {
+ affectedClasses.add(beanInfo.getBeanId());
+ }
+ return "Affects: " + affectedClasses.toString();
+ }
+
+ private Object[] convertToType(List<String> values, Class<?> type)
+ throws MalformedURLException, ClassNotFoundException {
+ if (type.equals(File.class)) {
+ List<Object> files = new LinkedList<Object>();
+ for (String value : values)
+ files.add(new File(value));
+ return files.toArray(new Object[files.size()]);
+ } else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
+ List<Object> booleans = new LinkedList<Object>();
+ for (String value : values)
+ booleans.add(value.toLowerCase().trim().equals("true"));
+ return booleans.toArray(new Object[booleans.size()]);
+ } else if (type.equals(URL.class)) {
+ List<Object> urls = new LinkedList<Object>();
+ for (String value : values)
+ urls.add(new URL(value));
+ return urls.toArray(new Object[urls.size()]);
+ } else if (type.equals(Class.class)) {
+ List<Object> classes = new LinkedList<Object>();
+ for (String value : values)
+ classes.add(Class.forName(value));
+ return classes.toArray(new Object[classes.size()]);
+ } else if (type.equals(List.class)) {
+ return new Object[] { values };
+ } else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) {
+ List<Object> ints = new LinkedList<Object>();
+ for (String value : values)
+ ints.add(new Integer(value));
+ return ints.toArray(new Object[ints.size()]);
+ } else if (type.equals(Long.class) || type.equals(Long.TYPE)) {
+ List<Object> longs = new LinkedList<Object>();
+ for (String value : values)
+ longs.add(new Long(value));
+ return longs.toArray(new Object[longs.size()]);
+ } else if (type.equals(Double.class) || type.equals(Double.TYPE)) {
+ List<Object> doubles = new LinkedList<Object>();
+ for (String value : values)
+ doubles.add(new Double(value));
+ return doubles.toArray(new Object[doubles.size()]);
+ } else if (type.equals(String.class)) {
+ StringBuffer combinedString = new StringBuffer("");
+ for (String value : values)
+ combinedString.append(value + " ");
+ return new String[] { combinedString.toString().trim() };
+ } else {
+ return values.toArray(new Object[values.size()]);
+ }
+ }
+
+ @Override
+ public boolean affectsOption(CmdLineOptionInstance optionInstance) {
+ Validate.notNull(appContext, "Spring ApplicationContext must be set!");
+
+ @SuppressWarnings("unchecked")
+ Map<String, CmdLineOptionInstance> instances = appContext.getBeansOfType(CmdLineOptionInstance.class);
+ for (Entry<String, CmdLineOptionInstance> instance : instances.entrySet()) {
+ if (instance.getValue().equals(optionInstance)) {
+ for (BeanInfo beanInfo : this.applyToBeans) {
+ if (beanInfo.getBeanId().equals(instance.getKey())) {
+ return true;
+ }
+ }
+ break;
+ }
+ }
+ return false;
+ }
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionHandler.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionHandler.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionHandler.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionHandler.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.oodt.cas.cl.option.handler;
+
+//JDK imports
+import java.util.List;
+
+//OODT imports
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+
+//Spring imports
+import org.springframework.context.ApplicationContext;
+
+/**
+ * @author bfoster
+ * @version $Revision$
+ */
+public abstract class CmdLineOptionHandler {
+
+ private ApplicationContext appContext;
+
+ public void setApplicationContext(ApplicationContext appContext) {
+ this.appContext = appContext;
+ }
+
+ public ApplicationContext getApplicationContext() {
+ return appContext;
+ }
+
+ public abstract void handleOption(CmdLineOption option, List<String> values);
+
+ public abstract String getCustomOptionHelp(CmdLineOption option);
+
+ public abstract boolean affectsOption(CmdLineOptionInstance optionInstance);
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionHandler.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/StdCmdLineOptionHandler.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/StdCmdLineOptionHandler.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/StdCmdLineOptionHandler.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/StdCmdLineOptionHandler.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.oodt.cas.cl.option.handler;
+
+//JDK imports
+import java.util.List;
+
+//OODT imports
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+
+/**
+ * @author bfoster
+ * @version $Revision$
+ */
+public class StdCmdLineOptionHandler extends CmdLineOptionHandler {
+
+ @Override
+ public String getCustomOptionHelp(CmdLineOption option) {
+ return "";
+ }
+
+ @Override
+ public void handleOption(CmdLineOption option, List<String> values) {
+ // do nothing
+ }
+
+ @Override
+ public boolean affectsOption(CmdLineOptionInstance optionInstance) {
+ return false;
+ }
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/StdCmdLineOptionHandler.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/StdCmdLineOptionHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/CmdLineOptionStore.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/CmdLineOptionStore.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/CmdLineOptionStore.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/CmdLineOptionStore.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,13 @@
+package org.apache.oodt.cas.cl.option.store;
+
+import java.util.Set;
+
+import org.apache.oodt.cas.cl.option.CmdLineAction;
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+
+public interface CmdLineOptionStore {
+
+ public Set<CmdLineOption> loadSupportedOptions();
+
+ public Set<CmdLineAction> loadSupportedActions();
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/CmdLineOptionStore.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/CmdLineOptionStoreFactory.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/CmdLineOptionStoreFactory.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/CmdLineOptionStoreFactory.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/CmdLineOptionStoreFactory.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,7 @@
+package org.apache.oodt.cas.cl.option.store;
+
+public interface CmdLineOptionStoreFactory {
+
+ public CmdLineOptionStore createStore();
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/CmdLineOptionStoreFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringCmdLineOptionStore.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringCmdLineOptionStore.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringCmdLineOptionStore.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringCmdLineOptionStore.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,45 @@
+package org.apache.oodt.cas.cl.option.store.spring;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.oodt.cas.cl.option.CmdLineAction;
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.option.store.CmdLineOptionStore;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.FileSystemXmlApplicationContext;
+
+public class SpringCmdLineOptionStore implements CmdLineOptionStore {
+
+ private ApplicationContext appContext;
+
+ public SpringCmdLineOptionStore(String springConfig) {
+ appContext = new FileSystemXmlApplicationContext(springConfig);
+ handleSpringSetContextInjectionType();
+ }
+
+ private void handleSpringSetContextInjectionType() {
+ @SuppressWarnings("unchecked")
+ Map<String, SpringSetContextInjectionType> beans = appContext
+ .getBeansOfType(SpringSetContextInjectionType.class);
+ for (SpringSetContextInjectionType bean : beans.values()) {
+ bean.setContext(appContext);
+ }
+ }
+
+ public Set<CmdLineOption> loadSupportedOptions() {
+ @SuppressWarnings("unchecked")
+ Map<String, CmdLineOption> optionsMap = appContext
+ .getBeansOfType(CmdLineOption.class);
+ return new HashSet<CmdLineOption>(optionsMap.values());
+ }
+
+ public Set<CmdLineAction> loadSupportedActions() {
+ @SuppressWarnings("unchecked")
+ Map<String, CmdLineAction> actionsMap = appContext
+ .getBeansOfType(CmdLineOption.class);
+ return new HashSet<CmdLineAction>(actionsMap.values());
+ }
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringCmdLineOptionStore.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringCmdLineOptionStoreFactory.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringCmdLineOptionStoreFactory.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringCmdLineOptionStoreFactory.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringCmdLineOptionStoreFactory.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,21 @@
+package org.apache.oodt.cas.cl.option.store.spring;
+
+import org.apache.oodt.cas.cl.option.store.CmdLineOptionStore;
+import org.apache.oodt.cas.cl.option.store.CmdLineOptionStoreFactory;
+
+public class SpringCmdLineOptionStoreFactory implements CmdLineOptionStoreFactory {
+
+ private String config;
+
+ public void setConfig(String config) {
+ this.config = config;
+ }
+
+ public CmdLineOptionStore createStore() {
+ if (config != null) {
+ return new SpringCmdLineOptionStore(config);
+ } else {
+ return null;
+ }
+ }
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringCmdLineOptionStoreFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringSetContextInjectionType.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringSetContextInjectionType.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringSetContextInjectionType.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringSetContextInjectionType.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,10 @@
+package org.apache.oodt.cas.cl.option.store.spring;
+
+import org.springframework.context.ApplicationContext;
+
+public interface SpringSetContextInjectionType {
+
+ public void setContext(ApplicationContext appContext);
+
+ public ApplicationContext getContext();
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/store/spring/SpringSetContextInjectionType.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,265 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.oodt.cas.cl.option.util;
+
+//JDK imports
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+//OODT imports
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.Validate;
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+import org.apache.oodt.cas.cl.option.RequirementRule;
+import org.apache.oodt.cas.cl.help.presenter.StdCmdLineOptionHelpPresenter;
+import org.apache.oodt.cas.cl.help.formatter.StdCmdLineOptionHelpFormatter;
+import org.apache.oodt.cas.cl.help.printer.StdCmdLineOptionHelpPrinter;
+import org.apache.oodt.cas.cl.help.printer.StdCmdLineOptionSpecificHelpPrinter;
+import org.apache.oodt.cas.cl.CmdLineOptionParser;
+import org.apache.oodt.cas.cl.option.validator.CmdLineOptionValidator;
+
+/**
+ * @author bfoster
+ * @version $Revision$
+ */
+public class CmdLineOptionUtils {
+
+ private CmdLineOptionUtils() {}
+
+ public static Set<CmdLineOption> getOptionalOptions(
+ Set<CmdLineOption> options, CmdLineOptionInstance specifiedOption) {
+ HashSet<CmdLineOption> optionalOptions = new HashSet<CmdLineOption>();
+ for (CmdLineOption option : options) {
+ if (isOptionalOption(option, specifiedOption)) {
+ optionalOptions.add(option);
+ }
+ }
+ return optionalOptions;
+ }
+
+ public static boolean isOptionalOption(CmdLineOption option,
+ CmdLineOptionInstance specifiedOption) {
+ return !isConditionallyRequired(option, specifiedOption)
+ && option.getHandler().affectsOption(specifiedOption);
+ }
+
+ public static Set<CmdLineOption> getConditionallyRequiredOptions(
+ Set<CmdLineOption> options, CmdLineOptionInstance specifiedOption) {
+ HashSet<CmdLineOption> requiredOptions = new HashSet<CmdLineOption>();
+ for (CmdLineOption option : options) {
+ if (isConditionallyRequired(option, specifiedOption)) {
+ requiredOptions.add(option);
+ }
+ }
+ return requiredOptions;
+ }
+
+ public static Set<CmdLineOption> getConditionallyRequiredOptions(
+ Set<CmdLineOption> options, Set<CmdLineOptionInstance> specifiedOptions) {
+ HashSet<CmdLineOption> requiredOptions = new HashSet<CmdLineOption>();
+ for (CmdLineOption option : options) {
+ for (CmdLineOptionInstance specifiedOption : specifiedOptions) {
+ if (isConditionallyRequired(option, specifiedOption)) {
+ requiredOptions.add(option);
+ break;
+ }
+ }
+ }
+ return requiredOptions;
+ }
+
+ public static boolean isConditionallyRequired(CmdLineOption option,
+ CmdLineOptionInstance specifiedOption) {
+ Validate.notNull(option);
+ Validate.notNull(specifiedOption);
+
+ for (RequirementRule requirementRule : option.getRequirementRules()) {
+ if (((requirementRule.isRequireAllValues() && specifiedOption.getValues()
+ .containsAll(requirementRule.getOptionValues())) || (!requirementRule
+ .isRequireAllValues() && !Collections.disjoint(
+ specifiedOption.getValues(), requirementRule.getOptionValues())))) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static Set<CmdLineOption> getAlwaysRequiredOptions(
+ Set<CmdLineOption> options) {
+ HashSet<CmdLineOption> requiredOptions = new HashSet<CmdLineOption>();
+ for (CmdLineOption option : options) {
+ if (option.isRequired()) {
+ requiredOptions.add(option);
+ }
+ }
+ return requiredOptions;
+ }
+
+ public static Set<CmdLineOption> getRequiredOptions(
+ Set<CmdLineOption> supportedOptions,
+ Set<CmdLineOptionInstance> specifiedOptions) {
+ Set<CmdLineOption> reqOptions = getAlwaysRequiredOptions(supportedOptions);
+ reqOptions.addAll(getConditionallyRequiredOptions(supportedOptions,
+ specifiedOptions));
+ return reqOptions;
+ }
+
+ public static Set<CmdLineOption> getRequiredOptionsNotSet(
+ Set<CmdLineOption> supportedOptions, Set<CmdLineOptionInstance> setOptions) {
+ return getOptionsNotSet(getRequiredOptions(supportedOptions, setOptions),
+ setOptions);
+ }
+
+ public static Set<CmdLineOption> getOptionsNotSet(
+ Set<CmdLineOption> expectedOptions, Set<CmdLineOptionInstance> setOptions) {
+ HashSet<CmdLineOption> nonSetRequiredOptions = new HashSet<CmdLineOption>();
+ TOP:
+ for (CmdLineOption reqOption : expectedOptions) {
+ for (CmdLineOptionInstance optionInst : setOptions) {
+ if (reqOption.equals(optionInst.getOption()))
+ continue TOP;
+ }
+ nonSetRequiredOptions.add(reqOption);
+ }
+ return nonSetRequiredOptions;
+ }
+
+ public static List<CmdLineOption> sortOptionsByRequiredStatus(Set<CmdLineOption> options) {
+ ArrayList<CmdLineOption> optionsList = new ArrayList<CmdLineOption>(options);
+ Collections.sort(optionsList);
+ Collections.reverse(optionsList);
+ return optionsList;
+ }
+
+ public static CmdLineOption getOptionByName(String optionName,
+ Set<CmdLineOption> options) {
+ for (CmdLineOption option : options)
+ if (option.getLongOption().equals(optionName)
+ || option.getShortOption().equals(optionName))
+ return option;
+ return null;
+ }
+
+ public static CmdLineOptionInstance getOptionInstanceByName(
+ String optionName, Set<CmdLineOptionInstance> optionInsts) {
+ for (CmdLineOptionInstance optionInst : optionInsts)
+ if (optionInst.getOption().getLongOption().equals(optionName)
+ || optionInst.getOption().getShortOption().equals(optionName))
+ return optionInst;
+ return null;
+ }
+
+ public static List<String> getOptionValues(String optionName,
+ Set<CmdLineOptionInstance> options) {
+ for (CmdLineOptionInstance optionInst : options)
+ if (optionInst.getOption().getLongOption().equals(optionName)
+ || optionInst.getOption().getShortOption().equals(optionName))
+ return optionInst.getValues();
+ return null;
+ }
+
+
+ public static Set<CmdLineOptionInstance> parseArgs(Set<CmdLineOption> supportedOptions, String[] args) throws IOException {
+ CmdLineOptionParser parser = new CmdLineOptionParser();
+ parser.setValidOptions(supportedOptions);
+ return parser.parse(args);
+ }
+
+ public static Set<CmdLineOptionInstance> validateOptions(Set<CmdLineOptionInstance> options) throws IOException {
+ Validate.notNull(options);
+
+ HashSet<CmdLineOptionInstance> optionsFailed = new HashSet<CmdLineOptionInstance>();
+ for (CmdLineOptionInstance optionInst : options) {
+ for (CmdLineOptionValidator validator : optionInst.getOption()
+ .getValidators())
+ if (!validator.validate(optionInst)) {
+ optionsFailed.add(optionInst);
+ }
+ }
+ return optionsFailed;
+ }
+
+ public static void handleOptions(Set<CmdLineOptionInstance> options) {
+ for (CmdLineOptionInstance option : options) {
+ option.getOption().getHandler()
+ .handleOption(option.getOption(), option.getValues());
+ }
+ }
+
+ public static Set<CmdLineOptionInstance> validateAndHandleInstances(
+ Set<CmdLineOption> supportedOptions, String[] args) throws IOException {
+
+ // parse args
+ Set<CmdLineOptionInstance> setOptions = parseArgs(supportedOptions, args);
+
+ // check that required args have been specified
+ Set<CmdLineOption> unsetReqOptions = getRequiredOptionsNotSet(supportedOptions, setOptions);
+ if (!unsetReqOptions.isEmpty()) {
+ throw new IOException("Miss required options: " + sortOptionsByRequiredStatus(unsetReqOptions));
+ }
+
+ // validate options
+ Set<CmdLineOptionInstance> optionsFailedVal = validateOptions(setOptions);
+ if (!optionsFailedVal.isEmpty()) {
+ throw new IOException("Options failed validations: " + optionsFailedVal);
+ }
+
+ // if all looks good . . . handle options
+ handleOptions(setOptions);
+
+ return setOptions;
+ }
+
+ public static void printHelp(Set<CmdLineOption> options) {
+ new StdCmdLineOptionHelpPresenter()
+ .presentHelp(new StdCmdLineOptionHelpFormatter().format(
+ new StdCmdLineOptionHelpPrinter(), options));
+ }
+
+ public static void printSpecificHelp(Set<CmdLineOption> options,
+ CmdLineOptionInstance specifiedOption) {
+ new StdCmdLineOptionHelpPresenter()
+ .presentSpecificHelp(new StdCmdLineOptionHelpFormatter()
+ .format(new StdCmdLineOptionSpecificHelpPrinter(), options,
+ specifiedOption));
+ }
+
+ public static String getFormattedString(String string, int startIndex,
+ int endIndex) {
+ StringBuffer outputString = new StringBuffer("");
+ String[] splitStrings = StringUtils.split(string, " ");
+ StringBuffer curLine = null;
+ for (int i = 0; i < splitStrings.length; i++) {
+ curLine = new StringBuffer("");
+ curLine.append(splitStrings[i] + " ");
+
+ for (; i + 1 < splitStrings.length
+ && curLine.length() + splitStrings[i + 1].length() <= (endIndex - startIndex); i++)
+ curLine.append(splitStrings[i + 1] + " ");
+
+ outputString.append(StringUtils.repeat(" ", startIndex)
+ + curLine.toString() + "\n");
+ }
+ return outputString.toString();
+ }
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/AllowedArgsCmdLineOptionValidator.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/AllowedArgsCmdLineOptionValidator.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/AllowedArgsCmdLineOptionValidator.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/AllowedArgsCmdLineOptionValidator.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.oodt.cas.cl.option.validator;
+
+//JDK imports
+import java.util.LinkedList;
+import java.util.List;
+
+//OODT imports
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+
+/**
+ * @author bfoster
+ * @version $Revision$
+ */
+public class AllowedArgsCmdLineOptionValidator implements
+ CmdLineOptionValidator {
+
+ private List<String> allowedArgs;
+
+ public AllowedArgsCmdLineOptionValidator() {
+ this.allowedArgs = new LinkedList<String>();
+ }
+
+ public boolean validate(CmdLineOptionInstance optionInst) {
+ for (String value : optionInst.getValues()) {
+ if (!allowedArgs.contains(value)) {
+ LOG.severe("Option value " + value + " is not allowed for option "
+ + optionInst.getOption().getLongOption() + " - Allowed values = "
+ + this.getAllowedArgs());
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public List<String> getAllowedArgs() {
+ return allowedArgs;
+ }
+
+ public void setAllowedArgs(List<String> allowedArgs) {
+ this.allowedArgs = allowedArgs;
+ }
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/AllowedArgsCmdLineOptionValidator.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/AllowedArgsCmdLineOptionValidator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/ArgRegExpCmdLineOptionValidator.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/ArgRegExpCmdLineOptionValidator.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/ArgRegExpCmdLineOptionValidator.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/ArgRegExpCmdLineOptionValidator.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.oodt.cas.cl.option.validator;
+
+//JDK imports
+import java.util.regex.Pattern;
+
+//OODT imports
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+
+/**
+ * @author bfoster
+ * @version $Revision$
+ */
+public class ArgRegExpCmdLineOptionValidator extends
+ AllowedArgsCmdLineOptionValidator {
+
+ @Override
+ public boolean validate(CmdLineOptionInstance optionInst) {
+ TOP: for (String value : optionInst.getValues()) {
+ for (String regex : this.getAllowedArgs()) {
+ if (Pattern.matches(regex, value))
+ continue TOP;
+ }
+ LOG.severe("Option value " + value + " is not allowed for option "
+ + optionInst.getOption().getLongOption() + " - Allowed values = "
+ + this.getAllowedArgs());
+ return false;
+ }
+ return true;
+ }
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/ArgRegExpCmdLineOptionValidator.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/ArgRegExpCmdLineOptionValidator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/ClassExistsCmdLineOptionValidator.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/ClassExistsCmdLineOptionValidator.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/ClassExistsCmdLineOptionValidator.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/ClassExistsCmdLineOptionValidator.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.oodt.cas.cl.option.validator;
+
+//OODT imports
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+
+/**
+ * @author bfoster
+ * @version $Revision$
+ */
+public class ClassExistsCmdLineOptionValidator implements
+ CmdLineOptionValidator {
+
+ public boolean validate(CmdLineOptionInstance optionInst) {
+ for (String value : optionInst.getValues()) {
+ try {
+ Class.forName(value);
+ } catch (Exception e) {
+ LOG.severe("Option value " + value + " for option "
+ + optionInst.getOption().getLongOption() + " is not a valid class");
+ return false;
+ }
+ }
+ return true;
+ }
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/ClassExistsCmdLineOptionValidator.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/ClassExistsCmdLineOptionValidator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/CmdLineOptionValidator.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/CmdLineOptionValidator.java?rev=1180819&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/CmdLineOptionValidator.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/CmdLineOptionValidator.java Mon Oct 10 08:07:07 2011
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.oodt.cas.cl.option.validator;
+
+//JDK imports
+import java.util.logging.Logger;
+
+//OODT imports
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+
+/**
+ *
+ * @author bfoster
+ * @version $Revision$
+ *
+ * <p>Describe your class here</p>.
+ */
+public interface CmdLineOptionValidator {
+
+ static Logger LOG = Logger
+ .getLogger(CmdLineOptionValidator.class.getName());
+
+ public boolean validate(CmdLineOptionInstance optionInst);
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/CmdLineOptionValidator.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/validator/CmdLineOptionValidator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain