mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-12-21 12:17:56 +01:00
Merge branch '4.4'
This also fixes a errornous merge where the same branch with different commit was merged into master and 4.4 The conflicting commits are 4.4:8f760eaeb3
getRawValueCharSequencese626580f68
master:b47225c2c1
getRawValues097d245358
This commit is contained in:
commit
001985647a
13 changed files with 119 additions and 84 deletions
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2003-2007 Jive Software, 2016-2020 Florian Schmaus.
|
||||
* Copyright 2003-2007 Jive Software, 2016-2021 Florian Schmaus.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -20,6 +20,7 @@ package org.jivesoftware.smack.util;
|
|||
import java.io.IOException;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
@ -605,4 +606,13 @@ public class StringUtils {
|
|||
String[] lines = input.split(PORTABLE_NEWLINE_REGEX);
|
||||
return Arrays.asList(lines);
|
||||
}
|
||||
|
||||
public static List<String> toStrings(Collection<? extends CharSequence> charSequences) {
|
||||
List<String> res = new ArrayList<>(charSequences.size());
|
||||
for (CharSequence cs : charSequences) {
|
||||
String string = cs.toString();
|
||||
res.add(string);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -700,7 +700,7 @@ public final class EntityCapsManager extends Manager {
|
|||
for (FormField f : fs) {
|
||||
sb.append(f.getFieldName());
|
||||
sb.append('<');
|
||||
formFieldValuesToCaps(f.getRawValues(), sb);
|
||||
formFieldValuesToCaps(f.getRawValueCharSequences(), sb);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -38,11 +38,7 @@ public class FormNode extends NodeExtension {
|
|||
* @param submitForm The form
|
||||
*/
|
||||
public FormNode(FormNodeType formType, DataForm submitForm) {
|
||||
super(formType.getNodeElement());
|
||||
|
||||
if (submitForm == null)
|
||||
throw new IllegalArgumentException("Submit form cannot be null");
|
||||
configForm = submitForm;
|
||||
this(formType, null, submitForm);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -55,9 +51,6 @@ public class FormNode extends NodeExtension {
|
|||
*/
|
||||
public FormNode(FormNodeType formType, String nodeId, DataForm submitForm) {
|
||||
super(formType.getNodeElement(), nodeId);
|
||||
|
||||
if (submitForm == null)
|
||||
throw new IllegalArgumentException("Submit form cannot be null");
|
||||
configForm = submitForm;
|
||||
}
|
||||
|
||||
|
|
|
@ -35,6 +35,11 @@ import org.jivesoftware.smackx.xdata.packet.DataForm;
|
|||
public class FormNodeProvider extends EmbeddedExtensionProvider<FormNode> {
|
||||
@Override
|
||||
protected FormNode createReturnExtension(String currentElement, String currentNamespace, Map<String, String> attributeMap, List<? extends XmlElement> content) {
|
||||
return new FormNode(FormNodeType.valueOfFromElementName(currentElement, currentNamespace), attributeMap.get("node"), (DataForm) content.iterator().next());
|
||||
DataForm dataForm = null;
|
||||
if (!content.isEmpty()) {
|
||||
dataForm = (DataForm) content.get(0);
|
||||
}
|
||||
|
||||
return new FormNode(FormNodeType.valueOfFromElementName(currentElement, currentNamespace), attributeMap.get("node"), dataForm);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,35 +27,26 @@ import org.jxmpp.util.XmppDateTime;
|
|||
|
||||
public class AbstractMultiFormField extends FormField {
|
||||
|
||||
private final List<String> values;
|
||||
|
||||
private final List<String> rawValues;
|
||||
private final List<Value> values;
|
||||
|
||||
protected AbstractMultiFormField(Builder<?, ?> builder) {
|
||||
super(builder);
|
||||
values = CollectionUtil.cloneAndSeal(builder.values);
|
||||
rawValues = CollectionUtil.cloneAndSeal(builder.rawValues);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<String> getValues() {
|
||||
public final List<Value> getRawValues() {
|
||||
return values;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<String> getRawValues() {
|
||||
return rawValues;
|
||||
}
|
||||
|
||||
public abstract static class Builder<F extends AbstractMultiFormField, B extends FormField.Builder<F, B>>
|
||||
extends FormField.Builder<F, B> {
|
||||
|
||||
private List<String> values;
|
||||
private List<String> rawValues;
|
||||
private List<Value> values;
|
||||
|
||||
protected Builder(AbstractMultiFormField formField) {
|
||||
super(formField);
|
||||
values = CollectionUtil.newListWith(formField.getValues());
|
||||
values = CollectionUtil.newListWith(formField.getRawValues());
|
||||
}
|
||||
|
||||
protected Builder(String fieldName, FormField.Type type) {
|
||||
|
@ -65,7 +56,6 @@ public class AbstractMultiFormField extends FormField {
|
|||
private void ensureValuesAreInitialized() {
|
||||
if (values == null) {
|
||||
values = new ArrayList<>();
|
||||
rawValues = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,11 +67,13 @@ public class AbstractMultiFormField extends FormField {
|
|||
public abstract B addValue(CharSequence value);
|
||||
|
||||
public B addValueVerbatim(CharSequence value) {
|
||||
return addValueVerbatim(new Value(value));
|
||||
}
|
||||
|
||||
public B addValueVerbatim(Value value) {
|
||||
ensureValuesAreInitialized();
|
||||
|
||||
String valueString = value.toString();
|
||||
values.add(valueString);
|
||||
rawValues.add(valueString);
|
||||
values.add(value);
|
||||
return getThis();
|
||||
}
|
||||
|
||||
|
|
|
@ -74,8 +74,15 @@ public class AbstractSingleStringValueFormField extends SingleValueFormField {
|
|||
return setValue(value);
|
||||
}
|
||||
|
||||
public B setValue(Value value) {
|
||||
this.value = value.getValue().toString();
|
||||
this.rawValue = value;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public B setValue(CharSequence value) {
|
||||
this.rawValue = this.value = value.toString();
|
||||
this.value = value.toString();
|
||||
rawValue = new Value(this.value);
|
||||
return getThis();
|
||||
}
|
||||
|
||||
|
|
|
@ -71,17 +71,21 @@ public class BooleanFormField extends SingleValueFormField {
|
|||
@Deprecated
|
||||
// TODO: Remove in Smack 4.6.
|
||||
public Builder addValue(CharSequence value) {
|
||||
return setValue(value);
|
||||
return setValue(new Value(value));
|
||||
}
|
||||
|
||||
public Builder setValue(CharSequence value) {
|
||||
rawValue = value.toString();
|
||||
boolean valueBoolean = ParserUtils.parseXmlBoolean(rawValue);
|
||||
return setValue(valueBoolean);
|
||||
return setValue(new Value(value));
|
||||
}
|
||||
|
||||
public Builder setValue(Value value) {
|
||||
this.value = ParserUtils.parseXmlBoolean(value.getValue().toString());
|
||||
rawValue = value;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public Builder setValue(boolean value) {
|
||||
rawValue = Boolean.toString(value);
|
||||
rawValue = new Value(Boolean.toString(value));
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
|
|
@ -269,9 +269,25 @@ public abstract class FormField implements XmlElement {
|
|||
*
|
||||
* @return a List of the default values or answered values of the question.
|
||||
*/
|
||||
public abstract List<? extends CharSequence> getValues();
|
||||
public List<? extends CharSequence> getValues() {
|
||||
return getRawValueCharSequences();
|
||||
}
|
||||
|
||||
public abstract List<String> getRawValues();
|
||||
public abstract List<Value> getRawValues();
|
||||
|
||||
private transient List<CharSequence> rawValueCharSequences;
|
||||
|
||||
public final List<CharSequence> getRawValueCharSequences() {
|
||||
if (rawValueCharSequences == null) {
|
||||
List<Value> rawValues = getRawValues();
|
||||
rawValueCharSequences = new ArrayList<>(rawValues.size());
|
||||
for (Value value : rawValues) {
|
||||
rawValueCharSequences.add(value.value);
|
||||
}
|
||||
}
|
||||
|
||||
return rawValueCharSequences;
|
||||
}
|
||||
|
||||
public boolean hasValueSet() {
|
||||
List<?> values = getValues();
|
||||
|
@ -385,12 +401,15 @@ public abstract class FormField implements XmlElement {
|
|||
|
||||
protected transient List<XmlElement> extraXmlChildElements;
|
||||
|
||||
/**
|
||||
* Populate @{link {@link #extraXmlChildElements}}. Note that this method may be overridden by subclasses.
|
||||
*/
|
||||
protected void populateExtraXmlChildElements() {
|
||||
List<? extends CharSequence> values = getValues();
|
||||
List<Value> values = getRawValues();
|
||||
// Note that we need to create a new ArrayList here, since subclasses may add to it by overriding
|
||||
// populateExtraXmlChildElements.
|
||||
extraXmlChildElements = new ArrayList<>(values.size());
|
||||
for (CharSequence value : values) {
|
||||
extraXmlChildElements.add(new Value(value));
|
||||
}
|
||||
extraXmlChildElements.addAll(values);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -414,7 +433,8 @@ public abstract class FormField implements XmlElement {
|
|||
populateExtraXmlChildElements();
|
||||
}
|
||||
|
||||
if (formFieldChildElements.isEmpty() && extraXmlChildElements == null) {
|
||||
if (formFieldChildElements.isEmpty()
|
||||
&& (extraXmlChildElements == null || extraXmlChildElements.isEmpty())) {
|
||||
buf.closeEmptyElement();
|
||||
} else {
|
||||
buf.rightAngleBracket();
|
||||
|
|
|
@ -23,13 +23,14 @@ import java.util.List;
|
|||
import org.jivesoftware.smack.util.CollectionUtil;
|
||||
|
||||
import org.jxmpp.jid.Jid;
|
||||
import org.jxmpp.jid.util.JidUtil;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
|
||||
public final class JidMultiFormField extends FormField {
|
||||
|
||||
private final List<Jid> values;
|
||||
|
||||
private final List<String> rawValues;
|
||||
private final List<Value> rawValues;
|
||||
|
||||
JidMultiFormField(Builder builder) {
|
||||
super(builder);
|
||||
|
@ -43,7 +44,7 @@ public final class JidMultiFormField extends FormField {
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<String> getRawValues() {
|
||||
public List<Value> getRawValues() {
|
||||
return rawValues;
|
||||
}
|
||||
|
||||
|
@ -54,7 +55,7 @@ public final class JidMultiFormField extends FormField {
|
|||
public static final class Builder extends FormField.Builder<JidMultiFormField, JidMultiFormField.Builder> {
|
||||
private List<Jid> values;
|
||||
|
||||
private List<String> rawValues;
|
||||
private List<Value> rawValues;
|
||||
|
||||
private Builder(JidMultiFormField jidMultiFormField) {
|
||||
super(jidMultiFormField);
|
||||
|
@ -79,27 +80,29 @@ public final class JidMultiFormField extends FormField {
|
|||
}
|
||||
|
||||
public Builder addValue(Jid jid) {
|
||||
return addValue(jid, null);
|
||||
}
|
||||
|
||||
public Builder addValue(Jid jid, String rawValue) {
|
||||
if (rawValue == null) {
|
||||
rawValue = jid.toString();
|
||||
}
|
||||
Value value = new Value(jid);
|
||||
|
||||
ensureValuesAreInitialized();
|
||||
|
||||
values.add(jid);
|
||||
rawValues.add(rawValue);
|
||||
rawValues.add(value);
|
||||
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public Builder addValue(Value value) throws XmppStringprepException {
|
||||
Jid jid = JidCreate.from(value.getValue());
|
||||
|
||||
ensureValuesAreInitialized();
|
||||
values.add(jid);
|
||||
rawValues.add(value);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addValues(Collection<? extends Jid> jids) {
|
||||
ensureValuesAreInitialized();
|
||||
|
||||
values.addAll(jids);
|
||||
rawValues.addAll(JidUtil.toStringList(jids));
|
||||
for (Jid jid : jids) {
|
||||
addValue(jid);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
package org.jivesoftware.smackx.xdata;
|
||||
|
||||
import org.jxmpp.jid.Jid;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
|
||||
public class JidSingleFormField extends SingleValueFormField {
|
||||
|
||||
|
@ -60,11 +62,13 @@ public class JidSingleFormField extends SingleValueFormField {
|
|||
|
||||
public Builder setValue(Jid value, String rawValue) {
|
||||
this.value = value;
|
||||
if (rawValue != null) {
|
||||
this.rawValue = rawValue;
|
||||
} else {
|
||||
this.rawValue = value.toString();
|
||||
}
|
||||
this.rawValue = new Value(value);
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public Builder setValue(Value value) throws XmppStringprepException {
|
||||
this.value = JidCreate.from(value.getValue());
|
||||
this.rawValue = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ import org.jivesoftware.smack.util.CollectionUtil;
|
|||
|
||||
public abstract class SingleValueFormField extends FormField {
|
||||
|
||||
private final String rawValue;
|
||||
private final Value rawValue;
|
||||
|
||||
protected SingleValueFormField(Builder<?, ?> builder) {
|
||||
super(builder);
|
||||
|
@ -38,24 +38,23 @@ public abstract class SingleValueFormField extends FormField {
|
|||
|
||||
public abstract CharSequence getValue();
|
||||
|
||||
public final String getRawValue() {
|
||||
public final Value getRawValue() {
|
||||
return rawValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<String> getRawValues() {
|
||||
String rawValue = getRawValue();
|
||||
public final List<Value> getRawValues() {
|
||||
Value rawValue = getRawValue();
|
||||
return CollectionUtil.emptyOrSingletonListFrom(rawValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void populateExtraXmlChildElements() {
|
||||
CharSequence value = getValue();
|
||||
if (value == null) {
|
||||
if (rawValue == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
extraXmlChildElements = Collections.singletonList(new Value(value));
|
||||
extraXmlChildElements = Collections.singletonList(rawValue);
|
||||
}
|
||||
|
||||
public abstract static class Builder<F extends SingleValueFormField, B extends Builder<F, B>>
|
||||
|
@ -65,11 +64,12 @@ public abstract class SingleValueFormField extends FormField {
|
|||
super(fieldName, type);
|
||||
}
|
||||
|
||||
protected Builder(FormField formField) {
|
||||
protected Builder(SingleValueFormField formField) {
|
||||
super(formField);
|
||||
rawValue = formField.getRawValue();
|
||||
}
|
||||
|
||||
protected String rawValue;
|
||||
protected Value rawValue;
|
||||
|
||||
@Override
|
||||
protected void resetInternal() {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2020 Florian Schmaus
|
||||
* Copyright 2020-2021 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -21,6 +21,8 @@ import java.util.Collections;
|
|||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
|
||||
import org.jivesoftware.smackx.xdata.AbstractMultiFormField;
|
||||
import org.jivesoftware.smackx.xdata.AbstractSingleStringValueFormField;
|
||||
import org.jivesoftware.smackx.xdata.BooleanFormField;
|
||||
|
@ -54,7 +56,8 @@ public interface FormReader {
|
|||
return Collections.emptyList();
|
||||
}
|
||||
AbstractMultiFormField multiFormField = formField.ifPossibleAs(AbstractMultiFormField.class);
|
||||
return multiFormField.getValues();
|
||||
List<? extends CharSequence> charSequences = multiFormField.getValues();
|
||||
return StringUtils.toStrings(charSequences);
|
||||
}
|
||||
|
||||
default Boolean readBoolean(String fieldName) {
|
||||
|
|
|
@ -49,9 +49,6 @@ import org.jivesoftware.smackx.xdata.packet.DataForm;
|
|||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout;
|
||||
import org.jivesoftware.smackx.xdatalayout.provider.DataLayoutProvider;
|
||||
|
||||
import org.jxmpp.jid.Jid;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
|
||||
/**
|
||||
* The DataFormProvider parses DataForm packets.
|
||||
*
|
||||
|
@ -237,9 +234,7 @@ public class DataFormProvider extends ExtensionElementProvider<DataForm> {
|
|||
case jid_multi:
|
||||
JidMultiFormField.Builder jidMultiBuilder = FormField.jidMultiBuilder(fieldName);
|
||||
for (FormField.Value value : values) {
|
||||
String rawValue = value.getValue().toString();
|
||||
Jid jid = JidCreate.from(rawValue);
|
||||
jidMultiBuilder.addValue(jid, rawValue);
|
||||
jidMultiBuilder.addValue(value);
|
||||
}
|
||||
builder = jidMultiBuilder;
|
||||
break;
|
||||
|
@ -247,9 +242,8 @@ public class DataFormProvider extends ExtensionElementProvider<DataForm> {
|
|||
ensureAtMostSingleValue(type, values);
|
||||
JidSingleFormField.Builder jidSingleBuilder = FormField.jidSingleBuilder(fieldName);
|
||||
if (!values.isEmpty()) {
|
||||
String rawValue = values.get(0).getValue().toString();
|
||||
Jid jid = JidCreate.from(rawValue);
|
||||
jidSingleBuilder.setValue(jid, rawValue);
|
||||
FormField.Value value = values.get(0);
|
||||
jidSingleBuilder.setValue(value);
|
||||
}
|
||||
builder = jidSingleBuilder;
|
||||
break;
|
||||
|
@ -303,7 +297,7 @@ public class DataFormProvider extends ExtensionElementProvider<DataForm> {
|
|||
BooleanFormField.Builder builder = FormField.booleanBuilder(fieldName);
|
||||
ensureAtMostSingleValue(builder.getType(), values);
|
||||
if (values.size() == 1) {
|
||||
String value = values.get(0).getValue().toString();
|
||||
FormField.Value value = values.get(0);
|
||||
builder.setValue(value);
|
||||
}
|
||||
return builder;
|
||||
|
|
Loading…
Reference in a new issue