If you think the Android project cube-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package in.srain.cube.uri;
//fromwww.java2s.comimport android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import in.srain.cube.request.JsonData;
publicclass CustomizedUri {
privatestaticfinal String KEY = "CUBE_URL";
private JsonData mQueryData;
private String mPath = null;
private String mQuery = null;
private String mUrl = null;
public String getPath() {
return mPath;
}
public String getQueryString() {
return mQuery;
}
public JsonData getQueryData() {
return mQueryData;
}
public String getUrl() {
return mUrl;
}
publicstatic CustomizedUri parse(String url, String customizedSchema) {
if (TextUtils.isEmpty(url)) {
thrownew RuntimeException("url is null");
}
if (TextUtils.isEmpty(url)) {
thrownew RuntimeException("url is null");
}
if (!url.startsWith(customizedSchema)) {
return null;
}
returnnew CustomizedUri(url, customizedSchema.length());
}
publicvoid writeToBundle(Intent intent) {
intent.putExtra(KEY, getUrl());
}
publicstatic CustomizedUri fromIntent(Intent intent, String schema) {
if (intent == null) {
return null;
}
String url = intent.getStringExtra(KEY);
if (TextUtils.isEmpty(url)) {
return null;
}
return parse(url, schema);
}
private CustomizedUri(String url, int len) {
mUrl = url;
// process segment
int pos_seg = url.indexOf('#');
if (pos_seg > 0) {
url = url.substring(0, pos_seg);
}
// process query
int pos_sp = url.indexOf('?');
if (pos_sp > 0) {
mQuery = url.substring(pos_sp + 1);
url = url.substring(0, pos_sp);
}
mPath = url.substring(len);
if (!TextUtils.isEmpty(mQuery)) {
parseQuery();
}
}
privatevoid parseQuery() {
mQueryData = JsonData.newMap();
String query = mQuery;
int start = 0;
do {
int nextAmpersand = query.indexOf('&', start);
// to next & or to the end of String
int end = nextAmpersand != -1 ? nextAmpersand : query.length();
// no more key-value
int separator = query.indexOf('=', start);
if (separator > end || separator == -1) {
break;
}
// make sure key is no empty
if (separator != start) {
mQueryData.put(Uri.decode(query.substring(start, separator)),
Uri.decode(query.substring(separator + 1, end)));
}
// Move start to end of name.
if (nextAmpersand != -1) {
start = nextAmpersand + 1;
} else {
break;
}
} while (true);
}
}