aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java
diff options
context:
space:
mode:
authorVincent Breitmoser <valodim@mugenguild.com>2014-06-04 20:27:25 +0200
committerVincent Breitmoser <valodim@mugenguild.com>2014-06-04 20:27:25 +0200
commitcc1e4dce0a71161911091ff7c2bfa0947642cd79 (patch)
tree0ed52004db9d107c6e0f30b6c1db35555d7b3bfb /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java
parent52f1f30846ad7efa6e6ae11ed96f5b68626bfb3b (diff)
downloadopen-keychain-cc1e4dce0a71161911091ff7c2bfa0947642cd79.tar.gz
open-keychain-cc1e4dce0a71161911091ff7c2bfa0947642cd79.tar.bz2
open-keychain-cc1e4dce0a71161911091ff7c2bfa0947642cd79.zip
neatness refactoring
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java42
1 files changed, 42 insertions, 0 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java
index dc0c722b9..47b827677 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java
@@ -2,6 +2,9 @@ package org.sufficientlysecure.keychain.pgp;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
/** An abstract KeyRing.
*
* This is an abstract class for all KeyRing constructs. It serves as a common
@@ -19,6 +22,10 @@ public abstract class KeyRing {
abstract public String getPrimaryUserId() throws PgpGeneralException;
+ public String[] getSplitPrimaryUserId() throws PgpGeneralException {
+ return splitUserId(getPrimaryUserId());
+ }
+
abstract public boolean isRevoked() throws PgpGeneralException;
abstract public boolean canCertify() throws PgpGeneralException;
@@ -33,4 +40,39 @@ public abstract class KeyRing {
abstract public int getVerified() throws PgpGeneralException;
+ private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$");
+
+ /**
+ * Splits userId string into naming part, email part, and comment part
+ *
+ * @param userId
+ * @return array with naming (0), email (1), comment (2)
+ */
+ public static String[] splitUserId(String userId) {
+ String[] result = new String[]{null, null, null};
+
+ 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]"
+ */
+ Matcher matcher = USER_ID_PATTERN.matcher(userId);
+ if (matcher.matches()) {
+ result[0] = matcher.group(1);
+ result[1] = matcher.group(3);
+ result[2] = matcher.group(2);
+ }
+
+ return result;
+ }
+
}