Skip to content

Instantly share code, notes, and snippets.

@saml
Created October 5, 2012 17:34
Show Gist options
  • Save saml/3841196 to your computer and use it in GitHub Desktop.
Save saml/3841196 to your computer and use it in GitHub Desktop.
<%--
resource type query
--%><%@page
import="org.apache.jackrabbit.util.ISO9075,
org.apache.jackrabbit.util.Text,
javax.jcr.query.*,
org.apache.sling.api.resource.*,
java.io.*"
%><%@include file="/libs/foundation/global.jsp"%><%!
static final String PROP_SUPER_TYPE = "sling:resourceSuperType";
static String normalizeResourceType(String resourceType) {
if (resourceType.startsWith("/")) {
return resourceType.substring(6);//strip out /apps/ or /libs/
}
return resourceType;
}
static NodeIterator queryChildren(String basePath, String resourceType, QueryManager queryManager) throws RepositoryException {
final String xpath = String.format(
"/jcr:root%s//*[jcr:contains(@%s, '%s')]",
ISO9075.encodePath(basePath),
PROP_SUPER_TYPE,
Text.escapeIllegalXpathSearchChars(resourceType).replaceAll("'", "''"));
final Query query = queryManager.createQuery(xpath, Query.XPATH);
return query.execute().getNodes();
}
static void printChildren(String basePath, String resourceType, QueryManager queryManager, JspWriter out) throws RepositoryException, IOException {
out.append("<ul>");
final NodeIterator iter = queryChildren(basePath, resourceType, queryManager);
while (iter.hasNext()) {
final Node node = iter.nextNode();
final String path = node.getPath();
out.append(String.format("<li><a href=\"%s/\">%s</a>", path, path));
if (node.hasProperty(PROP_SUPER_TYPE)) {
final String superType = normalizeResourceType(node.getPath());
out.append("<!-- " + superType + " " + resourceType + " -->\n");
if (!resourceType.equals(superType)) {
printChildren(basePath, superType, queryManager, out);
}
}
out.append("</li>");
}
out.append("</ul>");
}
%><%
String basePath = slingRequest.getParameter("b");
if (basePath == null || "".equals(basePath)) {
basePath = "/apps";
}
String resourceType = slingRequest.getParameter("t");
if (resourceType == null || "".equals(resourceType)) {
resourceType = "foundation/components/parbase";
}
resourceType = normalizeResourceType(resourceType);
final QueryManager queryManager = resourceResolver.adaptTo(Session.class).getWorkspace().getQueryManager();
%><!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Resource Types</title>
</head>
<body>
<div id="content">
<h1>hierarcy of <%= resourceType %></h1>
<h2>accepts these params: <a href="?t=<%= resourceType %>&b=<%= basePath %>">?t=<%= resourceType %>&b=<%= basePath %></a></h2>
<ul>
<li><%= resourceType %></li>
<% printChildren(basePath, resourceType, queryManager, out); %>
</ul>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment