Пример #1
0
  /**
   * Initializes the JXPathContext based on any relevant properties set for the filter.
   *
   * @param context the JXPathContext to initialize
   */
  protected void initialise(JXPathContext context) {
    Map.Entry entry;
    if (namespaces != null) {
      if (logger.isDebugEnabled()) {
        logger.debug("Initializing JXPathContext with namespaces: " + namespaces);
      }

      for (Iterator iterator = namespaces.entrySet().iterator(); iterator.hasNext(); ) {
        entry = (Map.Entry) iterator.next();
        context.registerNamespace(entry.getKey().toString(), entry.getValue().toString());
      }
    }

    if (contextProperties != null) {
      if (logger.isDebugEnabled()) {
        logger.debug("Initializing JXPathContext with properties: " + contextProperties);
      }

      for (Iterator iterator = contextProperties.entrySet().iterator(); iterator.hasNext(); ) {
        entry = (Map.Entry) iterator.next();
        context.setValue(entry.getKey().toString(), entry.getValue());
      }
    }

    if (factory != null) {
      context.setFactory(factory);
    }

    context.setLenient(lenient);
  }
 /**
  * @param c Company to cut salary
  * @param condition condition for cutting salary. The condition works on the Employee attributes
  *     salary, name and address. Use <a href="http://www.w3schools.com/xpath/xpath_operators.asp"
  *     >w3schools-operators</a> and <a href="http://www.w3schools.com/xpath/xpath_examples.asp"
  *     >w3schools-examples</a> as reference. Example: "salary>2000 and name!='Koblenz'"
  * @exception JXPathInvalidSyntaxException if condition is empty or is only whitespace
  */
 @SuppressWarnings("unchecked")
 public static void conditionedCut(Company c, String condition)
     throws JXPathInvalidSyntaxException {
   JXPathContext con = JXPathContext.newContext(c);
   ArrayList<Employee> es = new ArrayList<Employee>();
   es.addAll(con.selectNodes("//employees[" + condition + "]"));
   es.addAll(con.selectNodes("//manager[" + condition + "]"));
   // for (Employee e : es)
   // e.setSalary(e.getSalary() / 2);
   con = JXPathContext.newContext(es);
   for (int i = 1; i <= es.size(); i++) {
     con.setValue("@salary[" + i + "]", (Double) con.getValue("@salary[" + i + "]") / 2);
   }
 }