private Element evaluateXPathNode(
      Node target, String expression, NamespaceContext namespaceContext) {
    NodeList nlst;
    try {
      xpath.setNamespaceContext(namespaceContext);
      nlst = (NodeList) xpath.evaluate(expression, target, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
      Util.fail("internalizer.XPathEvaluationError", e.getMessage());
      return null; // abort processing this <jaxb:bindings>
    }

    if (nlst.getLength() == 0) {
      Util.fail("internalizer.XPathEvaluatesToNoTarget", new Object[] {expression});
      return null; // abort
    }

    if (nlst.getLength() != 1) {
      Util.fail(
          "internalizer.XPathEvaulatesToTooManyTargets",
          new Object[] {expression, nlst.getLength()});
      return null; // abort
    }

    Node rnode = nlst.item(0);
    if (!(rnode instanceof Element)) {
      Util.fail("internalizer.XPathEvaluatesToNonElement", new Object[] {expression});
      return null; // abort
    }
    return (Element) rnode;
  }
示例#2
0
  public EvaluationResult evaluate(List<Evaluatable> inputs, EvaluationCtx context) {

    // Evaluate the arguments
    AttributeValue[] argValues = new AttributeValue[inputs.size()];
    EvaluationResult result = evalArgs(inputs, context, argValues);
    if (result != null) {
      return result;
    }

    switch (getFunctionId()) {
      case ID_XPATH_NODE_COUNT:
        {
          XPathAttribute xpathAttribute = ((XPathAttribute) argValues[0]);
          String xpathValue = xpathAttribute.getValue();
          String category = xpathAttribute.getXPathCategory();

          Node contextNode = null;

          // this must be XACML 3
          Set<Attributes> attributesSet = ((XACML3EvaluationCtx) context).getAttributes(category);
          if (attributesSet != null) {
            // only one attributes can be there
            Attributes attributes = attributesSet.iterator().next();
            contextNode = attributes.getContent();
          }

          if (contextNode != null) {
            // now apply XPath
            try {
              NodeList nodeList = getXPathResults(contextNode, xpathValue);
              return new EvaluationResult(new IntegerAttribute(nodeList.getLength()));
            } catch (XPathExpressionException e) {
              List<String> codes = new ArrayList<String>();
              codes.add(Status.STATUS_SYNTAX_ERROR);
              Status status = new Status(codes, e.getMessage());
              return new EvaluationResult(status);
            }
          }
        }

      case ID_XPATH_NODE_EQUAL:
        {
          // TODO
        }
    }

    List<String> codes = new ArrayList<String>();
    codes.add(Status.STATUS_SYNTAX_ERROR);
    Status status = new Status(codes, "Not supported function");
    return new EvaluationResult(status);
  }
 public NodeList xpathRealQuery(String doc, String query) {
   log.debug("xpathRealQuery(String doc, String query)");
   XPathFactory xpathFact = XPathFactory.newInstance();
   XPath xpath = xpathFact.newXPath();
   XPathExpression expr = null;
   NodeList res = null;
   try {
     expr = xpath.compile(query);
     Object result = expr.evaluate(doc, XPathConstants.STRING);
     res = (NodeList) result;
   } catch (XPathExpressionException e) {
     log.debug(e.getMessage());
     return null;
   }
   return res;
 }
  public boolean xpathQuery(Document doc, String query) {
    log.debug("xpathQuery(Document doc, String query)");
    log.debug("path: " + query);

    XPathFactory xpathFact = XPathFactory.newInstance();
    XPath xpath = xpathFact.newXPath();
    boolean ret = false;
    try {
      ret = (xpath.evaluate(query, doc) == null) ? false : true;
    } catch (XPathExpressionException e) {
      log.debug("exception");
      log.debug(e.getMessage());
      return false;
    }

    if (ret == false) {
      log.debug("ret is false");
      log.debug(BioFuzzUtils.domToString(doc));
    }

    return ret;
  }