A Generic representation of a Template Expression as defined by RFC 6570 , with some relaxed rules, allowing the concept to be used in areas outside of the uri.
| 31 | * concept to be used in areas outside of the uri. |
| 32 | */ |
| 33 | public class Template { |
| 34 | |
| 35 | private static final Logger logger = Logger.getLogger(Template.class.getName()); |
| 36 | private static final Pattern QUERY_STRING_PATTERN = Pattern.compile("(?<!\\{)(\\?)"); |
| 37 | private final String template; |
| 38 | private final boolean allowUnresolved; |
| 39 | private final EncodingOptions encode; |
| 40 | private final boolean encodeSlash; |
| 41 | private final Charset charset; |
| 42 | private final List<TemplateChunk> templateChunks = new ArrayList<>(); |
| 43 | |
| 44 | /** |
| 45 | * Create a new Template. |
| 46 | * |
| 47 | * @param value of the template. |
| 48 | * @param allowUnresolved if unresolved expressions should remain. |
| 49 | * @param encode all values. |
| 50 | * @param encodeSlash if slash characters should be encoded. |
| 51 | */ |
| 52 | Template( |
| 53 | String value, |
| 54 | ExpansionOptions allowUnresolved, |
| 55 | EncodingOptions encode, |
| 56 | boolean encodeSlash, |
| 57 | Charset charset) { |
| 58 | if (value == null) { |
| 59 | throw new IllegalArgumentException("template is required."); |
| 60 | } |
| 61 | this.template = value; |
| 62 | this.allowUnresolved = ExpansionOptions.ALLOW_UNRESOLVED == allowUnresolved; |
| 63 | this.encode = encode; |
| 64 | this.encodeSlash = encodeSlash; |
| 65 | this.charset = charset; |
| 66 | this.parseTemplate(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Create a new Template from the provided {@link TemplateChunk}s. |
| 71 | * |
| 72 | * @param allowUnresolved if unresolved expressions should remain. |
| 73 | * @param encode all values |
| 74 | * @param encodeSlash if slash characters should be encoded. |
| 75 | * @param charset of the result. |
| 76 | * @param chunks for this template. |
| 77 | */ |
| 78 | Template( |
| 79 | ExpansionOptions allowUnresolved, |
| 80 | EncodingOptions encode, |
| 81 | boolean encodeSlash, |
| 82 | Charset charset, |
| 83 | List<TemplateChunk> chunks) { |
| 84 | this.templateChunks.addAll(chunks); |
| 85 | this.allowUnresolved = ExpansionOptions.ALLOW_UNRESOLVED == allowUnresolved; |
| 86 | this.encode = encode; |
| 87 | this.encodeSlash = encodeSlash; |
| 88 | this.charset = charset; |
| 89 | this.template = this.toString(); |
| 90 | } |