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) * @return array with naming (0), email (1), comment (2)
*/ */
public static String[] splitUserId(String userId) { 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); Matcher matcher = withComment.matcher(userId);
if (matcher.matches()) { if (matcher.matches()) {
result[0] = matcher.group(1); result[0] = matcher.group(1);
@ -550,13 +564,6 @@ public class PgpKeyHelper {
return result; 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; return result;
} }

View File

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

View File

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

View File

@ -89,18 +89,15 @@ public class KeyListPublicAdapter extends CursorAdapter implements StickyListHea
mainUserIdRest.setText(""); mainUserIdRest.setText("");
String userId = cursor.getString(mIndexUserId); 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) { if (userIdSplit[0] != null) {
mainUserId.setText(userIdSplit[0]); mainUserId.setText(userIdSplit[0]);
} }
if (userIdSplit[1] != null) {
if (userIdSplit[1] != null && userIdSplit[1].length() > 0) {
mainUserIdRest.setText(userIdSplit[1]); mainUserIdRest.setText(userIdSplit[1]);
} }
} }
}
@Override @Override
public View newView(Context context, Cursor cursor, ViewGroup parent) { public View newView(Context context, Cursor cursor, ViewGroup parent) {

View File

@ -76,18 +76,15 @@ public class KeyListSecretAdapter extends CursorAdapter {
mainUserIdRest.setText(""); mainUserIdRest.setText("");
String userId = cursor.getString(mIndexUserId); 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) { if (userIdSplit[0] != null) {
mainUserId.setText(userIdSplit[0]); mainUserId.setText(userIdSplit[0]);
} }
if (userIdSplit[1] != null) {
if (userIdSplit[1] != null && userIdSplit[1].length() > 0) {
mainUserIdRest.setText(userIdSplit[1]); mainUserIdRest.setText(userIdSplit[1]);
} }
} }
}
@Override @Override
public View newView(Context context, Cursor cursor, ViewGroup parent) { public View newView(Context context, Cursor cursor, ViewGroup parent) {

View File

@ -105,17 +105,14 @@ public class SelectKeyCursorAdapter extends CursorAdapter {
status.setText(R.string.unknown_status); status.setText(R.string.unknown_status);
String userId = cursor.getString(mIndexUserId); 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) { if (userIdSplit[0] != null) {
mainUserId.setText(userIdSplit[0]); mainUserId.setText(userIdSplit[0]);
} }
if (userIdSplit[1] != null) {
if (userIdSplit[1] != null && userIdSplit[1].length() > 0) {
mainUserIdRest.setText(userIdSplit[1]); mainUserIdRest.setText(userIdSplit[1]);
} }
}
long masterKeyId = cursor.getLong(mIndexMasterKeyId); long masterKeyId = cursor.getLong(mIndexMasterKeyId);
keyId.setText(PgpKeyHelper.convertKeyIdToHex(masterKeyId)); keyId.setText(PgpKeyHelper.convertKeyIdToHex(masterKeyId));