package fmpp.dataloaders;
import java.util.List;
import fmpp.tdd.DataLoader;
public class SqlDataLoader implements DataLoader {
@SuppressWarnings("unchecked")
@Override
public java.lang.Object load(fmpp.Engine arg0, List args)
throws java.lang.Exception {
if (args.size() < 5) {
throw new IllegalArgumentException(
"At least 5 argument (driverClassName, url, username, password, sql) needed.");
}
Object obj = args.get(0);
if (!(obj instanceof String)) {
throw new IllegalArgumentException(
"The 1st argument (driverClassName) must be a string.");
}
String driverClassName = obj.toString();
obj = args.get(1);
if (!(obj instanceof String)) {
throw new IllegalArgumentException(
"The 1st argument (url) must be a string.");
}
String url = obj.toString();
obj = args.get(2);
if (!(obj instanceof String)) {
throw new IllegalArgumentException(
"The 1st argument (username) must be a string.");
}
String username = obj.toString();
obj = args.get(3);
if (!(obj instanceof String)) {
throw new IllegalArgumentException(
"The 1st argument (password) must be a string.");
}
String password = obj.toString();
obj = args.get(4);
if (!(obj instanceof String)) {
throw new IllegalArgumentException(
"The 1st argument (sql) must be a string.");
}
String sql = obj.toString();
SqlTemplateModel model = new SqlTemplateModel();
model.load(driverClassName, url, username, password, sql);
return model;
}
}
package fmpp.dataloaders;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import fmpp.models.TemplateModelArrayCollection;
import fmpp.models.TemplateModelListCollection;
import fmpp.models.TemplateModelListSequence;
import freemarker.template.SimpleDate;
import freemarker.template.SimpleNumber;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateBooleanModel;
import freemarker.template.TemplateCollectionModel;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateHashModelEx;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateSequenceModel;
public class SqlTemplateModel implements TemplateSequenceModel,
TemplateHashModel {
// data
private boolean loaded;
private List<RowHash> rows = new ArrayList<RowHash>();
private Map<String, Integer> nameToCol = new HashMap<String, Integer>();
private int colCount;
private List<String> keyList = new ArrayList<String>();
public TemplateModel get(int index) throws TemplateModelException {
return (TemplateModel) this.rows.get(index);
}
public int size() throws TemplateModelException {
return this.rows.size();
}
public TemplateModel get(String key) throws TemplateModelException {
if (key.equals("headers")) {
return new TemplateModelListSequence(this.keyList);
} else {
return null;
}
}
public boolean isEmpty() throws TemplateModelException {
return this.colCount == 0;
}
public void load(String driverClassName, String url, String username,
String password, String sql) throws SQLException,
ClassNotFoundException {
if (this.loaded) {
throw new IllegalStateException(
"Data already loaded into this CSV sequence.");
}
new Parser().load(driverClassName, url, username, password, sql);
this.loaded = true;
}
private class Parser {
private void load(String driverClassName, String url, String username,
String password, String sql) throws SQLException,
ClassNotFoundException {
Class.forName(driverClassName); // 加载驱动程序
Connection connection = null;
try {
connection = DriverManager.getConnection(url, username,
password);
connection.setAutoCommit(false);
doInConnection(connection, sql);
if (!connection.getAutoCommit()) {
connection.commit();
connection.setAutoCommit(true);
}
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
} catch (Throwable ex) {
}
connection = null;
}
}
}
private void doInConnection(Connection connection, String sql)
throws SQLException {
Statement stmt = null;
try {
stmt = connection.createStatement();
doInStatement(stmt, sql);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
} catch (Throwable ex) {
}
stmt = null;
}
}
}
private void doInStatement(Statement statement, String sql)
throws SQLException {
ResultSet rs = null;
try {
rs = statement.executeQuery(sql);
doInResultSet(rs);
} finally {
try {
rs.close();
} catch (SQLException e) {
} catch (Throwable ex) {
}
rs = null;
}
}
private void doInResultSet(ResultSet rs) throws SQLException {
ArrayList<Integer> headerTypes = new ArrayList<Integer>();
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
colCount = 0;
for (int column = 1; column <= columnCount; column++) {
String columnName = metaData.getColumnName(column);
int jdbcType = metaData.getColumnType(column);
nameToCol.put(columnName, colCount);
keyList.add(columnName);
headerTypes.add(jdbcType);
colCount++;
}
metaData = null;
if (colCount > 0) {
while (rs.next()) {
TemplateModel[] row = new TemplateModel[colCount];
for (int column = 0; column < colCount; column++) {
switch (headerTypes.get(column)) {
case Types.VARCHAR:
case Types.CHAR:
case Types.LONGVARCHAR:
String str = rs.getString(column + 1);
if (rs.wasNull()) {
row[column] = new SimpleScalar(""); // Field
// IsNull
} else {
row[column] = new SimpleScalar(str);
}
break;
case Types.SMALLINT:
case Types.TINYINT:
short s = rs.getShort(column + 1);
if (rs.wasNull()) {
row[column] = new SimpleNumber(0);// Field
// IsNull
} else {
row[column] = new SimpleNumber(s);
}
break;
case Types.INTEGER:
int i = rs.getInt(column + 1);
if (rs.wasNull()) {
row[column] = new SimpleNumber(0);// Field
// IsNull
} else {
row[column] = new SimpleNumber(i);
}
break;
case Types.BIGINT:
long l = rs.getLong(column + 1);
if (rs.wasNull()) {
row[column] = new SimpleNumber(0);// Field
// IsNull
} else {
row[column] = new SimpleNumber(l);
}
break;
case Types.REAL:
case Types.FLOAT:
float f = rs.getFloat(column + 1);
if (rs.wasNull()) {
row[column] = new SimpleNumber(0);// Field
// IsNull
} else {
row[column] = new SimpleNumber(f);
}
break;
case Types.DOUBLE:
double d = rs.getDouble(column + 1);
if (rs.wasNull()) {
row[column] = new SimpleNumber(0);// Field
// IsNull
} else {
row[column] = new SimpleNumber(d);
}
break;
case Types.NUMERIC:
case Types.DECIMAL:
BigDecimal bd = rs.getBigDecimal(column + 1);
if (rs.wasNull()) {
row[column] = new SimpleNumber(0);// Field
// IsNull
} else {
row[column] = new SimpleNumber(bd.doubleValue());
}
break;
case Types.DATE:
java.sql.Date date = rs.getDate(column + 1);
if (rs.wasNull()) {
row[column] = new SimpleDate(new java.sql.Date(
0));// Field
// IsNull
} else {
row[column] = new SimpleDate(date);
}
break;
case Types.TIME:
java.sql.Time t = rs.getTime(column + 1);
if (rs.wasNull()) {
row[column] = new SimpleDate(new java.sql.Time(
0)); // Field
// IsNull
} else {
row[column] = new SimpleDate(t);
}
break;
case Types.TIMESTAMP:
java.sql.Timestamp ts = rs.getTimestamp(column + 1);
if (rs.wasNull()) {
row[column] = new SimpleDate(
new java.sql.Timestamp(0)); // Field
// IsNull
} else {
row[column] = new SimpleDate(ts);
}
break;
case Types.CLOB:
Clob clob = rs.getClob(column + 1);
if (rs.wasNull()) {
row[column] = new SimpleScalar(""); // Field
// IsNull
} else {
row[column] = new SimpleScalar(clob
.getSubString(1l, (int) clob.length()));
}
break;
case Types.BIT:
case Types.BOOLEAN:
boolean b = rs.getBoolean(column + 1);
if (rs.wasNull()) {
row[column] = TemplateBooleanModel.FALSE; // Field
// IsNull
} else {
row[column] = b ? TemplateBooleanModel.TRUE
: TemplateBooleanModel.FALSE;
}
break;
case Types.BLOB:
Blob blob = rs.getBlob(column + 1);
if (rs.wasNull()) {
row[column] = new SimpleScalar(""); // Field
// IsNull
} else {
row[column] = new SimpleScalar(new String(blob
.getBytes(1l, (int) blob.length())));
}
break;
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
byte[] bs = rs.getBytes(column + 1);
if (rs.wasNull()) {
row[column] = new SimpleScalar(""); // Field
// IsNull
} else {
row[column] = new SimpleScalar(new String(bs));
}
break;
default:
row[column] = new SimpleScalar(""); // Unknow Type
}
}
rows.add(new RowHash(row));
}
}
}
}
private class RowHash implements TemplateHashModelEx, TemplateSequenceModel {
private TemplateModel[] cols;
private RowHash(TemplateModel[] cols) {
this.cols = cols;
}
public int size() {
return colCount;
}
public TemplateCollectionModel keys() {
return new TemplateModelListCollection(keyList);
}
public TemplateCollectionModel values() {
return new TemplateModelArrayCollection(cols);
}
public TemplateModel get(String key) {
Integer i = (Integer) nameToCol.get(key);
if (i != null) {
return cols[i.intValue()];
} else {
return null;
}
}
public boolean isEmpty() {
return size() == 0;
}
public TemplateModel get(int i) throws TemplateModelException {
return cols[i];
}
}
}
分享到:
- 2008-11-03 10:40
- 浏览 1504
- 评论(0)
- 论坛回复 / 浏览 (0 / 2204)
- 查看更多
相关推荐
**FMPP介绍** FMPP(FreeMarker Page Preprocessor)是一个强大的文本模板引擎,它能够将模板与数据模型合并,生成各种类型的文本输出,如HTML、XML、Java源代码等。FMPP不仅支持FreeMarker模板语言,还具备预处理...
本文将深入探讨“Fmpp+Ant生成类文件”这一主题,结合提供的标签“源码”和“工具”,我们将理解如何利用这两者进行高效地编译和构建。 Fmpp(FreeMarker Macro Processor)是一款强大的文本模板语言,它主要用于...
**FMPP Plus与FreeMarker更新** FMPP Plus是一款基于Java的文本模板引擎,它扩展了FMPP(FreeMarker-based Make-like Preprocessor)的功能,提供了一种强大的方式来生成各种类型的文本文件,如HTML、XML、Java源...
**FMPP - FreeMarker-Based PreProcessor 深度解析** FMPP,全称为FreeMarker-Based PreProcessor,是一款基于FreeMarker模板引擎的文本预处理器。它提供了丰富的功能,包括宏定义、流控制以及表达式处理,使得用户...
sbt-fmpp 插件可使用模板生成scala / java代码。 使用完成处理。用法添加插件: addSbtPlugin("com.github.sbt" %% "sbt-fmpp" % "0.3") 在构建中启用插件: import fmpp.FmppPlugin._object build extends Build { ...
D:/FMPP/bin/fmpp -C config.fmpp ``` 这会根据`config.fmpp`配置文件中的设定,处理源文件夹中的模板并生成最终的HTML文件到输出文件夹。 总的来说,FreeMarker作为一个独立于Web框架的模板引擎,其优势在于简化...
在现代Web开发中,构建工具扮演着至关重要的角色,它们自动化了诸如编译、压缩、合并、测试等繁琐任务,极大地提高了开发效率。本主题聚焦于如何利用gulp这个流行的JavaScript构建工具来集成Java FreeMarker模板引擎...
它在Java环境中运行,被广泛应用于Web应用中的视图层,尤其是与Spring框架结合使用时。这个"Text_FreeMarker"的压缩包文件很可能是提供了一些FreeMarker的示例代码或者教程,用于帮助学习者理解并掌握FreeMarker的...
Ant Task手册
FMPP(FreeMarker PreProcessor)是用于处理Freemarker模板的一个辅助工具,它可以帮助开发者快速调试模板,生成输出,而无需完整的Java应用程序环境。通过配置FMPP,可以指定模板的源目录、输出目录、日志文件,...
它与Lucene和Heritrix的关系可能在于,FMPP可以用来自动化生成配置文件或者索引展示页面,这样在构建搜索引擎时可以提高效率和一致性。 总结一下,Lucene和Heritrix是构建搜索引擎的关键组件。Lucene负责高效地索引...
FMPP is a general-purpose text file preprocessor tool that uses FreeMarker templates. It is particularly designed for HTML preprocessor, for the generation of complete (static) home-pages: directory ...
FMPP(FreeMarker PreProcessor)是Freemarker的一个辅助工具,可以帮助你实现更多功能,如预处理、包含其他模板等。在使用Freemarker时,需要下载并配置FMPP来增强模板处理能力。 7. **优势与适用场景**: ...
FMPP是Freemarker的一个辅助工具,可以扩展Freemarker的功能,如处理多种输入格式和执行预处理步骤。 6. **应用实例** 通过处理XML文件的例子,可以直观地展示Freemarker的强大之处。例如,你可以使用Freemarker...
- FMPP(Freemarker PreProcessor)是一个辅助工具,它可以扩展Freemarker的功能,如处理多个输入文件、模板合并等。 Freemarker的灵活性和强大功能使其成为许多开发者的首选模板引擎。其过程调用、递归和XML处理...
2. **安装Fmpp**:Fmpp是一个Freemaker的辅助工具,可以增强其功能。下载地址为http://fmpp.sourceforge.net/ 。 #### 六、示例 为了更好地理解Freemaker的工作原理,我们可以通过一个简单的XML文件示例来展示如何...
- **Fmpp**: 一个辅助工具,可增强Freemaker的功能,[官方网站](http://fmpp.sourceforge.net/) 以上介绍了Freemaker的基本语法和核心特性,希望对初学者有所帮助。Freemaker不仅适用于Web开发中的动态页面生成,还...
FMPP/FMtoll freemarker.js gulp-freemarker ftl-server(@安然) nei 1.1 解决问题思路 上图是ftl文件解析的过程,输入ftl模板和对应java对象,经过freemarker.jar包解析后,得到输出文本;这个是我们在前后端...