cn.hutool.extra.template.engine.jetbrick.loader.StringResourceLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hutool-all Show documentation
Show all versions of hutool-all Show documentation
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
package cn.hutool.extra.template.engine.jetbrick.loader;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import jetbrick.io.resource.AbstractResource;
import jetbrick.io.resource.Resource;
import jetbrick.io.resource.ResourceNotFoundException;
import jetbrick.template.loader.AbstractResourceLoader;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
/**
* 字符串模板加载器
*
* @author looly
* @since 5.7.21
*/
public class StringResourceLoader extends AbstractResourceLoader {
private Charset charset;
@Override
public Resource load(String name) {
return new StringTemplateResource(name, charset);
}
/**
* 设置编码
* @param charset 编码
*/
public void setCharset(Charset charset){
this.charset = charset;
}
/**
* 字符串模板
*
* @author looly
*/
static class StringTemplateResource extends AbstractResource {
private final String content;
private final Charset charset;
/**
* 构造
* @param content 模板内容
* @param charset 编码
*/
public StringTemplateResource(String content, Charset charset){
this.content = content;
this.charset = charset;
}
@Override
public InputStream openStream() throws ResourceNotFoundException {
return IoUtil.toStream(content, charset);
}
@Override
public URL getURL() {
throw new UnsupportedOperationException();
}
@Override
public boolean exist() {
return StrUtil.isEmpty(content);
}
@Override
public String toString() {
return this.content;
}
@Override
public long lastModified() {
return 1;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy