1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 20:12:07 +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 { public static Message parseMessage(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
return parseMessage(parser, XmlEnvironment.EMPTY); return parseMessage(parser, XmlEnvironment.EMPTY);
} }
@ -142,15 +156,11 @@ public class PacketParserUtils {
XmlEnvironment messageXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment); XmlEnvironment messageXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
final int initialDepth = parser.getDepth(); final int initialDepth = parser.getDepth();
Message message = new Message(); Message message = new Message();
message.setStanzaId(parser.getAttributeValue("", "id")); parseCommonStanzaAttributes(message, parser, outerXmlEnvironment);
message.setTo(ParserUtils.getJidAttribute(parser, "to"));
message.setFrom(ParserUtils.getJidAttribute(parser, "from"));
String typeString = parser.getAttributeValue("", "type"); String typeString = parser.getAttributeValue("", "type");
if (typeString != null) { if (typeString != null) {
message.setType(Message.Type.fromString(typeString)); 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 // 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 // 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); type = Presence.Type.fromString(typeString);
} }
Presence presence = new Presence(type); Presence presence = new Presence(type);
presence.setTo(ParserUtils.getJidAttribute(parser, "to")); parseCommonStanzaAttributes(presence, parser, outerXmlEnvironment);
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);
}
// Parse sub-elements // Parse sub-elements
outerloop: while (true) { 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.UInt16;
import org.jivesoftware.smack.datatypes.UInt32; import org.jivesoftware.smack.datatypes.UInt32;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException; import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.parsing.SmackParsingException.SmackTextParseException; import org.jivesoftware.smack.parsing.SmackParsingException.SmackTextParseException;
import org.jivesoftware.smack.parsing.SmackParsingException.SmackUriSyntaxParsingException; import org.jivesoftware.smack.parsing.SmackParsingException.SmackUriSyntaxParsingException;
@ -341,6 +342,14 @@ public class ParserUtils {
return text; 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) { public static String getXmlLang(XmlPullParser parser) {
return parser.getAttributeValue("http://www.w3.org/XML/1998/namespace", "lang"); return parser.getAttributeValue("http://www.w3.org/XML/1998/namespace", "lang");
} }