/**
  * @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);
   }
 }
  /** Tests simple XPath expressions. */
  public void testSimpleXPath() {
    List nodes = context.selectNodes(CHILD_NAME1);
    assertEquals("Incorrect number of results", 2, nodes.size());
    for (Iterator it = nodes.iterator(); it.hasNext(); ) {
      ConfigurationNode node = (ConfigurationNode) it.next();
      assertEquals("Incorrect node name", CHILD_NAME1, node.getName());
      assertEquals("Incorrect parent node", root, node.getParentNode());
    }

    nodes = context.selectNodes("/" + CHILD_NAME1);
    assertEquals("Incorrect number of results", 2, nodes.size());

    nodes = context.selectNodes(CHILD_NAME2 + "/" + CHILD_NAME1 + "/" + CHILD_NAME2);
    assertEquals("Incorrect number of results", 18, nodes.size());
  }
 /** Tests accessing the following sibling axis. */
 public void testFollowingSiblingAxis() {
   List nodes = context.selectNodes("/" + CHILD_NAME1 + "[2]/following-sibling::*");
   assertEquals("Wrong number of following siblings", 1, nodes.size());
   ConfigurationNode node = (ConfigurationNode) nodes.get(0);
   assertEquals("Wrong node type", CHILD_NAME2, node.getName());
   assertEquals("Wrong index", String.valueOf(CHILD_COUNT), node.getValue());
 }
  /** Tests accessing attributes. */
  public void testAttributes() {
    root.addAttribute(new DefaultConfigurationNode("testAttr", "true"));
    assertEquals("Did not find attribute of root node", "true", context.getValue("@testAttr"));
    assertEquals(
        "Incorrect attribute value",
        "1",
        context.getValue("/" + CHILD_NAME2 + "[1]/@" + ATTR_NAME));

    assertTrue(
        "Found elements with name attribute",
        context.selectNodes("//" + CHILD_NAME2 + "[@name]").isEmpty());
    ConfigurationNode node =
        (ConfigurationNode) root.getChild(2).getChild(1).getChildren(CHILD_NAME2).get(1);
    node.addAttribute(new DefaultConfigurationNode("name", "testValue"));
    List nodes = context.selectNodes("//" + CHILD_NAME2 + "[@name]");
    assertEquals("Name attribute not found", 1, nodes.size());
    assertEquals("Wrong node returned", node, nodes.get(0));
  }
 /** Tests accessing the preceding sibling axis. */
 public void testPrecedingSiblingAxis() {
   List nodes = context.selectNodes("/" + CHILD_NAME1 + "[2]/preceding-sibling::*");
   assertEquals("Wrong number of preceding siblings", 3, nodes.size());
   for (int index = 0, value = 3; index < nodes.size(); index++, value--) {
     assertEquals(
         "Wrong node index",
         String.valueOf(value),
         ((ConfigurationNode) nodes.get(index)).getValue());
   }
 }
Пример #6
0
  @SuppressWarnings("unchecked")
  public static synchronized Dir getDir(Dir dir, String code)
      throws ExlpXpathNotFoundException, ExlpXpathNotUniqueException {
    JXPathContext context = JXPathContext.newContext(dir);

    List<Dir> list = (List<Dir>) context.selectNodes("dir[@code='" + code + "']");
    if (list.size() == 0) {
      throw new ExlpXpathNotFoundException("No " + Dir.class.getSimpleName() + " for code=" + code);
    } else if (list.size() > 1) {
      throw new ExlpXpathNotUniqueException(
          "Multiple " + Dir.class.getSimpleName() + "s for code=" + code);
    }
    return list.get(0);
  }
Пример #7
0
  public static synchronized Listing getSeed(Section section, String id)
      throws ExlpXpathNotFoundException, ExlpXpathNotUniqueException {
    JXPathContext context = JXPathContext.newContext(section);

    StringBuffer sb = new StringBuffer();
    sb.append("//*[@id='" + id + "']");

    @SuppressWarnings("unchecked")
    List<Listing> listResult = (List<Listing>) context.selectNodes(sb.toString());
    if (listResult.size() == 0) {
      throw new ExlpXpathNotFoundException("No " + Listing.class.getSimpleName() + " for id=" + id);
    } else if (listResult.size() > 1) {
      throw new ExlpXpathNotUniqueException(
          "Multiple " + Listing.class.getSimpleName() + " for id=" + id);
    }
    return listResult.get(0);
  }
  /** Tests using indices to specify elements. */
  public void testIndices() {
    assertEquals(
        "Incorrect value",
        "1.2.3",
        context.getValue("/" + CHILD_NAME2 + "[1]/" + CHILD_NAME1 + "[1]/" + CHILD_NAME2 + "[2]"));
    assertEquals(
        "Incorrect value of last node",
        String.valueOf(CHILD_COUNT),
        context.getValue(CHILD_NAME2 + "[last()]"));

    List nodes = context.selectNodes("/" + CHILD_NAME1 + "[1]/*");
    assertEquals("Wrong number of children", CHILD_COUNT, nodes.size());
    int index = 1;
    for (Iterator it = nodes.iterator(); it.hasNext(); index++) {
      ConfigurationNode node = (ConfigurationNode) it.next();
      assertEquals("Wrong node value for child " + index, "2." + index, node.getValue());
    }
  }
 /** Tests accessing the parent axis. */
 public void testParentAxis() {
   List nodes = context.selectNodes("/" + CHILD_NAME2 + "/parent::*");
   assertEquals("Wrong number of parent nodes", 1, nodes.size());
 }
 /** Tests accessing a node's text. */
 public void testText() {
   List nodes = context.selectNodes("//" + CHILD_NAME2 + "[text()='1.1.1']");
   assertEquals("Incorrect number of result nodes", 1, nodes.size());
 }