Class XmppStringUtils
- java.lang.Object
-
- org.jxmpp.util.XmppStringUtils
-
public class XmppStringUtils extends Object
Utility class for handling Strings in XMPP.
-
-
Constructor Summary
Constructors Constructor Description XmppStringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
completeJidFrom(CharSequence localpart, CharSequence domainpart)
Construct a JID String from the given parts.static String
completeJidFrom(CharSequence localpart, CharSequence domainpart, CharSequence resource)
Construct a JID String from the given parts.static String
completeJidFrom(String localpart, String domainpart)
Construct a JID String from the given parts.static String
completeJidFrom(String localpart, String domainpart, String resource)
Construct a JID String from the given parts.static String
escapeLocalpart(String localpart)
Escapes the localpart of a JID according to "JID Escaping" (XEP-0106).static String
generateKey(String element, String namespace)
Generate a unique key from a element name and namespace.static boolean
isBareJid(String jid)
Returns true ifjid
is a bare JID ("foo@bar.com").static boolean
isFullJID(String jid)
Returns true if jid is a full JID (i.e.static String
parseBareJid(String jid)
Returns the JID with any resource information removed.static String
parseDomain(String jid)
Returns the domain of an XMPP address (JID).static String
parseLocalpart(String jid)
Returns the localpart of an XMPP address (JID).static String
parseResource(String jid)
Returns the resource portion of an XMPP address (JID).static String
unescapeLocalpart(String localpart)
Un-escapes the localpart of a JID according to "JID Escaping" (XEP-0106).
-
-
-
Constructor Detail
-
XmppStringUtils
public XmppStringUtils()
-
-
Method Detail
-
parseLocalpart
public static String parseLocalpart(String jid)
Returns the localpart of an XMPP address (JID). For example, for the address "user@xmpp.org/Resource", "user" would be returned. Returnsnull
if the given JID has no localpart. Returns the empty string if the given JIDs localpart is the empty string (which is invalid).- Parameters:
jid
- the XMPP address to parse.- Returns:
- the name portion of the XMPP address, the empty String or
null
.
-
parseDomain
public static String parseDomain(String jid)
Returns the domain of an XMPP address (JID). For example, for the address "user@xmpp.org/Resource", "xmpp.org" would be returned. Ifjid
isnull
, then this method returns alsonull
. If the input String is no valid JID or has no domainpart, then this method will return the empty String.- Parameters:
jid
- the XMPP address to parse.- Returns:
- the domainpart of the XMPP address, the empty String or
null
.
-
parseResource
public static String parseResource(String jid)
Returns the resource portion of an XMPP address (JID). For example, for the address "user@xmpp.org/Resource", "Resource" would be returned. Returnsnull
if the given JID has no resourcepart. Returns the empty string if the given JID has an empty resourcepart (which is invalid).- Parameters:
jid
- the XMPP address to parse.- Returns:
- the resource portion of the XMPP address.
-
parseBareJid
public static String parseBareJid(String jid)
Returns the JID with any resource information removed. For example, for the address "matt@jivesoftware.com/Smack", "matt@jivesoftware.com" would be returned.- Parameters:
jid
- the XMPP JID.- Returns:
- the bare XMPP JID without resource information.
-
isFullJID
public static boolean isFullJID(String jid)
Returns true if jid is a full JID (i.e. a JID with resource part).- Parameters:
jid
- the String to check.- Returns:
- true if full JID, false otherwise
-
isBareJid
public static boolean isBareJid(String jid)
Returns true ifjid
is a bare JID ("foo@bar.com").This method may return true for Strings that are not valid JIDs (e.g. because of Stringprep violations). Consider using
org.jxmpp.jid.util.JidUtil.validateBareJid(String)
from jxmpp-jid instead of this method as it exceptions provide a meaningful message string why the JID is not a bare JID and will also check for Stringprep errors.- Parameters:
jid
- the String to check.- Returns:
- true if bare JID, false otherwise
-
escapeLocalpart
public static String escapeLocalpart(String localpart)
Escapes the localpart of a JID according to "JID Escaping" (XEP-0106). Escaping replaces characters prohibited by Nodeprep with escape sequences, as follows:Character mappings Unescaped Character Encoded Sequence <space> \20 " \22 & \26 ' \27 / \2f : \3a < \3c > \3e @ \40 \ \5c This process is useful when the localpart comes from an external source that doesn't conform to Nodeprep. For example, a username in LDAP may be "Joe Smith". Because the <space> character isn't a valid part of a localpart, the username should be escaped to "Joe\20Smith" before being made into a JID (e.g. "joe\20smith@example.com" after case-folding, etc. has been applied).
All localpart escaping and un-escaping must be performed manually at the appropriate time; the JID class will not escape or un-escape automatically.- Parameters:
localpart
- the localpart.- Returns:
- the escaped version of the localpart.
- See Also:
- XEP-106: JID Escaping
-
unescapeLocalpart
public static String unescapeLocalpart(String localpart)
Un-escapes the localpart of a JID according to "JID Escaping" (XEP-0106). Escaping replaces characters prohibited by Nodeprep with escape sequences, as follows:Character mapping Unescaped Character Encoded Sequence <space> \20 " \22 & \26 ' \27 / \2f : \3a < \3c > \3e @ \40 \ \5c This process is useful when the localpart comes from an external source that doesn't conform to Nodeprep. For example, a username in LDAP may be "Joe Smith". Because the <space> character isn't a valid part of a localpart, the username should be escaped to "Joe\20Smith" before being made into a JID (e.g. "joe\20smith@example.com" after case-folding, etc. has been applied).
All localpart escaping and un-escaping must be performed manually at the appropriate time; the JID class will not escape or un-escape automatically.- Parameters:
localpart
- the escaped version of the localpart.- Returns:
- the un-escaped version of the localpart.
- See Also:
- XEP-106: JID Escaping
-
completeJidFrom
public static String completeJidFrom(CharSequence localpart, CharSequence domainpart)
Construct a JID String from the given parts.- Parameters:
localpart
- the localpart.domainpart
- the domainpart.- Returns:
- the constructed JID String.
-
completeJidFrom
public static String completeJidFrom(String localpart, String domainpart)
Construct a JID String from the given parts.- Parameters:
localpart
- the localpart.domainpart
- the domainpart.- Returns:
- the constructed JID String.
-
completeJidFrom
public static String completeJidFrom(CharSequence localpart, CharSequence domainpart, CharSequence resource)
Construct a JID String from the given parts.- Parameters:
localpart
- the localpart.domainpart
- the domainpart.resource
- the resourcepart.- Returns:
- the constructed JID String.
-
completeJidFrom
public static String completeJidFrom(String localpart, String domainpart, String resource)
Construct a JID String from the given parts.- Parameters:
localpart
- the localpart.domainpart
- the domainpart.resource
- the resourcepart.- Returns:
- the constructed JID String.
-
generateKey
public static String generateKey(String element, String namespace)
Generate a unique key from a element name and namespace. This key can be used to lookup element/namespace information. The key is simply generated by concatenating the strings as follows:element + '\t' + namespace
.The tab character (\t) was chosen because it will be normalized, i.e. replace by space, in attribute values. It therefore should never appear in
element
ornamespace
. For more information about the normalization, see the XML specification ยง 3.3.3 Attribute-Value Normalization.- Parameters:
element
- the element.namespace
- the namespace.- Returns:
- the unique key of element and namespace.
-
-