1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-19 10:32:05 +01:00

Merge pull request #465 from Flowdalic/xdata-order-fields

[xdata] Order fields
This commit is contained in:
Florian Schmaus 2021-03-03 10:11:40 +01:00 committed by GitHub
commit bca3821343
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 0 deletions

View file

@ -100,6 +100,9 @@ public final class DataForm implements ExtensionElement {
instructions = CollectionUtil.cloneAndSeal(builder.instructions);
reportedData = builder.reportedData;
items = CollectionUtil.cloneAndSeal(builder.items);
builder.orderFields();
fields = CollectionUtil.cloneAndSeal(builder.fields);
fieldsMap = CollectionUtil.cloneAndSeal(builder.fieldsMap);
extensionElements = CollectionUtil.cloneAndSeal(builder.extensionElements);
@ -382,6 +385,30 @@ public final class DataForm implements ExtensionElement {
extensionElements = CollectionUtil.newListWith(dataForm.getExtensionElements());
}
private void orderFields() {
Iterator<FormField> it = fields.iterator();
if (!it.hasNext()) {
return;
}
FormField hiddenFormTypeField = it.next().asHiddenFormTypeFieldIfPossible();
if (hiddenFormTypeField != null) {
// The hidden FROM_TYPE field is already in first position, nothing to do.
return;
}
while (it.hasNext()) {
hiddenFormTypeField = it.next().asHiddenFormTypeFieldIfPossible();
if (hiddenFormTypeField != null) {
// Remove the hidden FORM_TYPE field that is not on first position.
it.remove();
// And insert it again at first position.
fields.add(0, hiddenFormTypeField);
break;
}
}
}
/**
* Deprecated do not use.
*

View file

@ -153,4 +153,14 @@ public class DataFormTest extends SmackTestSuite {
DataForm df = pr.parse(PacketParserUtils.getParserFor(formWithFixedField));
assertEquals(Type.fixed, df.getFields().get(0).getType());
}
@Test
public void testReorderHiddenFormTypeFieldAtFirstPosition() {
DataForm dataForm = DataForm.builder()
.addField(FormField.textSingleBuilder("foo1").setValue("bar").build())
.addField(FormField.textSingleBuilder("foo2").setValue("baz").build())
.setFormType("my-form-type")
.build();
assertNotNull(dataForm.getFields().get(0).asHiddenFormTypeFieldIfPossible());
}
}