Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2011-2014 the original author or authors.
*/
package com.jetdrone.vertx.yoke.engine;
import com.jetdrone.vertx.yoke.core.YokeAsyncResult;
import org.vertx.java.core.AsyncResult;
import org.vertx.java.core.AsyncResultHandler;
import org.vertx.java.core.Handler;
import org.vertx.java.core.buffer.Buffer;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* # StringPlaceholderEngine
*/
public class StringPlaceholderEngine extends AbstractEngine {
private static final String placeholderPrefix = "${";
private static final String placeholderSuffix = "}";
private static final boolean ignoreUnresolvablePlaceholders = true;
private static final String funcName = "([a-zA-Z0-9]+)";
private static final String arguments = "\\((.*)\\)";
private static final Pattern FUNCTION = Pattern.compile(funcName + "\\s*" + arguments);
private static final String argument = "(.*?)";
private static final String quote = "\'";
private static final String sep = "(,\\s*)?";
private static final Pattern ARG = Pattern.compile(quote + argument + quote + sep);
private final String extension;
private final String prefix;
public StringPlaceholderEngine(final String views) {
this(views, ".shtml");
}
public StringPlaceholderEngine(final String views, final String extension) {
this.extension = extension;
if ("".equals(views)) {
prefix = views;
} else {
prefix = views.endsWith("/") ? views : views + "/";
}
}
@Override
public String extension() {
return extension;
}
/**
* An interpreter for strings with named placeholders.
*
* For example given the string "hello ${myName}" and the map
* Map<String, Object> map = new HashMap<>();
* map.put("myName", "world");
*
*
* the call returns "hello world"
*
* It replaces every occurrence of a named placeholder with its given value
* in the map. If there is a named place holder which is not found in the
* map then the string will retain that placeholder. Likewise, if there is
* an entry in the map that does not have its respective placeholder, it is
* ignored.
*/
@Override
public void render(final String file, final Map context, final Handler> handler) {
// verify if the file is still fresh in the cache
read(prefix + file, new AsyncResultHandler() {
@Override
public void handle(AsyncResult asyncResult) {
if (asyncResult.failed()) {
handler.handle(new YokeAsyncResult(asyncResult.cause()));
} else {
try {
handler.handle(new YokeAsyncResult<>(parseStringValue(asyncResult.result(), context, new HashSet())));
} catch (IllegalArgumentException iae) {
handler.handle(new YokeAsyncResult(iae));
}
}
}
});
}
private Buffer parseStringValue(String template, Map context, Set visitedPlaceholders) {
StringBuilder buf = new StringBuilder(template);
int startIndex = template.indexOf(placeholderPrefix);
while (startIndex != -1) {
int endIndex = findPlaceholderEndIndex(buf, startIndex);
if (endIndex != -1) {
String placeholder = buf.substring(startIndex + placeholderPrefix.length(), endIndex);
if (!visitedPlaceholders.add(placeholder)) {
throw new IllegalArgumentException(
"Circular placeholder reference '" + placeholder + "' in property definitions");
}
// Recursive invocation, parsing placeholders contained in the placeholder key.
placeholder = parseStringValue(placeholder, context, visitedPlaceholders).toString(contentEncoding());
// Now obtain the value for the fully resolved key...
Object propVal;
boolean isFn = false;
Matcher fn = FUNCTION.matcher(placeholder);
if (fn.find()) {
// function syntax used, get the object from context with the proper name
propVal = context.get(fn.group(1));
isFn = true;
} else {
propVal = context.get(placeholder);
}
if (propVal != null) {
// Recursive invocation, parsing placeholders contained in the
// previously resolved placeholder value.
String propValStr;
if (isFn && propVal instanceof Function) {
Matcher arg = ARG.matcher(fn.group(2));
List