001/*
002 * Copyright 2018 Leibniz-Institut für Analytische Wissenschaften – ISAS – e.V..
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package de.isas.mztab2.validation.handlers;
017
018import de.isas.mztab2.cvmapping.CvMappingUtils;
019import de.isas.mztab2.cvmapping.RuleEvaluationResult;
020import de.isas.mztab2.cvmapping.SetOperations;
021import de.isas.mztab2.io.serialization.ParameterConverter;
022import de.isas.mztab2.model.Parameter;
023import de.isas.mztab2.model.ValidationMessage;
024import de.isas.mztab2.validation.CvTermValidationHandler;
025import java.util.ArrayList;
026import java.util.List;
027import java.util.Set;
028import org.apache.commons.jxpath.Pointer;
029import org.apache.commons.lang3.tuple.Pair;
030import uk.ac.ebi.pride.jmztab2.utils.errors.CrossCheckErrorType;
031import uk.ac.ebi.pride.jmztab2.utils.errors.MZTabError;
032
033/**
034 * Implements handling of cv parameters (not user params) that are found at the object.
035 * @author nilshoffmann
036 */
037public class ExtraParametersValidationHandler implements CvTermValidationHandler {
038
039    @Override
040    public List<ValidationMessage> handleParameters(RuleEvaluationResult result,
041        boolean errorOnTermNotInRule) {
042        final List<ValidationMessage> messages = new ArrayList<>();
043        //handle (non-user) parameters that were found at the object but are not defined in the rule
044        Set<String> extraObjectParams = SetOperations.complement(
045            result.getFoundParameters().
046                keySet(),
047            result.getAllowedParameters().
048                keySet());
049        if (!extraObjectParams.isEmpty()) {
050            for (String s : extraObjectParams) {
051                Pair<Pointer, ? extends Parameter> p = result.
052                    getFoundParameters().
053                    get(s);
054                MZTabError error;
055                if (errorOnTermNotInRule) {
056                    error = new MZTabError(CrossCheckErrorType.CvTermNotAllowed,
057                        -1,
058                        new ParameterConverter().convert(p.
059                            getValue()), result.getRule().
060                            getCvElementPath(),
061                        CvMappingUtils.niceToString(result.getRule()));
062                } else {
063                    error = new MZTabError(CrossCheckErrorType.CvTermNotInRule,
064                        -1,
065                        new ParameterConverter().convert(p.
066                            getValue()), result.getRule().
067                            getCvElementPath(),
068                        CvMappingUtils.niceToString(result.getRule()));
069                }
070                messages.add(error.toValidationMessage());
071            }
072        }
073        return messages;
074    }
075}