文档
承诺可以选择拥有客户资源加载器。 相应的文档链接
截至2015-04-15,已被打破。这个问题已经提交为
逆向工程的情况导致一些来源如:和一些错误报告如:
问题: 自定义ResourceLoader可用的简单示例在哪里?
要实施的最低要求是什么 - 特别是如果资源不从文件系统加载但来自其他来源,例如通过RESTFul网络电话?
下面是一些示例的代码片段 - 这是一个有效的例子吗? codesnippets假设自定义模板使用由前缀“custom”表示的“名称空间”。所以@ custom.test会尝试通过这个自定义ResourceLoader加载模板“test”。为简单起见,简单地忽略了Rythmsuffix,代码类型和内容。
CustomTemplateResourceLoader:
package org.rythm.custom;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.rythmengine.RythmEngine;
import org.rythmengine.extension.ICodeType;
import org.rythmengine.extension.ITemplateResourceLoader;
import org.rythmengine.internal.compiler.TemplateClass;
import org.rythmengine.resource.ITemplateResource;
import org.rythmengine.resource.TemplateResourceManager;
import org.rythmengine.template.ITemplate;
/**
* a custom TemplateResourceLoader for the Rythm template engine
*/
public class CustomResourceLoader implements ITemplateResourceLoader {
protected static Logger LOGGER = Logger.getLogger("org.rythm.custom");
private RythmEngine engine;
private Map<String,CustomTemplateResource> resources=new LinkedHashMap<String,CustomTemplateResource>();
@Override
public String getResourceLoaderRoot() {
LOGGER.log(Level.WARNING, "getResourceLoaderRoot not implemented");
String result = "";
return result;
}
/**
* get the resource for the given path
* @param path
* @return
*/
public CustomTemplateResource getResource(String path) {
if (resources.containsKey(path)) {
return resources.get(path);
}
if (path.startsWith("custom.")) {
try {
CustomTemplateResource template = new CustomTemplateResource(this,path);
resources.put(path,template);
return template;
} catch (Exception e) {
LOGGER.log(Level.SEVERE,e.getMessage());
}
}
return null;
}
@Override
public ITemplateResource load(String path) {
LOGGER.log(Level.WARNING, "load '" + path + "' not implemented");
return getResource(path);
}
@Override
public TemplateClass tryLoadTemplate(String tmplName, RythmEngine engine,
TemplateClass callerClass, ICodeType codeType) {
if (engine.templateRegistered(tmplName))
return null;
if (!tmplName.startsWith("custom."))
return null;
String rythmSuffix = engine.conf().resourceNameSuffix();
String key = tmplName;
TemplateClass tc = engine.classes().getByTemplate(key);
if (tc == null) {
try {
CustomTemplateResource customTemplate =getResource(key);
tc = new TemplateClass(customTemplate, engine);
ITemplate template = tc.asTemplate(engine);
if (null != template) {
engine.registerTemplate(key, template);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE,e.getMessage()):
}
}
return tc;
}
@Override
public void scan(TemplateResourceManager manager) {
LOGGER.log(Level.WARN,"scan not implemented");
}
@Override
public void setEngine(RythmEngine engine) {
this.engine = engine;
}
@Override
public RythmEngine getEngine() {
return this.engine;
}
}
CustomTemplateResourceLoader:
package org.rythm.custom;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.rythmengine.resource.ITemplateResource;
import org.rythmengine.resource.TemplateResourceBase;
/**
*
* @author wf
*
*/
public class CustomTemplateResource extends TemplateResourceBase implements ITemplateResource{
/**
*
*/
private static final long serialVersionUID = 67049021377257267L;
protected static Logger LOGGER = Logger.getLogger("org.rythm.custom");
String key;
/**
*
* @param customTemplateResourceLoader
* @param tmplName
* @throws Exception
*/
public CustomTemplateResource(CustomTemplateResourceLoader wikiTemplateResourceLoader, String tmplName) throws Exception {
super(customTemplateResourceLoader);
key=tmplName;
}
@Override
public Object getKey() {
return key;
}
@Override
public boolean isValid() {
return true;
}
@Override
protected long defCheckInterval() {
// 5 minutes?
long result=1000 * 300;
return result;
}
@Override
protected long lastModified() {
return 0;
}
@Override
protected String reload() {
String pageContent=null;
... get the pageContent with the key here
return pageContent;
}
}