mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-12-09 14:41:08 +01:00
Get descriptive text from error without lang
A stanza with <error><text>Some text</text></error>, i.e. without a xml:lang attribute, did not return the value 'Some text' in getDescriptiveText(). Insert the empty string as key when the attribute is not present while parsing the xml. When writing, omit the attribute if the key is the empty string.
This commit is contained in:
parent
1d943aed20
commit
699145ee5f
4 changed files with 63 additions and 2 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue