Пример #1
0
  private static StringBuilder createURIComponent(
      final UriComponent.Type t,
      String template,
      final Map<String, ? extends Object> values,
      final boolean encode,
      final StringBuilder b) {
    if (template.indexOf('{') == -1) {
      b.append(template);
      return b;
    }

    // Find all template variables
    template = new UriTemplateParser(template).getNormalizedTemplate();
    final Matcher m = TEMPLATE_NAMES_PATTERN.matcher(template);

    int i = 0;
    while (m.find()) {
      b.append(template, i, m.start());
      Object tValue = values.get(m.group(1));
      if (tValue != null) {
        if (encode) tValue = UriComponent.encode(tValue.toString(), t);
        else tValue = UriComponent.contextualEncode(tValue.toString(), t);
        b.append(tValue);
      } else {
        throw templateVariableHasNoValue(m.group(1));
      }
      i = m.end();
    }
    b.append(template, i, template.length());
    return b;
  }
Пример #2
0
  private static int createURIComponent(
      final UriComponent.Type t,
      String template,
      final String[] values,
      final int offset,
      final boolean encode,
      final Map<String, String> mapValues,
      final StringBuilder b) {
    if (template.indexOf('{') == -1) {
      b.append(template);
      return offset;
    }

    // Find all template variables
    template = new UriTemplateParser(template).getNormalizedTemplate();
    final Matcher m = TEMPLATE_NAMES_PATTERN.matcher(template);
    int v = offset;
    int i = 0;
    while (m.find()) {
      b.append(template, i, m.start());
      final String tVariable = m.group(1);
      // Check if a template variable has already occurred
      // If so use the value to ensure that two or more declarations of
      // a template variable have the same value
      String tValue = mapValues.get(tVariable);
      if (tValue != null) {
        b.append(tValue);
      } else if (v < values.length) {
        tValue = values[v++];
        if (tValue != null) {
          if (encode) tValue = UriComponent.encode(tValue, t);
          else tValue = UriComponent.contextualEncode(tValue, t);
          mapValues.put(tVariable, tValue);
          b.append(tValue);
        } else {
          throw templateVariableHasNoValue(tVariable);
        }
      } else {
        throw templateVariableHasNoValue(tVariable);
      }
      i = m.end();
    }
    b.append(template, i, template.length());
    return v;
  }
Пример #3
0
 private String encode(String s, UriComponent.Type type) {
   return UriComponent.contextualEncode(s, type, true);
 }