use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class AbstractXMPPConnection method parseFeatures.
protected final void parseFeatures(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
streamFeatures.clear();
final int initialDepth = parser.getDepth();
while (true) {
XmlPullParser.Event eventType = parser.next();
if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getDepth() == initialDepth + 1) {
XmlElement streamFeature = null;
String name = parser.getName();
String namespace = parser.getNamespace();
switch(name) {
case StartTls.ELEMENT:
streamFeature = PacketParserUtils.parseStartTlsFeature(parser);
break;
case Mechanisms.ELEMENT:
streamFeature = new Mechanisms(PacketParserUtils.parseMechanisms(parser));
break;
case Bind.ELEMENT:
streamFeature = Bind.Feature.INSTANCE;
break;
case Session.ELEMENT:
streamFeature = PacketParserUtils.parseSessionFeature(parser);
break;
case Compress.Feature.ELEMENT:
streamFeature = PacketParserUtils.parseCompressionFeature(parser);
break;
default:
ExtensionElementProvider<ExtensionElement> provider = ProviderManager.getStreamFeatureProvider(name, namespace);
if (provider != null) {
streamFeature = provider.parse(parser, incomingStreamXmlEnvironment);
}
break;
}
if (streamFeature != null) {
addStreamFeature(streamFeature);
}
} else if (eventType == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == initialDepth) {
break;
}
}
}
use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class ExtensionElementFilter method accept.
@Override
public final boolean accept(Stanza stanza) {
XmlElement extensionElement = stanza.getExtension(extensionElementQName);
if (extensionElement == null) {
return false;
}
if (!extensionElementClass.isInstance(extensionElement)) {
return false;
}
E specificExtensionElement = extensionElementClass.cast(extensionElement);
return accept(specificExtensionElement);
}
use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class PacketParserUtils method parseMessage.
/**
* Parses a message packet.
*
* @param parser the XML parser, positioned at the start of a message packet.
* @param outerXmlEnvironment the outer XML environment (optional).
* @return a Message packet.
* @throws XmlPullParserException if an error in the XML parser occurred.
* @throws IOException if an I/O error occurred.
* @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
*/
public static Message parseMessage(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
ParserUtils.assertAtStartTag(parser);
assert parser.getName().equals(Message.ELEMENT);
XmlEnvironment messageXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
final int initialDepth = parser.getDepth();
MessageBuilder message = parseCommonStanzaAttributes(id -> {
return StanzaBuilder.buildMessage(id);
}, parser, outerXmlEnvironment);
String typeString = parser.getAttributeValue("", "type");
if (typeString != null) {
message.ofType(Message.Type.fromString(typeString));
}
// in arbitrary 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 "error":
message.setError(parseError(parser, messageXmlEnvironment));
break;
default:
XmlElement extensionElement = parseExtensionElement(elementName, namespace, parser, messageXmlEnvironment);
message.addExtension(extensionElement);
break;
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
// fall out
default:
}
}
return message.build();
}
use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class PacketParserUtils method parseError.
/**
* Parses error sub-packets.
*
* @param parser the XML parser.
* @param outerXmlEnvironment the outer XML environment (optional).
* @return an error sub-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 StanzaError parseError(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
final int initialDepth = parser.getDepth();
Map<String, String> descriptiveTexts = null;
XmlEnvironment stanzaErrorXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
List<XmlElement> extensions = new ArrayList<>();
StanzaError.Builder builder = StanzaError.getBuilder();
// Parse the error header
builder.setType(StanzaError.Type.fromString(parser.getAttributeValue("", "type")));
builder.setErrorGenerator(parser.getAttributeValue("", "by"));
outerloop: while (true) {
XmlPullParser.Event eventType = parser.next();
switch(eventType) {
case START_ELEMENT:
String name = parser.getName();
String namespace = parser.getNamespace();
switch(namespace) {
case StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE:
switch(name) {
case Stanza.TEXT:
descriptiveTexts = parseDescriptiveTexts(parser, descriptiveTexts);
break;
default:
builder.setCondition(StanzaError.Condition.fromString(name));
String conditionText = parser.nextText();
if (!conditionText.isEmpty()) {
builder.setConditionText(conditionText);
}
break;
}
break;
default:
PacketParserUtils.addExtensionElement(extensions, parser, name, namespace, stanzaErrorXmlEnvironment);
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
}
builder.setExtensions(extensions).setDescriptiveTexts(descriptiveTexts);
return builder.build();
}
use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class ContentElementProvider method parsePayload.
private static void parsePayload(XmlPullParser parser, XmlEnvironment outerXmlEnvironment, ContentElement.Builder builder) throws IOException, XmlPullParserException, SmackParsingException {
final int initialDepth = parser.getDepth();
while (true) {
XmlPullParser.Event tag = parser.next();
if (tag == XmlPullParser.Event.START_ELEMENT) {
String name = parser.getName();
String namespace = parser.getNamespace();
XmlElement element = PacketParserUtils.parseExtensionElement(name, namespace, parser, outerXmlEnvironment);
builder.addPayloadItem(element);
}
if (tag == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == initialDepth) {
return;
}
}
}
Aggregations