use of org.jivesoftware.smack.packet.XmlEnvironment in project Smack by igniterealtime.
the class IQProvider method parse.
public final I parse(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws IOException, XmlPullParserException, SmackParsingException {
// XPP3 calling convention assert: Parser should be at start tag
ParserUtils.assertAtStartTag(parser);
final int initialDepth = parser.getDepth();
final XmlEnvironment xmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
I e = wrapExceptions(() -> parse(parser, initialDepth, xmlEnvironment));
// XPP3 calling convention assert: Parser should be at end tag of the consumed/parsed element
ParserUtils.forwardToEndTagOfDepth(parser, initialDepth);
return e;
}
use of org.jivesoftware.smack.packet.XmlEnvironment in project Smack by igniterealtime.
the class IqProvider method parse.
public final I parse(XmlPullParser parser, IqData iqData, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
final int initialDepth = parser.getDepth();
final XmlEnvironment xmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
I i = wrapExceptions(() -> parse(parser, initialDepth, iqData, xmlEnvironment));
return i;
}
use of org.jivesoftware.smack.packet.XmlEnvironment 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.XmlEnvironment 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.XmlEnvironment in project Smack by igniterealtime.
the class PacketParserUtils method parseIQ.
/**
* Parses an IQ packet.
*
* @param parser the XML parser, positioned at the start of an IQ packet.
* @param outerXmlEnvironment the outer XML environment (optional).
* @return an IQ object.
* @throws XmlPullParserException if an error in the XML parser occurred.
* @throws XmppStringprepException if the provided string is invalid.
* @throws IOException if an I/O error occurred.
* @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
*/
public static IQ parseIQ(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, XmppStringprepException, IOException, SmackParsingException {
ParserUtils.assertAtStartTag(parser);
final int initialDepth = parser.getDepth();
XmlEnvironment iqXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
IQ iqPacket = null;
StanzaError error = null;
IqData iqData = parseIqData(parser);
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":
error = PacketParserUtils.parseError(parser, iqXmlEnvironment);
break;
// this element name and namespace.
default:
IqProvider<IQ> provider = ProviderManager.getIQProvider(elementName, namespace);
if (provider != null) {
iqPacket = provider.parse(parser, iqData, outerXmlEnvironment);
} else // Note that if we reach this code, it is guaranteed that the result IQ contained a child element
// (RFC 6120 ยง 8.2.3 6) because otherwise we would have reached the END_ELEMENT first.
{
// No Provider found for the IQ stanza, parse it to an UnparsedIQ instance
// so that the content of the IQ can be examined later on
iqPacket = new UnparsedIQ(elementName, namespace, parseElement(parser));
}
break;
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
}
// Decide what to do when an IQ packet was not understood
if (iqPacket == null) {
switch(iqData.getType()) {
case error:
// If an IQ packet wasn't created above, create an empty error IQ packet.
iqPacket = new ErrorIQ(error);
break;
case result:
iqPacket = new EmptyResultIQ();
break;
default:
break;
}
}
// Set basic values on the iq packet.
iqPacket.setStanzaId(iqData.getStanzaId());
iqPacket.setTo(iqData.getTo());
iqPacket.setFrom(iqData.getFrom());
iqPacket.setType(iqData.getType());
iqPacket.setError(error);
return iqPacket;
}
Aggregations