PathExpression extends ContainerExpression implements Cloneable { @Override public IJsonNode evaluate(final IJsonNode node, final IJsonNode target, final EvaluationContext context) { IJsonNode fragmentNode = node; for (int index = 0; index < this.fragments.size(); index++) this.fragmentTargets.set(index, fragmentNode = this.fragments.get(index).evaluate(fragmentNode, this.fragmentTargets.get(index), context)); return fragmentNode; } PathExpression(final EvaluationExpression... fragments);  PathExpression(final List<? extends EvaluationExpression> fragments); private  PathExpression(final LinkedList<EvaluationExpression> fragments); void add(final EvaluationExpression fragment); @Override PathExpression clone(); @Override boolean equals(final Object obj); @Override IJsonNode evaluate(final IJsonNode node, final IJsonNode target, final EvaluationContext context); int getDepth(); EvaluationExpression getFragment(final int index); EvaluationExpression getLastFragment(); List<EvaluationExpression> getFragments(); @Override int hashCode(); boolean isPrefix(final PathExpression prefix); @Override List<EvaluationExpression> getChildren(); @Override void setChildren(final List<? extends EvaluationExpression> children); @Override void toString(final StringBuilder builder); static PathExpression ensurePathExpression(final EvaluationExpression expression); static EvaluationExpression wrapIfNecessary(final List<EvaluationExpression> expressions); static EvaluationExpression wrapIfNecessary(final EvaluationExpression... expressions); @Override IJsonNode set(final IJsonNode node, final IJsonNode value, final EvaluationContext context); void removeLast(); void add(final int index, final EvaluationExpression fragment); PathExpression subPath(int start, int end);  }
@Test public void shouldReturnElementOfGivenPath() { final IJsonNode result = new PathExpression( new ObjectAccess("glossary"), new ObjectAccess("GlossDiv"), new ObjectAccess("GlossList"), new ObjectAccess("GlossEntry"), new ObjectAccess("GlossDef"), new ObjectAccess("GlossSeeAlso"), new ArrayAccess(1)).evaluate(doc, null, this.context); Assert.assertEquals(TextNode.valueOf("XML"), result); }
@Test public void shouldReplaceRightFragment() { final PathExpression expr = new PathExpression(new ObjectAccess("glossary"), new ObjectAccess("GlossDiv"), new ObjectAccess("GlossList"), new ObjectAccess("GlossEntry"), new ObjectAccess("ID")); expr.replace(new ObjectAccess("ID"), new ObjectAccess("GlossSee")); final IJsonNode result = expr.evaluate(doc, null, this.context); Assert.assertEquals(TextNode.valueOf("markup"), result); }
@Test public void shouldReplaceWholePath() { EvaluationExpression expr = new PathExpression(new ObjectAccess("glossary"), new ObjectAccess("GlossDiv"), new ObjectAccess("GlossList"), new ObjectAccess("GlossEntry"), new ObjectAccess("ID")); expr = expr.replace(new PathExpression(new ObjectAccess("glossary"), new ObjectAccess("GlossDiv"), new ObjectAccess("GlossList"), new ObjectAccess("GlossEntry"), new ObjectAccess("ID")), new PathExpression(new ObjectAccess("glossary"), new ObjectAccess("title"))); final IJsonNode result = expr.evaluate(doc, null, this.context); Assert.assertEquals(TextNode.valueOf("example glossary"), result); }