Expand data references in a URL string, using proper encoding for path and query parts.
@param urlString url
@param dataContext multi context
@param nodename default node context
@return expanded string
434715
public static String expandUrlString(
            final String urlString,
            final MultiDataContext<ContextView, DataContext> dataContext,
            final String nodename
    )
    {
        final int qindex = urlString.indexOf('?');
        final StringBuilder builder = new StringBuilder();

        builder.append(
                SharedDataContextUtils.replaceDataReferences(
                        qindex > 0 ? urlString.substring(0, qindex) : urlString,
                        dataContext,
                        ContextView.node(nodename),
                        ContextView::nodeStep,
                        urlPathEncoder,
                        true,
                        false
                ));
        if (qindex > 0) {
            builder.append("?");
            if (qindex < urlString.length() - 1) {
                builder.append(SharedDataContextUtils.replaceDataReferences(
                        urlString.substring(qindex + 1),
                        dataContext,
                        ContextView.node(nodename),
                        ContextView::nodeStep,
                        urlQueryEncoder,
                        true,
                        false
                ));
            }

        }

        return builder.toString();
    }
1060291
private void buildQueryString() throws MalformedURLException {
		StringBuilder builder = new StringBuilder();

		// Put the query parameters on the URL before issuing the request
		if (!query.isEmpty()) {
			for (Map.Entry<String, String> param : query.entrySet()) {
				builder.append(param.getKey());
				builder.append("=");
				builder.append(param.getValue());
				builder.append("&");
			}
			builder.deleteCharAt(builder.lastIndexOf("&")); // Remove the trailing ampersand
		}

		if (builder.length() > 0) {
			// If there was any query string at all, begin it with the question mark
			builder.insert(0, "?");
		}

		url = new URL(url.toString() + builder.toString());
	}
112406
public static URL createURL(URL base, String urlstring) throws MalformedURLException
    {
        if (urlstring.startsWith("data:"))
            return new URL(null, urlstring, new DataURLHandler());
        else
        {
            URL ret = new URL(base, urlstring);
            //fix the incorrect absolute URLs that contain ./ or ../
            String path = ret.getPath();
            if (path.startsWith("/./") || path.startsWith("/../"))
            {
                path = path.substring(1);
                while (path.startsWith("./") || path.startsWith("../"))
                {
                    if (path.startsWith("./"))
                        path = path.substring(2);
                    else
                        path = path.substring(3);
                }
                URL fixed = new URL(base, "/" + path);
                log.warn("Normalized non-standard URL %s to %s", ret.toString(), fixed.toString());
                ret = fixed;
            }
            return ret;
        }
    }
142421
private static String encodeExampleForUrl(Object example) {
        try {
            return URLEncoder.encode(String.valueOf(example), "UTF-8");
        } catch (UnsupportedEncodingException exception) {
            throw new RuntimeException(exception);
        }
    }
1332348
@SuppressWarnings({ "unchecked", "rawtypes" })
    public static Map<String, Object> fromQueryString(String queryString) {
        Map<String, Object> result = new LinkedHashMap<String, Object>();
        if (queryString.startsWith("?")) queryString = queryString.substring(1);

        String[] pairs = queryString.split("&");

        for (String pair : pairs) {
            int i = pair.indexOf('=');
            if (i > -1) {
                try {
                    String name = URLDecoder.decode(pair.substring(0, i), "UTF-8");
                    String value = URLDecoder.decode(pair.substring(i+1, pair.length()), "UTF-8");

                    Object current = result.get(name);
                    if (current instanceof List) {
                        ((List)current).add(value);
                    }
                    else if (current != null) {
                        List multi = new ArrayList();
                        multi.add(current);
                        multi.add(value);
                        result.put(name, multi);
                    }
                    else {
                        result.put(name, value);
                    }
                } catch (UnsupportedEncodingException e) {
                    // ignore
                }
            }
        }

        return result;
    }
1087859
public static List<Language> getLanguages(String urlString) throws TvDbException {
        List<Language> languages = new ArrayList<>();

        NodeList nlLanguages;
        Node nLanguage;
        Element eLanguage;

        Document doc = DOMHelper.getEventDocFromUrl(urlString);

        if (doc != null) {
            nlLanguages = doc.getElementsByTagName(LANGUAGE);

            for (int loop = 0; loop < nlLanguages.getLength(); loop++) {
                nLanguage = nlLanguages.item(loop);

                if (nLanguage.getNodeType() == Node.ELEMENT_NODE) {
                    eLanguage = (Element) nLanguage;
                    languages.add(parseNextLanguage(eLanguage));
                }
            }
        }

        return languages;
    }
831794
public static String expand(final String templateString, Map<String, Object> values)
    throws MalformedUriTemplateException, VariableExpansionException
    {
        UriTemplate t = new UriTemplate(templateString);
        t.set(values);
        return t.expand();
    }
284163
public static String expandHostNameVariable(String subjectDN, String nodeHostName) {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            Tr.entry(tc, "expandHostNameVariable", new Object[] { subjectDN, nodeHostName });

        String expandedSubjectDN = subjectDN;
        int index1 = subjectDN.indexOf("${hostname}");

        if (index1 != -1) {
            String firstPart = subjectDN.substring(0, index1);
            String lastPart = subjectDN.substring(index1 + "${hostname}".length());
            // String.substring always returns non-null
            if (!firstPart.equals("") && !lastPart.equals(""))
                expandedSubjectDN = firstPart + nodeHostName + lastPart;
            else if (!firstPart.equals(""))
                expandedSubjectDN = firstPart + nodeHostName;
            else if (!lastPart.equals(""))
                expandedSubjectDN = nodeHostName + lastPart;
        }

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            Tr.exit(tc, "expandHostNameVariable -> " + expandedSubjectDN);
        return expandedSubjectDN;
    }
676214
public static String buildUrlQuery(String baseUrl, Map<String, String> params) {
	if (baseUrl.isEmpty() || params.isEmpty()) {
	    return baseUrl;
	} else {
	    String query = baseUrl;
	    query += QUERY_STRING_SEPARATOR;

	    for (String key : params.keySet()) {
		query += key;
		query += PAIR_SEPARATOR;
		query += encodeString(params.get(key));
		query += PARAM_SEPARATOR;
	    }

	    return query.substring(0, query.length() - 1);
	}
    }
549974
@Override
  public String expand(Map<String, ?> variables) {
    String name = this.name.expand(variables);
    return this.queryString(name, super.expand(variables));
  }
974679
function url(queryString) {
  queryString = decodeURIComponent(queryString);
  queryString = queryString.split('&');
  return queryString.reduce((ret, url) => {
    url = url.split('=');
    var key = url[0];
    var val = url[1];

    // EXAMPLE: key[]=bar&key[]=world =>
    //  ret = { key: ['bar', 'world'] }
    if(key.slice(-2) === '[]') {
      key = key.slice(0, -2);
      ret[key] ? ret[key].push(val) : (ret[key] = [val]);

      // EXAMPLE: key[foo]=bar&key[hello]=world =>
      //  ret = { key: {foo: 'bar', hello: 'world'} }
    } else if(key.match(/\[\w+\]$/)) {
      let keys = key.split(/\[|\]/);
      let subkey = keys[1];
      key = keys[0];

      if(!ret[key]) ret[key] = {};
      ret[key][subkey] = val;
    } else {
      ret[key] = val;
    }
    return ret;
  }, {});
}
319859
public static String addQueryStringFragment(String url, String fragment) {
        StringBuilder newUrl = new StringBuilder(url);

        if (fragment.startsWith("?") || fragment.startsWith("&")) {
            fragment = fragment.substring(1);
        }

        if (!url.endsWith("?") && !url.endsWith("&")) {
            if (url.contains("?")) {
                newUrl.append('&');
            } else {
                newUrl.append('?');
            }
        }

        return newUrl.append(fragment).toString();
    }
1195100
public static String getData(String dataUrl) {
        int indexOfComma = dataUrl.indexOf(',');
        return dataUrl.substring(indexOfComma + 1);
    }
295864
public DocumentSource createDocumentSource(URL base, String urlstring)
    {
        try
        {
            Constructor<? extends DocumentSource> constr = getDocumentSourceClass().getConstructor(URL.class, String.class);
            return constr.newInstance(base, urlstring);
        } catch (Exception e) {
            log.warn("Could not create the DocumentSource instance: " + e.getMessage());
            return null;
        }
    }
28006
public String createQueryStringFromNode(Node node, @Nullable Class<?> domainType) {
		return createQueryStringFromNode(node, 0, domainType);
	}
715169
public static String getUrlParamsByMap(Map<String, String> data) throws UnsupportedEncodingException {
        if (data == null || data.isEmpty()) return null;

        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> i : data.entrySet()) {

            sb.append(i.getKey())
                    .append("=")
                    .append(URLEncoder.encode(i.getValue(), Charset.UTF_8))
                    .append("&");

        }
        String str = sb.toString();

        return str.substring(0, str.length() - 1);
    }
353154
public String concatQueryString(Map<String, String> parameters)
        throws UnsupportedEncodingException {
        if (null == parameters) {
            return null;
        }

        StringBuilder urlBuilder = new StringBuilder("");
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
            String key = entry.getKey();
            String val = entry.getValue();
            urlBuilder.append(encode(key));
            if (val != null) {
                urlBuilder.append("=").append(encode(val));
            }
            urlBuilder.append("&");
        }

        int strIndex = urlBuilder.length();
        if (parameters.size() > 0) {
            urlBuilder.deleteCharAt(strIndex - 1);
        }

        return urlBuilder.toString();
    }