当前位置:  编程技术>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模块及路径
    来源:    发布时间: 2013-11-06

在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
文件夹作为模块:
首先在文件夹的根下建立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/index.js
•./some-library/index.node

 

本文链接


    
[2]ECMAScript5新增的方法
    来源:    发布时间: 2013-11-06
// 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 {
//
    
[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      
    
最新技术文章:
▪css white-space:nowrap属性用法(可以强制文字不...
▪IE里button设置border:none属性无效解决方法
▪border:none与border:0使用区别
▪html清除浮动的6种方法示例
▪三个不常见的 HTML5 实用新特性简介
▪css代码优化的12个技巧
▪低版本IE正常运行HTML5+CSS3网站的3种解决方案
▪CSS Hack大全-教你如何区分出IE6-IE10、FireFox、Chr...
▪ie6,ie7,ie8完美支持position:fixed的终极解决方案
▪小技巧处理div内容溢出
▪html小技巧之td,div标签里内容不换行
▪纯CSS实现鼠标放上去改变文字内容
▪li中插入img图片间有空隙的解决方案
▪CSS3中Transition属性详解以及示例分享
▪父div高度不能自适应子div高度的解决方案
▪告别AJAX实现无刷新提交表单
▪从零学CSS系列之文本属性
▪HTML 标签
▪CSS3+Js实现响应式导航条
▪CSS3实例分享之多重背景的实现(Multiple background...
▪用css截取字符的几种方法详解(css排版隐藏溢...
▪页面遮罩层,并且阻止页面body滚动。bootstrap...
▪CSS可以做的几个令你叹为观止的实例分享
▪详细分析css float 属性以及position:absolute 的区...
▪IE6/IE7/IE8/IE9中tbody的innerHTML不能赋值的完美解...
▪CSS小例子(只显示下划线的文本框,像文字一...
▪可以给img元素设置背景图
▪不通过JavaScript实现的自动滚动视差效果
▪div+CSS 兼容小摘
▪CSS的inherit与auto使用分析
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3