use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class JingleProvider method parseJingleReason.
public static JingleReason parseJingleReason(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
ParserUtils.assertAtStartTag(parser);
final int initialDepth = parser.getDepth();
final String jingleNamespace = parser.getNamespace();
JingleReason.Reason reason = null;
XmlElement element = null;
String text = null;
// 'sid' is only set if the reason is 'alternative-session'.
String sid = null;
outerloop: while (true) {
XmlPullParser.TagEvent event = parser.nextTag();
switch(event) {
case START_ELEMENT:
String elementName = parser.getName();
String namespace = parser.getNamespace();
if (namespace.equals(jingleNamespace)) {
switch(elementName) {
case "text":
text = parser.nextText();
break;
case "alternative-session":
parser.next();
sid = parser.nextText();
break;
default:
reason = Reason.fromString(elementName);
break;
}
} else {
element = PacketParserUtils.parseExtensionElement(elementName, namespace, parser, null);
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
}
}
JingleReason res;
if (sid != null) {
res = new JingleReason.AlternativeSession(sid, text, element);
} else {
res = new JingleReason(reason, text, element);
}
return res;
}
use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class PacketParserUtils method addExtensionElement.
public static void addExtensionElement(StanzaBuilder<?> stanzaBuilder, XmlPullParser parser, String elementName, String namespace, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
XmlElement extensionElement = parseExtensionElement(elementName, namespace, parser, outerXmlEnvironment);
stanzaBuilder.addExtension(extensionElement);
}
use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class PacketParserUtils method parseStreamError.
/**
* Parses stream error packets.
*
* @param parser the XML parser.
* @param outerXmlEnvironment the outer XML environment (optional).
* @return an stream error packet.
* @throws IOException if an I/O error occurred.
* @throws XmlPullParserException if an error in the XML parser occurred.
* @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
*/
public static StreamError parseStreamError(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
final int initialDepth = parser.getDepth();
List<XmlElement> extensions = new ArrayList<>();
Map<String, String> descriptiveTexts = null;
StreamError.Condition condition = null;
String conditionText = null;
XmlEnvironment streamErrorXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
outerloop: while (true) {
XmlPullParser.Event eventType = parser.next();
switch(eventType) {
case START_ELEMENT:
String name = parser.getName();
String namespace = parser.getNamespace();
switch(namespace) {
case StreamError.NAMESPACE:
switch(name) {
case "text":
descriptiveTexts = parseDescriptiveTexts(parser, descriptiveTexts);
break;
default:
// If it's not a text element, that is qualified by the StreamError.NAMESPACE,
// then it has to be the stream error code
condition = StreamError.Condition.fromString(name);
conditionText = parser.nextText();
if (conditionText.isEmpty()) {
conditionText = null;
}
break;
}
break;
default:
PacketParserUtils.addExtensionElement(extensions, parser, name, namespace, streamErrorXmlEnvironment);
break;
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
}
return new StreamError(condition, conditionText, descriptiveTexts, extensions);
}
use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class PacketParserUtils method addExtensionElement.
public static void addExtensionElement(Stanza packet, XmlPullParser parser, String elementName, String namespace, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
XmlElement packetExtension = parseExtensionElement(elementName, namespace, parser, outerXmlEnvironment);
packet.addExtension(packetExtension);
}
use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class PacketParserUtils method parsePresence.
/**
* Parses a presence packet.
*
* @param parser the XML parser, positioned at the start of a presence packet.
* @param outerXmlEnvironment the outer XML environment (optional).
* @return a Presence packet.
* @throws IOException if an I/O error occurred.
* @throws XmlPullParserException if an error in the XML parser occurred.
* @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
*/
public static Presence parsePresence(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
ParserUtils.assertAtStartTag(parser);
final int initialDepth = parser.getDepth();
XmlEnvironment presenceXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
PresenceBuilder presence = parseCommonStanzaAttributes(stanzaId -> StanzaBuilder.buildPresence(stanzaId), parser, outerXmlEnvironment);
Presence.Type type = Presence.Type.available;
String typeString = parser.getAttributeValue("", "type");
if (typeString != null && !typeString.equals("")) {
type = Presence.Type.fromString(typeString);
}
presence.ofType(type);
// Parse sub-elements
outerloop: while (true) {
XmlPullParser.Event eventType = parser.next();
switch(eventType) {
case START_ELEMENT:
String elementName = parser.getName();
String namespace = parser.getNamespace();
switch(elementName) {
case "status":
presence.setStatus(parser.nextText());
break;
case "priority":
Byte priority = ParserUtils.getByteAttributeFromNextText(parser);
presence.setPriority(priority);
break;
case "show":
String modeText = parser.nextText();
if (StringUtils.isNotEmpty(modeText)) {
presence.setMode(Presence.Mode.fromString(modeText));
} else {
// Some implementations send presence stanzas with a
// '<show />' element, which is a invalid XMPP presence
// stanza according to RFC 6121 4.7.2.1
LOGGER.warning("Empty or null mode text in presence show element form " + presence + "' which is invalid according to RFC6121 4.7.2.1");
}
break;
case "error":
presence.setError(parseError(parser, presenceXmlEnvironment));
break;
default:
// failing completely here. See SMACK-390 for more information.
try {
XmlElement extensionElement = parseExtensionElement(elementName, namespace, parser, presenceXmlEnvironment);
presence.addExtension(extensionElement);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to parse extension element in Presence stanza: " + presence, e);
}
break;
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
}
return presence.build();
}
Aggregations