package com.l7tech.server.policy.assertion;

import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.policy.assertion.XpathBasedAssertion;
import com.l7tech.policy.variable.Syntax;
import com.l7tech.server.message.PolicyEnforcementContext;
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 var1) {
      super((AT)var1);
      this.d = var1.pattern();
      this.f = XpathBasedAssertion.isFullyDynamicXpath(this.d) ? Syntax.getReferencedNames(this.d) : null;

      CompiledXpath var2;
      try {
         var2 = this.f != null ? null : var1.getXpathExpression().compile();
      } catch (InvalidXpathException var4) {
         this.logAndAudit(AssertionMessages.XPATH_PATTERN_INVALID, null, ExceptionUtils.getDebugException(var4));
         var2 = null;
      }

      this.e = var2;
   }

   protected CompiledXpath getCompiledXpath(PolicyEnforcementContext var1) {
      if (this.e != null) {
         return this.e;
      } else {
         return this.f == null ? null : this.a(var1);
      }
   }

   private CompiledXpath a(PolicyEnforcementContext var1) {
      Map var2 = var1.getVariableMap(this.f, this.getAudit());
      String var3 = ExpandVariables.process(this.d, var2, this.getAudit());

      try {
         if (var3.length() < 1) {
            this.logAndAudit(AssertionMessages.XPATH_DYNAMIC_PATTERN_INVALID);
            return null;
         }
      } catch (InvalidXpathException var13) {
         throw a(var13);
      }

      XpathExpression var4 = this.assertion.getXpathExpression();

      XpathVersion var10000;
      label44: {
         try {
            if (var4 == null) {
               var10000 = XpathVersion.UNSPECIFIED;
               break label44;
            }
         } catch (InvalidXpathException var12) {
            throw a(var12);
         }

         var10000 = var4.getXpathVersion();
      }

      XpathVersion var5 = var10000;

      label36: {
         try {
            if (var4 == null) {
               var14 = null;
               break label36;
            }
         } catch (InvalidXpathException var11) {
            throw a(var11);
         }

         var14 = var4.getNamespaces();
      }

      Map var6 = var14;
      XpathExpression var7 = new XpathExpression(var3, var5, var6);

      CompiledXpath var8;
      try {
         var8 = var7.compile();
      } catch (InvalidXpathException var10) {
         this.logAndAudit(AssertionMessages.XPATH_DYNAMIC_PATTERN_INVALID, null, ExceptionUtils.getDebugException(var10));
         var8 = null;
      }

      return var8;
   }

   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 var0) {
      return var0;
   }
}
