Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to process xpath expressions during parse #733

Merged
merged 3 commits into from
Dec 1, 2023

Conversation

seadowg
Copy link
Member

@seadowg seadowg commented Nov 3, 2023

Work toward getodk/collect#5620

This adds a new XPathProcessor for XFormProcessor that allows a client to inspect every XPath expression in an XForm during parse:

parser.addProcessor(new XFormParser.XPathProcessor() {
    @Override
    public void processXPath(@NotNull XPathExpression xPathExpression) {
        // Do something
    }
});

I've also added a lock to XFormParser#parse that will prevent two forms being parsed at the same time (even using two different XFormParser instances) as XPathProcessor (and existing parse code) manipulates static state that could cause problems if that were to happen.

What has been done to verify that this works as intended?

New tests.

Why is this the best possible solution? Were any other approaches considered?

We had initially considered just using our existing bind processing with XPathExpression#containsFunc to find pulldata, but we realised that we needed a more general solution to be able to detect a custom function anywhere in the form (as it could appear in any xpath expression). I'm not a fan of the static state I've added here, but it felt like a losing battle to avoid it given XFormParser was already not thread safe in any way. Down the line, we should look at improving that so clients like Collect can parse multiple forms in parallel.

How does this change affect users? Describe intentional changes to behavior and behavior that could have accidentally been affected by code changes. In other words, what are the regression risks?

Stopping two forms from being parsed simultaneously is probably the riskiest change here. As far as I know, Collect doesn't do this on purpose, but it might be that it's a possibility with things like auto update or match exactly. That said, if that was happening, I'd potentially rather we crash the app than end up with whatever could happen mixing state from two form parses.

XFormParser is currently not thread safe as static state is
mutated during `parse` calls (such as `referencedInstanceIds`).

This means that two threads should never be allowed to parse two
forms at the same time, even if they are using separate instances
of `XFormParser`.
@seadowg seadowg marked this pull request as ready for review November 3, 2023 14:43
@@ -182,6 +183,9 @@ public class XFormParser implements IXFormParserFunctions {
private final List<FormDefProcessor> formDefProcessors = new ArrayList<>();
private final List<ModelAttributeProcessor> modelAttributeProcessors = new ArrayList<>();
private final List<QuestionProcessor> questionProcessors = new ArrayList<>();
private final List<XPathProcessor> xpathProcessors = new ArrayList<>();

public static final List<XPathProcessor> tempXPathProcessors = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took me a bit to understand that this is just so the processors can be accessed statically even though they need to be set on an instance to use the existing addProcessor.

@lognaturel lognaturel merged commit bee6652 into getodk:master Dec 1, 2023
3 checks passed
@seadowg seadowg deleted the func-detect branch December 1, 2023 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants