1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-25 07:42:06 +01:00

Add XmlEnvironment conscious ParserUtils.getXmlLang()

and unify parsing of common stanza attributes where this new method is
used.
This commit is contained in:
Florian Schmaus 2019-09-12 15:04:51 +02:00
parent 86c28e9d13
commit f2e1d7ddd5
2 changed files with 25 additions and 13 deletions

View file

@ -121,6 +121,20 @@ public class PacketParserUtils {
}
}
private static void parseCommonStanzaAttributes(Stanza stanza, XmlPullParser parser, XmlEnvironment xmlEnvironment) throws XmppStringprepException {
String id = parser.getAttributeValue("id");
stanza.setStanzaId(id);
Jid to = ParserUtils.getJidAttribute(parser, "to");
stanza.setTo(to);
Jid from = ParserUtils.getJidAttribute(parser, "from");
stanza.setFrom(from);
String language = ParserUtils.getXmlLang(parser, xmlEnvironment);
stanza.setLanguage(language);
}
public static Message parseMessage(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
return parseMessage(parser, XmlEnvironment.EMPTY);
}
@ -142,15 +156,11 @@ public class PacketParserUtils {
XmlEnvironment messageXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
final int initialDepth = parser.getDepth();
Message message = new Message();
message.setStanzaId(parser.getAttributeValue("", "id"));
message.setTo(ParserUtils.getJidAttribute(parser, "to"));
message.setFrom(ParserUtils.getJidAttribute(parser, "from"));
parseCommonStanzaAttributes(message, parser, outerXmlEnvironment);
String typeString = parser.getAttributeValue("", "type");
if (typeString != null) {
message.setType(Message.Type.fromString(typeString));
}
String language = ParserUtils.getXmlLang(parser);
message.setLanguage(language);
// Parse sub-elements. We include extra logic to make sure the values
// are only read once. This is because it's possible for the names to appear
@ -433,14 +443,7 @@ public class PacketParserUtils {
type = Presence.Type.fromString(typeString);
}
Presence presence = new Presence(type);
presence.setTo(ParserUtils.getJidAttribute(parser, "to"));
presence.setFrom(ParserUtils.getJidAttribute(parser, "from"));
presence.setStanzaId(parser.getAttributeValue("", "id"));
String language = ParserUtils.getXmlLang(parser);
if (language != null && !"".equals(language.trim())) {
presence.setLanguage(language);
}
parseCommonStanzaAttributes(presence, parser, outerXmlEnvironment);
// Parse sub-elements
outerloop: while (true) {

View file

@ -27,6 +27,7 @@ import javax.xml.namespace.QName;
import org.jivesoftware.smack.datatypes.UInt16;
import org.jivesoftware.smack.datatypes.UInt32;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.parsing.SmackParsingException.SmackTextParseException;
import org.jivesoftware.smack.parsing.SmackParsingException.SmackUriSyntaxParsingException;
@ -341,6 +342,14 @@ public class ParserUtils {
return text;
}
public static String getXmlLang(XmlPullParser parser, XmlEnvironment xmlEnvironment) {
String currentXmlLang = getXmlLang(parser);
if (currentXmlLang != null) {
return currentXmlLang;
}
return xmlEnvironment.getEffectiveLanguage();
}
public static String getXmlLang(XmlPullParser parser) {
return parser.getAttributeValue("http://www.w3.org/XML/1998/namespace", "lang");
}