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/13 23:14:05 UTC

svn commit: r1183104 - in /oodt/branches/cas-cl/src: main/java/org/apache/oodt/cas/cl/ main/java/org/apache/oodt/cas/cl/action/ main/java/org/apache/oodt/cas/cl/help/printer/ main/java/org/apache/oodt/cas/cl/option/ main/java/org/apache/oodt/cas/cl/opt...

Author: bfoster
Date: Thu Oct 13 21:14:04 2011
New Revision: 1183104

URL: http://svn.apache.org/viewvc?rev=1183104&view=rev
Log:
- introduced CmdLineArgs
- now has first working unit-test!!!!

Added:
    oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineArgs.java   (with props)
    oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/require/ActionDependency.java   (with props)
Removed:
    oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/GenericsSafeIterable.java
Modified:
    oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineUtility.java
    oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/action/CmdLineAction.java
    oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineActionHelpPrinter.java
    oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionHelpPrinter.java
    oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineActionHelpPrinter.java
    oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionHelpPrinter.java
    oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/SimpleCmdLineOption.java
    oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/require/StdRequirementRule.java
    oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java
    oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestCmdLineOptionUtils.java

Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineArgs.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineArgs.java?rev=1183104&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineArgs.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineArgs.java Thu Oct 13 21:14:04 2011
@@ -0,0 +1,92 @@
+package org.apache.oodt.cas.cl;
+
+import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.findAction;
+import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.findActionOption;
+import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.findHelpOption;
+import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.findSpecifiedOption;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.oodt.cas.cl.action.CmdLineAction;
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+
+public class CmdLineArgs {
+
+	private CmdLineAction specifiedAction;
+	private Set<CmdLineAction> supportedActions;
+
+	private CmdLineOption helpOption;
+	private CmdLineOptionInstance helpOptionInst;
+	private CmdLineOption actionOption;
+	private CmdLineOptionInstance actionOptionInst;
+	private Set<CmdLineOption> supportedOptions;
+	private Set<CmdLineOption> customSupportedOptions;
+	private Set<CmdLineOptionInstance> specifiedOptions;
+	private Set<CmdLineOptionInstance> customSpecifiedOptions;
+
+	public CmdLineArgs(Set<CmdLineAction> supportedActions, Set<CmdLineOption> supportedOptions, Set<CmdLineOptionInstance> specifiedOptions) {
+		helpOption = findHelpOption(supportedOptions);
+		helpOptionInst = findSpecifiedOption(helpOption, specifiedOptions);
+		actionOption = findActionOption(supportedOptions);
+		actionOptionInst = findSpecifiedOption(actionOption, specifiedOptions);
+
+		this.supportedOptions = new HashSet<CmdLineOption>(supportedOptions);
+
+		customSupportedOptions = new HashSet<CmdLineOption>(supportedOptions);
+		customSupportedOptions.remove(helpOption);
+		customSupportedOptions.remove(actionOption);
+
+		this.specifiedOptions = new HashSet<CmdLineOptionInstance>(specifiedOptions);
+
+		customSpecifiedOptions = new HashSet<CmdLineOptionInstance>(specifiedOptions);
+		customSpecifiedOptions.remove(helpOptionInst);
+		customSpecifiedOptions.remove(actionOptionInst);
+
+		this.supportedActions = supportedActions;
+		if(actionOptionInst != null) {
+			specifiedAction = findAction(actionOptionInst, supportedActions);
+		}
+	}
+
+	public CmdLineOption getHelpOption() {
+		return helpOption;
+	}
+
+	public CmdLineOptionInstance getHelpOptionInst() {
+		return helpOptionInst;
+	}
+
+	public CmdLineOption getActionOption() {
+		return actionOption;
+	}
+
+	public CmdLineOptionInstance getActionOptionInst() {
+		return actionOptionInst;
+	}
+
+	public Set<CmdLineOption> getSupportedOptions() {
+		return supportedOptions;
+	}
+
+	public Set<CmdLineOption> getCustomSupportedOptions() {
+		return customSupportedOptions;
+	}
+
+	public Set<CmdLineOptionInstance> getSpecifiedOptions() {
+		return specifiedOptions;
+	}
+
+	public Set<CmdLineOptionInstance> getCustomSpecifiedOptions() {
+		return customSpecifiedOptions;
+	}
+
+	public Set<CmdLineAction> getSupportedActions() {
+		return supportedActions;
+	}
+
+	public CmdLineAction getSpecifiedAction() {
+		return specifiedAction;
+	}
+}

Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineArgs.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineUtility.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineUtility.java?rev=1183104&r1=1183103&r2=1183104&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineUtility.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineUtility.java Thu Oct 13 21:14:04 2011
@@ -3,8 +3,6 @@ package org.apache.oodt.cas.cl;
 import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.determineRequired;
 import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.findActionOption;
 import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.findHelpOption;
-import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.findSpecifiedActionOption;
-import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.findSpecifiedOption;
 
 import java.io.IOException;
 import java.util.HashSet;
@@ -24,11 +22,8 @@ import org.apache.oodt.cas.cl.option.Cmd
 import org.apache.oodt.cas.cl.option.HandleableCmdLineOption;
 import org.apache.oodt.cas.cl.option.HelpCmdLineOption;
 import org.apache.oodt.cas.cl.option.ValidatableCmdLineOption;
-import org.apache.oodt.cas.cl.option.require.RequirementRule;
-import org.apache.oodt.cas.cl.option.require.RequirementRule.Relation;
 import org.apache.oodt.cas.cl.option.store.CmdLineOptionStore;
 import org.apache.oodt.cas.cl.option.store.spring.SpringCmdLineOptionStoreFactory;
-import org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils;
 import org.apache.oodt.cas.cl.option.validator.CmdLineOptionValidator;
 import org.apache.oodt.cas.cl.parser.CmdLineOptionParser;
 import org.apache.oodt.cas.cl.parser.StdCmdLineOptionParser;
@@ -81,22 +76,22 @@ public class CmdLineUtility {
 		this.helpPresenter = helpPresenter;
 	}
 
-	public void printOptionHelp(Set<CmdLineOption> options) {
-		helpPresenter.presentOptionHelp(optionHelpPrinter.printHelp(options));
+	public void printOptionHelp(CmdLineArgs cmdLineArgs) {
+		helpPresenter.presentOptionHelp(optionHelpPrinter.printHelp(cmdLineArgs));
 	}
 
-	public void printActionHelp(CmdLineAction action, Set<CmdLineOption> options) {
-		helpPresenter.presentActionHelp(actionHelpPrinter.printHelp(action, options));
+	public void printActionHelp(CmdLineArgs cmdLineArgs) {
+		helpPresenter.presentActionHelp(actionHelpPrinter.printHelp(cmdLineArgs));
 	}
 
 	public void run(String[] args) throws IOException {
-		Set<CmdLineOptionInstance> specifiedOptions = parse(args);
-		if (!handleHelp(specifiedOptions)) {
-			execute(specifiedOptions);
+		CmdLineArgs cmdLineArgs = parse(args);
+		if (!handleHelp(cmdLineArgs)) {
+			execute(cmdLineArgs);
 		}
 	}
 
-	public Set<CmdLineOptionInstance> parse(String[] args) throws IOException {
+	public CmdLineArgs parse(String[] args) throws IOException {
 		Validate.notNull(parser);
 		Validate.notNull(optionStore);
 
@@ -116,61 +111,41 @@ public class CmdLineUtility {
 		}
 
 		// Parse command line arguments.
-		return parser.parse(args, validOptions);
+		return new CmdLineArgs(optionStore.loadSupportedActions(), validOptions, parser.parse(args, validOptions));
 	}
 
-	public boolean handleHelp(Set<CmdLineOptionInstance> specifiedOptions) throws IOException {
-		Set<CmdLineOption> supportedOptions = optionStore.loadSupportedOptions();
-		HelpCmdLineOption helpOption = findHelpOption(supportedOptions); 
-		if (helpOption != null) {
-			CmdLineOptionInstance specifiedHelpOption = findSpecifiedOption(
-					helpOption, specifiedOptions);
-			if (specifiedHelpOption != null) {
-				if (specifiedHelpOption.getSubOptions().isEmpty()) {
-					printOptionHelp(supportedOptions);
-				} else {
-					printActionHelp(getSelectedAction(specifiedOptions), supportedOptions);
-				}
-				return true;
+	public boolean handleHelp(CmdLineArgs cmdLineArgs) throws IOException {
+		if (cmdLineArgs.getHelpOptionInst() != null) {
+			if (cmdLineArgs.getHelpOptionInst().getSubOptions().isEmpty()) {
+				printOptionHelp(cmdLineArgs);
+			} else {
+				printActionHelp(cmdLineArgs);
 			}
+			return true;
 		}
 		return false;
 	}
 
-	public CmdLineAction getSelectedAction(Set<CmdLineOptionInstance> specifiedOptions) throws IOException {
-		CmdLineOptionInstance actionOption = findSpecifiedActionOption(specifiedOptions);
-		if (actionOption == null) {
-			throw new IOException("Action option not specified");
-		}
-		CmdLineAction action = findAction(actionOption, optionStore.loadSupportedActions());
-		if (action == null) {
-			throw new IOException("Action '" + actionOption + "' is not a supported action");
-		}
-		return action;
-	}
-
-	public void execute(Set<CmdLineOptionInstance> specifiedOptions) throws IOException {
-		CmdLineAction action = getSelectedAction(specifiedOptions);
-
-		Set<CmdLineOption> requiredOptions = determineRequired(action, optionStore.loadSupportedOptions());
-		Set<CmdLineOption> requiredOptionsNotSet = check(requiredOptions, specifiedOptions);
+	public void execute(CmdLineArgs cmdLineArgs) throws IOException {
+		Set<CmdLineOption> requiredOptionsNotSet = check(cmdLineArgs);
 		if (!requiredOptionsNotSet.isEmpty()) {
 			throw new IOException("Required options are not set: '" + requiredOptionsNotSet + "'");
 		}
 
-		Set<CmdLineOptionInstance> optionsFailedValidation = validate(specifiedOptions);
+		Set<CmdLineOptionInstance> optionsFailedValidation = validate(cmdLineArgs.getSpecifiedOptions());
 		if (!optionsFailedValidation.isEmpty()) {
 			throw new IOException("Options failed validation: '" + optionsFailedValidation + "'");
 		}
 
-		handle(action, specifiedOptions);
+		handle(cmdLineArgs.getSpecifiedAction(), cmdLineArgs.getSpecifiedOptions());
 
-		action.execute();
+		cmdLineArgs.getSpecifiedAction().execute();
 	}
 
-	public static Set<CmdLineOption> check(Set<CmdLineOption> requiredOptions, Set<CmdLineOptionInstance> specifiedOptions) {
+	public static Set<CmdLineOption> check(CmdLineArgs cmdLineArgs) {
+		Set<CmdLineOption> requiredOptions = determineRequired(cmdLineArgs.getSpecifiedAction(), cmdLineArgs.getCustomSupportedOptions());
 		HashSet<CmdLineOption> requiredOptionsNotSet = new HashSet<CmdLineOption>(requiredOptions);
-		for (CmdLineOptionInstance specifiedOption : specifiedOptions) {
+		for (CmdLineOptionInstance specifiedOption : cmdLineArgs.getCustomSpecifiedOptions()) {
 			requiredOptionsNotSet.remove(specifiedOption.getOption());
 		}
 		return requiredOptionsNotSet;
@@ -199,19 +174,6 @@ public class CmdLineUtility {
 		return true;
 	}
 
-	public static CmdLineAction findAction(CmdLineOptionInstance actionOption, Set<CmdLineAction> supportedActions) {
-		Validate.isTrue(actionOption.isAction());
-		Validate.notEmpty(actionOption.getValues());
-
-		String actionName = actionOption.getValues().get(0);
-		for (CmdLineAction action : supportedActions) {
-			if (action.getName().equals(actionName)) {
-				return action;
-			}
-		}
-		return null;
-	}
-
 	public static void handle(CmdLineAction action, Set<CmdLineOptionInstance> options) {
 		for (CmdLineOptionInstance option : options) {
 			handle(action, option);

Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/action/CmdLineAction.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/action/CmdLineAction.java?rev=1183104&r1=1183103&r2=1183104&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/action/CmdLineAction.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/action/CmdLineAction.java Thu Oct 13 21:14:04 2011
@@ -5,6 +5,13 @@ public abstract class CmdLineAction {
 	private String name;
 	private String description;
 
+	public CmdLineAction() {}
+
+	public CmdLineAction(String name, String description) {
+		this.name = name;
+		this.description = description;
+	}
+
 	public void setName(String name) {
 		this.name = name;
 	}

Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineActionHelpPrinter.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineActionHelpPrinter.java?rev=1183104&r1=1183103&r2=1183104&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineActionHelpPrinter.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineActionHelpPrinter.java Thu Oct 13 21:14:04 2011
@@ -1,12 +1,9 @@
 package org.apache.oodt.cas.cl.help.printer;
 
-import java.util.Set;
-
-import org.apache.oodt.cas.cl.action.CmdLineAction;
-import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.CmdLineArgs;
 
 public interface CmdLineActionHelpPrinter {
 
-	public String printHelp(CmdLineAction action, Set<CmdLineOption> options);
+	public String printHelp(CmdLineArgs cmdLineArgs);
 
 }

Modified: 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=1183104&r1=1183103&r2=1183104&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionHelpPrinter.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineOptionHelpPrinter.java Thu Oct 13 21:14:04 2011
@@ -1,11 +1,9 @@
 package org.apache.oodt.cas.cl.help.printer;
 
-import java.util.Set;
-
-import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.CmdLineArgs;
 
 public interface CmdLineOptionHelpPrinter {
 
-	public String printHelp(Set<CmdLineOption> options);
+	public String printHelp(CmdLineArgs cmdLineArgs);
 
 }

Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineActionHelpPrinter.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineActionHelpPrinter.java?rev=1183104&r1=1183103&r2=1183104&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineActionHelpPrinter.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineActionHelpPrinter.java Thu Oct 13 21:14:04 2011
@@ -7,34 +7,32 @@ import static org.apache.oodt.cas.cl.opt
 import java.util.List;
 import java.util.Set;
 
-import org.apache.oodt.cas.cl.CmdLineUtility;
+import org.apache.oodt.cas.cl.CmdLineArgs;
 import org.apache.oodt.cas.cl.action.CmdLineAction;
 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;
 
 public class StdCmdLineActionHelpPrinter implements
 		CmdLineActionHelpPrinter {
 
-	public String printHelp(CmdLineAction action, Set<CmdLineOption> options) {
+	public String printHelp(CmdLineArgs cmdLineArgs) {
 		StringBuffer sb = new StringBuffer("");
-		sb.append(getHeader(action)).append("\n");
+		sb.append(getHeader(cmdLineArgs.getSpecifiedAction())).append("\n");
 
 		sb.append(getRequiredSubHeader()).append("\n");
-		Set<CmdLineOption> requiredOptions = determineRequired(action, options);
+		Set<CmdLineOption> requiredOptions = determineRequired(cmdLineArgs.getSpecifiedAction(), cmdLineArgs.getCustomSupportedOptions());
 		List<CmdLineOption> sortedRequiredOptions = sortOptionsByRequiredStatus(requiredOptions);
 		for (CmdLineOption option : sortedRequiredOptions) {
 			sb.append(getRequiredOptionHelp(option)).append("\n");
 		}
 
 		sb.append(getOptionalSubHeader()).append("\n");
-		Set<CmdLineOption> optionalOptions = determineOptional(action, options);
+		Set<CmdLineOption> optionalOptions = determineOptional(cmdLineArgs.getSpecifiedAction(), cmdLineArgs.getCustomSupportedOptions());
 		List<CmdLineOption> sortedOptionalOptions = sortOptionsByRequiredStatus(optionalOptions);
 		for (CmdLineOption option : sortedOptionalOptions) {
 			sb.append(getOptionalOptionHelp(option)).append("\n");
 		}
 
-		sb.append(getFooter(action)).append("\n");
+		sb.append(getFooter(cmdLineArgs.getSpecifiedAction())).append("\n");
 		return sb.toString();
 	}
 

Modified: 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=1183104&r1=1183103&r2=1183104&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionHelpPrinter.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineOptionHelpPrinter.java Thu Oct 13 21:14:04 2011
@@ -4,17 +4,17 @@ import static org.apache.oodt.cas.cl.opt
 import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.sortOptionsByRequiredStatus;
 
 import java.util.List;
-import java.util.Set;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.oodt.cas.cl.CmdLineArgs;
 import org.apache.oodt.cas.cl.option.AdvancedCmdLineOption;
 import org.apache.oodt.cas.cl.option.CmdLineOption;
 
 public class StdCmdLineOptionHelpPrinter implements CmdLineOptionHelpPrinter {
 
-	public String printHelp(Set<CmdLineOption> options) {
+	public String printHelp(CmdLineArgs cmdLineArgs) {
 		StringBuffer sb = new StringBuffer("");
-		List<CmdLineOption> sortedOptions = sortOptionsByRequiredStatus(options);
+		List<CmdLineOption> sortedOptions = sortOptionsByRequiredStatus(cmdLineArgs.getSupportedOptions());
 		sb.append(getHeader()).append("\n");
 		for (CmdLineOption option : sortedOptions) {
 			sb.append(getOptionHelp(option)).append("\n");

Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/SimpleCmdLineOption.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/SimpleCmdLineOption.java?rev=1183104&r1=1183103&r2=1183104&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/SimpleCmdLineOption.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/SimpleCmdLineOption.java Thu Oct 13 21:14:04 2011
@@ -153,4 +153,9 @@ public class SimpleCmdLineOption impleme
 	public int hashCode() {
 		return shortOption.hashCode();
 	}
+
+	public String toString() {
+		return "Action [longOption='" + longOption + "',shortOption='"
+				+ shortOption + "',description='" + description + "']";
+	}
 }

Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/require/ActionDependency.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/require/ActionDependency.java?rev=1183104&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/require/ActionDependency.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/require/ActionDependency.java Thu Oct 13 21:14:04 2011
@@ -0,0 +1,60 @@
+package org.apache.oodt.cas.cl.option.require;
+
+import org.apache.oodt.cas.cl.option.require.RequirementRule.Relation;
+
+public class ActionDependency {
+	String actionName;
+	Relation relation;
+
+	public ActionDependency() {}
+
+	public ActionDependency(String actionName, Relation relation) {
+		this.actionName = actionName;
+		this.relation = relation;
+	}
+
+	public String getActionName() {
+		return actionName;
+	}
+
+	public void setActionName(String actionName) {
+		this.actionName = actionName;
+	}
+
+	public Relation getRelation() {
+		return relation;
+	}
+
+	public void setRelation(Relation relation) {
+		this.relation = relation;
+	}
+
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result
+				+ ((actionName == null) ? 0 : actionName.hashCode());
+		result = prime * result + ((relation == null) ? 0 : relation.hashCode());
+		return result;
+	}
+
+	@Override
+	public boolean equals(Object obj) {
+		if (this == obj)
+			return true;
+		if (obj == null)
+			return false;
+		if (getClass() != obj.getClass())
+			return false;
+		ActionDependency other = (ActionDependency) obj;
+		if (actionName == null) {
+			if (other.actionName != null)
+				return false;
+		} else if (!actionName.equals(other.actionName))
+			return false;
+		if (relation != other.relation)
+			return false;
+		return true;
+	}
+}

Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/require/ActionDependency.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/require/StdRequirementRule.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/require/StdRequirementRule.java?rev=1183104&r1=1183103&r2=1183104&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/require/StdRequirementRule.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/require/StdRequirementRule.java Thu Oct 13 21:14:04 2011
@@ -1,5 +1,6 @@
 package org.apache.oodt.cas.cl.option.require;
 
+import java.util.HashSet;
 import java.util.Set;
 
 import org.apache.oodt.cas.cl.action.CmdLineAction;
@@ -8,8 +9,16 @@ public class StdRequirementRule implemen
 
 	private Set<ActionDependency> dependencies;
 
+	public StdRequirementRule() {
+		dependencies = new HashSet<ActionDependency>();
+	}
+
 	public void setActionDependency(Set<ActionDependency> dependencies) {
-		this.dependencies = dependencies;
+		this.dependencies = new HashSet<ActionDependency>(dependencies);
+	}
+
+	public void addActionDependency(ActionDependency dependency) {
+		dependencies.add(dependency);
 	}
 
 	public Set<ActionDependency> getActionDependency() {
@@ -24,68 +33,4 @@ public class StdRequirementRule implemen
 		}
 		return Relation.NONE;
 	}
-
-	public class ActionDependency {
-		String actionName;
-		Relation relation;
-
-		public ActionDependency() {}
-
-		public ActionDependency(String actionName, Relation relation) {
-			this.actionName = actionName;
-			this.relation = relation;
-		}
-
-		public String getActionName() {
-			return actionName;
-		}
-
-		public void setActionName(String actionName) {
-			this.actionName = actionName;
-		}
-
-		public Relation getRelation() {
-			return relation;
-		}
-
-		public void setRelation(Relation relation) {
-			this.relation = relation;
-		}
-
-		@Override
-		public int hashCode() {
-			final int prime = 31;
-			int result = 1;
-			result = prime * result + getOuterType().hashCode();
-			result = prime * result
-					+ ((actionName == null) ? 0 : actionName.hashCode());
-			result = prime * result + ((relation == null) ? 0 : relation.hashCode());
-			return result;
-		}
-
-		@Override
-		public boolean equals(Object obj) {
-			if (this == obj)
-				return true;
-			if (obj == null)
-				return false;
-			if (getClass() != obj.getClass())
-				return false;
-			ActionDependency other = (ActionDependency) obj;
-			if (!getOuterType().equals(other.getOuterType()))
-				return false;
-			if (actionName == null) {
-				if (other.actionName != null)
-					return false;
-			} else if (!actionName.equals(other.actionName))
-				return false;
-			if (relation != other.relation)
-				return false;
-			return true;
-		}
-
-		private StdRequirementRule getOuterType() {
-			return StdRequirementRule.this;
-		}
-	}
 }

Modified: 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=1183104&r1=1183103&r2=1183104&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java Thu Oct 13 21:14:04 2011
@@ -61,6 +61,10 @@ public class CmdLineOptionUtils {
 		Validate.notNull(option);
 		Validate.notNull(action);
 
+		if (option instanceof ActionCmdLineOption) {
+			return false;
+		}
+
 		for (RequirementRule requirementRule : option.getRequirementRules()) {
 			if (requirementRule.getRelation(action) == Relation.REQUIRED) {
 				return true;
@@ -70,19 +74,23 @@ public class CmdLineOptionUtils {
 	}
 
 	public static Set<CmdLineOption> determineOptional(CmdLineAction action, Set<CmdLineOption> options) {
-		Set<CmdLineOption> requiredOptions = getRequiredOptions(options);
+		Set<CmdLineOption> optionalOptions = new HashSet<CmdLineOption>();
 		for (CmdLineOption option : options) {
 			if (isOptional(action, option)) {
-				requiredOptions.add(option);
+				optionalOptions.add(option);
 			}
 		}
-		return requiredOptions;
+		return optionalOptions;
 	}
 
 	public static boolean isOptional(CmdLineAction action, CmdLineOption option) {
 		Validate.notNull(option);
 		Validate.notNull(action);
 
+		if (option instanceof ActionCmdLineOption) {
+			return false;
+		}
+
 		for (RequirementRule requirementRule : option.getRequirementRules()) {
 			if (requirementRule.getRelation(action) == Relation.OPTIONAL) {
 				return true;
@@ -220,6 +228,20 @@ public class CmdLineOptionUtils {
 		return null;
 	}
 
+	public static CmdLineAction findAction(CmdLineOptionInstance actionOption,
+			Set<CmdLineAction> supportedActions) {
+		Validate.isTrue(actionOption.isAction());
+		Validate.notEmpty(actionOption.getValues());
+
+		String actionName = actionOption.getValues().get(0);
+		for (CmdLineAction action : supportedActions) {
+			if (action.getName().equals(actionName)) {
+				return action;
+			}
+		}
+		return null;
+	}
+
 	public static String getFormattedString(String string, int startIndex,
 			int endIndex) {
 		StringBuffer outputString = new StringBuffer("");

Modified: oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestCmdLineOptionUtils.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestCmdLineOptionUtils.java?rev=1183104&r1=1183103&r2=1183104&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestCmdLineOptionUtils.java (original)
+++ oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestCmdLineOptionUtils.java Thu Oct 13 21:14:04 2011
@@ -6,29 +6,43 @@ import java.util.Set;
 
 import junit.framework.TestCase;
 
+import org.apache.oodt.cas.cl.action.CmdLineAction;
+import org.apache.oodt.cas.cl.option.ActionCmdLineOption;
 import org.apache.oodt.cas.cl.option.CmdLineOption;
-import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
 import org.apache.oodt.cas.cl.option.GroupCmdLineOption;
+import org.apache.oodt.cas.cl.option.SimpleCmdLineOption;
 import org.apache.oodt.cas.cl.option.require.RequirementRule;
+import org.apache.oodt.cas.cl.option.require.RequirementRule.Relation;
+import org.apache.oodt.cas.cl.option.require.StdRequirementRule;
+import org.apache.oodt.cas.cl.option.require.ActionDependency;
 
 public class TestCmdLineOptionUtils extends TestCase {
 
-	public void testGetOptionalOptions() {
-		BasicCmdLineOption urlOption = createBasicOption("url", new RequirementRule("operation", Collections.singletonList("test"), false));
-		BasicCmdLineOption operationOption = createBasicOption("operation", true); 
-		HashSet<CmdLineOption<?>> options = new HashSet<CmdLineOption<?>>();
+	public void testDetermineOptional() {
+		CmdLineAction action = createAction("TestAction");
+		SimpleCmdLineOption urlOption = createSimpleOption("url", createRequiredRequirementRule(action));
+		ActionCmdLineOption operationOption = createActionOption("operation"); 
+		HashSet<CmdLineOption> options = new HashSet<CmdLineOption>();
 		options.add(urlOption);
-		options.add(createBasicOption("pass", false));
-		options.add(createBasicOption("user", false));
+		options.add(createSimpleOption("pass", false));
+		options.add(createSimpleOption("user", false));
 		options.add(operationOption);
 
-//		specifiedOptions.add(new BasicCmdLineOptionInstance(urlOption, Collections.singletonList("http://oodt.apache.org")));
-
-		Set<CmdLineOption<?>> optionalOptions = CmdLineOptionUtils.getConditionallyRequiredOptions(options, new BasicCmdLineOptionInstance(operationOption, Collections.singletonList("test")));
-		assertEquals(1, optionalOptions.size());
-		assertNotNull(CmdLineOptionUtils.getOptionByName("url", optionalOptions));
+		Set<CmdLineOption> optionalOptions = CmdLineOptionUtils.determineOptional(action, options);
+		System.out.println(optionalOptions);
+		assertEquals(0, optionalOptions.size());
 	}
 
+	private static CmdLineAction createAction(String name) {
+		return new CmdLineAction(name, "This is an action description") {
+
+			@Override
+			public void execute() {
+				// do nothing
+			}
+			
+		};
+	}
 	private static GroupCmdLineOption createGroupOption(String longName, boolean required) {
 		GroupCmdLineOption option = new GroupCmdLineOption();
 		option.setLongOption(longName);
@@ -37,19 +51,32 @@ public class TestCmdLineOptionUtils exte
 		return option;
 	}
 
-	private static BasicCmdLineOption createBasicOption(String longName, RequirementRule rule) {
-		BasicCmdLineOption option = new BasicCmdLineOption();
+	private static SimpleCmdLineOption createSimpleOption(String longName, boolean required) {
+		SimpleCmdLineOption option = new SimpleCmdLineOption();
+		option.setLongOption(longName);
+		option.setShortOption(longName);
+		option.setRequired(required);
+		return option;
+	}
+
+	private static SimpleCmdLineOption createSimpleOption(String longName, RequirementRule rule) {
+		SimpleCmdLineOption option = new SimpleCmdLineOption();
 		option.setLongOption(longName);
 		option.setShortOption(longName);
 		option.setRequirementRules(Collections.singletonList(rule));
 		return option;
 	}
 
-	public static BasicCmdLineOption createBasicOption(String longName, boolean required) {
-		BasicCmdLineOption option = new BasicCmdLineOption();
+	public static ActionCmdLineOption createActionOption(String longName) {
+		ActionCmdLineOption option = new ActionCmdLineOption();
 		option.setLongOption(longName);
 		option.setShortOption(longName);
-		option.setRequired(required);
 		return option;
 	}
+
+	private static RequirementRule createRequiredRequirementRule(CmdLineAction action) {
+		StdRequirementRule rule = new StdRequirementRule();
+		rule.addActionDependency(new ActionDependency(action.getName(), Relation.REQUIRED));
+		return rule;
+	}
 }