org.apache.oodt.commons.option.util.CmdLineOptionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of oodt-commons Show documentation
Show all versions of oodt-commons Show documentation
Apache OODT Common Utilities Project
/*
* 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.commons.option.util;
//JDK imports
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
//OODT imports
import org.apache.oodt.commons.option.CmdLineOption;
import org.apache.oodt.commons.option.CmdLineOptionInstance;
import org.apache.oodt.commons.option.parser.CmdLineOptionParser;
import org.apache.oodt.commons.option.required.RequiredOption;
import org.apache.oodt.commons.option.validator.CmdLineOptionValidator;
//Spring imports
import org.springframework.context.ApplicationContext;
/**
*
* @author bfoster
* @version $Revision$
*
* Describe your class here
.
*/
public class CmdLineOptionUtils {
private CmdLineOptionUtils() {
}
public static CmdLineOption getOptionByName(String optionName,
List options) {
for (CmdLineOption option : options)
if (option.getLongOption().equals(optionName)
|| option.getShortOption().equals(optionName))
return option;
return null;
}
public static CmdLineOptionInstance getOptionInstanceByName(
String optionName, List optionInsts) {
for (CmdLineOptionInstance optionInst : optionInsts)
if (optionInst.getOption().getLongOption().equals(optionName)
|| optionInst.getOption().getShortOption().equals(
optionName))
return optionInst;
return null;
}
public static List getOptionValues(String optionName,
List options) {
for (CmdLineOptionInstance optionInst : options)
if (optionInst.getOption().getLongOption().equals(optionName)
|| optionInst.getOption().getShortOption().equals(
optionName))
return optionInst.getValues();
return null;
}
public static List getOptions(ApplicationContext appContext) {
List options = new LinkedList();
Map optionsMap = appContext.getBeansOfType(CmdLineOption.class);
for (Iterator iter = optionsMap.entrySet().iterator(); iter
.hasNext();)
options.add((CmdLineOption) iter.next().getValue());
return options;
}
public static List loadValidateAndHandleInstances(
ApplicationContext appContext, String[] args) throws IOException {
// parse args
CmdLineOptionParser parser = new CmdLineOptionParser();
List supportedOptions = CmdLineOptionUtils
.getOptions(appContext);
parser.setValidOptions(supportedOptions);
List optionInstances = parser.parse(args);
// check that required args have been specified
List reqOptions = CmdLineOptionUtils
.getRequiredOptions(supportedOptions);
List unsetReqOptions = CmdLineOptionUtils
.getRequiredOptionsNotSet(optionInstances, reqOptions);
for (CmdLineOption unsetReqOption : unsetReqOptions) {
if (unsetReqOption.getRequiredOptions().size() > 0) {
for (RequiredOption reqOption : unsetReqOption
.getRequiredOptions()) {
CmdLineOptionInstance optionInst = getOptionInstanceByName(
reqOption.getOptionLongName(), optionInstances);
if (optionInst != null
&& ((reqOption.isRequireAllValues() && optionInst
.getValues().containsAll(
reqOption.getOptionValues())) || (!reqOption
.isRequireAllValues() && !Collections
.disjoint(optionInst.getValues(), reqOption
.getOptionValues()))))
throw new IOException("Option "
+ unsetReqOption.getId() + " is required");
}
} else
throw new IOException("Option " + unsetReqOption.getId()
+ " is required");
}
// validate options
for (CmdLineOptionInstance optionInst : optionInstances) {
for (CmdLineOptionValidator validator : optionInst.getOption()
.getValidators())
if (!validator.validate(optionInst))
throw new IOException("Option "
+ optionInst.getOption().getId()
+ " failed validation");
}
// if all looks good . . . handle options
for (CmdLineOptionInstance optionInst : optionInstances)
optionInst.getOption().getHandler().handleOption(
optionInst.getOption(), optionInst.getValues());
return optionInstances;
}
public static List getRequiredOptionsNotSet(
List optionInsts,
List reqOptions) {
List nonSetRequiredOptions = new LinkedList();
TOP: for (CmdLineOption reqOption : reqOptions) {
for (CmdLineOptionInstance optionInst : optionInsts) {
if (reqOption.equals(optionInst.getOption()))
continue TOP;
}
nonSetRequiredOptions.add(reqOption);
}
return nonSetRequiredOptions;
}
public static List getRequiredOptions(
List options) {
List requiredOptions = new LinkedList();
for (CmdLineOption option : options) {
if (option.isRequired())
requiredOptions.add(option);
}
return requiredOptions;
}
}