Fix and simplify user id parsing

This commit is contained in:
Dominik Schürmann 2014-01-31 17:55:24 +01:00
parent 3c897d68ff
commit d8a91f15df
6 changed files with 50 additions and 54 deletions

View File

@ -539,9 +539,23 @@ public class PgpKeyHelper {
* @return array with naming (0), email (1), comment (2)
*/
public static String[] splitUserId(String userId) {
String[] result = new String[] { "", "", "" };
String[] result = new String[] { null, null, null };
Pattern withComment = Pattern.compile("^(.*) \\((.*)\\) <(.*)>$");
if (userId == null || userId.equals("")) {
return result;
}
/*
* User ID matching:
* http://fiddle.re/t4p6f
*
* test cases:
* "Max Mustermann (this is a comment) <max@example.com>"
* "Max Mustermann <max@example.com>"
* "Max Mustermann (this is a comment)"
* "Max Mustermann [this is nothing]"
*/
Pattern withComment = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$");
Matcher matcher = withComment.matcher(userId);
if (matcher.matches()) {
result[0] = matcher.group(1);
@ -550,13 +564,6 @@ public class PgpKeyHelper {
return result;
}
Pattern withoutComment = Pattern.compile("^(.*) <(.*)>$");
matcher = withoutComment.matcher(userId);
if (matcher.matches()) {
result[0] = matcher.group(1);
result[1] = matcher.group(2);
return result;
}
return result;
}

View File

@ -300,7 +300,7 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements
// get name, email, and comment from USER_ID
String[] mainUserId = PgpKeyHelper.splitUserId(data
.getString(KEYRING_INDEX_USER_ID));
if (mainUserId[0] != null && mainUserId[0].length() > 0) {
if (mainUserId[0] != null) {
setTitle(mainUserId[0]);
mName.setText(mainUserId[0]);
} else {

View File

@ -93,26 +93,24 @@ public class ImportKeysAdapter extends ArrayAdapter<ImportKeysListEntry> {
// main user id
String userId = entry.userIds.get(0);
if (userId != null) {
String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
// name
if (userIdSplit[0] != null && userIdSplit[0].length() > 0) {
// show red user id if it is a secret key
if (entry.secretKey) {
userIdSplit[0] = mActivity.getString(R.string.secret_key) + " " + userIdSplit[0];
mainUserId.setTextColor(Color.RED);
}
mainUserId.setText(userIdSplit[0]);
// name
if (userIdSplit[0] != null) {
// show red user id if it is a secret key
if (entry.secretKey) {
userIdSplit[0] = mActivity.getString(R.string.secret_key) + " " + userIdSplit[0];
mainUserId.setTextColor(Color.RED);
}
mainUserId.setText(userIdSplit[0]);
}
// email
if (userIdSplit[1] != null && userIdSplit[1].length() > 0) {
mainUserIdRest.setText(userIdSplit[1]);
mainUserIdRest.setVisibility(View.VISIBLE);
} else {
mainUserIdRest.setVisibility(View.GONE);
}
// email
if (userIdSplit[1] != null) {
mainUserIdRest.setText(userIdSplit[1]);
mainUserIdRest.setVisibility(View.VISIBLE);
} else {
mainUserIdRest.setVisibility(View.GONE);
}
keyId.setText(entry.hexKeyId);

View File

@ -89,16 +89,13 @@ public class KeyListPublicAdapter extends CursorAdapter implements StickyListHea
mainUserIdRest.setText("");
String userId = cursor.getString(mIndexUserId);
if (userId != null) {
String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
if (userIdSplit[0] != null && userIdSplit[0].length() > 0) {
mainUserId.setText(userIdSplit[0]);
}
if (userIdSplit[1] != null && userIdSplit[1].length() > 0) {
mainUserIdRest.setText(userIdSplit[1]);
}
if (userIdSplit[0] != null) {
mainUserId.setText(userIdSplit[0]);
}
if (userIdSplit[1] != null) {
mainUserIdRest.setText(userIdSplit[1]);
}
}

View File

@ -76,16 +76,13 @@ public class KeyListSecretAdapter extends CursorAdapter {
mainUserIdRest.setText("");
String userId = cursor.getString(mIndexUserId);
if (userId != null) {
String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
if (userIdSplit[0] != null && userIdSplit[0].length() > 0) {
mainUserId.setText(userIdSplit[0]);
}
if (userIdSplit[1] != null && userIdSplit[1].length() > 0) {
mainUserIdRest.setText(userIdSplit[1]);
}
if (userIdSplit[0] != null) {
mainUserId.setText(userIdSplit[0]);
}
if (userIdSplit[1] != null) {
mainUserIdRest.setText(userIdSplit[1]);
}
}

View File

@ -105,16 +105,13 @@ public class SelectKeyCursorAdapter extends CursorAdapter {
status.setText(R.string.unknown_status);
String userId = cursor.getString(mIndexUserId);
if (userId != null) {
String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
if (userIdSplit[0] != null && userIdSplit[0].length() > 0) {
mainUserId.setText(userIdSplit[0]);
}
if (userIdSplit[1] != null && userIdSplit[1].length() > 0) {
mainUserIdRest.setText(userIdSplit[1]);
}
if (userIdSplit[0] != null) {
mainUserId.setText(userIdSplit[0]);
}
if (userIdSplit[1] != null) {
mainUserIdRest.setText(userIdSplit[1]);
}
long masterKeyId = cursor.getLong(mIndexMasterKeyId);