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.jaxen.Context; 014import org.jaxen.Function; 015import org.jaxen.FunctionCallException; 016 017import eu.cqse.check.framework.shallowparser.framework.ShallowEntity; 018 019/** 020 * Base class for functions that convert input arguments to a given type and 021 * then check for each {@link ShallowEntity}-node, if it should be kept in the 022 * node set. 023 * 024 * @param <T> 025 * the converted argument type 026 */ 027public abstract class FunctionBase<T> implements Function { 028 029 /** {@inheritDoc} */ 030 @SuppressWarnings("unchecked") 031 @Override 032 public Object call(Context context, @SuppressWarnings("rawtypes") List args) throws FunctionCallException { 033 T parsedArguments = parseArguments(args); 034 035 List<ShallowEntity> elements = new ArrayList<>(); 036 for (ShallowEntity node : (List<ShallowEntity>) context.getNodeSet()) { 037 if (checkNode(node, parsedArguments)) { 038 elements.add(node); 039 } 040 } 041 return elements; 042 } 043 044 /** Converts the input arguments. */ 045 protected abstract T parseArguments(List<?> args) throws FunctionCallException; 046 047 /** Checks if the given node should be kept in the node set. */ 048 protected abstract boolean checkNode(ShallowEntity node, T arguments) throws FunctionCallException; 049}