Coverage Report - com.damnhandy.uri.template.impl.VarSpec
 
Classes in this File Line Coverage Branch Coverage Complexity
VarSpec
86%
39/45
81%
18/22
2.4
VarSpec$VarFormat
100%
2/2
N/A
2.4
 
 1  
 /*
 2  
  * Copyright 2012, Ryan J. McDonough
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package com.damnhandy.uri.template.impl;
 17  
 
 18  
 import com.damnhandy.uri.template.MalformedUriTemplateException;
 19  
 
 20  
 import java.io.Serializable;
 21  
 import java.util.regex.Matcher;
 22  
 import java.util.regex.Pattern;
 23  
 
 24  
 
 25  
 /**
 26  
  * Represents a variable in a URI template expression.
 27  
  *
 28  
  * @author <a href="ryan@damnhandy.com">Ryan J. McDonough</a>
 29  
  * @version $Revision: 1.1 $
 30  
  */
 31  
 public final class VarSpec implements Serializable
 32  
 {
 33  
     /**
 34  
      * The serialVersionUID
 35  
      */
 36  
     private static final long serialVersionUID = 5850478145190940514L;
 37  
 
 38  
     /**
 39  
      * Regex to validate the variable name.
 40  
      */
 41  2
     private static final Pattern VARNAME_REGEX = Pattern.compile("([\\w\\_\\.]|%[A-Fa-f0-9]{2})+");
 42  
 
 43  
     /**
 44  
      *
 45  
      */
 46  8
     public enum VarFormat
 47  
     {
 48  2
         SINGLE, ARRAY, PAIRS;
 49  
     }
 50  
 
 51  
     private static final String BASE_PATTERN = "([\\w.~\\-\\_]|%[A-Fa-f0-9]{2})";
 52  
     /**
 53  
      *
 54  
      */
 55  1542
     private Modifier modifier = Modifier.NONE;
 56  
 
 57  
     /**
 58  
      *
 59  
      */
 60  
     private String value;
 61  
 
 62  
     /**
 63  
      *
 64  
      */
 65  1542
     private Integer position = 0;
 66  
 
 67  
     /**
 68  
      *
 69  
      */
 70  
     private String variableName;
 71  
 
 72  
     /**
 73  
      *
 74  
      */
 75  
     private String regexMatchString;
 76  
 
 77  
 
 78  
     /**
 79  
      * Create a new VarSpec.
 80  
      *
 81  
      * @param modifier
 82  
      * @param value
 83  
      */
 84  
     public VarSpec(String value, Modifier modifier)
 85  
     {
 86  1310
         this(value, modifier, -1);
 87  1274
     }
 88  
 
 89  
     /**
 90  
      * Create a new VarSpec.
 91  
      *
 92  
      * @param modifier
 93  
      * @param value
 94  
      * @param position
 95  
      */
 96  
     public VarSpec(String value, Modifier modifier, Integer position)
 97  1542
     {
 98  1542
         this.modifier = modifier;
 99  1542
         this.value = value;
 100  1542
         if(position != null)
 101  
         {
 102  1426
             this.position = position;
 103  
         }
 104  1542
         initVariableName();
 105  1498
         initRegExMatchString();
 106  1498
     }
 107  
 
 108  
 
 109  
 
 110  
     /**
 111  
      * Get the modifier.
 112  
      *
 113  
      * @return the modifier.
 114  
      */
 115  
     public Modifier getModifier()
 116  
     {
 117  6370
         return modifier;
 118  
     }
 119  
 
 120  
     private void initRegExMatchString()
 121  
     {
 122  1498
         StringBuilder b = new StringBuilder(BASE_PATTERN);
 123  1498
         if (modifier == Modifier.PREFIX)
 124  
         {
 125  116
             b.append("{").append(getPosition()).append("}");
 126  
         }
 127  
         else
 128  
         {
 129  1382
             b.append("+");
 130  
         }
 131  1498
         regexMatchString = b.toString();
 132  1498
     }
 133  
 
 134  
     /**
 135  
      * Returns a regex pattern that matches the variable.
 136  
      *
 137  
      * @return
 138  
      */
 139  
     public String getRegExMatchString()
 140  
     {
 141  0
         if (regexMatchString == null)
 142  
         {
 143  0
             initRegExMatchString();
 144  
         }
 145  0
         return regexMatchString;
 146  
     }
 147  
 
 148  
     /**
 149  
      * Get the value.
 150  
      *
 151  
      * @return the value.
 152  
      */
 153  
     public String getValue()
 154  
     {
 155  4040
         return value;
 156  
     }
 157  
 
 158  
     /**
 159  
      * Get the position.
 160  
      *
 161  
      * @return the position.
 162  
      */
 163  
     public Integer getPosition()
 164  
     {
 165  228
         return position;
 166  
     }
 167  
 
 168  
     private void initVariableName()
 169  
     {
 170  1542
         variableName = getValue();
 171  
 
 172  1542
         if (modifier != Modifier.NONE)
 173  
         {
 174  392
             if (modifier == Modifier.PREFIX)
 175  
             {
 176  116
                 String[] values = getValue().split(Modifier.PREFIX.getValue());
 177  116
                 variableName = values[0];
 178  
             }
 179  
             // Strip the '*' from the variable name if it's presnet on the variable
 180  
             // name. Note that in the case of the UriTemplateBuilder, the VarSpec
 181  
             // is not responsible for rendering the '*' on the generated template
 182  
             // output as that is done in the UriTemplateBuilder
 183  392
             if (modifier == Modifier.EXPLODE && getValue().lastIndexOf('*') != -1)
 184  
             {
 185  248
                 variableName = getValue().substring(0, getValue().length() - 1);
 186  
             }
 187  
         }
 188  
         // Double check if the name has an explode modifier. This could happen
 189  
         // using one of the template builder APIs. If the ends with '*'
 190  
         // strip it and set the modifier to EXPLODE
 191  1150
         else if (variableName.lastIndexOf('*') != -1)
 192  
         {
 193  4
             variableName = getValue().substring(0, getValue().length() - 1);
 194  4
             modifier = Modifier.EXPLODE;
 195  
         }
 196  
         // Validation needs to happen after strip out the modifier or prefix
 197  1542
         Matcher matcher = VARNAME_REGEX.matcher(variableName);
 198  1542
         if (!matcher.matches())
 199  
         {
 200  44
             throw new MalformedUriTemplateException("The variable name " + variableName + " contains invalid characters", position);
 201  
         }
 202  
 
 203  1498
         if (variableName.contains(" "))
 204  
         {
 205  0
             throw new MalformedUriTemplateException("The variable name " + variableName + " cannot contain spaces (leading or trailing)", position);
 206  
         }
 207  1498
     }
 208  
 
 209  
 
 210  
 
 211  
     /**
 212  
      * Returns the variable name
 213  
      *
 214  
      * @return
 215  
      */
 216  
     public String getVariableName()
 217  
     {
 218  1892
         if (variableName == null)
 219  
         {
 220  0
             return getValue();
 221  
         }
 222  1892
         return variableName;
 223  
     }
 224  
 
 225  
     @Override
 226  
     public String toString()
 227  
     {
 228  0
         return "VarSpec [modifier=" + modifier + ", value=" + value + ", position=" + position + ", variableName="
 229  
         + variableName + "]";
 230  
     }
 231  
 
 232  
 }