/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.policy.assertion.XpathBasedAssertion
 *  com.l7tech.policy.variable.Syntax
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.xml.InvalidXpathException
 *  com.l7tech.xml.xpath.CompiledXpath
 *  com.l7tech.xml.xpath.XpathExpression
 *  com.l7tech.xml.xpath.XpathVersion
 */
package com.l7tech.server.policy.assertion;

import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.policy.assertion.XpathBasedAssertion;
import com.l7tech.policy.variable.Syntax;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.server.policy.variable.ExpandVariables;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.xml.InvalidXpathException;
import com.l7tech.xml.xpath.CompiledXpath;
import com.l7tech.xml.xpath.XpathExpression;
import com.l7tech.xml.xpath.XpathVersion;
import java.util.Map;

public abstract class ServerXpathBasedAssertion<AT extends XpathBasedAssertion>
extends AbstractServerAssertion<AT> {
    private final String d;
    private final CompiledXpath e;
    private final String[] f;

    public ServerXpathBasedAssertion(AT AT) {
        super(AT);
        CompiledXpath compiledXpath;
        this.d = AT.pattern();
        this.f = XpathBasedAssertion.isFullyDynamicXpath((String)this.d) ? Syntax.getReferencedNames((String)this.d) : null;
        try {
            compiledXpath = this.f != null ? null : AT.getXpathExpression().compile();
        }
        catch (InvalidXpathException invalidXpathException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.XPATH_PATTERN_INVALID, null, ExceptionUtils.getDebugException((Throwable)invalidXpathException));
            compiledXpath = null;
        }
        this.e = compiledXpath;
    }

    protected CompiledXpath getCompiledXpath(PolicyEnforcementContext policyEnforcementContext) {
        if (this.e != null) {
            return this.e;
        }
        if (this.f == null) {
            return null;
        }
        return this.a(policyEnforcementContext);
    }

    private CompiledXpath a(PolicyEnforcementContext policyEnforcementContext) {
        CompiledXpath compiledXpath;
        Map map;
        XpathVersion xpathVersion;
        Map<String, Object> map2 = policyEnforcementContext.getVariableMap(this.f, this.getAudit());
        String string = ExpandVariables.process(this.d, map2, this.getAudit());
        try {
            if (string.length() < 1) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.XPATH_DYNAMIC_PATTERN_INVALID);
                return null;
            }
        }
        catch (InvalidXpathException invalidXpathException) {
            throw ServerXpathBasedAssertion.a(invalidXpathException);
        }
        XpathExpression xpathExpression = ((XpathBasedAssertion)this.assertion).getXpathExpression();
        try {
            xpathVersion = xpathExpression == null ? XpathVersion.UNSPECIFIED : xpathExpression.getXpathVersion();
        }
        catch (InvalidXpathException invalidXpathException) {
            throw ServerXpathBasedAssertion.a(invalidXpathException);
        }
        XpathVersion xpathVersion2 = xpathVersion;
        try {
            map = xpathExpression == null ? null : xpathExpression.getNamespaces();
        }
        catch (InvalidXpathException invalidXpathException) {
            throw ServerXpathBasedAssertion.a(invalidXpathException);
        }
        Map map3 = map;
        XpathExpression xpathExpression2 = new XpathExpression(string, xpathVersion2, map3);
        try {
            compiledXpath = xpathExpression2.compile();
        }
        catch (InvalidXpathException invalidXpathException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.XPATH_DYNAMIC_PATTERN_INVALID, null, ExceptionUtils.getDebugException((Throwable)invalidXpathException));
            compiledXpath = null;
        }
        return compiledXpath;
    }

    protected boolean compiledXpathReferencesTargetDocument() {
        return this.e == null || this.e.requiresTargetDocument();
    }

    protected boolean compiledXpathUsesVariables() {
        return this.e == null || this.e.usesVariables();
    }

    protected String getXpath() {
        return this.d;
    }

    private static InvalidXpathException a(InvalidXpathException invalidXpathException) {
        return invalidXpathException;
    }
}

