From f2e1d7ddd594e6bda1bb4c650fe3c83c7a6d1d3e Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 12 Sep 2019 15:04:51 +0200 Subject: [PATCH] Add XmlEnvironment conscious ParserUtils.getXmlLang() and unify parsing of common stanza attributes where this new method is used. --- .../smack/util/PacketParserUtils.java | 29 ++++++++++--------- .../jivesoftware/smack/util/ParserUtils.java | 9 ++++++ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java index 54b27879d..a71361af1 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java @@ -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) { diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java index 81d232f10..3486b372d 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java @@ -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"); }