org.refcodes.configuration.ext.console.ArgsParserPropertiesImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of refcodes-configuration-ext-console Show documentation
Show all versions of refcodes-configuration-ext-console Show documentation
Artifact for providing event based extended functionality for the
refcodes-configuration artifact.
// /////////////////////////////////////////////////////////////////////////////
// REFCODES.ORG
// /////////////////////////////////////////////////////////////////////////////
// This code is copyright (c) by Siegfried Steiner, Munich, Germany and licensed
// under the following (see "http://en.wikipedia.org/wiki/Multi-licensing")
// licenses:
// -----------------------------------------------------------------------------
// GNU General Public License, v3.0 ("http://www.gnu.org/licenses/gpl-3.0.html")
// -----------------------------------------------------------------------------
// Apache License, v2.0 ("http://www.apache.org/licenses/LICENSE-2.0")
// -----------------------------------------------------------------------------
// Please contact the copyright holding author(s) of the software artifacts in
// question for licensing issues not being covered by the above listed licenses,
// also regarding commercial licensing models or regarding the compatibility
// with other open source licenses.
// /////////////////////////////////////////////////////////////////////////////
package org.refcodes.configuration.ext.console;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.refcodes.configuration.Properties;
import org.refcodes.configuration.PropertiesBuilderImpl;
import org.refcodes.configuration.PropertiesImpl;
import org.refcodes.console.AmbiguousArgsException;
import org.refcodes.console.ArgsParserImpl;
import org.refcodes.console.Condition;
import org.refcodes.console.Operand;
import org.refcodes.console.Option;
import org.refcodes.console.ParseArgsException;
import org.refcodes.console.SuperfluousArgsException;
import org.refcodes.console.SyntaxNotation;
import org.refcodes.console.UnknownArgsException;
import org.refcodes.data.CommandArgPrefix;
import org.refcodes.runtime.RuntimeUtility;
public class ArgsParserPropertiesImpl extends ArgsParserImpl implements ArgsParserProperties {
// /////////////////////////////////////////////////////////////////////////
// STATICS:
// /////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////
// CONSTANTS:
// /////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////
// VARIABLES:
// /////////////////////////////////////////////////////////////////////////
private Properties _properties = new PropertiesImpl();
// /////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS:
// /////////////////////////////////////////////////////////////////////////
/**
* Constructs the {@link ArgsParserProperties} instance with no syntax
* notation (no root {@link Condition}). As no syntax notation is required
* by the constructor (no root {@link Condition}), no syntax validation is
* done by {@link #evalArgs(String[])} and the like methods. When no syntax
* validation is fine for you, you can directly go for the constructor
* {@link #ArgsParserPropertiesImpl(String[])}.
*/
public ArgsParserPropertiesImpl() {
super();
}
/**
* Constructs the {@link ArgsParserProperties} instance with the given root
* {@link Condition} and the default {@link SyntaxNotation#REFCODES}.
*
* @param aRootCondition The root condition being the node from which
* parsing the command line arguments starts.
*/
public ArgsParserPropertiesImpl( Condition aRootCondition ) {
super( aRootCondition );
}
/**
* Constructs the {@link ArgsParserProperties} instance with the given
* arguments and the default {@link SyntaxNotation#REFCODES}. As no syntax
* notation is required by the constructor (no root {@link Condition}), no
* syntax validation is done. Therefore the properties are heuristically
* determined from the provided command line arguments.
*
* @param aArgs The command line arguments to be evaluated.
*/
public ArgsParserPropertiesImpl( String[] aArgs ) {
_properties = new PropertiesBuilderImpl( RuntimeUtility.toProperties( aArgs, CommandArgPrefix.toPrefixes(), getDelimiter() ) );
}
/**
* Constructs the {@link ArgsParserProperties} instance with the given root
* {@link Condition} and the default {@link SyntaxNotation#REFCODES}.
*
* @param aArgs The command line arguments to be evaluated.
*
* @param aRootCondition The root condition being the node from which
* parsing the command line arguments starts.
*
* @throws UnknownArgsException Thrown in case not one command line argument
* matched regarding the provided args vs. the expected args.
* @throws AmbiguousArgsException Thrown in case at least one command line
* argument is ambiguous regarding expected args vs. provided args.
* @throws SuperfluousArgsException Thrown in case there were arguments
* found not being used (superfluous arguments).
* @throws ParseArgsException Thrown in case the provided command line
* arguments do not respect the required syntax or cannot be
* converted to the required type
*/
public ArgsParserPropertiesImpl( String[] aArgs, Condition aRootCondition ) throws UnknownArgsException, AmbiguousArgsException, SuperfluousArgsException, ParseArgsException {
super( aRootCondition );
evalArgs( aArgs );
}
// /////////////////////////////////////////////////////////////////////////
// INJECTION:
// /////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////
// METHODS:
// /////////////////////////////////////////////////////////////////////////
/**
* {@inheritDoc}
*/
@Override
public List> evalArgs( String[] aArgs ) throws UnknownArgsException, AmbiguousArgsException, SuperfluousArgsException, ParseArgsException {
return evalArgs( getRootPath(), aArgs );
}
/**
* {@inheritDoc}
*/
@Override
public List> evalArgs( List aArgs ) throws UnknownArgsException, AmbiguousArgsException, SuperfluousArgsException, ParseArgsException {
return evalArgs( getRootPath(), aArgs.toArray( new String[aArgs.size()] ) );
}
/**
* {@inheritDoc}
*/
@Override
public List> evalArgs( String aToPath, List aArgs ) throws UnknownArgsException, AmbiguousArgsException, SuperfluousArgsException, ParseArgsException {
return evalArgs( aToPath, aArgs.toArray( new String[aArgs.size()] ) );
}
/**
* {@inheritDoc}
*/
@Override
public char getDelimiter() {
return _properties.getDelimiter();
}
/**
* {@inheritDoc}
*/
@Override
public int size() {
return _properties.size();
}
/**
* {@inheritDoc}
*/
@Override
public boolean containsKey( Object aKey ) {
return _properties.containsKey( aKey );
}
/**
* {@inheritDoc}
*/
@Override
public boolean isEmpty() {
return _properties.isEmpty();
}
/**
* {@inheritDoc}
*/
@Override
public String get( Object aKey ) {
return _properties.get( aKey );
}
/**
* {@inheritDoc}
*/
@Override
public Set keySet() {
return _properties.keySet();
}
/**
* {@inheritDoc}
*/
@Override
public Collection values() {
return _properties.values();
}
/**
* {@inheritDoc}
*/
@Override
public Properties retrieveFrom( String aFromPath ) {
return _properties.retrieveFrom( aFromPath );
}
/**
* {@inheritDoc}
*/
@Override
public Properties retrieveTo( String aToPath ) {
return _properties.retrieveTo( aToPath );
}
/**
* {@inheritDoc}
*/
@Override
public Object toDataStructure( String aPath ) {
return _properties.toDataStructure( aPath );
}
// /////////////////////////////////////////////////////////////////////////
// HOOKS:
// /////////////////////////////////////////////////////////////////////////
/**
* {@inheritDoc}
*/
@Override
public List> evalArgs( String aToPath, String[] aArgs ) throws UnknownArgsException, AmbiguousArgsException, SuperfluousArgsException, ParseArgsException {
List> theEvalArgs = super.evalArgs( aArgs );
PropertiesBuilder theBuilder = new PropertiesBuilderImpl();
String eAlias;
for ( Operand e : theEvalArgs ) {
if ( e.hasValue() ) {
eAlias = e.getAlias();
if ( eAlias == null ) {
if ( e instanceof Option ) {
eAlias = ((Option) e).getLongOption();
if ( eAlias == null ) {
eAlias = ((Option) e).getShortOption();
}
}
}
theBuilder.put( toPath( aToPath, eAlias ), e.getValue().toString() );
}
}
_properties = theBuilder;
return theEvalArgs;
}
// /////////////////////////////////////////////////////////////////////////
// HELPER:
// /////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////
// INNER CLASSES:
// /////////////////////////////////////////////////////////////////////////
}