1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public final class VarSpec implements Serializable |
32 | |
{ |
33 | |
|
34 | |
|
35 | |
|
36 | |
private static final long serialVersionUID = 5850478145190940514L; |
37 | |
|
38 | |
|
39 | |
|
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 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public VarSpec(String value, Modifier modifier) |
85 | |
{ |
86 | 1310 | this(value, modifier, -1); |
87 | 1274 | } |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
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 | |
|
112 | |
|
113 | |
|
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 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public String getRegExMatchString() |
140 | |
{ |
141 | 0 | if (regexMatchString == null) |
142 | |
{ |
143 | 0 | initRegExMatchString(); |
144 | |
} |
145 | 0 | return regexMatchString; |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
public String getValue() |
154 | |
{ |
155 | 4040 | return value; |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
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 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | 392 | if (modifier == Modifier.EXPLODE && getValue().lastIndexOf('*') != -1) |
184 | |
{ |
185 | 248 | variableName = getValue().substring(0, getValue().length() - 1); |
186 | |
} |
187 | |
} |
188 | |
|
189 | |
|
190 | |
|
191 | 1150 | else if (variableName.lastIndexOf('*') != -1) |
192 | |
{ |
193 | 4 | variableName = getValue().substring(0, getValue().length() - 1); |
194 | 4 | modifier = Modifier.EXPLODE; |
195 | |
} |
196 | |
|
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 | |
|
213 | |
|
214 | |
|
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 | |
} |