Class PacketParserUtils


  • public class PacketParserUtils
    extends java.lang.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.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addExtensionElement​(java.util.Collection<ExtensionElement> collection, org.jivesoftware.smack.xml.XmlPullParser parser, java.lang.String elementName, java.lang.String namespace, XmlEnvironment outerXmlEnvironment)  
      static void addExtensionElement​(java.util.Collection<ExtensionElement> collection, org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment)  
      static void addExtensionElement​(StanzaBuilder<?> stanzaBuilder, org.jivesoftware.smack.xml.XmlPullParser parser, java.lang.String elementName, java.lang.String namespace, XmlEnvironment outerXmlEnvironment)  
      static void addExtensionElement​(StanzaBuilder<?> stanzaBuilder, org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment)  
      static void addExtensionElement​(Stanza packet, org.jivesoftware.smack.xml.XmlPullParser parser, java.lang.String elementName, java.lang.String namespace, XmlEnvironment outerXmlEnvironment)  
      static void addExtensionElement​(Stanza packet, org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment)  
      static org.jivesoftware.smack.xml.XmlPullParser getParserFor​(java.io.InputStream inputStream)  
      static org.jivesoftware.smack.xml.XmlPullParser getParserFor​(java.io.Reader reader)  
      static org.jivesoftware.smack.xml.XmlPullParser getParserFor​(java.lang.String stanza)  
      static Compress.Feature parseCompressionFeature​(org.jivesoftware.smack.xml.XmlPullParser parser)
      Parse the Compression Feature reported from the server.
      static java.lang.CharSequence parseContentDepth​(org.jivesoftware.smack.xml.XmlPullParser parser, int depth)  
      static java.lang.CharSequence parseContentDepth​(org.jivesoftware.smack.xml.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 java.util.Map<java.lang.String,​java.lang.String> parseDescriptiveTexts​(org.jivesoftware.smack.xml.XmlPullParser parser, java.util.Map<java.lang.String,​java.lang.String> descriptiveTexts)  
      static java.lang.CharSequence parseElement​(org.jivesoftware.smack.xml.XmlPullParser parser)
      Returns the current element as string.
      static java.lang.CharSequence parseElement​(org.jivesoftware.smack.xml.XmlPullParser parser, boolean fullNamespaces)  
      static java.lang.String parseElementText​(org.jivesoftware.smack.xml.XmlPullParser parser)
      Returns the textual content of an element as String.
      static StanzaError parseError​(org.jivesoftware.smack.xml.XmlPullParser parser)  
      static StanzaError parseError​(org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
      Parses error sub-packets.
      static ExtensionElement parseExtensionElement​(java.lang.String elementName, java.lang.String namespace, org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
      Parses an extension element.
      static IQ parseIQ​(org.jivesoftware.smack.xml.XmlPullParser parser)  
      static IQ parseIQ​(org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
      Parses an IQ packet.
      static java.util.Collection<java.lang.String> parseMechanisms​(org.jivesoftware.smack.xml.XmlPullParser parser)
      Parse the available SASL mechanisms reported from the server.
      static Message parseMessage​(org.jivesoftware.smack.xml.XmlPullParser parser)  
      static Message parseMessage​(org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
      Parses a message packet.
      static Presence parsePresence​(org.jivesoftware.smack.xml.XmlPullParser parser)  
      static Presence parsePresence​(org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
      Parses a presence packet.
      static Session.Feature parseSessionFeature​(org.jivesoftware.smack.xml.XmlPullParser parser)  
      static <S extends Stanza>
      S
      parseStanza​(java.lang.String stanza)  
      static Stanza parseStanza​(org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
      Tries to parse and return either a Message, IQ or Presence stanza.
      static StartTls parseStartTlsFeature​(org.jivesoftware.smack.xml.XmlPullParser parser)  
      static StreamError parseStreamError​(org.jivesoftware.smack.xml.XmlPullParser parser)  
      static StreamError parseStreamError​(org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
      Parses stream error packets.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PacketParserUtils

        public PacketParserUtils()
    • Method Detail

      • getParserFor

        public static org.jivesoftware.smack.xml.XmlPullParser getParserFor​(java.lang.String stanza)
                                                                     throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                                            java.io.IOException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
      • getParserFor

        public static org.jivesoftware.smack.xml.XmlPullParser getParserFor​(java.io.InputStream inputStream)
                                                                     throws org.jivesoftware.smack.xml.XmlPullParserException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
      • getParserFor

        public static org.jivesoftware.smack.xml.XmlPullParser getParserFor​(java.io.Reader reader)
                                                                     throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                                            java.io.IOException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
      • parseStanza

        public static <S extends Stanza> S parseStanza​(java.lang.String stanza)
                                                throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                       SmackParsingException,
                                                       java.io.IOException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        SmackParsingException
        java.io.IOException
      • parseStanza

        public static Stanza parseStanza​(org.jivesoftware.smack.xml.XmlPullParser parser,
                                         XmlEnvironment outerXmlEnvironment)
                                  throws org.jivesoftware.smack.xml.XmlPullParserException,
                                         SmackParsingException,
                                         java.io.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 please
        outerXmlEnvironment - the outer XML environment (optional).
        Returns:
        a stanza which is either a Message, IQ or Presence.
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException - if an error in the XML parser occurred.
        SmackParsingException - if the Smack parser (provider) encountered invalid input.
        java.io.IOException - if an I/O error occurred.
      • parseMessage

        public static Message parseMessage​(org.jivesoftware.smack.xml.XmlPullParser parser)
                                    throws org.jivesoftware.smack.xml.XmlPullParserException,
                                           java.io.IOException,
                                           SmackParsingException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        SmackParsingException
      • parseMessage

        public static Message parseMessage​(org.jivesoftware.smack.xml.XmlPullParser parser,
                                           XmlEnvironment outerXmlEnvironment)
                                    throws org.jivesoftware.smack.xml.XmlPullParserException,
                                           java.io.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:
        org.jivesoftware.smack.xml.XmlPullParserException - if an error in the XML parser occurred.
        java.io.IOException - if an I/O error occurred.
        SmackParsingException - if the Smack parser (provider) encountered invalid input.
      • parseElementText

        public static java.lang.String parseElementText​(org.jivesoftware.smack.xml.XmlPullParser parser)
                                                 throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                        java.io.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:
        org.jivesoftware.smack.xml.XmlPullParserException - if an error in the XML parser occurred.
        java.io.IOException - if an I/O error occurred.
      • parseElement

        public static java.lang.CharSequence parseElement​(org.jivesoftware.smack.xml.XmlPullParser parser)
                                                   throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                          java.io.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:
        org.jivesoftware.smack.xml.XmlPullParserException - if an error in the XML parser occurred.
        java.io.IOException - if an I/O error occurred.
      • parseElement

        public static java.lang.CharSequence parseElement​(org.jivesoftware.smack.xml.XmlPullParser parser,
                                                          boolean fullNamespaces)
                                                   throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                          java.io.IOException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
      • parseContentDepth

        public static java.lang.CharSequence parseContentDepth​(org.jivesoftware.smack.xml.XmlPullParser parser,
                                                               int depth)
                                                        throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                               java.io.IOException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
      • parseContentDepth

        public static java.lang.CharSequence parseContentDepth​(org.jivesoftware.smack.xml.XmlPullParser parser,
                                                               int depth,
                                                               boolean fullNamespaces)
                                                        throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                               java.io.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, if fullNamespaces 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 please
        depth - TODO javadoc me please
        fullNamespaces - TODO javadoc me please
        Returns:
        the content of the current depth
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException - if an error in the XML parser occurred.
        java.io.IOException - if an I/O error occurred.
      • parsePresence

        public static Presence parsePresence​(org.jivesoftware.smack.xml.XmlPullParser parser)
                                      throws org.jivesoftware.smack.xml.XmlPullParserException,
                                             java.io.IOException,
                                             SmackParsingException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        SmackParsingException
      • parsePresence

        public static Presence parsePresence​(org.jivesoftware.smack.xml.XmlPullParser parser,
                                             XmlEnvironment outerXmlEnvironment)
                                      throws org.jivesoftware.smack.xml.XmlPullParserException,
                                             java.io.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:
        java.io.IOException - if an I/O error occurred.
        org.jivesoftware.smack.xml.XmlPullParserException - if an error in the XML parser occurred.
        SmackParsingException - if the Smack parser (provider) encountered invalid input.
      • parseIQ

        public static IQ parseIQ​(org.jivesoftware.smack.xml.XmlPullParser parser)
                          throws java.lang.Exception
        Throws:
        java.lang.Exception
      • parseIQ

        public static IQ parseIQ​(org.jivesoftware.smack.xml.XmlPullParser parser,
                                 XmlEnvironment outerXmlEnvironment)
                          throws org.jivesoftware.smack.xml.XmlPullParserException,
                                 org.jxmpp.stringprep.XmppStringprepException,
                                 java.io.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:
        org.jivesoftware.smack.xml.XmlPullParserException - if an error in the XML parser occurred.
        org.jxmpp.stringprep.XmppStringprepException - if the provided string is invalid.
        java.io.IOException - if an I/O error occurred.
        SmackParsingException - if the Smack parser (provider) encountered invalid input.
      • parseMechanisms

        public static java.util.Collection<java.lang.String> parseMechanisms​(org.jivesoftware.smack.xml.XmlPullParser parser)
                                                                      throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                                             java.io.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:
        java.io.IOException - if an I/O error occurred.
        org.jivesoftware.smack.xml.XmlPullParserException - if an error in the XML parser occurred.
      • parseCompressionFeature

        public static Compress.Feature parseCompressionFeature​(org.jivesoftware.smack.xml.XmlPullParser parser)
                                                        throws java.io.IOException,
                                                               org.jivesoftware.smack.xml.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:
        java.io.IOException - if an I/O error occurred.
        org.jivesoftware.smack.xml.XmlPullParserException - if an exception occurs while parsing the stanza.
      • parseDescriptiveTexts

        public static java.util.Map<java.lang.String,​java.lang.String> parseDescriptiveTexts​(org.jivesoftware.smack.xml.XmlPullParser parser,
                                                                                                   java.util.Map<java.lang.String,​java.lang.String> descriptiveTexts)
                                                                                            throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                                                                   java.io.IOException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
      • parseStreamError

        public static StreamError parseStreamError​(org.jivesoftware.smack.xml.XmlPullParser parser)
                                            throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                   java.io.IOException,
                                                   SmackParsingException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        SmackParsingException
      • parseStreamError

        public static StreamError parseStreamError​(org.jivesoftware.smack.xml.XmlPullParser parser,
                                                   XmlEnvironment outerXmlEnvironment)
                                            throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                   java.io.IOException,
                                                   SmackParsingException
        Parses stream error packets.
        Parameters:
        parser - the XML parser.
        outerXmlEnvironment - the outer XML environment (optional).
        Returns:
        an stream error packet.
        Throws:
        java.io.IOException - if an I/O error occurred.
        org.jivesoftware.smack.xml.XmlPullParserException - if an error in the XML parser occurred.
        SmackParsingException - if the Smack parser (provider) encountered invalid input.
      • parseError

        public static StanzaError parseError​(org.jivesoftware.smack.xml.XmlPullParser parser)
                                      throws org.jivesoftware.smack.xml.XmlPullParserException,
                                             java.io.IOException,
                                             SmackParsingException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        SmackParsingException
      • parseError

        public static StanzaError parseError​(org.jivesoftware.smack.xml.XmlPullParser parser,
                                             XmlEnvironment outerXmlEnvironment)
                                      throws org.jivesoftware.smack.xml.XmlPullParserException,
                                             java.io.IOException,
                                             SmackParsingException
        Parses error sub-packets.
        Parameters:
        parser - the XML parser.
        outerXmlEnvironment - the outer XML environment (optional).
        Returns:
        an error sub-packet.
        Throws:
        java.io.IOException - if an I/O error occurred.
        org.jivesoftware.smack.xml.XmlPullParserException - if an error in the XML parser occurred.
        SmackParsingException - if the Smack parser (provider) encountered invalid input.
      • parseExtensionElement

        public static ExtensionElement parseExtensionElement​(java.lang.String elementName,
                                                             java.lang.String namespace,
                                                             org.jivesoftware.smack.xml.XmlPullParser parser,
                                                             XmlEnvironment outerXmlEnvironment)
                                                      throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                             java.io.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:
        org.jivesoftware.smack.xml.XmlPullParserException - if an error in the XML parser occurred.
        java.io.IOException - if an I/O error occurred.
        SmackParsingException - if the Smack parser (provider) encountered invalid input.
      • parseStartTlsFeature

        public static StartTls parseStartTlsFeature​(org.jivesoftware.smack.xml.XmlPullParser parser)
                                             throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                    java.io.IOException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
      • parseSessionFeature

        public static Session.Feature parseSessionFeature​(org.jivesoftware.smack.xml.XmlPullParser parser)
                                                   throws org.jivesoftware.smack.xml.XmlPullParserException,
                                                          java.io.IOException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
      • addExtensionElement

        public static void addExtensionElement​(StanzaBuilder<?> stanzaBuilder,
                                               org.jivesoftware.smack.xml.XmlPullParser parser,
                                               XmlEnvironment outerXmlEnvironment)
                                        throws org.jivesoftware.smack.xml.XmlPullParserException,
                                               java.io.IOException,
                                               SmackParsingException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        SmackParsingException
      • addExtensionElement

        public static void addExtensionElement​(StanzaBuilder<?> stanzaBuilder,
                                               org.jivesoftware.smack.xml.XmlPullParser parser,
                                               java.lang.String elementName,
                                               java.lang.String namespace,
                                               XmlEnvironment outerXmlEnvironment)
                                        throws org.jivesoftware.smack.xml.XmlPullParserException,
                                               java.io.IOException,
                                               SmackParsingException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        SmackParsingException
      • addExtensionElement

        public static void addExtensionElement​(Stanza packet,
                                               org.jivesoftware.smack.xml.XmlPullParser parser,
                                               XmlEnvironment outerXmlEnvironment)
                                        throws org.jivesoftware.smack.xml.XmlPullParserException,
                                               java.io.IOException,
                                               SmackParsingException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        SmackParsingException
      • addExtensionElement

        public static void addExtensionElement​(Stanza packet,
                                               org.jivesoftware.smack.xml.XmlPullParser parser,
                                               java.lang.String elementName,
                                               java.lang.String namespace,
                                               XmlEnvironment outerXmlEnvironment)
                                        throws org.jivesoftware.smack.xml.XmlPullParserException,
                                               java.io.IOException,
                                               SmackParsingException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        SmackParsingException
      • addExtensionElement

        public static void addExtensionElement​(java.util.Collection<ExtensionElement> collection,
                                               org.jivesoftware.smack.xml.XmlPullParser parser,
                                               XmlEnvironment outerXmlEnvironment)
                                        throws org.jivesoftware.smack.xml.XmlPullParserException,
                                               java.io.IOException,
                                               SmackParsingException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        SmackParsingException
      • addExtensionElement

        public static void addExtensionElement​(java.util.Collection<ExtensionElement> collection,
                                               org.jivesoftware.smack.xml.XmlPullParser parser,
                                               java.lang.String elementName,
                                               java.lang.String namespace,
                                               XmlEnvironment outerXmlEnvironment)
                                        throws org.jivesoftware.smack.xml.XmlPullParserException,
                                               java.io.IOException,
                                               SmackParsingException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        SmackParsingException