1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.jcurl.demo.smack;
21
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 public class XmppAddress implements CharSequence {
26 private static final Pattern p = Pattern
27 .compile("([^@]+)@([^/]+)(?:/(.*))?");
28
29 public static final XmppAddress parse(final CharSequence str) {
30 if (str instanceof XmppAddress)
31 return (XmppAddress) str;
32 final Matcher m = p.matcher(str);
33 if (!m.matches())
34 return null;
35 return new XmppAddress(m.group(1), m.group(2), m.group(3));
36 }
37
38 public static String toString(final String account, final String host,
39 final String resource) {
40 if (resource != null && resource.length() > 0)
41 return account + "@" + host + "/" + resource;
42 else
43 return account + "@" + host;
44 }
45
46 private final String account;
47 private final String host;
48 private final String resource;
49 private final transient String str;
50
51 public XmppAddress(final String account, final String host) {
52 this(account, host, null);
53 }
54
55 public XmppAddress(final String account, final String host,
56 final String resource) {
57 this.account = account;
58 this.host = host;
59 this.resource = resource == null || resource.length() <= 0 ? null
60 : resource;
61 str = toString(this.account, this.host, this.resource);
62 }
63
64 public char charAt(final int index) {
65 return str.charAt(index);
66 }
67
68 public String getAccount() {
69 return account;
70 }
71
72 public String getHost() {
73 return host;
74 }
75
76 public String getResource() {
77 return resource;
78 }
79
80 public int length() {
81 return str.length();
82 }
83
84 public CharSequence subSequence(final int start, final int end) {
85 return str.subSequence(start, end);
86 }
87
88 @Override
89 public String toString() {
90 return str;
91 }
92
93 public String toString(final String resource) {
94 return toString(account, host, resource);
95 }
96 }