001/*-----------------------------------------------------------------------+
002 | com.teamscale.checks
003 |                                                                       |
004   $Id$            
005 |                                                                       |
006 | Copyright (c)  2009-2015 CQSE GmbH                                 |
007 +-----------------------------------------------------------------------*/
008package eu.cqse.check.framework.core.xpath.functions;
009
010import java.util.ArrayList;
011import java.util.List;
012
013import org.conqat.lib.commons.enums.EnumUtils;
014import org.jaxen.FunctionCallException;
015
016import eu.cqse.check.framework.scanner.ETokenType;
017
018/**
019 * Base class for functions that use an array of {@link ETokenType} as
020 * arguments.
021 */
022public abstract class TokenFunctionBase extends FunctionBase<ETokenType[]> {
023
024        /** {@inheritDoc} */
025        @Override
026        protected ETokenType[] parseArguments(List<?> args) throws FunctionCallException {
027                if (args.size() < 1) {
028                        throw new FunctionCallException(getArgumentSizeInvalidErrorMessage());
029                }
030
031                List<ETokenType> tokenTypes = new ArrayList<>();
032                for (Object tokenTypeName : args) {
033                        ETokenType tokenType = EnumUtils.valueOf(ETokenType.class, (tokenTypeName.toString()).toUpperCase());
034                        if (tokenType == null) {
035                                throw new FunctionCallException("Argument " + tokenTypeName + " is no valid token.");
036                        }
037                        tokenTypes.add(tokenType);
038                }
039                return tokenTypes.toArray(new ETokenType[tokenTypes.size()]);
040        }
041
042        /**
043         * Returns the error message that is displayed if the number of arguments
044         * does not match.
045         */
046        protected abstract String getArgumentSizeInvalidErrorMessage();
047}