URIBuilder class
class URIBuilder { Map<String,String> queryParams = new Map<String,String>(); String _scheme = "http"; String _userInfo = ""; String _host = ""; String _path = ""; String _fragment = ""; int port = 0; URIBuilder() { } URIBuilder.fromString(final String string) { _digestURI(Uri.parse(string)); } URIBuilder.fromUri(final Uri uri) { _digestURI(uri); } /** * Builds a Uri instance. */ Uri build({final bool encode: true}) { final Uri uri = new Uri( scheme: _scheme, userInfo: _userInfo, host: _host, port: port, path: _path, queryParameters: queryParams, fragment: _fragment ); return encode ? Uri.parse(Uri.encodeFull(uri.toString())) : uri; } Uri decode() { return Uri.parse(Uri.decodeFull(build().toString())); } /// checks if the path is empty bool get isEmpty => _path.isEmpty; /// checks if the path is not empty bool get isNotEmpty => _path.isNotEmpty; /** * Sets Uri scheme. */ URIBuilder setScheme(final String scheme) { this._scheme = scheme; return this; } /** * Sets Uri user-info in a form of 'username:password'. */ URIBuilder setUserInfo(final String username, final String password) { this._userInfo = "$username:$password"; return this; } /** * Sets Uri host. */ URIBuilder setHost(final String host) { this._host = host; return this; } /** * Sets Uri port. */ URIBuilder setPort(final int port) { this.port = port < 0 ? 0 : port; return this; } /** * Sets Uri path. */ URIBuilder setPath(final String path) { this._path = path; return this; } /** * Removes all query parameters. */ URIBuilder removeQuery() { this.queryParams.clear(); return this; } /** * Set Uri query. */ URIBuilder setQuery(final String query) { queryParams = new Map<String,String>.from(_parseQuery(query)); return this; } /** * Sets parameter-value pair to Uri query removing existing parameters with the same name. */ URIBuilder setParameter(final String name, final String value) { this.queryParams[name] = value; return this; } /** * Sets Uri fragment. */ URIBuilder setFragment(final String fragment) { this._fragment = fragment; return this; } //--------------------------------------------------------------------------------------------- // private //--------------------------------------------------------------------------------------------- Map<String,String> _parseQuery(final String query) { final Map<String,String> nvps = new Map<String,String>(); if (query != null && query.length > 0) { final List<String> namevalues = query.split("&"); for(final String namevalue in namevalues) { final List<String> pair = namevalue.split("="); if(pair.length == 2) { nvps[pair[0]] = pair[1]; } } } return nvps; } void _digestURI(final Uri uri) { this._scheme = uri.scheme; this._host = uri.host; this.port = uri.port; this._userInfo = ""; this._path = uri.path; this._fragment = uri.fragment; this.queryParams = new Map<String,String>.from(uri.queryParameters); } }
Constructors
new URIBuilder() #
URIBuilder() { }
Properties
Methods
Uri build({bool encode: true}) #
Builds a Uri instance.
Uri build({final bool encode: true}) { final Uri uri = new Uri( scheme: _scheme, userInfo: _userInfo, host: _host, port: port, path: _path, queryParameters: queryParams, fragment: _fragment ); return encode ? Uri.parse(Uri.encodeFull(uri.toString())) : uri; }
URIBuilder removeQuery() #
Removes all query parameters.
URIBuilder removeQuery() { this.queryParams.clear(); return this; }
URIBuilder setFragment(String fragment) #
Sets Uri fragment.
URIBuilder setFragment(final String fragment) { this._fragment = fragment; return this; }
URIBuilder setHost(String host) #
Sets Uri host.
URIBuilder setHost(final String host) { this._host = host; return this; }
URIBuilder setParameter(String name, String value) #
Sets parameter-value pair to Uri query removing existing parameters with the same name.
URIBuilder setParameter(final String name, final String value) { this.queryParams[name] = value; return this; }
URIBuilder setPath(String path) #
Sets Uri path.
URIBuilder setPath(final String path) { this._path = path; return this; }
URIBuilder setPort(int port) #
Sets Uri port.
URIBuilder setPort(final int port) { this.port = port < 0 ? 0 : port; return this; }
URIBuilder setQuery(String query) #
Set Uri query.
URIBuilder setQuery(final String query) { queryParams = new Map<String,String>.from(_parseQuery(query)); return this; }
URIBuilder setScheme(String scheme) #
Sets Uri scheme.
URIBuilder setScheme(final String scheme) { this._scheme = scheme; return this; }
URIBuilder setUserInfo(String username, String password) #
Sets Uri user-info in a form of 'username:password'.
URIBuilder setUserInfo(final String username, final String password) { this._userInfo = "$username:$password"; return this; }