当前位置: 编程技术>WEB前端
本页文章导读:
▪nodejs的require模块及路径 在nodejs中,模块大概可以分为核心模块和文件模块。核心模块是被编译成二进制代码,引用的时候只需require表示符即可,如(require('net'))。文件模块,则是指js文件、json文件或者是.node文件。.........
▪ECMAScript5新增的方法 // Copyright 2009-2012 by contributors, MIT License// vim: ts=4 sts=4 sw=4 expandtab // Module systems magic dance(function (definition) { // RequireJS if (typeof define == "function") { define(definition); // YUI3 } else if (typeof .........
▪第十三章 BIRT报表引擎API及报表API (续)-自定义web BIRT展示器 13.2 自定义BIRT viewer(报表展示器)
前面我们提到了可以通过java定制报表查看器,通过servlet访问webreport,方便系统集成。下面我详细讲解如何实现这个过程:
首先实现自定义报表引擎Repo.........
[1]nodejs的require模块及路径
在nodejs中,模块大概可以分为核心模块和文件模块。
核心模块是被编译成二进制代码,引用的时候只需require表示符即可,如(require('net'))。
文件模块,则是指js文件、json文件或者是.node文件。在引用文件模块的时候后要加上文件的路径:/.../.../xxx.js表示绝对路径、./xxx.js表示相对路径(同一文件夹下的xxx.js),../表示上一级目录。如果既不加/.../、../又不加./的话,则该模块要么是核心模块,要么是从一个node_modules文件夹加载。
对于加载模块时既没指出./ ../ /.../时,加载模块的搜索路径。如果'/home/ry/projects/foo.js' 中的文件调用了 require('bar.js') ,node将在下面的位置进行搜索:
•/home/ry/projects/node_modules/bar.js
•/home/ry/node_modules/bar.js
•/home/node_modules/bar.js
•/node_modules/bar.js
•/home/ry/node_modules/bar.js
•/home/node_modules/bar.js
•/node_modules/bar.js
文件夹作为模块:
首先在文件夹的根下建立package.json文件,它标识了一个主模块。一个package.json中的内容可能如下:
{ "name" : "some-library", "main" : "./lib/some-library.js" }
如果这是在一个文件夹./some-library下,那么require('./some-library')时将试图加载./some-library/lib/some-library.js
如果在这个目录下没有package.json文件,node将试图从这个目录下加载index.js或index.node文件。例如,如果上面没有package.json文件,那么require('./some-library')时,将试图加载下面的文件:
如果这是在一个文件夹./some-library下,那么require('./some-library')时将试图加载./some-library/lib/some-library.js
如果在这个目录下没有package.json文件,node将试图从这个目录下加载index.js或index.node文件。例如,如果上面没有package.json文件,那么require('./some-library')时,将试图加载下面的文件:
•./some-library/index.js
•./some-library/index.node
•./some-library/index.node
本文链接
[2]ECMAScript5新增的方法
// Copyright 2009-2012 by contributors, MIT License
// vim: ts=4 sts=4 sw=4 expandtab
// Module systems magic dance
(function (definition) {
// RequireJS
if (typeof define == "function") {
define(definition);
// YUI3
} else if (typeof YUI == "function") {
YUI.add("es5-sham", definition);
// CommonJS and <script>
} else {
definition();
}
})(function () {
// ES5 15.2.3.2
// http://es5.github.com/#x15.2.3.2
if (!Object.getPrototypeOf) {
// https://github.com/kriskowal/es5-shim/issues#issue/2
// http://ejohn.org/blog/objectgetprototypeof/
// recommended by fschaefer on github
Object.getPrototypeOf = function getPrototypeOf(object) {
return object.__proto__ || (
object.constructor
? object.constructor.prototype
: prototypeOfObject
);
};
}
// ES5 15.2.3.3
// http://es5.github.com/#x15.2.3.3
if (!Object.getOwnPropertyDescriptor) {
var ERR_NON_OBJECT = "Object.getOwnPropertyDescriptor called on a non-object: ";
Object.getOwnPropertyDescriptor = function getOwnPropertyDescriptor(object, property) {
if ((typeof object != "object" && typeof object != "function") || object === null) {
throw new TypeError(ERR_NON_OBJECT + object);
}
// If object does not owns property return undefined immediately.
if (!owns(object, property)) {
return;
}
// If object has a property then it's for sure both `enumerable` and
// `configurable`.
var descriptor = { enumerable: true, configurable: true };
// If JS engine supports accessor properties then property may be a
// getter or setter.
if (supportsAccessors) {
// Unfortunately `__lookupGetter__` will return a getter even
// if object has own non getter property along with a same named
// inherited getter. To avoid misbehavior we temporary remove
// `__proto__` so that `__lookupGetter__` will return getter only
// if it's owned by an object.
var prototype = object.__proto__;
object.__proto__ = prototypeOfObject;
var getter = lookupGetter(object, property);
var setter = lookupSetter(object, property);
// Once we have getter and setter we can put values back.
object.__proto__ = prototype;
if (getter || setter) {
if (getter) {
descriptor.get = getter;
}
if (setter) {
descriptor.set = setter;
}
// If it was accessor property we're done and return here
// in order to avoid adding `value` to the descriptor.
return descriptor;
}
}
// If we got this far we know that object has an own property that is
// not an accessor so we set it as a value and return descriptor.
descriptor.value = object[property];
return descriptor;
};
}
// ES5 15.2.3.4
// http://es5.github.com/#x15.2.3.4
if (!Object.getOwnPropertyNames) {
Object.getOwnPropertyNames = function getOwnPropertyNames(object) {
return Object.keys(object);
};
}
// ES5 15.2.3.5
// http://es5.github.com/#x15.2.3.5
if (!Object.create) {
// Contributed by Brandon Benvie, October, 2012
var createEmpty;
var supportsProto = Object.prototype.__proto__ === null;
if (supportsProto || typeof document == 'undefined') {
createEmpty = function () {
return { "__proto__": null };
};
} else {
//
// vim: ts=4 sts=4 sw=4 expandtab
// Module systems magic dance
(function (definition) {
// RequireJS
if (typeof define == "function") {
define(definition);
// YUI3
} else if (typeof YUI == "function") {
YUI.add("es5-sham", definition);
// CommonJS and <script>
} else {
definition();
}
})(function () {
// ES5 15.2.3.2
// http://es5.github.com/#x15.2.3.2
if (!Object.getPrototypeOf) {
// https://github.com/kriskowal/es5-shim/issues#issue/2
// http://ejohn.org/blog/objectgetprototypeof/
// recommended by fschaefer on github
Object.getPrototypeOf = function getPrototypeOf(object) {
return object.__proto__ || (
object.constructor
? object.constructor.prototype
: prototypeOfObject
);
};
}
// ES5 15.2.3.3
// http://es5.github.com/#x15.2.3.3
if (!Object.getOwnPropertyDescriptor) {
var ERR_NON_OBJECT = "Object.getOwnPropertyDescriptor called on a non-object: ";
Object.getOwnPropertyDescriptor = function getOwnPropertyDescriptor(object, property) {
if ((typeof object != "object" && typeof object != "function") || object === null) {
throw new TypeError(ERR_NON_OBJECT + object);
}
// If object does not owns property return undefined immediately.
if (!owns(object, property)) {
return;
}
// If object has a property then it's for sure both `enumerable` and
// `configurable`.
var descriptor = { enumerable: true, configurable: true };
// If JS engine supports accessor properties then property may be a
// getter or setter.
if (supportsAccessors) {
// Unfortunately `__lookupGetter__` will return a getter even
// if object has own non getter property along with a same named
// inherited getter. To avoid misbehavior we temporary remove
// `__proto__` so that `__lookupGetter__` will return getter only
// if it's owned by an object.
var prototype = object.__proto__;
object.__proto__ = prototypeOfObject;
var getter = lookupGetter(object, property);
var setter = lookupSetter(object, property);
// Once we have getter and setter we can put values back.
object.__proto__ = prototype;
if (getter || setter) {
if (getter) {
descriptor.get = getter;
}
if (setter) {
descriptor.set = setter;
}
// If it was accessor property we're done and return here
// in order to avoid adding `value` to the descriptor.
return descriptor;
}
}
// If we got this far we know that object has an own property that is
// not an accessor so we set it as a value and return descriptor.
descriptor.value = object[property];
return descriptor;
};
}
// ES5 15.2.3.4
// http://es5.github.com/#x15.2.3.4
if (!Object.getOwnPropertyNames) {
Object.getOwnPropertyNames = function getOwnPropertyNames(object) {
return Object.keys(object);
};
}
// ES5 15.2.3.5
// http://es5.github.com/#x15.2.3.5
if (!Object.create) {
// Contributed by Brandon Benvie, October, 2012
var createEmpty;
var supportsProto = Object.prototype.__proto__ === null;
if (supportsProto || typeof document == 'undefined') {
createEmpty = function () {
return { "__proto__": null };
};
} else {
//
[3]第十三章 BIRT报表引擎API及报表API (续)-自定义web BIRT展示器
来源: 互联网 发布时间: 2013-11-06
13.2 自定义BIRT viewer(报表展示器)
前面我们提到了可以通过java定制报表查看器,通过servlet访问webreport,方便系统集成。下面我详细讲解如何实现这个过程:
首先实现自定义报表引擎ReportEngine
package birtbird; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.logging.Level; import javax.servlet.ServletContext; import org.eclipse.birt.core.exception.BirtException; import org.eclipse.birt.core.framework.IPlatformContext; import org.eclipse.birt.core.framework.Platform; import org.eclipse.birt.core.framework.PlatformServletContext; import org.eclipse.birt.report.engine.api.EngineConfig; import org.eclipse.birt.report.engine.api.EngineConstants; import org.eclipse.birt.report.engine.api.IReportEngine; import org.eclipse.birt.report.engine.api.IReportEngineFactory; public class BirtEngine { private static IReportEngine birtEngine = null; private static Properties configProps = new Properties(); private final static String configFile = "BirtConfig.properties"; public static synchronized void initBirtConfig() { loadEngineProps(); } public static synchronized IReportEngine getBirtEngine(ServletContext sc) { if (birtEngine == null) { EngineConfig config = new EngineConfig(); if (configProps != null) { String logLevel = configProps.getProperty("logLevel"); Level level = Level.OFF; if ("SEVERE".equalsIgnoreCase(logLevel)) { level = Level.SEVERE; } else if ("WARNING".equalsIgnoreCase(logLevel)) { level = Level.WARNING; } else if ("INFO".equalsIgnoreCase(logLevel)) { level = Level.INFO; } else if ("CONFIG".equalsIgnoreCase(logLevel)) { level = Level.CONFIG; } else if ("FINE".equalsIgnoreCase(logLevel)) { level = Level.FINE; } else if ("FINER".equalsIgnoreCase(logLevel)) { level = Level.FINER; } else if ("FINEST".equalsIgnoreCase(logLevel)) { level = Level.FINEST; } else if ("OFF".equalsIgnoreCase(logLevel)) { level = Level.OFF; } config.setLogConfig(configProps.getProperty("logDirectory"), level); } config.getAppContext().put( EngineConstants.APPCONTEXT_CLASSLOADER_KEY, Thread.currentThread().getContextClassLoader()); config.setEngineHome(""); IPlatformContext context = new PlatformServletContext(sc); config.setPlatformContext(context); try { Platform.startup(config); } catch (BirtException e) { e.printStackTrace(); } IReportEngineFactory factory = (IReportEngineFactory) Platform .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY); birtEngine = factory.createReportEngine(config); } return birtEngine; } public static synchronized void destroyBirtEngine() { if (birtEngine == null) { return; } birtEngine.destroy(); Platform.shutdown(); birtEngine = null; } public Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException(); } private static void loadEngineProps() { try { // Config File must be in classpath ClassLoader cl = Thread.currentThread().getContextClassLoader(); InputStream in = null; in = cl.getResourceAsStream(configFile); configProps.load(in); in.close(); } catch (IOException e) { e.printStackTrace(); } } }
再其次实现自定义birtviewer selvlet,下面的代码很重要,直接决定了报表展示器的外观
package birtbird; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.logging.Logger; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.birt.report.engine.api.HTMLRenderOption; import org.eclipse.birt.report.engine.api.IGetParameterDefinitionTask; import org.eclipse.birt.report.engine.api.IParameterDefnBase; import org.eclipse.birt.report.engine.api.IReportEngine; import org.eclipse.birt.report.engine.api.IReportRunnable; import org.eclipse.birt.report.engine.api.IRunAndRenderTask; import org.eclipse.birt.report.engine.api.IScalarParameterDefn; import org.eclipse.birt.report.engine.api.PDFRenderOption; import org.eclipse.birt.report.engine.api.RenderOption; public class ReportServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; /** * Constructor of the object. */ private IReportEngine birtReportEngine = null; protected static Logger logger = Logger.getLogger("org.eclipse.birt"); public ReportServlet() { super(); } /** * Destruction of the servlet. */ public void destroy() { super.destroy(); BirtEngine.destroyBirtEngine(); } /** * The doGet method of the servlet. * */ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("utf-8"); try { String reportName = req.getParameter("ReportName"); String baseUrl = req.getContextPath() + "/webReport?ReportName=" + reportName; String imgBaseUrl = req.getContextPath() + "/reports/images"; String outFormat = "html"; // 处理页面传过来的参数 HashMap<String, String> paramMap = new HashMap<String, String>(); Enumeration en = req.getParameterNames(); while (en.hasMoreElements()) { String pName = (String) en.nextElement(); String pValue = req.getParameter(pName); if (pName.equals("O_FORMAT")) { outFormat = pValue; } else { paramMap.put(pName, pValue); } } ServletContext sc = req.getSession().getServletContext(); this.birtReportEngine = BirtEngine.getBirtEngine(sc); IReportRunnable design = birtReportEngine.openReportDesign(sc .getRealPath("/reports") + "/" + reportName); HashMap<String, Object> parameters = new HashMap<String, Object>(); // 以打开的报表设计文件为参数,创建一个获取参数的对象 IGetParameterDefinitionTask paramTask = birtReportEngine .createGetParameterDefinitionTask(design); // 获取报表设计文件中的参数定义 Collection paramDefns = paramTask.getParameterDefns(false); // 为获取的参数定义赋值 Iterator iter = paramDefns.iterator(); while (iter.hasNext()) { IParameterDefnBase pBase = (IParameterDefnBase) iter.next(); if (pBase instanceof IScalarParameterDefn) { IScalarParameterDefn paramDefn = (IScalarParameterDefn) pBase; String pName = paramDefn.getName();// 报表参数名称 // int pType = paramDefn.getDataType( ); String pValue = paramMap.get(pName);// 页面传过来的值 if (pValue == null) { pValue = ""; } else { baseUrl += "&" + pName + "=" + pValue; } // Object pObject = stringToObject( pType, pValue ); parameters.put(pName, pValue); } } if (outFormat.equals("html")) {// 页面展示 resp.setContentType("text/html"); HTMLRenderOption htmlOptions = new HTMLRenderOption(); htmlOptions.setOutputFormat(RenderOption.OUTPUT_FORMAT_HTML); // setup image directory // HTMLRenderContext renderContext = new HTMLRenderContext(); // renderContext.setBaseImageURL(req.getContextPath() + // "/images"); // renderContext.setImageDirectory(sc.getRealPath("/images")); // logger.log(Level.FINE, "image directory " + // sc.getRealPath("/images")); // HashMap contextMap = new HashMap(); // contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, // renderContext); // htmlOptions.setImageDirectory("output/image"); // htmlOptions.setMasterPageContent(true); // htmlOptions.setPageFooterFloatFlag(true); ByteArrayOutputStream ostream = new ByteArrayOutputStream(); htmlOptions.setOutputStream(ostream); htmlOptions.setHtmlPagination(true);// 加入分页条 htmlOptions.setEmbeddable(true);// 去除html、head、body标记 IRunAndRenderTask task = birtReportEngine .createRunAndRenderTask(design); if (parameters != null && !parameters.isEmpty()) { task.setParameterValues(parameters); task.validateParameters(); } String maxRow = req.getParameter("MaxRowsPerQuery"); if (maxRow != null && maxRow.length() > 0) { task.setMaxRowsPerQuery(Integer.parseInt(maxRow));// 设置每次查询最大行数 } task.setRenderOption(htmlOptions); task.run(); task.close(); ServletOutputStream out = resp.getOutputStream(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<html>"); out.println(" <head>"); out.println(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"); out.println(" <style type=text/css>"); // out.println(" td{border:1px solid #9EBFE0;font-Size:12px;}"); // out.println(" th{background-color:#C3D5ED;border:1px solid #9EBFE0;f
最新技术文章:
 
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!