Skip to content

Commit

Permalink
Ignore saveto attributes with incorrect namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Sep 30, 2022
1 parent d8d28c6 commit 43fff8d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@

public class EntityFormParseProcessor implements BindAttributeProcessor, FormDefProcessor {

private static final String ENTITIES_NAMESPACE = "http://www.opendatakit.org/xforms/entities";

private final List<Pair<XPathReference, String>> saveTos = new ArrayList<>();

@Override
public Set<String> getUsedAttributes() {
HashSet<String> attributes = new HashSet<>();
attributes.add("saveto");
public Set<Pair<String, String>> getUsedAttributes() {
HashSet<Pair<String, String>> attributes = new HashSet<>();
attributes.add(new Pair<>(ENTITIES_NAMESPACE, "saveto"));

return attributes;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.javarosa.xform.parse;

import kotlin.Pair;
import org.javarosa.core.model.DataBinding;

import java.util.Set;

public interface BindAttributeProcessor {

Set<String> getUsedAttributes();
Set<Pair<String, String>> getUsedAttributes();

void processBindingAttribute(String name, String value, DataBinding binding);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import kotlin.Pair;
import org.javarosa.core.model.DataBinding;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.IDataReference;
Expand Down Expand Up @@ -118,16 +119,18 @@ DataBinding createBinding(IXFormParserFunctions parserFunctions, FormDef formDef

bindAttributeProcessors.stream().forEach(bindAttributeProcessor -> {
for (int i = 0; i < element.getAttributeCount(); i++) {
String namespace = element.getAttributeNamespace(i);
String name = element.getAttributeName(i);
if (bindAttributeProcessor.getUsedAttributes().contains(name)) {

if (bindAttributeProcessor.getUsedAttributes().contains(new Pair<>(namespace, name))) {
bindAttributeProcessor.processBindingAttribute(name, element.getAttributeValue(i), binding);
}
}
});

List<String> processorAttributes = bindAttributeProcessors.stream()
.flatMap((Function<BindAttributeProcessor, Stream<String>>) bindAttributeProcessor -> {
return bindAttributeProcessor.getUsedAttributes().stream();
return bindAttributeProcessor.getUsedAttributes().stream().map(Pair::getSecond);
})
.collect(Collectors.toList());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.javarosa.entities.internal;

import kotlin.Pair;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.util.XFormsElement;
import org.javarosa.xform.parse.XFormParser;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.javarosa.core.util.BindBuilderXFormsElement.bind;
import static org.javarosa.core.util.XFormsElement.body;
import static org.javarosa.core.util.XFormsElement.head;
import static org.javarosa.core.util.XFormsElement.input;
import static org.javarosa.core.util.XFormsElement.mainInstance;
import static org.javarosa.core.util.XFormsElement.model;
import static org.javarosa.core.util.XFormsElement.t;
import static org.javarosa.core.util.XFormsElement.title;

public class EntityFormParseProcessorTest {

@Test
public void saveTosWithIncorrectNamespaceAreIgnored() throws IOException {
XFormsElement form = XFormsElement.html(
asList(
new Pair<>("correct", "http://www.opendatakit.org/xforms/entities"),
new Pair<>("incorrect", "blah")
),
head(
title("Create entity form"),
model(
mainInstance(
t("data id=\"create-entity-form\"",
t("name"),
t("orx:meta",
t("correct:entity dataset=\"people\"",
t("correct:create")
)
)
)
),
bind("/data/name").type("string").withAttribute("incorrect", "saveto", "name")
)
),
body(
input("/data/name")
)
);

EntityFormParseProcessor processor = new EntityFormParseProcessor();

XFormParser parser = new XFormParser(new InputStreamReader(new ByteArrayInputStream(form.asXml().getBytes())));
parser.addBindAttributeProcessor(processor);
parser.addFormDefProcessor(processor);

FormDef formDef = parser.parse(null);
assertThat(formDef.getExtras().get(EntityFormExtra.class).getSaveTos(), is(empty()));
}
}

0 comments on commit 43fff8d

Please sign in to comment.