String identifier = XPathHelper.valueOf(config, "@id", xpath);
if (identifier == null)
throw new IllegalStateException("Unable to create actions without identifier");
// class
Action action = null;
String className = XPathHelper.valueOf(config, "m:class", xpath);
if (className != null) {
try {
Class<? extends Action> c = (Class<? extends Action>) classLoader.loadClass(className);
action = c.newInstance();
action.setIdentifier(identifier);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Implementation " + className + " for action handler '" + identifier + "' not found", e);
} catch (InstantiationException e) {
throw new IllegalStateException("Error instantiating impelementation " + className + " for action handler '" + identifier + "'", e);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Access violation instantiating implementation " + className + " for action handler '" + identifier + "'", e);
} catch (Throwable t) {
throw new IllegalStateException("Error loading implementation " + className + " for action handler '" + identifier + "'", t);
}
} else {
action = new HTMLActionSupport();
action.setIdentifier(identifier);
}
// mountpoint
String mountpoint = XPathHelper.valueOf(config, "m:mountpoint", xpath);
if (mountpoint == null)
throw new IllegalStateException("Action '" + identifier + " has no mountpoint");
action.setPath(mountpoint);
// TODO: handle /, /*
// content url
String targetUrl = XPathHelper.valueOf(config, "m:page", xpath);
if (StringUtils.isNotBlank(targetUrl)) {
if (!(action instanceof HTMLActionSupport))
throw new IllegalStateException("Target page configuration for '" + action.getIdentifier() + "' requires subclassing HTMLActionSupport");
((HTMLActionSupport) action).setPageURI(targetUrl);
}
// template
String targetTemplate = XPathHelper.valueOf(config, "m:template", xpath);
if (StringUtils.isNotBlank(targetTemplate)) {
if (!(action instanceof HTMLActionSupport))
throw new IllegalStateException("Target template configuration for '" + action.getIdentifier() + "' requires subclassing HTMLActionSupport");
((HTMLActionSupport) action).setDefaultTemplate(targetTemplate);
}
// client revalidation time
String recheck = XPathHelper.valueOf(config, "m:recheck", xpath);
if (recheck != null) {
try {
action.setClientRevalidationTime(ConfigurationUtils.parseDuration(recheck));
} catch (NumberFormatException e) {
throw new IllegalStateException("The action revalidation time is malformed: '" + recheck + "'");
} catch (IllegalArgumentException e) {
throw new IllegalStateException("The action revalidation time is malformed: '" + recheck + "'");
}
}
// cache expiration time
String valid = XPathHelper.valueOf(config, "m:valid", xpath);
if (valid != null) {
try {
action.setCacheExpirationTime(ConfigurationUtils.parseDuration(valid));
} catch (NumberFormatException e) {
throw new IllegalStateException("The action valid time is malformed: '" + valid + "'", e);
} catch (IllegalArgumentException e) {
throw new IllegalStateException("The action valid time is malformed: '" + valid + "'", e);
}
}
// scripts
NodeList scripts = XPathHelper.selectList(config, "m:includes/m:script", xpath);
for (int i = 0; i < scripts.getLength(); i++) {
action.addHTMLHeader(ScriptImpl.fromXml(scripts.item(i)));
}
// links
NodeList includes = XPathHelper.selectList(config, "m:includes/m:link", xpath);
for (int i = 0; i < includes.getLength(); i++) {
action.addHTMLHeader(LinkImpl.fromXml(includes.item(i)));
}
// name
String name = XPathHelper.valueOf(config, "m:name", xpath);
action.setName(name);
// options
Node optionsNode = XPathHelper.select(config, "m:options", xpath);
OptionsHelper.fromXml(optionsNode, action, xpath);