aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDominik Schürmann <dominik@dominikschuermann.de>2015-03-16 13:53:28 +0100
committerDominik Schürmann <dominik@dominikschuermann.de>2015-03-16 13:53:28 +0100
commit10be5948d68bb5e80aed77fdf93cada598ee7667 (patch)
tree4f460250588e948cd444d55cdc55c64e7329684d
parentbc177ed5e3f110cf372d6303c8e9d21e46fc76d2 (diff)
downloadopenpgp-api-10be5948d68bb5e80aed77fdf93cada598ee7667.tar.gz
openpgp-api-10be5948d68bb5e80aed77fdf93cada598ee7667.tar.bz2
openpgp-api-10be5948d68bb5e80aed77fdf93cada598ee7667.zip
object oriented split user id
-rw-r--r--src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java33
1 files changed, 27 insertions, 6 deletions
diff --git a/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java b/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java
index ef0a88c..ad5f47b 100644
--- a/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java
+++ b/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java
@@ -71,7 +71,8 @@ public class OpenPgpUtils {
return hexString;
}
- private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: (\\[.*\\]))?(?: \\((.*)\\))?(?: <(.*)>)?$");
+
+ private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$");
/**
* Splits userId string into naming part, email part, and comment part
@@ -82,22 +83,42 @@ public class OpenPgpUtils {
* @param userId
* @return theParsedUserInfo
*/
- public static UserInfo splitUserId(final String userId) {
+ public static UserId splitUserId(final String userId) {
if (!TextUtils.isEmpty(userId)) {
final Matcher matcher = USER_ID_PATTERN.matcher(userId);
if (matcher.matches()) {
- return new UserInfo(matcher.group(1), matcher.group(4), matcher.group(3));
+ return new UserId(matcher.group(1), matcher.group(3), matcher.group(2));
}
}
- return new UserInfo(null, null, null);
+ return new UserId(null, null, null);
+ }
+
+ /**
+ * Returns a composed user id. Returns null if name is null!
+ *
+ * @param name
+ * @param email
+ * @param comment
+ * @return
+ */
+ public static String createUserId(UserId userId) {
+ String userIdString = userId.name; // consider name a required value
+ if (userIdString != null && !TextUtils.isEmpty(userId.comment)) {
+ userIdString += " (" + userId.comment + ")";
+ }
+ if (userIdString != null && !TextUtils.isEmpty(userId.email)) {
+ userIdString += " <" + userId.email + ">";
+ }
+
+ return userIdString;
}
- public static class UserInfo {
+ public static class UserId {
public final String name;
public final String email;
public final String comment;
- public UserInfo(String name, String email, String comment) {
+ public UserId(String name, String email, String comment) {
this.name = name;
this.email = email;
this.comment = comment;