第一步.新建一个模板文件以.ftl结尾。

Max.ftl

/*
* Copyright 2002-2007 Robert Breidecker.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package daicy.formula.freemarker; import daicy.formula.ActiveOperand; /**
* This class is a function that executes within Evaluator. The function returns
* the greater of two Integer values. See the Math.${function_name}(Integer) method in the
* JDK for a complete description of how this function works.
*/
public class ${class_name} implements Function {
/**
*
* @return The name of this function class.
*/
public String getName() {
return "${function_name}";
} @Override
public ActiveOperand execute(ActiveOperand[] arguments)
throws FunctionException {
// TODO Auto-generated method stub
Object result = null; if (null == arguments || arguments.length != 2) {
throw new FunctionException("Two numeric arguments are required.");
} try {
if ((arguments[0].getValue() instanceof Double)) {
Double argumentOne = ((Double) arguments[0].getValue());
Double argumentTwo = ((Double) arguments[1].getValue());
result = new Double(Math.${function_name}(argumentOne, argumentTwo));
} else if ((arguments[0].getValue() instanceof Float)) {
Float argumentOne = ((Float) arguments[0].getValue());
Float argumentTwo = ((Float) arguments[1].getValue());
result = new Float(Math.${function_name}(argumentOne, argumentTwo));
} else if ((arguments[0].getValue() instanceof Integer)) {
Integer argumentOne = ((Integer) arguments[0].getValue());
Integer argumentTwo = ((Integer) arguments[1].getValue());
result = new Integer(Math.${function_name}(argumentOne, argumentTwo));
} else if ((arguments[0].getValue() instanceof Long)) {
Long argumentOne = ((Long) arguments[0].getValue());
Long argumentTwo = ((Long) arguments[1].getValue());
result = new Long(Math.${function_name}(argumentOne, argumentTwo));
} else if ((arguments[0].getValue() instanceof Short)) {
Short argumentOne = ((Short) arguments[0].getValue());
Short argumentTwo = ((Short) arguments[1].getValue());
result = new Integer(Math.${function_name}(argumentOne, argumentTwo));
} } catch (Exception e) {
throw new FunctionException("Two numeric arguments are required.",
e);
} return new ActiveOperand(result.getClass(), result);
}
}

第二步.写一个freemaker的工具类用于生成代码。

FreeMarkerUtil.java

注意:工程必须引入freemaker.jar

package daicy.formula.freemarker;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map; import freemarker.template.Configuration;
import freemarker.template.Template; public class FreeMarkerUtil { private Configuration cfg; public void init() throws Exception {
// 初始化FreeMarker配置
// 创建一个Configuration实例
cfg = new Configuration();
// 设置FreeMarker的模版文件位置
cfg.setDirectoryForTemplateLoading(new File(
"src/daicy/formula/function"));
} public void process(FreeMarkerUtil hf) throws Exception { Map root = new HashMap();
String savePath = "src/daicy/formula/function/"; Template template = cfg.getTemplate("Max.ftl"); String[] functionNames = new String[] { "Min" }; for (int i = 0; i < functionNames.length; i++) {
String class_name = functionNames[i];
String function_name = functionNames[i].toLowerCase(); root.put("class_name", class_name);
root.put("function_name", function_name);
String fileName = class_name + ".java"; hf.buildTemplate(root, savePath, fileName, template);
} } public void buildTemplate(Map root, String savePath, String fileName,
Template template) {
String realFileName = savePath + fileName; String realSavePath = savePath; File newsDir = new File(realSavePath);
if (!newsDir.exists()) {
newsDir.mkdirs();
} try {
// SYSTEM_ENCODING = "UTF-8";
Writer out = new OutputStreamWriter(new FileOutputStream(
realFileName)); template.process(root, out);
} catch (Exception e) {
e.printStackTrace();
} } public static void main(String[] args) throws Exception {
FreeMarkerUtil hf = new FreeMarkerUtil();
hf.init();
hf.process(hf);
} }

基于antlr的表达式解析器——函数生成(通过freemarker)的更多相关文章

  1. java字符串应用之表达式解析器

    一.表达式的组成    1.数字    2.运算符:+ - / * ^ % =    3.圆括号    4.变量二.运算符优先级    由高到低分别为:+-(正负号).^.*/%.+-.=    优先 ...

  2. 基于Jquery的XML解析器,返回定制的HTML

    依据HTML模板返回解析的XML 依赖jQuery 1.4​1. [代码]基于Jquery的xml解析器并返回定制的HTML     /** *  jQuery插件 *  Author: pureco ...

  3. OO第四单元——基于UML的UML解析器总结&OO课程总结

    OO第四单元--基于UML的UML解析器总结&OO课程总结 前言:一学期愉快(痛苦)的OO课程学习结束了,OO几个单元作业都各有特色,实验也各有特色,仔细回味起来,不再是单纯的敲代码(但自己还 ...

  4. OSS.Core基于Dapper封装(表达式解析+Emit)仓储层的构思及实现

    最近趁着不忙,在构思一个搭建一个开源的完整项目,至于原因以及整个项目框架后边文章我再说明.既然要起一个完整的项目,那么数据仓储访问就必不可少,这篇文章我主要介绍这个新项目(OSS.Core)中我对仓储 ...

  5. [LeetCode] Ternary Expression Parser 三元表达式解析器

    Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...

  6. C 四则运算表达式解析器

    下载实例:http://www.wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=1074 程序主要包括:基础结构定义.词法分析.语法分 ...

  7. dom4j解析器 基于dom4j的xpath技术 简单工厂设计模式 分层结构设计思想 SAX解析器 DOM编程

    *1 dom4j解析器   1)CRUD的含义:CreateReadUpdateDelete增删查改   2)XML解析器有二类,分别是DOM和SAX(simple Api for xml).     ...

  8. 基于Java的简易表达式解析工具(一)

    最近需要用到相关表达式解析的工具,然后去网上搜索,找到了一个用C#写的表达式解析工具,仔细看了功能后发现,这正是我需要的,如果我能将它改造成基于Java语言的方式,岂不是更好吗,所以花了一段时间,把网 ...

  9. atitit.java解析sql语言解析器解释器的实现

    atitit.java解析sql语言解析器解释器的实现 1. 解析sql的本质:实现一个4gl dsl编程语言的编译器 1 2. 解析sql的主要的流程,词法分析,而后进行语法分析,语义分析,构建sq ...

  10. 在.NET Core中使用Irony实现自己的查询语言语法解析器

    在之前<在ASP.NET Core中使用Apworks快速开发数据服务>一文的评论部分,.NET大神张善友为我提了个建议,可以使用Compile As a Service的Roslyn为语 ...

随机推荐

  1. maven 网关应用:[NACOS ConnectException httpPost] currentServerAddr: http://localhost:8848,

    网关应用运行忽然报错:[NACOS ConnectException httpPost] currentServerAddr: http://localhost:8848, 虽然调整了代码逻辑,但是n ...

  2. SQL Server Temporary Table & Table Variable (临时表和表变量)

    参考: 在数据库中临时表什么时候会被清除呢 Temporary Tables And Table Variables In SQL 基本常识 1. 局部临时表(#开头)只对当前连接有效,当前连接断开时 ...

  3. MybatisPlus——入门案例

    MyBatisPlus MyBatisPlus(简称MP)是基于MyBatis框架基础上开发的增强型工具,旨在简化开发.提高效率 开发方式 基于MyBatis使用MyBatisPlus 基于Sprin ...

  4. 三大硬核方式揭秘:Java如何与底层硬件和工业设备轻松通信!

    大家好,我是V哥,程序员聊天真是三句不到离不开技术啊,这不前两天跟一个哥们吃饭,他是我好多年前的学员了,一直保持着联系,现在都李总了,在做工业互联网相关的项目,真是只要 Java 学得好,能干一辈子, ...

  5. 新手指南-新人入职-maven相关

    一.前言 入职后,发现公司是用Maven对项目进行管理和构建. 一般来说,自己先确定以下几点: 1.公司对版本是否有要求. 2.是否要求IDEA对maven有特殊的配置. 3.确定自己的 MAVEN_ ...

  6. USB协议详解第8讲(USB描述符-字符串和语言ID描述符)

    1.字符串描述符相关概念 字符串描述符:首先,字符串描述符就是用字符串描述一个设备的一些属性,毕竟人能看懂的是字符,而不是十六进制,描述的属性包括设备厂商名字.产品名字.产品序列号.各个配置名字.各个 ...

  7. qemu的使用

    一.QEMU的运行模式 直接摘抄自己<揭秘家用路由器0day漏洞挖掘技术>,网上查了一下也没有找到令人满意的QEMU的使用说明,就采用这本书上的介绍.如果后期能够找到比较满意的QEMU的使 ...

  8. 4.3.2 等比数列的前n项和公式

    \({\color{Red}{欢迎到学科网下载资料学习 }}\) [ [基础过关系列]高二数学同步精品讲义与分层练习(人教A版2019)] ( https://www.zxxk.com/docpack ...

  9. 关于softmax在CV多通道中的理解

    1.采用分类任务时,我们通常会采用逻辑回归算法,最关键的步骤就是将线性模型输出的实数域映射到[0, 1]表示概率分布的有效实数空间,其中Sigmoid函数刚好具有这样的功能.但是这通常只适用于二分类问 ...

  10. filter 加 indexOf 方法去重数组

    let arr = [1, 2, 3, 4, 3, 2, 3, 4, 6, 7, 6] let unique = (arr) => { console.log(arr) return arr.f ...