tag | 198190317321276dfa9434e5c5241dafe0b2558a | |
---|---|---|
tagger | tomekr <tomekr@unknown> | Wed Mar 04 13:18:36 2015 +0000 |
object | 50129fe735854f69df850f7565c9ad4b6b383d1a |
org.apache.sling.query-3.0.0
commit | 50129fe735854f69df850f7565c9ad4b6b383d1a | [log] [tgz] |
---|---|---|
author | tomekr <tomekr@unknown> | Wed Mar 04 13:18:36 2015 +0000 |
committer | tomekr <tomekr@unknown> | Wed Mar 04 13:18:36 2015 +0000 |
tree | ac3b79bed698679c36fcbbbd2076e15dcaaa1ef0 | |
parent | 77b59496def10bdbd0fae69aecb6920b7316dc15 [diff] |
[maven-release-plugin] copy for tag org.apache.sling.query-3.0.0 git-svn-id: https://svn.apache.org/repos/asf/sling/tags/org.apache.sling.query-3.0.0@1663985 13f79535-47bb-0310-9956-ffa450edef68
SlingQuery is a Sling resource tree traversal tool inspired by the jQuery.
Recommended way to find resources in the Sling repository is using tree-traversal methods, like listChildren()
and getParent()
rather than JCR queries. The latter are great for listing resources with given properties, but we can't leverage the repository tree structure with such queries. On the other hand, using tree-traversal method is quite verbose. Consider following code that takes an resource and returns its first ancestor, being cq:Page
, with given jcr:content/cq:template
attribute:
Resource resource = ...; while ((resource = resource.getParent()) != null) { if (!resource.isResourceType("cq:Page")) { continue; } Resource template = resource.getChild("jcr:content/cq:template"); if (template != null && "my/template".equals(template.adaptTo(String.class))) { break; } } if (resource != null) { // we've found appropriate ancestor }
SlingQuery is a tool that helps creating such queries in a more concise way. Above code could be written as:
import static com.cognifide.sling.query.api.SlingQuery.$; // ... $(resource).closest("cq:Page[jcr:content/cq:template=my/template]")
Dollar sign is a static method that takes the resource array and creates SlingQuery object. The closest()
method returns the first ancestor matching the selector string passed as the argument.
SlingQuery is inspired by the jQuery framework. jQuery is the source of method names, selector string syntax and the dollar sign method used as a collection constructor.
SlingQuery
object is immutable (thread-safe),Add following Maven dependency to your pom.xml
:
<dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.query</artifactId> <version>2.0.0</version> </dependency>