View Javadoc

1   /*
2    * jcurl java curling software framework https://JCurl.mro.name Copyright (C)
3    * 2005-2009 M. Rohrmoser
4    * 
5    * This program is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License as published by the
7    * Free Software Foundation; either version 2 of the License, or (at your
8    * option) any later version.
9    * 
10   * This program is distributed in the hope that it will be useful, but
11   * WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13   * Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License along
16   * with this program; if not, write to the Free Software Foundation, Inc.,
17   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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  }