星期二, 八月 31, 2004

如何在IE Javascript 中传递Node-seet到XSLT

目的: 在Javascript 利用Microsoft MXSML Engine来做XML/XSLT的ProtoTye开发.
传递一个XML的所谓.Result TRee Fragment 到XSLT 去处理.


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml06192000.asp

例子:
关键处:MSXSL的DoMDocument的属性 .documentElement把一个node-set直接传递到XSLT中去处理.
MSXML:4.0 sp2.可能是个Bug.


<SCRIPT language = "javascript">
function init()
{

var basicinfo = new ActiveXObject("Msxml2.DOMDocument.4.0");
var employment = new ActiveXObject("Msxml2.DOMDocument.4.0");
var paycatagory = new ActiveXObject("Msxml2.DOMDocument.4.0");
var attendence = new ActiveXObject("Msxml2.DOMDocument.4.0");
var mainpage = new ActiveXObject("Msxml2.DOMDocument.4.0");

mainpage.async=false;
mainpage.resolveExternals = false;

basicinfo.async=false;
employment.async=false;
paycatagory.async=false;
attendence.async=false;

// You can substitute other XML file names here.
try
{
mainpage.load("hello.xml");

basicinfo.load("emp_M_Basicinfo.xml");

employment.load("emp_M_EmploymentInfo.xml");

paycatagory.load("pay_M_CatagoryItem.xml");
attendence.load("attendence.xml");
}catch(e)
{
alert("error:"+e.parseError);
}
//Display basicinfo nodeset
// alert(basicinfo.documentElement.childNodes.item(0).xml);

var xslt = new ActiveXObject("Msxml2.XSLTemplate.4.0");
var xsltTree= new ActiveXObject("Msxml2.FreeThreadedDOMDocument.4.0");

xsltTree.async = false;
// You can substitute other XSLT file names here.
xsltTree.load("hello.xslt");
xslt.stylesheet = xsltTree

xslProc = xslt.createProcessor();

xslProc.input = mainpage
try
{
xslProc.addParameter("basicinfo",basicinfo.documentElement);//.selectNodes("/Results")
xslProc.addParameter("employment",employment);
xslProc.addParameter("paycatagory",paycatagory);
xslProc.addParameter("attendence",attendence);
xslProc.addParameter("yourname","William Wang");
}catch(e)
{
alert(e);
}
xslProc.transform();

resTree.innerHTML = xslProc.output;

// Save the result of the XSL transformation

//htmlCode = xslProc.output
//fso=new ActiveXObject("Scripting.FileSystemObject")
//htmlFile=fso.CreateTextFile("sample.html",true)
//htmlFile.WriteLine(htmlCode)
//htmlFile.Close()
}
</SCRIPT>
</HEAD>
<BODY onload = "init()" >
<div id="resTree"></div>
</BODY>
</HTML>

星期五, 八月 20, 2004

j_Security_check 同Filter之间的矛盾

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=21795

如果通过Filter来解决某些问题,在用户验证之前,Filter将不会被激活.
这是一个问题.
因为,j_security_check 有"remember me"功能,这样很多DBCS的参数将在Filter之前被编码.

在Java里如何准确显示相除操作后的小数点位数

可以使用Formater族类.
一个常用的方法,使用BigDecimal类:

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class testDe
{
public static void main(String[] args)
{

BigDecimal b1 = new BigDecimal(3);
BigDecimal b2 = new BigDecimal(12);
System.out.println(b1.divide(b2,2,BigDecimal.ROUND_HALF_UP).doubleValue());
}
}

星期四, 八月 12, 2004

JAAS and Form-Authenticate Invalid direct reference to form login page Solution

Tomcat
臭名昭著的:Invalid direct reference to form login page 异常
完全解决方法.
PreCondition:
不要直接访问Login页面,让Tomcat Container自己在需要的时候调用Login页面

其他的Callbacker和其他相关问题,参考2004.3月文章.

出现Invalid direct reference to form login page 的关键原因在web.xml中的配置有问题.
注意的问题,千万不要遗漏下面几行:
<realm-name>FDS<realm-name>
<auth-constraint>
<role-name></role-name>
</auth-constraint>

完整的例子:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>ehrm</display-name>
<description>ehrm JAAS</description>

<filter>
<filter-name>encodecontroler</filter-name>
<filter-class>com.goldpeak.ehrm.services.EncodeControler</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodecontroler</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-name>FMEntryPoint</servlet-name>
<servlet-class>com.gp.framework.control.web.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FMEntryPoint</servlet-name>
<url-pattern>/control/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>/control/security/login</welcome-file>
</welcome-file-list>

<security-constraint>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/control/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name></role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>

</security-constraint>

<login-config>
<auth-method>FORM</auth-method>
<realm-name>FDS</realm-name>
<form-login-config>
<form-login-page>/control/security/login</form-login-page>
<form-error-page>/config/error.jsp</form-error-page>
</form-login-config>

</login-config>
<!--security-role>
<description>A Funky User</description>
<role-name>FunkyUser</role-name>
</security-role-->

<env-entry>
<env-entry-name>DatabaseJNI</env-entry-name>
<env-entry-value>java:/MySqlDS</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>

</web-app>

星期六, 八月 07, 2004

Google如何利用URLEncoder来处理事情。

红色的注释encodeURIComponent是关键。

<html><head><meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8">
<script>
function qs()
{
el=document.mainform.chinese;
if (window.RegExp && window.encodeURIComponent)
{
var qe=encodeURIComponent(el.value);
prompt("Transmit",qe);
window.location.href="http://www.google.com/search?q="+qe+"&sourceid=firefox&start=0&start=0&ie=utf-8&oe=utf-8";
}
}
// -->
</script>

<body>
<form name="mainform">
<input type="text" name="chinese"><input type="button" value="Trasmition" onclick="qs()">
</form>

星期二, 八月 03, 2004

常用词汇

YMMV:
----------------
"YMMV Net-language for 'your mileage may vary'. A warning that not everything described in a manual will work exactly the way it promised to. "

星期一, 八月 02, 2004

JAAS术语说明

[Subject]:JAAS 用术语Subject来引用请求访问的资源的实体.
[Principals]:主体(或委托人).
[credential]:凭证或信任装(可以是密码或数字签名)
一个Subject可以是一个用户或一种服务.由于一个实体可能有多个名字,
或Principals

http://java.sun.com/j2se/1.4.2/docs/guide/security/jaas/tutorials/glossary.html

Subjects, Principals, Authentication, and Credentials
To authorize access to resources, applications first need to authenticate the source of the request. The JAAS framework defines the term subject to represent the source of a request. A subject may be any entity, such as a person or service. A subject is represented by the javax.security.auth.Subject class.

Authentication represents the process by which the identity of a subject is verified, and must be performed in a secure fashion; otherwise a perpetrator may impersonate others to gain access to a system. Authentication typically involves the subject demonstrating some form of evidence to prove its identity. Such evidence may be information only the subject would likely know or have (such as a password or fingerprint), or it may be information only the subject could produce (such as signed data using a private key).

Once authenticated, a Subject is populated with associated identities, or Principals (of type java.security.Principal). A Subject may have many Principals. For example, a person may have a name Principal ("John Doe") and an SSN Principal ("123-45-6789"), which distinguish it from other Subjects.

In addition to associated Principals, a Subject may own security-related attributes, which are referred to as credentials. A credential may contain information used to authenticate the subject to new services. Such credentials include passwords, Kerberos tickets, and public key certificates. Credentials might also contain data that simply enables the subject to perform certain activities. Cryptographic keys, for example, represent credentials that enable the subject to sign or encrypt data. Public and private credential classes are not part of the core JAAS class library. Any class, therefore, can represent a credential.

星期六, 七月 31, 2004

Groovy快速打开一个文件

import java.io.*;

FileInputStream('foo.txt').using { ain |
while (true) {
line = ain.readLine()
if (line == null) break
print line
}
}

星期五, 七月 30, 2004

Groovy 核心语法----在字符串中嵌入Groovy表达式

Groovy使用${}或$来封包Groovy 表达式.

Groovy 一:Closure(闭合)关键词汇

1.定义

"A closure is a chunk of groovy code that can be assigned to a variable, passed to other operations, and
executed."

所以Closure类似于Java的inner class,就是一大段代码可以将其赋予一个变量,传递给其他操作并且可以执行.

2.隐含的参数
每个Closure对象都有一个确省参数,应用关键词是"it".如:
c={
print it;}
c("print Groovy")

3.明显的参数
通过"|"管道符号来分开参数和可执行的语句.
如:
c={x|print x+":)";};
c("ok")
go
你可以同"|"管道符号把多个参数包围起来.
c = { | x, y, z |println x + y + z;};
c(1,2,10);
go

一个不错的XML代码站点

入门级别Newbie开始的地方。

http://www.xmlpitstop.com/

通过XSLT如何得到唯一的值(精彩例子)

Question:
1.一般在一组node()里得到唯一的代表node(),类似SQL Select Distinct 关键词的作用.
在XSLT是通过 node()[not(aixs=preceding::node/aixs)]类似的语法得到.

如:XML
<?xml version="1.0" encoding="UTF-8"?>
<xmldata>
<current_row>
<dept>70</dept>
<deptname>??部</deptname>
<section>10</section>
<sectionname>正常班</sectionname>
<empid>HMB00660</empid>
<positioncode>490</positioncode>
<positiondesc>???</positiondesc>
<gradetype>30</gradetype>
<gradedesc>工人</gradedesc>
<employmentstatus>002</employmentstatus>
<employmentstatusdesc>在职</employmentstatusdesc>
<namechin>???</namechin>
<nameeng/>
</current_row>
<current_row>
<dept>70</dept>
<deptname>??部</deptname>
<section>10</section>
<sectionname>正常班</sectionname>
<empid>HMB10203</empid>
<positioncode>80</positioncode>
<positiondesc>文?</positiondesc>
<gradetype>10</gradetype>
<gradedesc>初?</gradedesc>
<employmentstatus>002</employmentstatus>
<employmentstatusdesc>在职</employmentstatusdesc>
<namechin>??花</namechin>
<nameeng/>
</current_row>
<current_row>
<dept>90</dept>
<deptname>人事部</deptname>
<section>10</section>
<sectionname>正常班</sectionname>
<empid>HMB00404</empid>
<positioncode>50</positioncode>
<positiondesc>?堂</positiondesc>
<gradetype>30</gradetype>
<gradedesc>工人</gradedesc>
<employmentstatus>002</employmentstatus>
<employmentstatusdesc>在职</employmentstatusdesc>
<namechin>?定金</namechin>
<nameeng/>
</current_row>
<current_row>
<dept>90</dept>
<deptname>人事部</deptname>
<section>10</section>
<sectionname>正常班</sectionname>
<empid>HMB10196</empid>
<positioncode>90</positioncode>
<positiondesc>司机</positiondesc>
<gradetype>40</gradetype>
<gradedesc>??</gradedesc>
<employmentstatus>002</employmentstatus>
<employmentstatusdesc>在职</employmentstatusdesc>
<namechin>???</namechin>
<nameeng/>
</current_row>
<current_row>
<dept>100</dept>
<deptname>生?部</deptname>
<section>130</section>
<sectionname>??合B班</sectionname>
<empid>HMB00481</empid>
<positioncode>360</positioncode>
<positiondesc>普工</positiondesc>
<gradetype>30</gradetype>
<gradedesc>工人</gradedesc>
<employmentstatus>002</employmentstatus>
<employmentstatusdesc>在职</employmentstatusdesc>
<namechin>??秀</namechin>
<nameeng/>
</current_row>
<current_row>
<dept>100</dept>
<deptname>生?部</deptname>
<section>130</section>
<sectionname>??合B班</sectionname>
<empid>HMB00891</empid>
<positioncode>360</positioncode>
<positiondesc>普工</positiondesc>
<gradetype>30</gradetype>
<gradedesc>工人</gradedesc>
<employmentstatus>002</employmentstatus>
<employmentstatusdesc>在职</employmentstatusdesc>
<namechin>邢巨芳</namechin>
<nameeng/>
</current_row>
</xmldata>

希望得到的结果如图:



最终得到的XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
<html>
<head>
<title>ok</title>
</head>
<link href="ehrm.css" rel="stylesheet" type="text/css"/>
<body>
<xsl:text disable-output-escaping="yes">
<![CDATA[
<script>
function folder(referen,objname)
{
var localobj=findObj(objname);
if(localobj.style.display=='none')
{

localobj.style.display='';
referen.style.backgroundImage='url(../images/image2/opengroup.gif)';}else{

localobj.style.display='none';
referen.style.backgroundImage='url(../images/image2/group.gif)';
}
}

// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
var p, i, foundObj;

if(!theDoc) theDoc = document;
if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
{
theDoc = parent.frames[theObj.substring(p+1)].document;
theObj = theObj.substring(0,p);
}
if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
for (i=0; !foundObj && i < theDoc.forms.length; i++)
foundObj = theDoc.forms[i][theObj];
for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
foundObj = findObj(theObj,theDoc.layers[i].document);
if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);

return foundObj;
}

</script>
]]>
</xsl:text>
<xsl:apply-templates select="//xmldata"/>
</body>
</html>
</xsl:template>

<xsl:template match="xmldata">
<div id="Layer3" style="position:absolute; width:412px; height:300px; z-index:3; top: 141px; overflow-y: scroll; left: 26px;">
<table width="400" border="0" cellspacing="0">
<xsl:for-each select="current_row[not(dept = preceding-sibling::current_row/dept)]">
<xsl:variable name="deptv" select="dept"/>
<tr>
<td class="folder1" onMouseUp="with(findObj('dept{position()}'))if(style.display=='none'){{style.display='';this.style.backgroundImage='url(../images/image2/opengroup.gif)'}}else{{style.display='none';this.style.backgroundImage='url(../images/image2/group.gif)'}}">
<xsl:value-of select="deptname"/>/<xsl:value-of select="dept"/>(<xsl:value-of select="count(/xmldata/current_row/section[preceding-sibling::dept=$deptv])"/>)
</td>
</tr>
<tr>
<td class="subfolder1" id="dept{position()}">
<table width="373" border="0" cellspacing="0">
<xsl:variable name="section_row" select="//current_row[ dept = $deptv and not(section=preceding-sibling::current_row[dept=$deptv]/section)]"/>
<!--section group-->
<xsl:for-each select="$section_row">
<tr>
<td width="371" class="folder1" onMouseUp="with(findObj('{$deptv}group{position()}'))if(style.display=='none'){{style.display='';this.style.backgroundImage='url(../images/image2/opengroup.gif)'}}else{{style.display='none';this.style.backgroundImage='url(../images/image2/group.gif)'}}">
<xsl:value-of select="current()/sectionname"/>/<xsl:value-of select="current()/section"/>
</td>
</tr>
<tr>
<!--sss-->
<td class="subfolder1" id="{$deptv}group{position()}">
<table width="350" border="0" cellspacing="0">
<!--Employee circle-->
<xsl:for-each select="/xmldata/current_row[dept = $deptv and sectionname=current()/sectionname]">
<tr>
<td width="66" class="file1" >
<xsl:choose>
<xsl:when test="not(namechin = 'null')">
<xsl:value-of select="namechin"/>
</xsl:when>
<xsl:when test="namechin = 'null' and not(nameeng = 'null')">
<xsl:value-of select="nameeng"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>NameNull</xsl:text>
</xsl:otherwise>
</xsl:choose>
</td>
<td width="60" class="cao2" onMouseOver="this.style.fontWeight='600';this.style.cursor='hand'" onMouseOut="style.fontWeight='400'" onClick="openWindow('{empid}','','features');parclose()">
<xsl:value-of select="empid"/>
</td>
<td width="90" class="cao1">
<xsl:value-of select="gradedesc"/>
</td>
<td width="25" class="cao1">--</td>
<td width="55" class="cao1">
<xsl:value-of select="positiondesc"/>
</td>
<td width="42" class="cao1">
<xsl:value-of select="employmentstatusdesc"/>
</td>
</tr>
</xsl:for-each>
<!--Employee circle over-->
</table>
</td>
<!--sss-->
</tr>
</xsl:for-each>
<!--section group over-->

</table>
</td>
</tr>
</xsl:for-each>
</table>
</div>
</xsl:template>
</xsl:stylesheet>



星期一, 七月 26, 2004

Xalan XSLT extension Javascript/java出现错误:

使用的javascript:engine:
http://www.mozilla.org/rhino/download.html 1.5R5

出现错误:Line number can not be negative:-1

这是Bug,必须使用:1.5 R3版本.可以正常使用.

extension使用Java出现错误:
不能正确映射Java对象的方法:
For extension function, could not find method static java.lang.System.currentTimeMills([ExpressionContext,] ).
原因:拼写错误.
------------------
例子:
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xalan"
exclude-result-prefixes="xalan"
xmlns:java="http://xml.apache.org/xalan/java"
xmlns:my-ext="ext1"
extension-element-prefixes="my-ext"
>
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="employment"/>
<xsl:param name="basicinfo"/>
<xsl:param name="paymentinfo"/>
<xsl:param name="attendance"/>


<xalan:component prefix="my-ext" functions="getdate">
<xalan:script lang="javascript">
var aaa=1;
function getdate()
{
return "ddd";
}
</xalan:script>
</xalan:component>


<xsl:template match="/">
<xsl:choose>
<xsl:when test="not($employment)">
<xsl:text>Nothing</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>Yeah</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:text> </xsl:text>
<xsl:value-of select="my-ext:getdate()"/>
<xsl:text> </xsl:text>
<xsl:value-of select="$employment/employe/name"/>
<xsl:text> </xsl:text>
<xsl:value-of select="java:java.lang.Math.random()" />
<xsl:text> </xsl:text>
<xsl:value-of select="java:java.lang.System.currentTimeMillis()" />
</xsl:template>
</xsl:stylesheet>


快速切换JVM语言和编码选项

1.java -Duser.language=语言(zh_CN) -encoding=(Encoding)

星期六, 七月 24, 2004

XML解析常见错误:org.xml.sax.SAXParseException: Document root element is missing

问题引起:
用可以编辑16 HEX CODE的编辑器,如UltraEdit查看文件类型是输入UTF8-DOS类型,
入图:

就会引起错误.
改正方式,用Ascll方式保存.就可以了.


XML解析中的问题

http://www.unicode.org/unicode/faq/utf_bom.html
这里解释了一些规范.
* BOM(Byte order mark):
* 00 00 FE FF = UTF-32, big-endian
* FF FE 00 00 = UTF-32, little-endian
* FE FF = UTF-16, big-endian
* FF FE = UTF-16, little-endian
* EF BB BF = UTF-8
*/

星期三, 七月 21, 2004

Groovy 基本语发:

这里的语言级别的材料比较齐备。

http://wiki.codehaus.org/groovy/FrontPage

基本语发训练:
http://wiki.codehaus.org/groovy/GroovyTutorial

BlocksAndClosures - Groovy Wiki

BlocksAndClosures - Groovy Wiki: "Closures"

Groovy设计到的词汇,Closure:闭包,也可做:回路

1. In a reduction system, a closure is a data
structure that holds an expression and an environment of
variable bindings in which that expression is to be evaluated.
The variables may be local or global. Closures are used to
represent unevaluated expressions when implementing
functional programming languages with lazy evaluation. In
a real implementation, both expression and environment are
represented by pointers.

A suspension is a closure which includes a flag to say
whether or not it has been evaluated. The term "thunk" has
come to be synonymous with "closure" but originated outside
functional programming.

2. In domain theory, given a partially ordered
set, D and a subset, X of D, the upward closure of X in D is
the union over all x in X of the sets of all d in D such that
x <= d. Thus the upward closure of X in D contains the
elements of X and any greater element of D. A set is "upward
closed" if it is the same as its upward closure, i.e. any d
greater than an element is also an element. The downward
closure (or "left closure") is similar but with d <= x. A
downward closed set is one for which any d less than an
element is also an element.

("<=" is written in LaTeX as \subseteq and the upward
closure of X in D is written \uparrow_\{D} X).