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.
package com.sampullara.mustache;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Stack;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.sampullara.util.FutureWriter;
import com.sampullara.util.TemplateFunction;
import org.codehaus.jackson.JsonNode;
import static com.sampullara.mustache.Scope.EMPTY;
import static com.sampullara.mustache.Scope.NULL;
/**
* Base class for Mustaches.
*
* User: sam
* Date: May 3, 2010
* Time: 10:12:47 AM
*/
public class Mustache {
protected static Logger logger = Logger.getLogger(Mustache.class.getName());
protected static final boolean debug = Boolean.getBoolean("mustache.debug");
public static final boolean trace = Boolean.getBoolean("mustache.trace");
public static final boolean profile = Boolean.getBoolean("mustache.profile");
private static final String IMPLICIT_CURRENT_ELEMENT_TOKEN = ".";
// Debug
protected static final ThreadLocal line = new ThreadLocal();
private String name;
private Code[] compiled;
protected MustacheJava mj;
/**
* Given text that was created by or that matches the shape of a mustache
* template, return the scope that can be used to recreate the text.
*
* @param text
* @return
* @throws MustacheException
*/
public Scope unexecute(String text) throws MustacheException {
AtomicInteger position = new AtomicInteger(0);
Scope unexecuted = unexecute(text, position);
if (unexecuted == null) {
int min = Math.min(position.get() + 50,
position.get() + Math.max(0, text.length() - position.get()));
throw new MustacheException(
"Failed to match template at " + name + ":" + line.get() + " with text " +
text.substring(position.get(), min));
}
return unexecuted;
}
protected Scope unexecute(String text, AtomicInteger position) throws MustacheException {
Scope current = new Scope();
for (int i = 0; i < compiled.length && current != null; i++) {
if (debug) {
line.set(compiled[i].getLine());
}
Code[] truncate = truncate(compiled, i + 1, new Code[0]);
current = compiled[i].unexecute(current, text, position, truncate);
}
return current;
}
public static Code[] truncate(Code[] codes, int start, Code[] next) {
if (codes.length <= 1) return next;
Code[] truncate = new Code[codes.length - start];
System.arraycopy(codes, start, truncate, 0, truncate.length);
return truncate;
}
/**
* Execute the Mustache using a Map as the backing data and write the result
* to the provided writer.
*
* @param writer
* @param map
* @return
* @throws MustacheException
*/
public final void execute(Writer writer, Map map) throws MustacheException, IOException {
FutureWriter fw = new FutureWriter(writer);
execute(fw, new Scope(map));
fw.flush();
}
/**
* Execute the Mustache using a JsonNode as the backing data and write the
* result to the provided writer.
*
* @param writer
* @param jsonNode
* @return
* @throws MustacheException
*/
public final void execute(Writer writer, JsonNode jsonNode) throws MustacheException, IOException {
FutureWriter fw = new FutureWriter(writer);
execute(fw, new Scope(jsonNode));
fw.flush();
}
/**
* Execute the Mustache using a scope as the backing data and write the
* result to the provided writer.
*
* @param writer a writer to write to
* @param ctx context of the execution
* @throws MustacheException
*/
public final void execute(Writer writer, Scope ctx) throws MustacheException, IOException {
FutureWriter fw = new FutureWriter(writer);
execute(fw, ctx);
fw.flush();
}
/**
* Execute the Mustache using a Map as the backing data.
*
* @param writer
* @param map
* @throws MustacheException
*/
public final void execute(FutureWriter writer, Map map) throws MustacheException {
execute(writer, new Scope(map));
}
/**
* Execute the Mustache using a JsonNode as a the backing data.
*
* @param writer
* @param jsonNode
* @throws MustacheException
*/
public final void execute(FutureWriter writer, JsonNode jsonNode) throws MustacheException {
execute(writer, new Scope(jsonNode));
}
/**
* Execute the Mustache using the provided Scope as the backing data.
*
* @param writer
* @param ctx
* @throws MustacheException
*/
public void execute(FutureWriter writer, Scope ctx) throws MustacheException {
for (Code code : compiled) {
if (debug) {
line.set(code.getLine());
}
code.execute(writer, ctx);
}
}
public void identity(FutureWriter writer) throws MustacheException {
for (Code code : compiled) {
if (debug) {
line.set(code.getLine());
}
code.identity(writer);
}
}
protected ThreadLocal> capturedWriter = new ThreadLocal>() {
@Override
protected Stack initialValue() {
return new Stack();
}
};
protected ThreadLocal> actual = new ThreadLocal>() {
@Override
protected Stack initialValue() {
return new Stack();
}
};
/**
* Enqueues a Mustache into the FutureWriter.
*
* @param writer
* @param m
* @param s
* @throws IOException
*/
protected void enqueue(FutureWriter writer, final Mustache m, final Scope s) throws IOException {
writer = pushWriter(writer);
writer.enqueue(new Callable