Coverage Report - com.sdmetrics.metrics.MetricProcedureSubString - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
MetricProcedureSubString
100%
39/39
100%
16/16
11
 
 1  
 /*
 2  
  * SDMetrics Open Core for UML design measurement
 3  
  * Copyright (c) Juergen Wuest
 4  
  * To contact the author, see <http://www.sdmetrics.com/Contact.html>.
 5  
  * 
 6  
  * This file is part of the SDMetrics Open Core.
 7  
  * 
 8  
  * SDMetrics Open Core is free software: you can redistribute it and/or modify
 9  
  * it under the terms of the GNU Affero General Public License as
 10  
  * published by the Free Software Foundation, either version 3 of the
 11  
  * License, or (at your option) any later version.
 12  
     
 13  
  * SDMetrics Open Core is distributed in the hope that it will be useful,
 14  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  
  * GNU Affero General Public License for more details.
 17  
  *
 18  
  * You should have received a copy of the GNU Affero General Public License
 19  
  * along with SDMetrics Open Core.  If not, see <http://www.gnu.org/licenses/>.
 20  
  *
 21  
  */
 22  
 package com.sdmetrics.metrics;
 23  
 
 24  
 import java.util.regex.Pattern;
 25  
 
 26  
 import com.sdmetrics.math.ExpressionNode;
 27  
 import com.sdmetrics.model.ModelElement;
 28  
 
 29  
 /**
 30  
  * Calculates the "substring" procedure to extract substrings from a string.
 31  
  * 
 32  
  * @since 2.31
 33  
  */
 34  3
 public class MetricProcedureSubString extends MetricProcedure {
 35  
 
 36  
         @Override
 37  
         public Object calculate(ModelElement element, Metric metric)
 38  
                         throws SDMetricsException {
 39  
 
 40  10
                 ProcedureAttributes attributes = metric.getAttributes();
 41  10
                 Variables vars = new Variables(element);
 42  
 
 43  
                 // get source string, separator, and position
 44  10
                 ExpressionNode sourceNode = attributes.getRequiredExpression("source");
 45  10
                 String source = evalExpression(element, sourceNode, vars).toString();
 46  
 
 47  10
                 ExpressionNode sepNode = attributes.getRequiredExpression("separator");
 48  10
                 String separator = evalExpression(element, sepNode, vars).toString();
 49  10
                 separator = Pattern.quote(separator);
 50  
 
 51  10
                 int position = -1;
 52  10
                 ExpressionNode positionNode = attributes.getExpression("position");
 53  10
                 if (positionNode != null)
 54  10
                         position = ((Number) evalExpression(element, positionNode, vars))
 55  5
                                         .intValue();
 56  
 
 57  10
                 int limit = -1;
 58  10
                 ExpressionNode limitNode = attributes.getExpression("limit");
 59  10
                 if (limitNode != null) {
 60  2
                         limit = ((Number) evalExpression(element, limitNode, vars))
 61  1
                                         .intValue();
 62  
                 }
 63  
 
 64  
                 // extract the substring
 65  10
                 String[] fragments = source.split(separator, limit);
 66  10
                 int positionToUse = position;
 67  10
                 if (position < 0)
 68  7
                         positionToUse = fragments.length + position;
 69  
 
 70  10
                 if (positionToUse < 0 || positionToUse >= fragments.length) {
 71  4
                         throw new SDMetricsException(element, metric,
 72  4
                                         "Illegal separator position " + position + " for source '"
 73  2
                                                         + source + "'");
 74  
                 }
 75  
 
 76  8
                 String result = fragments[positionToUse].trim();
 77  
 
 78  
                 // apply end separator
 79  8
                 ExpressionNode endSepNode = attributes.getExpression("endseparator");
 80  8
                 if (endSepNode != null) {
 81  6
                         String endseparator = evalExpression(element, endSepNode, vars)
 82  3
                                         .toString();
 83  3
                         int endIndex = result.indexOf(endseparator);
 84  3
                         if (endIndex >= 0)
 85  2
                                 result = result.substring(0, endIndex).trim();
 86  
                 }
 87  
 
 88  
                 // apply result processing expression
 89  8
                 ExpressionNode resultNode = attributes.getExpression("result");
 90  8
                 if (resultNode != null) {
 91  1
                         vars.setVariable("_value", result);
 92  1
                         return evalExpression(element, resultNode, vars);
 93  
                 }
 94  
 
 95  7
                 return result;
 96  
         }
 97  
 }