示例#1
0
  /**
   * Performs an XPATH lookup in an xml document whose filename is specified by the first parameter
   * (assumed to be in the auxiliary subdirectory) The XPATH expression is supplied as the second
   * string parameter The return value is of type string e.g. this is currently used primarily for
   * looking up the Company Prefix Index for encoding a GS1 Company Prefix into a 64-bit EPC tag
   */
  private String xpathlookup(String xml, String expression) {

    try {

      // Parse the XML as a W3C document.
      DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document document =
          builder.parse(new File(xmldir + File.separator + "auxiliary" + File.separator + xml));

      XPath xpath = XPathFactory.newInstance().newXPath();

      String rv = (String) xpath.evaluate(expression, document, XPathConstants.STRING);

      return rv;

    } catch (ParserConfigurationException e) {
      System.err.println("ParserConfigurationException caught...");
      e.printStackTrace();
      return null;
    } catch (XPathExpressionException e) {
      System.err.println("XPathExpressionException caught...");
      e.printStackTrace();
      return null;
    } catch (SAXException e) {
      System.err.println("SAXException caught...");
      e.printStackTrace();
      return null;
    } catch (IOException e) {
      System.err.println("IOException caught...");
      e.printStackTrace();
      return null;
    }
  }
  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;
  }
示例#3
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 double measureMetric(Document document) {
    double count = 0;

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();

    try {
      XPathExpression expression = xpath.compile(XPATH_QUERY);
      count = (Double) expression.evaluate(document, XPathConstants.NUMBER);
    } catch (XPathExpressionException e) {
      e.printStackTrace();
    }

    return count;

    // Para retornar so 0 ou 1 usar o seguinte:
    // return (count > 0.0) ? 1.0 : 0.0;
  }
  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;
  }