// Class
String className = XPathHelper.valueOf(node, "m:class", xpath);
// Create the pagelet renderer
PageletRenderer renderer = null;
if (className != null) {
Class<? extends PageletRenderer> c = null;
try {
c = (Class<? extends PageletRenderer>) classLoader.loadClass(className);
renderer = c.newInstance();
renderer.setIdentifier(id);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Implementation " + className + " for pagelet renderer '" + id + "' not found", e);
} catch (InstantiationException e) {
throw new IllegalStateException("Error instantiating impelementation " + className + " for pagelet renderer '" + id + "'", e);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Access violation instantiating implementation " + className + " for pagelet renderer '" + id + "'", e);
} catch (Throwable t) {
throw new IllegalStateException("Error loading implementation " + className + " for pagelet renderer '" + id + "'", t);
}
} else {
renderer = new PageletRendererImpl();
renderer.setIdentifier(id);
}
// Renderer url
NodeList rendererUrlNodes = XPathHelper.selectList(node, "m:renderer", xpath);
if (rendererUrlNodes.getLength() == 0)
throw new IllegalStateException("Missing renderer in page template definition");
for (int i = 0; i < rendererUrlNodes.getLength(); i++) {
Node rendererUrlNode = rendererUrlNodes.item(i);
URL rendererUrl = null;
Node typeNode = rendererUrlNode.getAttributes().getNamedItem("type");
String type = (typeNode != null) ? typeNode.getNodeValue() : RendererType.Page.toString();
try {
rendererUrl = new URL(rendererUrlNode.getFirstChild().getNodeValue());
renderer.addRenderer(rendererUrl, type);
} catch (MalformedURLException e) {
throw new IllegalStateException("Malformed renderer url in page template definition: " + rendererUrlNode);
}
}
// Composeable
renderer.setComposeable("true".equals(XPathHelper.valueOf(node, "@composeable", xpath)));
// Preview mode
String previewMode = XPathHelper.valueOf(node, "m:preview", xpath);
if (previewMode != null)
renderer.setPreviewMode(PagePreviewMode.parse(previewMode));
// Editor url
String editorUrlNode = XPathHelper.valueOf(node, "m:editor", xpath);
try {
if (editorUrlNode != null) {
URL editorUrl = new URL(editorUrlNode);
renderer.setEditor(editorUrl);
}
} catch (MalformedURLException e) {
throw new IllegalStateException("Malformed editor url in page template definition: " + editorUrlNode);
}
// client revalidation time
String recheck = XPathHelper.valueOf(node, "m:recheck", xpath);
if (recheck != null) {
try {
renderer.setClientRevalidationTime(ConfigurationUtils.parseDuration(recheck));
} catch (NumberFormatException e) {
throw new IllegalStateException("The pagelet renderer revalidation time is malformed: '" + recheck + "'");
} catch (IllegalArgumentException e) {
throw new IllegalStateException("The pagelet renderer revalidation time is malformed: '" + recheck + "'");
}
}
// cache expiration time
String valid = XPathHelper.valueOf(node, "m:valid", xpath);
if (valid != null) {
try {
renderer.setCacheExpirationTime(ConfigurationUtils.parseDuration(valid));
} catch (NumberFormatException e) {
throw new IllegalStateException("The pagelet renderer valid time is malformed: '" + valid + "'", e);
} catch (IllegalArgumentException e) {
throw new IllegalStateException("The pagelet renderer valid time is malformed: '" + valid + "'", e);
}
}
// name
String name = XPathHelper.valueOf(node, "m:name", xpath);
renderer.setName(name);
// scripts
NodeList scripts = XPathHelper.selectList(node, "m:includes/m:script", xpath);
for (int i = 0; i < scripts.getLength(); i++) {
renderer.addHTMLHeader(ScriptImpl.fromXml(scripts.item(i)));
}
// links
NodeList includes = XPathHelper.selectList(node, "m:includes/m:link", xpath);
for (int i = 0; i < includes.getLength(); i++) {
renderer.addHTMLHeader(LinkImpl.fromXml(includes.item(i)));
}
return renderer;
}