| 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.Expression; |
| 19 | |
import com.damnhandy.uri.template.Literal; |
| 20 | |
import com.damnhandy.uri.template.MalformedUriTemplateException; |
| 21 | |
import com.damnhandy.uri.template.UriTemplateComponent; |
| 22 | |
|
| 23 | |
import java.util.LinkedList; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | 1032 | public final class UriTemplateParser |
| 33 | |
{ |
| 34 | |
|
| 35 | |
private static final char EXPR_START = '{'; |
| 36 | |
|
| 37 | |
private static final char EXPR_END = '}'; |
| 38 | |
|
| 39 | 1032 | private boolean startedTemplate = false; |
| 40 | |
|
| 41 | 1032 | private boolean expressionCaptureOn = false; |
| 42 | |
|
| 43 | 1032 | private boolean literalCaptureOn = false; |
| 44 | |
|
| 45 | 1032 | private LinkedList<UriTemplateComponent> components = new LinkedList<UriTemplateComponent>(); |
| 46 | |
|
| 47 | |
private StringBuilder buffer; |
| 48 | |
|
| 49 | |
private int startPosition; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public LinkedList<UriTemplateComponent> scan(String templateString) throws MalformedUriTemplateException |
| 59 | |
{ |
| 60 | 1032 | char[] template = templateString.toCharArray(); |
| 61 | 1032 | startTemplate(); |
| 62 | |
int i; |
| 63 | 14808 | for (i = 0; i < template.length; i++) |
| 64 | |
{ |
| 65 | 13834 | char c = template[i]; |
| 66 | |
|
| 67 | 13834 | if (c == EXPR_START) |
| 68 | |
{ |
| 69 | 1066 | if (literalCaptureOn) |
| 70 | |
{ |
| 71 | 374 | endLiteral(i); |
| 72 | |
} |
| 73 | 1066 | startExpression(i); |
| 74 | |
} |
| 75 | |
|
| 76 | 13828 | if (c != EXPR_START || c != EXPR_END) |
| 77 | |
{ |
| 78 | 13828 | startLiteral(i); |
| 79 | |
} |
| 80 | |
|
| 81 | |
|
| 82 | 13828 | if (expressionCaptureOn || literalCaptureOn) |
| 83 | |
{ |
| 84 | 13828 | capture(c); |
| 85 | |
} |
| 86 | |
|
| 87 | 13828 | if (c == EXPR_END) |
| 88 | |
{ |
| 89 | 1054 | endExpression(i); |
| 90 | 1002 | startLiteral(i); |
| 91 | |
} |
| 92 | |
} |
| 93 | 974 | if (literalCaptureOn) |
| 94 | |
{ |
| 95 | 974 | endLiteral(i); |
| 96 | |
} |
| 97 | 974 | endTemplate(i); |
| 98 | 970 | return components; |
| 99 | |
} |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
private void capture(char currentChar) |
| 107 | |
{ |
| 108 | 13828 | if (buffer == null) |
| 109 | |
{ |
| 110 | 1482 | buffer = new StringBuilder(); |
| 111 | |
} |
| 112 | 13828 | buffer.append(currentChar); |
| 113 | 13828 | } |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
private void startTemplate() |
| 119 | |
{ |
| 120 | 1032 | startedTemplate = true; |
| 121 | 1032 | } |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
private void endTemplate(int position) throws MalformedUriTemplateException |
| 128 | |
{ |
| 129 | 974 | startedTemplate = false; |
| 130 | 974 | if (expressionCaptureOn) |
| 131 | |
{ |
| 132 | 4 | throw new MalformedUriTemplateException("The expression at position " + startPosition + " was never terminated", startPosition); |
| 133 | |
} |
| 134 | |
|
| 135 | 970 | } |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
private void startLiteral(int position) throws MalformedUriTemplateException |
| 143 | |
{ |
| 144 | |
|
| 145 | 14830 | if (startedTemplate) |
| 146 | |
{ |
| 147 | 14830 | if (!literalCaptureOn) |
| 148 | |
{ |
| 149 | |
|
| 150 | 1400 | literalCaptureOn = true; |
| 151 | 1400 | startPosition = position; |
| 152 | |
} |
| 153 | |
} |
| 154 | |
else |
| 155 | |
{ |
| 156 | 0 | throw new IllegalStateException("Cannot start a literal without beginning the template"); |
| 157 | |
} |
| 158 | 14830 | } |
| 159 | |
|
| 160 | |
private void endLiteral(int position) throws MalformedUriTemplateException |
| 161 | |
{ |
| 162 | 1348 | if (startedTemplate) |
| 163 | |
{ |
| 164 | 1348 | if (!literalCaptureOn) |
| 165 | |
{ |
| 166 | 0 | throw new IllegalStateException("Can't end a literal without starting it first"); |
| 167 | |
} |
| 168 | |
|
| 169 | |
|
| 170 | 1348 | if (buffer != null) |
| 171 | |
{ |
| 172 | 428 | components.add(new Literal(buffer.toString(), this.startPosition)); |
| 173 | 428 | literalCaptureOn = false; |
| 174 | 428 | buffer = null; |
| 175 | |
} |
| 176 | |
|
| 177 | |
} |
| 178 | |
else |
| 179 | |
{ |
| 180 | 0 | throw new IllegalStateException("Cannot end a literal without beginning the template"); |
| 181 | |
} |
| 182 | 1348 | } |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
private void startExpression(int position) throws MalformedUriTemplateException |
| 189 | |
{ |
| 190 | |
|
| 191 | 1066 | if (startedTemplate) |
| 192 | |
{ |
| 193 | 1066 | if (expressionCaptureOn) |
| 194 | |
|
| 195 | |
{ |
| 196 | 6 | throw new MalformedUriTemplateException("A new expression start brace found at " + position |
| 197 | |
+ " but another unclosed expression was found at " + startPosition, position); |
| 198 | |
} |
| 199 | 1060 | literalCaptureOn = false; |
| 200 | 1060 | expressionCaptureOn = true; |
| 201 | 1060 | startPosition = position; |
| 202 | |
} |
| 203 | |
else |
| 204 | |
{ |
| 205 | 0 | throw new IllegalStateException("Cannot start an expression without beginning the template"); |
| 206 | |
} |
| 207 | 1060 | } |
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
private void endExpression(int position) throws MalformedUriTemplateException |
| 213 | |
{ |
| 214 | |
|
| 215 | |
|
| 216 | 1054 | if (startedTemplate) |
| 217 | |
{ |
| 218 | 1054 | if (!expressionCaptureOn) |
| 219 | |
|
| 220 | |
{ |
| 221 | 4 | throw new MalformedUriTemplateException("Expression close brace was found at position " + position |
| 222 | |
+ " yet there was no start brace.", position); |
| 223 | |
} |
| 224 | 1050 | expressionCaptureOn = false; |
| 225 | 1050 | components.add(new Expression(buffer.toString(), this.startPosition)); |
| 226 | 1002 | buffer = null; |
| 227 | |
} |
| 228 | |
else |
| 229 | |
{ |
| 230 | 0 | throw new IllegalStateException("Cannot end an expression without beginning the template"); |
| 231 | |
} |
| 232 | 1002 | } |
| 233 | |
} |