Class PacketParserUtils
- java.lang.Object
-
- org.jivesoftware.smack.util.PacketParserUtils
-
public class PacketParserUtils extends Object
Utility class that helps to parse packets. Any parsing packets method that must be shared between many clients must be placed in this utility class.
-
-
Constructor Summary
Constructors Constructor Description PacketParserUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addExtensionElement(Collection<ExtensionElement> collection, XmlPullParser parser, String elementName, String namespace, XmlEnvironment outerXmlEnvironment)
static void
addExtensionElement(Collection<ExtensionElement> collection, XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
static void
addExtensionElement(StanzaBuilder<?> stanzaBuilder, XmlPullParser parser, String elementName, String namespace, XmlEnvironment outerXmlEnvironment)
static void
addExtensionElement(StanzaBuilder<?> stanzaBuilder, XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
static void
addExtensionElement(Stanza packet, XmlPullParser parser, String elementName, String namespace, XmlEnvironment outerXmlEnvironment)
static void
addExtensionElement(Stanza packet, XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
static XmlPullParser
getParserFor(InputStream inputStream)
static XmlPullParser
getParserFor(Reader reader)
static XmlPullParser
getParserFor(String stanza)
static Compress.Feature
parseCompressionFeature(XmlPullParser parser)
Parse the Compression Feature reported from the server.static CharSequence
parseContentDepth(XmlPullParser parser, int depth)
static CharSequence
parseContentDepth(XmlPullParser parser, int depth, boolean fullNamespaces)
Returns the content from the current position of the parser up to the closing tag of the given depth.static Map<String,String>
parseDescriptiveTexts(XmlPullParser parser, Map<String,String> descriptiveTexts)
static CharSequence
parseElement(XmlPullParser parser)
Returns the current element as string.static CharSequence
parseElement(XmlPullParser parser, boolean fullNamespaces)
static String
parseElementText(XmlPullParser parser)
Returns the textual content of an element as String.static StanzaError
parseError(XmlPullParser parser)
static StanzaError
parseError(XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
Parses error sub-packets.static ExtensionElement
parseExtensionElement(String elementName, String namespace, XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
Parses an extension element.static IQ
parseIQ(XmlPullParser parser)
static IQ
parseIQ(XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
Parses an IQ packet.static Collection<String>
parseMechanisms(XmlPullParser parser)
Parse the available SASL mechanisms reported from the server.static Message
parseMessage(XmlPullParser parser)
static Message
parseMessage(XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
Parses a message packet.static Presence
parsePresence(XmlPullParser parser)
static Presence
parsePresence(XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
Parses a presence packet.static Session.Feature
parseSessionFeature(XmlPullParser parser)
static <S extends Stanza>
SparseStanza(String stanza)
static Stanza
parseStanza(XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
Tries to parse and return either a Message, IQ or Presence stanza.static StartTls
parseStartTlsFeature(XmlPullParser parser)
static StreamError
parseStreamError(XmlPullParser parser)
static StreamError
parseStreamError(XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
Parses stream error packets.
-
-
-
Constructor Detail
-
PacketParserUtils
public PacketParserUtils()
-
-
Method Detail
-
getParserFor
public static XmlPullParser getParserFor(String stanza) throws XmlPullParserException, IOException
- Throws:
XmlPullParserException
IOException
-
getParserFor
public static XmlPullParser getParserFor(InputStream inputStream) throws XmlPullParserException
- Throws:
XmlPullParserException
-
getParserFor
public static XmlPullParser getParserFor(Reader reader) throws XmlPullParserException, IOException
- Throws:
XmlPullParserException
IOException
-
parseStanza
public static <S extends Stanza> S parseStanza(String stanza) throws XmlPullParserException, SmackParsingException, IOException
-
parseStanza
public static Stanza parseStanza(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, SmackParsingException, IOException
Tries to parse and return either a Message, IQ or Presence stanza. connection is optional and is used to return feature-not-implemented errors for unknown IQ stanzas.- Parameters:
parser
- TODO javadoc me pleaseouterXmlEnvironment
- the outer XML environment (optional).- Returns:
- a stanza which is either a Message, IQ or Presence.
- Throws:
XmlPullParserException
- if an error in the XML parser occurred.SmackParsingException
- if the Smack parser (provider) encountered invalid input.IOException
- if an I/O error occurred.
-
parseMessage
public static Message parseMessage(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException
-
parseMessage
public static Message parseMessage(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException
Parses a message packet.- Parameters:
parser
- the XML parser, positioned at the start of a message packet.outerXmlEnvironment
- the outer XML environment (optional).- Returns:
- a Message packet.
- Throws:
XmlPullParserException
- if an error in the XML parser occurred.IOException
- if an I/O error occurred.SmackParsingException
- if the Smack parser (provider) encountered invalid input.
-
parseElementText
public static String parseElementText(XmlPullParser parser) throws XmlPullParserException, IOException
Returns the textual content of an element as String. After this method returns the parser position will be END_ELEMENT, following the established pull parser calling convention.The parser must be positioned on a START_ELEMENT of an element which MUST NOT contain Mixed Content (as defined in XML 3.2.2), or else an XmlPullParserException will be thrown.
This method is used for the parts where the XMPP specification requires elements that contain only text or are the empty element.- Parameters:
parser
- TODO javadoc me please- Returns:
- the textual content of the element as String
- Throws:
XmlPullParserException
- if an error in the XML parser occurred.IOException
- if an I/O error occurred.
-
parseElement
public static CharSequence parseElement(XmlPullParser parser) throws XmlPullParserException, IOException
Returns the current element as string.The parser must be positioned on START_ELEMENT.
Note that only the outermost namespace attributes ("xmlns") will be returned, not nested ones.- Parameters:
parser
- the XML pull parser- Returns:
- the element as string
- Throws:
XmlPullParserException
- if an error in the XML parser occurred.IOException
- if an I/O error occurred.
-
parseElement
public static CharSequence parseElement(XmlPullParser parser, boolean fullNamespaces) throws XmlPullParserException, IOException
- Throws:
XmlPullParserException
IOException
-
parseContentDepth
public static CharSequence parseContentDepth(XmlPullParser parser, int depth) throws XmlPullParserException, IOException
- Throws:
XmlPullParserException
IOException
-
parseContentDepth
public static CharSequence parseContentDepth(XmlPullParser parser, int depth, boolean fullNamespaces) throws XmlPullParserException, IOException
Returns the content from the current position of the parser up to the closing tag of the given depth. Note that only the outermost namespace attributes ("xmlns") will be returned, not nested ones, iffullNamespaces
is false. If it is true, then namespaces of parent elements will be added to child elements that don't define a different namespace.This method is able to parse the content with MX- and KXmlParser. KXmlParser does not support xml-roundtrip. i.e. return a String on getText() on START_ELEMENT and END_ELEMENT. We check for the XML_ROUNDTRIP feature. If it's not found we are required to work around this limitation, which results in only partial support for XML namespaces ("xmlns"): Only the outermost namespace of elements will be included in the resulting String, if
fullNamespaces
is set to false.In particular Android's XmlPullParser does not support XML_ROUNDTRIP.
- Parameters:
parser
- TODO javadoc me pleasedepth
- TODO javadoc me pleasefullNamespaces
- TODO javadoc me please- Returns:
- the content of the current depth
- Throws:
XmlPullParserException
- if an error in the XML parser occurred.IOException
- if an I/O error occurred.
-
parsePresence
public static Presence parsePresence(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException
-
parsePresence
public static Presence parsePresence(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException
Parses a presence packet.- Parameters:
parser
- the XML parser, positioned at the start of a presence packet.outerXmlEnvironment
- the outer XML environment (optional).- Returns:
- a Presence packet.
- Throws:
IOException
- if an I/O error occurred.XmlPullParserException
- if an error in the XML parser occurred.SmackParsingException
- if the Smack parser (provider) encountered invalid input.
-
parseIQ
public static IQ parseIQ(XmlPullParser parser) throws Exception
- Throws:
Exception
-
parseIQ
public static IQ parseIQ(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, XmppStringprepException, IOException, SmackParsingException
Parses an IQ packet.- Parameters:
parser
- the XML parser, positioned at the start of an IQ packet.outerXmlEnvironment
- the outer XML environment (optional).- Returns:
- an IQ object.
- Throws:
XmlPullParserException
- if an error in the XML parser occurred.XmppStringprepException
- if the provided string is invalid.IOException
- if an I/O error occurred.SmackParsingException
- if the Smack parser (provider) encountered invalid input.
-
parseMechanisms
public static Collection<String> parseMechanisms(XmlPullParser parser) throws XmlPullParserException, IOException
Parse the available SASL mechanisms reported from the server.- Parameters:
parser
- the XML parser, positioned at the start of the mechanisms stanza.- Returns:
- a collection of Stings with the mechanisms included in the mechanisms stanza.
- Throws:
IOException
- if an I/O error occurred.XmlPullParserException
- if an error in the XML parser occurred.
-
parseCompressionFeature
public static Compress.Feature parseCompressionFeature(XmlPullParser parser) throws IOException, XmlPullParserException
Parse the Compression Feature reported from the server.- Parameters:
parser
- the XML parser, positioned at the start of the compression stanza.- Returns:
- The CompressionFeature stream element
- Throws:
IOException
- if an I/O error occurred.XmlPullParserException
- if an exception occurs while parsing the stanza.
-
parseDescriptiveTexts
public static Map<String,String> parseDescriptiveTexts(XmlPullParser parser, Map<String,String> descriptiveTexts) throws XmlPullParserException, IOException
- Throws:
XmlPullParserException
IOException
-
parseStreamError
public static StreamError parseStreamError(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException
-
parseStreamError
public static StreamError parseStreamError(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException
Parses stream error packets.- Parameters:
parser
- the XML parser.outerXmlEnvironment
- the outer XML environment (optional).- Returns:
- an stream error packet.
- Throws:
IOException
- if an I/O error occurred.XmlPullParserException
- if an error in the XML parser occurred.SmackParsingException
- if the Smack parser (provider) encountered invalid input.
-
parseError
public static StanzaError parseError(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException
-
parseError
public static StanzaError parseError(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException
Parses error sub-packets.- Parameters:
parser
- the XML parser.outerXmlEnvironment
- the outer XML environment (optional).- Returns:
- an error sub-packet.
- Throws:
IOException
- if an I/O error occurred.XmlPullParserException
- if an error in the XML parser occurred.SmackParsingException
- if the Smack parser (provider) encountered invalid input.
-
parseExtensionElement
public static ExtensionElement parseExtensionElement(String elementName, String namespace, XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException
Parses an extension element.- Parameters:
elementName
- the XML element name of the extension element.namespace
- the XML namespace of the stanza extension.parser
- the XML parser, positioned at the starting element of the extension.outerXmlEnvironment
- the outer XML environment (optional).- Returns:
- an extension element.
- Throws:
XmlPullParserException
- if an error in the XML parser occurred.IOException
- if an I/O error occurred.SmackParsingException
- if the Smack parser (provider) encountered invalid input.
-
parseStartTlsFeature
public static StartTls parseStartTlsFeature(XmlPullParser parser) throws XmlPullParserException, IOException
- Throws:
XmlPullParserException
IOException
-
parseSessionFeature
public static Session.Feature parseSessionFeature(XmlPullParser parser) throws XmlPullParserException, IOException
- Throws:
XmlPullParserException
IOException
-
addExtensionElement
public static void addExtensionElement(StanzaBuilder<?> stanzaBuilder, XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException
-
addExtensionElement
public static void addExtensionElement(StanzaBuilder<?> stanzaBuilder, XmlPullParser parser, String elementName, String namespace, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException
-
addExtensionElement
public static void addExtensionElement(Stanza packet, XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException
-
addExtensionElement
public static void addExtensionElement(Stanza packet, XmlPullParser parser, String elementName, String namespace, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException
-
addExtensionElement
public static void addExtensionElement(Collection<ExtensionElement> collection, XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException
-
addExtensionElement
public static void addExtensionElement(Collection<ExtensionElement> collection, XmlPullParser parser, String elementName, String namespace, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException
-
-