use of org.apache.velocity.tools.generic.NumberTool in project lucene-solr by apache.
the class VelocityResponseWriter method createContext.
private VelocityContext createContext(SolrQueryRequest request, SolrQueryResponse response) {
VelocityContext context = new VelocityContext();
// Register useful Velocity "tools"
String locale = request.getParams().get(LOCALE);
Map toolConfig = new HashMap();
toolConfig.put("locale", locale);
// TODO: add test; TODO: should this be overridable with a custom "log" named tool?
context.put("log", log);
context.put("esc", new EscapeTool());
context.put("date", new ComparisonDateTool());
context.put("list", new ListTool());
context.put(SORT, new SortTool());
MathTool mathTool = new MathTool();
mathTool.configure(toolConfig);
context.put("math", mathTool);
NumberTool numberTool = new NumberTool();
numberTool.configure(toolConfig);
context.put("number", numberTool);
DisplayTool displayTool = new DisplayTool();
displayTool.configure(toolConfig);
context.put("display", displayTool);
ResourceTool resourceTool = new SolrVelocityResourceTool(request.getCore().getSolrConfig().getResourceLoader().getClassLoader());
resourceTool.configure(toolConfig);
context.put("resource", resourceTool);
// Custom tools can override any of the built-in tools provided above, by registering one with the same name
for (String name : customTools.keySet()) {
Object customTool = SolrCore.createInstance(customTools.get(name), Object.class, "VrW custom tool: " + name, request.getCore(), request.getCore().getResourceLoader());
if (customTool instanceof LocaleConfig) {
((LocaleConfig) customTool).configure(toolConfig);
}
context.put(name, customTool);
}
// custom tools _cannot_ override context objects added below, like $request and $response
// TODO: at least log a warning when one of the *fixed* tools classes in name with a custom one, currently silently ignored
// Turn the SolrQueryResponse into a SolrResponse.
// QueryResponse has lots of conveniences suitable for a view
// Problem is, which SolrResponse class to use?
// One patch to SOLR-620 solved this by passing in a class name as
// as a parameter and using reflection and Solr's class loader to
// create a new instance. But for now the implementation simply
// uses QueryResponse, and if it chokes in a known way, fall back
// to bare bones SolrResponseBase.
// Can this writer know what the handler class is? With echoHandler=true it can get its string name at least
SolrResponse rsp = new QueryResponse();
NamedList<Object> parsedResponse = BinaryResponseWriter.getParsedResponse(request, response);
try {
rsp.setResponse(parsedResponse);
// page only injected if QueryResponse works
// page tool only makes sense for a SearchHandler request
context.put("page", new PageTool(request, response));
context.put("debug", ((QueryResponse) rsp).getDebugMap());
} catch (ClassCastException e) {
// known edge case where QueryResponse's extraction assumes "response" is a SolrDocumentList
// (AnalysisRequestHandler emits a "response")
rsp = new SolrResponseBase();
rsp.setResponse(parsedResponse);
}
context.put("request", request);
context.put("response", rsp);
return context;
}
use of org.apache.velocity.tools.generic.NumberTool in project cachecloud by sohutv.
the class VelocityUtils method createText.
/**
* 邮件模板
*
* @param appDesc 应用信息
* @param appAudit 处理信息
* @param templatePath 模板路径
* @param customCharset 编码
*/
public static synchronized String createText(VelocityEngine engine, AppDesc appDesc, AppAudit appAudit, AppDailyData appDailyData, List<InstanceAlertValueResult> instanceAlertValueResultList, String templatePath, String customCharset) {
if (!StringUtils.isEmpty(customCharset)) {
charset = customCharset;
}
Properties p = new Properties();
p.setProperty("file.resource.loader.path", Thread.currentThread().getContextClassLoader().getResource("").getPath());
p.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
Velocity.init(p);
logger.info("velocity: init done.");
VelocityContext context = new VelocityContext();
context.put("appDesc", appDesc);
context.put("appAudit", appAudit);
context.put("appDailyData", appDailyData);
context.put("instanceAlertValueResultList", instanceAlertValueResultList);
context.put("numberTool", new NumberTool());
context.put("ccDomain", ConstUtils.CC_DOMAIN);
context.put("decimalFormat", new DecimalFormat("###,###"));
context.put("StringUtils", StringUtils.class);
FileOutputStream fos = null;
StringWriter writer = null;
try {
Template template = engine.getTemplate(templatePath);
writer = new StringWriter();
template.merge(context, writer);
} catch (ResourceNotFoundException ex) {
logger.error("error: velocity vm resource not found.", ex);
} catch (ParseErrorException ex) {
logger.error("error: velocity parse vm file error.", ex);
} catch (MethodInvocationException ex) {
logger.error("error: velocity template merge.", ex);
} catch (Exception ex) {
logger.error("error", ex);
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
logger.error("error: close writer", e);
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
logger.error("error: close output stream.", e);
}
}
logger.info("velocity: create text done.");
if (writer != null) {
return writer.toString();
}
return null;
}
Aggregations