| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| UriTemplateBuilder |
|
| 1.4857142857142858;1.486 |
| 1 | /* | |
| 2 | * | |
| 3 | * | |
| 4 | */ | |
| 5 | package com.damnhandy.uri.template; | |
| 6 | ||
| 7 | import com.damnhandy.uri.template.impl.Modifier; | |
| 8 | import com.damnhandy.uri.template.impl.Operator; | |
| 9 | import com.damnhandy.uri.template.impl.UriTemplateParser; | |
| 10 | import com.damnhandy.uri.template.impl.VarSpec; | |
| 11 | import org.joda.time.format.DateTimeFormat; | |
| 12 | import org.joda.time.format.DateTimeFormatter; | |
| 13 | ||
| 14 | import java.text.DateFormat; | |
| 15 | import java.text.SimpleDateFormat; | |
| 16 | import java.util.Collection; | |
| 17 | import java.util.LinkedList; | |
| 18 | import java.util.Map; | |
| 19 | ||
| 20 | /** | |
| 21 | * <p> | |
| 22 | * A utility class used for programatically generating a {@link UriTemplate}. | |
| 23 | * The class can be used as follows: | |
| 24 | * </p> | |
| 25 | * <pre> | |
| 26 | * UriTemplate template = UriTemplate.buildFromTemplate("http://example.com") | |
| 27 | * .literal("/foo") | |
| 28 | * .path(var("thing1"), var("explodedThing", true)) | |
| 29 | * .fragment(var("prefix", 2)) | |
| 30 | * .build(); | |
| 31 | * </pre> | |
| 32 | * <p> | |
| 33 | * This code will return a {@link UriTemplate} with the following value: | |
| 34 | * </p> | |
| 35 | * <pre> | |
| 36 | * http://example.com/foo{/thing1,explodedThing*}{#prefix:2} | |
| 37 | * </pre> | |
| 38 | * | |
| 39 | * @author <a href="ryan@damnhandy.com">Ryan J. McDonough</a> | |
| 40 | * @version $Revision: 1.1 $ | |
| 41 | */ | |
| 42 | public final class UriTemplateBuilder | |
| 43 | { | |
| 44 | ||
| 45 | /** | |
| 46 | * The URI expression | |
| 47 | */ | |
| 48 | 432 | private LinkedList<UriTemplateComponent> components = new LinkedList<UriTemplateComponent>(); |
| 49 | ||
| 50 | /** | |
| 51 | * | |
| 52 | */ | |
| 53 | 432 | private DateTimeFormatter defaultDateTimeFormatter = null; |
| 54 | ||
| 55 | /** | |
| 56 | * | |
| 57 | */ | |
| 58 | 432 | private Map<String, Object> values = null; |
| 59 | ||
| 60 | ||
| 61 | 180 | UriTemplateBuilder() { |
| 62 | 180 | this.components = new LinkedList<UriTemplateComponent>(); |
| 63 | 180 | } |
| 64 | /** | |
| 65 | * Create a new UriTemplateBuilder. | |
| 66 | * | |
| 67 | * @param templateString | |
| 68 | */ | |
| 69 | UriTemplateBuilder(String templateString) throws MalformedUriTemplateException | |
| 70 | 252 | { |
| 71 | 252 | this.components = new UriTemplateParser().scan(templateString); |
| 72 | 252 | } |
| 73 | ||
| 74 | ||
| 75 | /** | |
| 76 | * Create a new UriTemplateBuilder. | |
| 77 | * | |
| 78 | * @param template | |
| 79 | */ | |
| 80 | UriTemplateBuilder(UriTemplate template) throws MalformedUriTemplateException | |
| 81 | { | |
| 82 | 182 | this(template.getTemplate()); |
| 83 | 182 | this.values = template.getValues(); |
| 84 | 182 | this.defaultDateTimeFormatter = template.defaultDateTimeFormatter; |
| 85 | 182 | } |
| 86 | ||
| 87 | ||
| 88 | /** | |
| 89 | * @param dateFormatString | |
| 90 | * @return | |
| 91 | * @since 2.0 | |
| 92 | */ | |
| 93 | public UriTemplateBuilder withDefaultDateFormat(String dateFormatString) | |
| 94 | { | |
| 95 | 2 | return this.withDefaultDateFormat(DateTimeFormat.forPattern(dateFormatString)); |
| 96 | } | |
| 97 | ||
| 98 | private UriTemplateBuilder withDefaultDateFormat(DateTimeFormatter dateTimeFormatter) | |
| 99 | { | |
| 100 | 2 | defaultDateTimeFormatter = dateTimeFormatter; |
| 101 | 2 | return this; |
| 102 | } | |
| 103 | ||
| 104 | /** | |
| 105 | * @param dateFormat | |
| 106 | * @return | |
| 107 | * @since 2.0 | |
| 108 | * @deprecated replaced by {@link #withDefaultDateFormat(String) withDefaultDateFormat} | |
| 109 | */ | |
| 110 | @Deprecated | |
| 111 | public UriTemplateBuilder withDefaultDateFormat(DateFormat dateFormat) | |
| 112 | { | |
| 113 | 2 | if (!(dateFormat instanceof SimpleDateFormat)) |
| 114 | { | |
| 115 | 0 | throw new IllegalArgumentException( |
| 116 | "The only supported subclass of java.text.DateFormat is java.text.SimpleDateFormat"); | |
| 117 | } | |
| 118 | 2 | defaultDateTimeFormatter = DateTimeFormat.forPattern(((SimpleDateFormat) dateFormat).toPattern()); |
| 119 | 2 | return this; |
| 120 | } | |
| 121 | ||
| 122 | ||
| 123 | void addComponent(UriTemplateComponent component) | |
| 124 | { | |
| 125 | 342 | this.components.add(component); |
| 126 | 342 | } |
| 127 | ||
| 128 | void addComponents(Collection<UriTemplateComponent> compoments) | |
| 129 | { | |
| 130 | 4 | this.components.addAll(compoments); |
| 131 | 4 | } |
| 132 | ||
| 133 | /** | |
| 134 | * Appends a {@link Literal} value to the {@link UriTemplate}. The following | |
| 135 | * code: | |
| 136 | * <p> | |
| 137 | * <pre> | |
| 138 | * UriTemplate template = UriTemplate.buildFromTemplate("http://example.com") | |
| 139 | * .literal("/foo") | |
| 140 | * .build(); | |
| 141 | * </pre> | |
| 142 | * <p> | |
| 143 | * Will generate the following template: | |
| 144 | * <p> | |
| 145 | * <pre> | |
| 146 | * http://example.com/foo | |
| 147 | * </pre> | |
| 148 | * <p> | |
| 149 | * Note that this particular example has no expressions, so it's not a valid URI template. | |
| 150 | * | |
| 151 | * @param string | |
| 152 | * @return | |
| 153 | */ | |
| 154 | public UriTemplateBuilder literal(String string) | |
| 155 | { | |
| 156 | 18 | if (string == null) |
| 157 | { | |
| 158 | 2 | return this; |
| 159 | } | |
| 160 | 16 | addComponent(new Literal(string, 0)); |
| 161 | 16 | return this; |
| 162 | } | |
| 163 | ||
| 164 | /** | |
| 165 | * @param varSpec | |
| 166 | * @return | |
| 167 | */ | |
| 168 | private static VarSpec[] toVarSpec(String... varSpec) | |
| 169 | { | |
| 170 | 60 | VarSpec[] vars = new VarSpec[varSpec.length]; |
| 171 | 112 | for (int i = 0; i < varSpec.length; i++) |
| 172 | { | |
| 173 | 60 | vars[i] = var(varSpec[i]); |
| 174 | } | |
| 175 | 52 | return vars; |
| 176 | } | |
| 177 | ||
| 178 | /** | |
| 179 | * Appends a template expression using no operator. The following | |
| 180 | * code: | |
| 181 | * <pre> | |
| 182 | * UriTemplate template = | |
| 183 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 184 | * .simple("foo") | |
| 185 | * .build(); | |
| 186 | * </pre> | |
| 187 | * Will generate the following URI Template string: | |
| 188 | * <pre> | |
| 189 | * http://example.com/{foo} | |
| 190 | * </pre> | |
| 191 | * | |
| 192 | * @param var | |
| 193 | * @return | |
| 194 | */ | |
| 195 | public UriTemplateBuilder simple(String... var) | |
| 196 | { | |
| 197 | 4 | simple(toVarSpec(var)); |
| 198 | 4 | return this; |
| 199 | } | |
| 200 | ||
| 201 | /** | |
| 202 | * Appends a template expression using no operator but with an optional | |
| 203 | * modifier. The following code: | |
| 204 | * <pre> | |
| 205 | * import static com.damnhandy.uri.template.UriTemplateBuilder.var; | |
| 206 | * | |
| 207 | * ... | |
| 208 | * | |
| 209 | * UriTemplate template = | |
| 210 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 211 | * .simple(var("foo",true)) | |
| 212 | * .build(); | |
| 213 | * </pre> | |
| 214 | * Will generate the following URI Template string: | |
| 215 | * <pre> | |
| 216 | * http://example.com/{foo*} | |
| 217 | * </pre> | |
| 218 | * | |
| 219 | * @param var | |
| 220 | * @return | |
| 221 | */ | |
| 222 | public UriTemplateBuilder simple(VarSpec... var) | |
| 223 | { | |
| 224 | 4 | addComponent(Expression.simple(var).build()); |
| 225 | 4 | return this; |
| 226 | } | |
| 227 | ||
| 228 | /** | |
| 229 | * Appends a template expression using the reserved operator (+). The following | |
| 230 | * code: | |
| 231 | * <pre> | |
| 232 | * UriTemplate template = | |
| 233 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 234 | * .reserved("foo") | |
| 235 | * .build(); | |
| 236 | * </pre> | |
| 237 | * Will generate the following URI Template string: | |
| 238 | * <pre> | |
| 239 | * http://example.com/{+foo} | |
| 240 | * </pre> | |
| 241 | * | |
| 242 | * @param var | |
| 243 | * @return | |
| 244 | */ | |
| 245 | public UriTemplateBuilder reserved(String... var) | |
| 246 | { | |
| 247 | 8 | reserved(toVarSpec(var)); |
| 248 | 8 | return this; |
| 249 | } | |
| 250 | ||
| 251 | /** | |
| 252 | * Appends a template expression using the reserved operator (+) along | |
| 253 | * with an optional modifier. The following code: | |
| 254 | * <pre> | |
| 255 | * import static com.damnhandy.uri.template.UriTemplateBuilder.var; | |
| 256 | * | |
| 257 | * ... | |
| 258 | * | |
| 259 | * UriTemplate template = | |
| 260 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 261 | * .reserved(var("foo",1)) | |
| 262 | * .build(); | |
| 263 | * </pre> | |
| 264 | * Will generate the following URI Template string: | |
| 265 | * <pre> | |
| 266 | * http://example.com/{+foo:1} | |
| 267 | * </pre> | |
| 268 | * | |
| 269 | * @param var | |
| 270 | * @return | |
| 271 | */ | |
| 272 | public UriTemplateBuilder reserved(VarSpec... var) | |
| 273 | { | |
| 274 | 12 | addComponent(Expression.reserved(var).build()); |
| 275 | 12 | return this; |
| 276 | } | |
| 277 | ||
| 278 | /** | |
| 279 | * Appends a template expression using the fragment operator (#). The | |
| 280 | * following code: | |
| 281 | * <pre> | |
| 282 | * UriTemplate template = | |
| 283 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 284 | * .fragement("foo") | |
| 285 | * .build(); | |
| 286 | * </pre> | |
| 287 | * Will generate the following URI Template string: | |
| 288 | * <pre> | |
| 289 | * http://example.com/{#foo} | |
| 290 | * </pre> | |
| 291 | * | |
| 292 | * @param var | |
| 293 | * @return | |
| 294 | * @throws UriTemplateBuilderException if you attempt to add more than one fragment expression, a UriTemplateBuilderException will be raised | |
| 295 | */ | |
| 296 | public UriTemplateBuilder fragment(String... var) throws UriTemplateBuilderException | |
| 297 | { | |
| 298 | 12 | fragment(toVarSpec(var)); |
| 299 | 8 | return this; |
| 300 | } | |
| 301 | ||
| 302 | /** | |
| 303 | * Appends a template expression using the fragment operator (#) with a | |
| 304 | * modifier. The following code: | |
| 305 | * <pre> | |
| 306 | * import static com.damnhandy.uri.template.UriTemplateBuilder.var; | |
| 307 | * | |
| 308 | * ... | |
| 309 | * | |
| 310 | * UriTemplate template = | |
| 311 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 312 | * .fragement(var("foo", 1)) | |
| 313 | * .build(); | |
| 314 | * </pre> | |
| 315 | * Will generate the following URI Template string: | |
| 316 | * <pre> | |
| 317 | * http://example.com/{#foo:1} | |
| 318 | * </pre> | |
| 319 | * | |
| 320 | * @param var | |
| 321 | * @return | |
| 322 | * @throws UriTemplateBuilderException if you attempt to add more than one fragment expression, a UriTemplateBuilderException will be raised | |
| 323 | */ | |
| 324 | public UriTemplateBuilder fragment(VarSpec... var) throws UriTemplateBuilderException | |
| 325 | { | |
| 326 | 20 | if (hasExpressionWithOperator(Operator.FRAGMENT)) |
| 327 | { | |
| 328 | 2 | throw new UriTemplateBuilderException("The template already has a fragment expression and this would not result in a valid URI"); |
| 329 | } | |
| 330 | 18 | addComponent(Expression.fragment(var).build()); |
| 331 | 18 | return this; |
| 332 | } | |
| 333 | ||
| 334 | /** | |
| 335 | * Scans the components for an expression with the specified operator. | |
| 336 | * | |
| 337 | * @param op | |
| 338 | * @return | |
| 339 | */ | |
| 340 | private boolean hasExpressionWithOperator(Operator op) | |
| 341 | { | |
| 342 | 20 | for (UriTemplateComponent c : components) |
| 343 | { | |
| 344 | 46 | if (Expression.class.isInstance(c)) |
| 345 | { | |
| 346 | 22 | Expression e = (Expression) c; |
| 347 | 22 | if (e.getOperator() == op) |
| 348 | { | |
| 349 | 2 | return true; |
| 350 | } | |
| 351 | } | |
| 352 | 44 | } |
| 353 | 18 | return false; |
| 354 | } | |
| 355 | ||
| 356 | /** | |
| 357 | * Appends a template expression using the label (.) operator. The following | |
| 358 | * code: | |
| 359 | * <pre> | |
| 360 | * UriTemplate template = | |
| 361 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 362 | * .label("foo") | |
| 363 | * .build(); | |
| 364 | * </pre> | |
| 365 | * Will generate the following URI Template string: | |
| 366 | * <pre> | |
| 367 | * http://example.com/{.foo} | |
| 368 | * </pre> | |
| 369 | * | |
| 370 | * @param var | |
| 371 | * @return | |
| 372 | */ | |
| 373 | public UriTemplateBuilder label(String... var) | |
| 374 | { | |
| 375 | 2 | label(toVarSpec(var)); |
| 376 | 2 | return this; |
| 377 | } | |
| 378 | ||
| 379 | /** | |
| 380 | * Appends a template expression using the label (.) operator and modifier. | |
| 381 | * The following code: | |
| 382 | * <pre> | |
| 383 | * UriTemplate template = | |
| 384 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 385 | * .label(var("foo", true)) | |
| 386 | * .build(); | |
| 387 | * </pre> | |
| 388 | * Will generate the following URI Template string: | |
| 389 | * <pre> | |
| 390 | * http://example.com/{.foo*} | |
| 391 | * </pre> | |
| 392 | * | |
| 393 | * @param var | |
| 394 | * @return | |
| 395 | */ | |
| 396 | public UriTemplateBuilder label(VarSpec... var) | |
| 397 | { | |
| 398 | 6 | addComponent(Expression.label(var).build()); |
| 399 | 6 | return this; |
| 400 | } | |
| 401 | ||
| 402 | /** | |
| 403 | * Appends a template expression using the matrix (;) operator. The following | |
| 404 | * code: | |
| 405 | * <pre> | |
| 406 | * UriTemplate template = | |
| 407 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 408 | * .matrix("foo") | |
| 409 | * .build(); | |
| 410 | * </pre> | |
| 411 | * Will generate the following URI Template string: | |
| 412 | * <pre> | |
| 413 | * http://example.com/{;foo} | |
| 414 | * </pre> | |
| 415 | * | |
| 416 | * @param var | |
| 417 | * @return | |
| 418 | */ | |
| 419 | public UriTemplateBuilder matrix(String... var) | |
| 420 | { | |
| 421 | 2 | matrix(toVarSpec(var)); |
| 422 | 2 | return this; |
| 423 | } | |
| 424 | ||
| 425 | /** | |
| 426 | * Appends a template expression using the matrix (;) operator and modifier. | |
| 427 | * The following code: | |
| 428 | * <pre> | |
| 429 | * UriTemplate template = | |
| 430 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 431 | * .matrix(var("foo", true)) | |
| 432 | * .build(); | |
| 433 | * </pre> | |
| 434 | * Will generate the following URI Template string: | |
| 435 | * <pre> | |
| 436 | * http://example.com/{;foo*} | |
| 437 | * </pre> | |
| 438 | * | |
| 439 | * @param var | |
| 440 | * @return | |
| 441 | */ | |
| 442 | public UriTemplateBuilder matrix(VarSpec... var) | |
| 443 | { | |
| 444 | 2 | addComponent(Expression.matrix(var).build()); |
| 445 | 2 | return this; |
| 446 | } | |
| 447 | ||
| 448 | /** | |
| 449 | * Appends a template expression using the path (/) operator. The following | |
| 450 | * code: | |
| 451 | * <pre> | |
| 452 | * UriTemplate template = | |
| 453 | * UriTemplate.buildFromTemplate("http://example.com") | |
| 454 | * .path("foo") | |
| 455 | * .build(); | |
| 456 | * </pre> | |
| 457 | * Will generate the following URI Template string: | |
| 458 | * <pre> | |
| 459 | * http://example.com{/foo} | |
| 460 | * </pre> | |
| 461 | * | |
| 462 | * @param var | |
| 463 | * @return | |
| 464 | */ | |
| 465 | public UriTemplateBuilder path(String... var) | |
| 466 | { | |
| 467 | 22 | path(toVarSpec(var)); |
| 468 | 20 | return this; |
| 469 | } | |
| 470 | ||
| 471 | /** | |
| 472 | * Appends a template expression using the path (/) operator and modifier. | |
| 473 | * The following code: | |
| 474 | * <pre> | |
| 475 | * UriTemplate template = | |
| 476 | * UriTemplate.buildFromTemplate("http://example.com") | |
| 477 | * .path(var("foo", 1)) | |
| 478 | * .build(); | |
| 479 | * </pre> | |
| 480 | * Will generate the following URI Template string: | |
| 481 | * <pre> | |
| 482 | * http://example.com{/foo:1} | |
| 483 | * </pre> | |
| 484 | * | |
| 485 | * @param var | |
| 486 | * @return | |
| 487 | */ | |
| 488 | public UriTemplateBuilder path(VarSpec... var) | |
| 489 | { | |
| 490 | 26 | addComponent(Expression.path(var).build()); |
| 491 | 26 | return this; |
| 492 | } | |
| 493 | ||
| 494 | /** | |
| 495 | * Appends a template expression using the query (?) operator. The following | |
| 496 | * code: | |
| 497 | * <pre> | |
| 498 | * UriTemplate template = | |
| 499 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 500 | * .query("foo") | |
| 501 | * .build(); | |
| 502 | * </pre> | |
| 503 | * Will generate the following URI Template string: | |
| 504 | * <pre> | |
| 505 | * http://example.com/{?foo} | |
| 506 | * </pre> | |
| 507 | * | |
| 508 | * @param var | |
| 509 | * @return | |
| 510 | */ | |
| 511 | public UriTemplateBuilder query(String... var) | |
| 512 | { | |
| 513 | 8 | query(toVarSpec(var)); |
| 514 | 4 | return this; |
| 515 | } | |
| 516 | ||
| 517 | /** | |
| 518 | * Appends a template expression using the query (?) operator and | |
| 519 | * and optional modifier. The following | |
| 520 | * code: | |
| 521 | * <pre> | |
| 522 | * import static com.damnhandy.uri.template.UriTemplateBuilder.var; | |
| 523 | * | |
| 524 | * ... | |
| 525 | * | |
| 526 | * UriTemplate template = | |
| 527 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 528 | * .query(var("foo",1)) | |
| 529 | * .build(); | |
| 530 | * </pre> | |
| 531 | * Will generate the following URI Template string: | |
| 532 | * <pre> | |
| 533 | * http://example.com/{?foo:1} | |
| 534 | * </pre> | |
| 535 | * | |
| 536 | * @param var | |
| 537 | * @return | |
| 538 | */ | |
| 539 | public UriTemplateBuilder query(VarSpec... var) | |
| 540 | { | |
| 541 | 14 | addComponent(Expression.query(var).build()); |
| 542 | 14 | return this; |
| 543 | } | |
| 544 | ||
| 545 | /** | |
| 546 | * Appends a template expression using the form-style query continuation. The following | |
| 547 | * code: | |
| 548 | * <pre> | |
| 549 | * UriTemplate template = | |
| 550 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 551 | * .continuation("foo") | |
| 552 | * .build(); | |
| 553 | * </pre> | |
| 554 | * Will generate the following URI Template string: | |
| 555 | * <pre> | |
| 556 | * http://example.com/{&foo} | |
| 557 | * </pre> | |
| 558 | * | |
| 559 | * @param var | |
| 560 | * @return | |
| 561 | */ | |
| 562 | public UriTemplateBuilder continuation(String... var) | |
| 563 | { | |
| 564 | 2 | return continuation(toVarSpec(var)); |
| 565 | } | |
| 566 | ||
| 567 | /** | |
| 568 | * Appends a template expression using the form-style query continuation and | |
| 569 | * and optional modifier. The following | |
| 570 | * code: | |
| 571 | * <pre> | |
| 572 | * import static com.damnhandy.uri.template.UriTemplateBuilder.var; | |
| 573 | * | |
| 574 | * ... | |
| 575 | * | |
| 576 | * UriTemplate template = | |
| 577 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 578 | * .query(var("foo",1)) | |
| 579 | * .build(); | |
| 580 | * </pre> | |
| 581 | * Will generate the following URI Template string: | |
| 582 | * <pre> | |
| 583 | * http://example.com/{&foo:1} | |
| 584 | * </pre> | |
| 585 | * | |
| 586 | * @param var | |
| 587 | * @return | |
| 588 | */ | |
| 589 | public UriTemplateBuilder continuation(VarSpec... var) | |
| 590 | { | |
| 591 | 6 | addComponent(Expression.continuation(var).build()); |
| 592 | 6 | return this; |
| 593 | } | |
| 594 | ||
| 595 | /** | |
| 596 | * Parses the template and appends the parsed components | |
| 597 | * to the builder. The following | |
| 598 | * code: | |
| 599 | * <pre> | |
| 600 | * import static com.damnhandy.uri.template.UriTemplateBuilder.var; | |
| 601 | * | |
| 602 | * ... | |
| 603 | * | |
| 604 | * UriTemplate template = | |
| 605 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 606 | * .template("foo/{id}{?filter}") | |
| 607 | * .build(); | |
| 608 | * </pre> | |
| 609 | * Will generate the following URI Template string: | |
| 610 | * <pre> | |
| 611 | * http://example.com/foo/{id}{?filter} | |
| 612 | * </pre> | |
| 613 | * | |
| 614 | * @param template | |
| 615 | * @return | |
| 616 | */ | |
| 617 | public UriTemplateBuilder template(UriTemplate... template) { | |
| 618 | 4 | for(UriTemplate t : template) { |
| 619 | 2 | addComponents(t.getComponents()); |
| 620 | } | |
| 621 | ||
| 622 | 2 | return this; |
| 623 | } | |
| 624 | ||
| 625 | /** | |
| 626 | * Parses the template and appends the parsed components | |
| 627 | * to the builder. The following | |
| 628 | * code: | |
| 629 | * <pre> | |
| 630 | * import static com.damnhandy.uri.template.UriTemplateBuilder.var; | |
| 631 | * | |
| 632 | * ... | |
| 633 | * | |
| 634 | * UriTemplate template = | |
| 635 | * UriTemplate.buildFromTemplate("http://example.com/") | |
| 636 | * .template("foo/{id}{?filter}") | |
| 637 | * .build(); | |
| 638 | * </pre> | |
| 639 | * Will generate the following URI Template string: | |
| 640 | * <pre> | |
| 641 | * http://example.com/foo/{id}{?filter} | |
| 642 | * </pre> | |
| 643 | * | |
| 644 | * @param template | |
| 645 | * @return | |
| 646 | */ | |
| 647 | public UriTemplateBuilder template(String... template) { | |
| 648 | 2 | UriTemplateParser parser = new UriTemplateParser(); |
| 649 | 4 | for(String t : template) { |
| 650 | 2 | addComponents(parser.scan(t)); |
| 651 | } | |
| 652 | ||
| 653 | 2 | return this; |
| 654 | } | |
| 655 | ||
| 656 | /** | |
| 657 | * Returns an array of the components in the Builder. | |
| 658 | * @return | |
| 659 | */ | |
| 660 | public UriTemplateComponent[] getComponents() | |
| 661 | { | |
| 662 | 178 | return this.components.toArray(new UriTemplateComponent[components.size()]); |
| 663 | } | |
| 664 | ||
| 665 | ||
| 666 | /** | |
| 667 | * <p> | |
| 668 | * Generates a {@link UriTemplate} instance from the builder. | |
| 669 | * </p> | |
| 670 | * | |
| 671 | * @return the UriTemplate | |
| 672 | * @since 2.0 | |
| 673 | */ | |
| 674 | public UriTemplate build() throws MalformedUriTemplateException | |
| 675 | { | |
| 676 | 244 | UriTemplate template = new UriTemplate(this.components); |
| 677 | 244 | if (this.values != null) |
| 678 | { | |
| 679 | 4 | template.set(values); |
| 680 | } | |
| 681 | ||
| 682 | 244 | if (defaultDateTimeFormatter != null) |
| 683 | { | |
| 684 | 8 | template.defaultDateTimeFormatter = defaultDateTimeFormatter; |
| 685 | } | |
| 686 | 244 | return template; |
| 687 | } | |
| 688 | ||
| 689 | /** | |
| 690 | * Adds a variable name to the expression. | |
| 691 | * <p> | |
| 692 | * <pre> | |
| 693 | * var("foo"); | |
| 694 | * </pre> | |
| 695 | * <p> | |
| 696 | * Will yield the following expression: | |
| 697 | * <pre> | |
| 698 | * {foo} | |
| 699 | * </pre> | |
| 700 | * | |
| 701 | * @param varName | |
| 702 | * @return the {@link com.damnhandy.uri.template.impl.VarSpec} for the specified name | |
| 703 | */ | |
| 704 | public static VarSpec var(String varName) | |
| 705 | { | |
| 706 | 88 | return var(varName, Modifier.NONE, null); |
| 707 | } | |
| 708 | ||
| 709 | /** | |
| 710 | * Adds a variable name to the expression with an explode modifier. | |
| 711 | * <p> | |
| 712 | * <pre> | |
| 713 | * var("foo",true); | |
| 714 | * </pre> | |
| 715 | * <p> | |
| 716 | * Will yield the following expression: | |
| 717 | * <pre> | |
| 718 | * {foo*} | |
| 719 | * </pre> | |
| 720 | * | |
| 721 | * @param varName | |
| 722 | * @param explode | |
| 723 | * @return the {@link com.damnhandy.uri.template.impl.VarSpec} for the specified name | |
| 724 | */ | |
| 725 | public static VarSpec var(String varName, boolean explode) | |
| 726 | { | |
| 727 | 26 | if (explode) |
| 728 | { | |
| 729 | 26 | return var(varName, Modifier.EXPLODE, null); |
| 730 | } | |
| 731 | 0 | return var(varName, Modifier.NONE, null); |
| 732 | } | |
| 733 | ||
| 734 | /** | |
| 735 | * Adds a variable name to the expression with a prefix modifier. | |
| 736 | * <p> | |
| 737 | * <pre> | |
| 738 | * var("foo",2); | |
| 739 | * </pre> | |
| 740 | * <p> | |
| 741 | * Will yield the following expression: | |
| 742 | * <pre> | |
| 743 | * {foo:2} | |
| 744 | * </pre> | |
| 745 | * | |
| 746 | * @param varName | |
| 747 | * @param prefix | |
| 748 | * @return the {@link com.damnhandy.uri.template.impl.VarSpec} for the specified name | |
| 749 | */ | |
| 750 | public static VarSpec var(String varName, int prefix) | |
| 751 | { | |
| 752 | 22 | return var(varName, Modifier.PREFIX, prefix); |
| 753 | } | |
| 754 | ||
| 755 | /** | |
| 756 | * @param varName | |
| 757 | * @param modifier | |
| 758 | * @param position | |
| 759 | * @return the {@link com.damnhandy.uri.template.impl.VarSpec} for the specified name | |
| 760 | */ | |
| 761 | private static VarSpec var(String varName, Modifier modifier, Integer position) | |
| 762 | { | |
| 763 | 136 | return new VarSpec(varName, modifier, position); |
| 764 | } | |
| 765 | ||
| 766 | } |