package com.poco.PoCoRuntime;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Root policy that defers all decisions to its single child Policy. Created so that we can
 * test the code generated by the compiler before its able to parse/codegen tree-defining policies.
 */
public class DummyRootPolicy {
    private Policy child;

    public DummyRootPolicy(Policy child) {
        this.child = child;
    }

    /**
     * AspectJ calls this method on any attempted action.
     * @param event security-relevant action caught by AspectJ
     */
    public void queryAction(Event event) {
        // TODO: Do more than allow an action or halt
        SRE result = child.query(event);

        if (result == null) {
            return;
        }

        // For debugging purposes:
        System.out.format("Root policy queried with event: \"%s\"\n", event.getSignature());
        System.out.format("Child policy returned +`%s' -`%s'\n", result.positiveRE(), result.negativeRE());

        boolean posMatch = false;
        boolean negMatch = false;

        if (result.positiveRE().length() > 0) {
            Pattern posPat = Pattern.compile(result.positiveRE());
            Matcher posMatcher = posPat.matcher(event.getSignature());
            posMatch = posMatcher.find();
        }

        if (result.negativeRE().length() > 0) {
            Pattern negPat = Pattern.compile(result.negativeRE());
            Matcher negMatcher = negPat.matcher(event.getSignature());
            negMatch = negMatcher.find();
        }


        if (posMatch) {
            return;
        }

        if (negMatch) {
            System.exit(-1);
        }

        // Placeholder for promoting a different action other than triggering event
    }
}

@@ -421,24 +421,100 @@
 ild;%0A    %7D%0A%0A
+    public void setChild(Policy child) %7B%0A        this.child = child;%0A    %7D%0A%0A
     /**%0A    

add setChild method