1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-12-08 20:11:08 +01:00

Merge pull request #160 from ibauersachs/parse-error-texts

Get descriptive text from error without lang
This commit is contained in:
Florian Schmaus 2017-09-29 19:04:14 +02:00 committed by GitHub
commit 122bf06ccc
4 changed files with 63 additions and 2 deletions

View file

@ -23,6 +23,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.PacketUtil;
import org.jivesoftware.smack.util.XmlStringBuilder;
@ -85,6 +86,7 @@ public class AbstractError {
* @return the descriptive text or null.
*/
public String getDescriptiveText(String xmllang) {
Objects.requireNonNull(xmllang, "xmllang must not be null");
return descriptiveTexts.get(xmllang);
}
@ -105,7 +107,8 @@ public class AbstractError {
String xmllang = entry.getKey();
String text = entry.getValue();
xml.halfOpenElement("text").xmlnsAttribute(textNamespace)
.xmllangAttribute(xmllang).rightAngleBracket();
.optXmlLangAttribute(xmllang)
.rightAngleBracket();
xml.escape(text);
xml.closeElement("text");
}
@ -120,6 +123,15 @@ public class AbstractError {
protected List<ExtensionElement> extensions;
public B setDescriptiveTexts(Map<String, String> descriptiveTexts) {
if (descriptiveTexts == null) {
this.descriptiveTexts = null;
return getThis();
}
for (String key : descriptiveTexts.keySet()) {
if (key == null) {
throw new IllegalArgumentException("descriptiveTexts cannot contain null key");
}
}
if (this.descriptiveTexts == null) {
this.descriptiveTexts = descriptiveTexts;
}

View file

@ -749,6 +749,12 @@ public class PacketParserUtils {
descriptiveTexts = new HashMap<String, String>();
}
String xmllang = getLanguageAttribute(parser);
if (xmllang == null) {
// XMPPError assumes the default locale, 'en', or the empty string.
// Establish the invariant that there is never null as a key.
xmllang = "";
}
String text = parser.nextText();
String previousValue = descriptiveTexts.put(xmllang, text);
assert (previousValue == null);

View file

@ -361,7 +361,7 @@ public class XmlStringBuilder implements Appendable, CharSequence {
}
public XmlStringBuilder optXmlLangAttribute(String lang) {
if (lang != null) {
if (!StringUtils.isNullOrEmpty(lang)) {
xmllangAttribute(lang);
}
return this;