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 Details

    • PacketParserUtils

      public PacketParserUtils()
  • Method Details

    • getParserFor

      public static org.jivesoftware.smack.xml.XmlPullParser getParserFor(String element) throws org.jivesoftware.smack.xml.XmlPullParserException, IOException
      Throws:
      org.jivesoftware.smack.xml.XmlPullParserException
      IOException
    • getParserFor

      public static org.jivesoftware.smack.xml.XmlPullParser getParserFor(InputStream inputStream) throws org.jivesoftware.smack.xml.XmlPullParserException, IOException
      Throws:
      org.jivesoftware.smack.xml.XmlPullParserException
      IOException
    • getParserFor

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

      public static Stanza parseStanza(org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment, org.jxmpp.JxmppContext jxmppContext) throws org.jivesoftware.smack.xml.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 please
      outerXmlEnvironment - the outer XML environment (optional).
      jxmppContext - the JXMPP context used when creating JIDs.
      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.
      IOException - if an I/O error occurred.
    • parseMessage

      public static Message parseMessage(org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment, org.jxmpp.JxmppContext jxmppContext) throws org.jivesoftware.smack.xml.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).
      jxmppContext - the JXMPP context used when creating JIDs.
      Returns:
      a Message packet.
      Throws:
      org.jivesoftware.smack.xml.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(org.jivesoftware.smack.xml.XmlPullParser parser) throws org.jivesoftware.smack.xml.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:
      org.jivesoftware.smack.xml.XmlPullParserException - if an error in the XML parser occurred.
      IOException - if an I/O error occurred.
    • parseElement

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

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

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

      public static CharSequence parseContentDepth(org.jivesoftware.smack.xml.XmlPullParser parser, int depth, boolean fullNamespaces) throws org.jivesoftware.smack.xml.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, 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.
      IOException - if an I/O error occurred.
    • parsePresence

      public static Presence parsePresence(org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment, org.jxmpp.JxmppContext jxmppContext) throws org.jivesoftware.smack.xml.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).
      jxmppContext - the JXMPP context used when creating JIDs.
      Returns:
      a Presence packet.
      Throws:
      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.
    • parseIqData

      public static IqData parseIqData(org.jivesoftware.smack.xml.XmlPullParser parser, org.jxmpp.JxmppContext jxmppContext) throws org.jxmpp.stringprep.XmppStringprepException
      Throws:
      org.jxmpp.stringprep.XmppStringprepException
    • parseIQ

      public static IQ parseIQ(org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment, org.jxmpp.JxmppContext jxmppContext) throws org.jivesoftware.smack.xml.XmlPullParserException, org.jxmpp.stringprep.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).
      jxmppContext - the JXMPP context used when creating JIDs.
      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.
      IOException - if an I/O error occurred.
      SmackParsingException - if the Smack parser (provider) encountered invalid input.
    • parseMechanisms

      public static Collection<String> parseMechanisms(org.jivesoftware.smack.xml.XmlPullParser parser) throws org.jivesoftware.smack.xml.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.
      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 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:
      IOException - if an I/O error occurred.
      org.jivesoftware.smack.xml.XmlPullParserException - if an exception occurs while parsing the stanza.
    • parseDescriptiveTexts

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

      public static StreamError parseStreamError(org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment, org.jxmpp.JxmppContext jxmppContext) throws org.jivesoftware.smack.xml.XmlPullParserException, IOException, SmackParsingException
      Parses stream error packets.
      Parameters:
      parser - the XML parser.
      outerXmlEnvironment - the outer XML environment (optional).
      jxmppContext - the JXMPP context used when creating JIDs.
      Returns:
      an stream error packet.
      Throws:
      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, XmlEnvironment outerXmlEnvironment, org.jxmpp.JxmppContext jxmppContext) throws org.jivesoftware.smack.xml.XmlPullParserException, IOException, SmackParsingException
      Parses error sub-packets.
      Parameters:
      parser - the XML parser.
      outerXmlEnvironment - the outer XML environment (optional).
      jxmppContext - the JXMPP context used when creating JIDs.
      Returns:
      an error sub-packet.
      Throws:
      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 XmlElement parseExtensionElement(String elementName, String namespace, org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment, org.jxmpp.JxmppContext jxmppContext) throws org.jivesoftware.smack.xml.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).
      jxmppContext - the JXMPP context used when creating JIDs.
      Returns:
      an extension element.
      Throws:
      org.jivesoftware.smack.xml.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(org.jivesoftware.smack.xml.XmlPullParser parser) throws org.jivesoftware.smack.xml.XmlPullParserException, IOException
      Throws:
      org.jivesoftware.smack.xml.XmlPullParserException
      IOException
    • parseSessionFeature

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

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

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

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

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

      public static void addExtensionElement(Collection<XmlElement> collection, org.jivesoftware.smack.xml.XmlPullParser parser, XmlEnvironment outerXmlEnvironment, org.jxmpp.JxmppContext jxmppContext) throws org.jivesoftware.smack.xml.XmlPullParserException, IOException, SmackParsingException
      Throws:
      org.jivesoftware.smack.xml.XmlPullParserException
      IOException
      SmackParsingException
    • addExtensionElement

      public static void addExtensionElement(Collection<XmlElement> collection, org.jivesoftware.smack.xml.XmlPullParser parser, String elementName, String namespace, XmlEnvironment outerXmlEnvironment, org.jxmpp.JxmppContext jxmppContext) throws org.jivesoftware.smack.xml.XmlPullParserException, IOException, SmackParsingException
      Throws:
      org.jivesoftware.smack.xml.XmlPullParserException
      IOException
      SmackParsingException