All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.sling.scripting.jst.HtmlContentRenderer Maven / Gradle / Ivy

package org.apache.sling.scripting.jst;

import java.io.PrintWriter;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Value;

import org.apache.sling.api.resource.Resource;

public class HtmlContentRenderer {

	// TODO make this configurable
	private static final String BREADCRUMB_PREFIX = "/content/";

	public void render(PrintWriter pw, Resource r, Node n, String title)
	throws RepositoryException {
		pw.print("

"); pw.print(escape(title)); pw.println("

"); renderBreadcrumbs(pw, n); renderChildNodes(pw, n); for (PropertyIterator pi = n.getProperties(); pi.hasNext();) { final Property p = pi.nextProperty(); if(displayProperty(p.getName())) { renderPropertyValue(pw, p); } } } protected void renderBreadcrumbs(PrintWriter pw, Node n) throws RepositoryException { final String path = n.getPath(); pw.println(""); } protected String getDotDots(int start, int len) { final StringBuffer sb = new StringBuffer(); for(int i=start; i < len - 1; i++) { sb.append("../"); } return sb.toString(); } protected void renderChildNodes(PrintWriter pw, Node parent) throws RepositoryException { pw.println("
"); final String prefix = parent.getName() + "/"; final NodeIterator it = parent.getNodes(); while(it.hasNext()) { final Node kid = it.nextNode(); pw.print(""); pw.print(kid.getName()); pw.println(""); } pw.println("
"); } protected void renderPropertyValue(PrintWriter pw, Property p) throws RepositoryException { pw.print("
"); if (p.getDefinition().isMultiple()) { Value[] values = p.getValues(); pw.print('['); for (int i = 0; i < values.length; i++) { if (i > 0) { pw.print(", "); } pw.print(escape(values[i].getString())); } pw.print(']'); } else { pw.print(escape(p.getValue().getString())); } pw.println("
"); } protected String escape(String str) { final StringBuffer sb = new StringBuffer(); for(int i = 0; i < str.length(); i++) { final char c = str.charAt(i); if(c == '<') { sb.append("<"); } else if(c == '>') { sb.append(">"); } else if(c == '&') { sb.append("&"); } else { sb.append(c); } } return sb.toString(); } protected boolean displayProperty(String name) { return !name.startsWith("jcr:") && !name.startsWith("sling:"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy