本文整理汇总了Java中com.github.mustachejava.MustacheException类的典型用法代码示例。如果您正苦于以下问题:Java MustacheException类的具体用法?Java MustacheException怎么用?Java MustacheException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MustacheException类属于com.github.mustachejava包,在下文中一共展示了MustacheException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extractVariableName
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
/**
* At compile time, this function extracts the name of the variable:
* {{#toJson}}variable_name{{/toJson}}
*/
protected static String extractVariableName(String fn, Mustache mustache, TemplateContext tc) {
Code[] codes = mustache.getCodes();
if (codes == null || codes.length != 1) {
throw new MustacheException("Mustache function [" + fn + "] must contain one and only one identifier");
}
try (StringWriter capture = new StringWriter()) {
// Variable name is in plain text and has type WriteCode
if (codes[0] instanceof WriteCode) {
codes[0].execute(capture, Collections.emptyList());
return capture.toString();
} else {
codes[0].identity(capture);
return capture.toString();
}
} catch (IOException e) {
throw new MustacheException("Exception while parsing mustache function [" + fn + "] at line " + tc.line(), e);
}
}
示例2: run
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
@Override
public Writer run(Writer writer, List<Object> scopes) {
if (getCodes() != null) {
for (Code code : getCodes()) {
try (StringWriter capture = new StringWriter()) {
code.execute(capture, scopes);
String s = capture.toString();
if (s != null) {
encoder.encode(s, writer);
}
} catch (IOException e) {
throw new MustacheException("Exception while parsing mustache function at line " + tc.line(), e);
}
}
}
return writer;
}
示例3: validateMissingValues
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
/**
* Throws a descriptive exception if {@code missingValues} is non-empty. Exposed as a utility function to allow
* custom filtering of missing values before the validation occurs.
*/
public static void validateMissingValues(
String templateName, Map<String, String> values, Collection<MissingValue> missingValues)
throws MustacheException {
if (!missingValues.isEmpty()) {
Map<String, String> orderedValues = new TreeMap<>();
orderedValues.putAll(values);
throw new MustacheException(String.format(
"Missing %d value%s when rendering %s:%n- Missing values: %s%n- Provided values: %s",
missingValues.size(),
missingValues.size() == 1 ? "" : "s",
templateName,
missingValues,
orderedValues));
}
}
示例4: testApplyMissingExceptionValueThrows
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
@Test
public void testApplyMissingExceptionValueThrows() throws IOException {
Map<String, String> env = new TreeMap<>();
env.put("foo", "bar");
env.put("bar", "baz");
env.put("baz", "foo");
try {
TemplateUtils.renderMustacheThrowIfMissing(
"testTemplate",
"hello this is {{a_missing_parameter}},\nand {{another_missing_parameter}}. thanks for reading bye",
env);
Assert.fail("expected exception");
} catch (MustacheException e) {
Assert.assertEquals(String.format(
"Missing 2 values when rendering testTemplate:%n" +
"- Missing values: [[email protected], [email protected]]%n" +
"- Provided values: %s", env), e.getMessage());
}
}
示例5: encode
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
@Override
public void encode(String pValue, Writer pWriter) {
try {
for(int i=0; i < pValue.length(); i++) {
char c = pValue.charAt(i);
if(c == '"') {
pWriter.append("<DQUOTE>");
}
else if(c == '\'') {
pWriter.append("<SQUOTE>");
}
else if(c == '&') {
pWriter.append("<AMP>");
}
else {
pWriter.append(c);
}
}
}
catch (IOException e) {
throw new MustacheException("Failed to encode value " + pValue, e);
}
}
示例6: setup
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
@Setup
public void setup() {
MustacheFactory mustacheFactory = new DefaultMustacheFactory() {
@Override
public void encode(String value, Writer writer) {
// Disable HTML escaping
try {
writer.write(value);
} catch (IOException e) {
throw new MustacheException(e);
}
}
};
template = mustacheFactory.compile("templates/stocks.mustache.html");
}
示例7: getReader
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
/**
* Gets a {@link java.io.Reader} object on the source of the template having the given name. This method is used
* to resolved partials.
*
* @param name the name of the template
* @return a reader to read the template's source.
* @throws com.github.mustachejava.MustacheException if the template cannot be found or read.
*/
@Override
public Reader getReader(String name) {
// On windows the path containing '..' are not stripped from the path, so we ensure they are.
String simplified = name;
if (name.contains("..")) {
simplified = Files.simplifyPath(name);
}
// Take into account absolute path
if (simplified.startsWith("/") && simplified.length() > 1) {
simplified = simplified.substring(1, simplified.length());
}
for (Template t : collector.getTemplates()) {
MustacheTemplate template = (MustacheTemplate) t;
if (template.name().equals(simplified)) {
try {
return IOUtils.toBufferedReader(new StringReader(IOUtils.toString(template.getURL())));
} catch (IOException e) {
throw new MustacheException("Cannot read the template " + name, e);
}
}
}
throw new MustacheException("Template \'" + name + "\' not found");
}
开发者ID:wisdom-framework,项目名称:wisdom-mustache-template-engine,代码行数:33,代码来源:ExtendedMustacheFactory.java
示例8: testSyntaxError
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
@Test(expected = MustacheException.class)
public void testSyntaxError() throws MalformedURLException {
File file = new File("src/test/resources/templates/erroneous/syntax-error.mst");
assertThat(file).isFile();
MustacheTemplate template = new MustacheTemplate(factory, file.toURI().toURL());
assertThat(template.engine()).isEqualTo("mustache");
assertThat(template.getURL()).isEqualTo(file.toURI().toURL());
assertThat(template.mimetype()).isEqualTo(MimeTypes.TEXT);
assertThat(template.name()).isEqualTo("erroneous/syntax-error");
Renderable renderable = template.render(new DefaultController() {
}, ImmutableMap.<String, Object>of("items", Cat.cats()));
fail("Syntax error expected");
}
示例9: getReader_internal
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
private Reader getReader_internal(String name) {
if (skipClasspath) {
File file = superFileRoot == null ? new File(name) : new File(superFileRoot, name);
if (file.exists() && file.isFile()) {
try {
InputStream in = new FileInputStream(file);
return new BufferedReader(new InputStreamReader(in, Charsets.UTF_8));
} catch (IOException e) {
throw new MustacheException("could not open: " + file, e);
}
} else {
throw new MustacheException("not a file: " + file);
}
} else {
return super.getReader(name);
}
}
示例10: encode
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
@Override
public void encode(String value, Writer writer) {
try {
encoder.encode(value, writer);
} catch (IOException e) {
throw new MustacheException("Unable to encode value", e);
}
}
示例11: createFunction
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
protected Function<String, String> createFunction(Object resolved) {
return s -> {
if (resolved == null) {
return null;
}
try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) {
if (resolved == null) {
builder.nullValue();
} else if (resolved instanceof Iterable) {
builder.startArray();
for (Object o : (Iterable) resolved) {
builder.value(o);
}
builder.endArray();
} else if (resolved instanceof Map) {
builder.map((Map<String, ?>) resolved);
} else {
// Do not handle as JSON
return oh.stringify(resolved);
}
return builder.string();
} catch (IOException e) {
throw new MustacheException("Failed to convert object to JSON", e);
}
};
}
示例12: extractDelimiter
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
private static String extractDelimiter(String variable) {
Matcher matcher = PATTERN.matcher(variable);
if (matcher.find()) {
return matcher.group(1);
}
throw new MustacheException("Failed to extract delimiter for join function");
}
示例13: testsUnsupportedTagsToJson
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
public void testsUnsupportedTagsToJson() {
MustacheException e = expectThrows(MustacheException.class, () -> compile("{{#toJson}}{{foo}}{{bar}}{{/toJson}}"));
assertThat(e.getMessage(), containsString("Mustache function [toJson] must contain one and only one identifier"));
e = expectThrows(MustacheException.class, () -> compile("{{#toJson}}{{/toJson}}"));
assertThat(e.getMessage(), containsString("Mustache function [toJson] must contain one and only one identifier"));
}
示例14: testsUnsupportedTagsJoin
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
public void testsUnsupportedTagsJoin() {
MustacheException e = expectThrows(MustacheException.class, () -> compile("{{#join}}{{/join}}"));
assertThat(e.getMessage(), containsString("Mustache function [join] must contain one and only one identifier"));
e = expectThrows(MustacheException.class, () -> compile("{{#join delimiter='a'}}{{/join delimiter='b'}}"));
assertThat(e.getMessage(), containsString("Mismatched start/end tags"));
}
示例15: encode
import com.github.mustachejava.MustacheException; //导入依赖的package包/类
@Override
public void encode(String value, Writer writer) {
try {
writer.write(JsonStringEncoder.getInstance().quoteAsString(value));
} catch (IOException e) {
throw new MustacheException("Failed to encode value: " + value);
}
}