Ejemplo n.º 1
0
  public FunctionArgument evaluate(XACMLEvalContext pip) throws XACML3EntitlementException {

    int args = getArgCount();
    if (args < 2) {
      throw new IndeterminateException(
          "Function Requires at least 2 arguments, " + "however " + getArgCount() + " in stack.");
    }
    // Create our union DataBag from other Bags.
    DataBag unionBag = new DataBag();
    // Iterate Over All DataBag's in Stack, Evaluate and create a Union of all Bags with Unique
    // Objects.
    try {
      for (int i = 0; i < args; i++) {
        DataBag bag = (DataBag) getArg(i).doEvaluate(pip);
        if (bag == null) {
          continue;
        }
        // Set our Union Data Bag with First Bag's Data Type and check subsequent Bags.
        if (i == 0) {
          unionBag.setType(bag.getType());
        } else {
          // Verify our Data Type with First Data Bag's Data Type.
          if (bag.getType().getIndex() != unionBag.getType().getIndex()) {
            throw new IndeterminateException(
                "First Bag Type: "
                    + unionBag.getType().getTypeName()
                    + ", however a subsequent Bag Type was "
                    + bag.getType().getTypeName());
          }
        }
        // Iterate over the current Bag.
        for (int b = 0; b < bag.size(); b++) {
          DataValue dataValue = (DataValue) bag.get(b).doEvaluate(pip);
          boolean contained = false;
          for (int z = 0; z < unionBag.size(); z++) {
            // Apply the Typed Equal Function to determine if
            // the object already exists in the Union Bag.
            TimeEqual fEquals = new TimeEqual();
            fEquals.addArgument(unionBag.get(z));
            fEquals.addArgument(dataValue);
            FunctionArgument result = fEquals.doEvaluate(pip);
            if (result.isTrue()) {
              contained = true;
              break;
            }
          }
          // Add the Object if not contained.
          if (!contained) {
            // Add the Unique DataValue Element into the Union Bag.
            unionBag.add(dataValue);
          }
        } // End of Inner For Loop.
      } // End of Outer For Loop.
    } catch (Exception e) {
      throw new IndeterminateException("Iterating over Arguments Exception: " + e.getMessage());
    }
    // Return our UnionBag Value.
    return unionBag;
  }