星期六, 七月 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).

CVS 常用关键词列表.

注意:在VSS中需要用双引号把下面的关键次扩起来"$ID$"
VSS:
To expand keywords

Type this keyword To add the following
$Archive: $ VSS archive file location
$Author: $ User who last changed the file
$Date: $ Date and time of last check in
$Header: $ Logfile, Revision, Date, Author
$History: $ File history, VSS format
$JustDate: $ Date, without the time addendum.
$Log: $ File history, RCS format
$Logfile: $ Same as Archive
$Modtime: $ Date and time of last modification
$Revision: $ VSS version number
$Workfile: $ File name
$NoKeywords: $ No keyword expansion for all keywords that follow

CVS:

$Id$ 关键字是用文件名、版本、时间、作者 及代码性质替换,如果使用-l选项取出,在Exp后
面会加上登录用户的名称。除了$Id$关键字,RCS还支持下面常用的关键字:
$Log$ : 你所提供的修改日志信息。
$Author$ :存入该版本的作者。
$Locker$ : 该版本的加锁者
$State$ : 该版本的状态 Exp(试验版), Stabe(稳定版), Rel(发行版).缺省是Exp
$Date$ : 该版本存入的时间,使用UTC时间格式。
$Revision$ : 该版本的版本号
$RCSfile$ : RCS文件名
$Source$ : RCS全路径名
$Name$ : 取回该版本的符号名
$Header$ : 相当于$ Source $$ Revision$$Date$$Author $$State$$Locker$的组合
比如:在java Source开头加入:
/*
*$Id$
*/

当commit该Java文件时候.cvs会把$Id替换为包含当前文件名,版本,时间,作者的字符串信息

星期六, 七月 17, 2004

Groovy及JSP在Web Module如何发现要打开的资源文件?

因为Web Module的.war文件,会在应用服务器上展开.所以如果在jsp或Groovy的脚本中有打开文件的操作.打开文件的目录的写法要注意.
import java.util.Date
import java.io.File;
if (session.counter == null) {
session.counter = 1
}
out.println(<<<EOS
<html>
<head>
<title>Groovy Servlet</title>
</head>
<body>
Hello, ${request.remoteHost}: ${session.counter}! ${new Date()}
<br>src
</body>
</html>
EOS)
session.counter = session.counter + 1

//研究了许久.:(,如何打开一个脚本文件

File fl=new File(application.getRealPath("/groovy/HelloWorld.groovy"))
evaluate(fl)


如何利用Groovy和Apache POI 生成 Excel

Easy Excel reports using Groovy and POI

import groovy.sql.Sql
import org.apache.poi.hssf.usermodel.*
import org.apache.poi.hssf.util.*
import java.io.File

if (args.size() != 1)
{
println(args.join(", "));
println("Error: expecting output filename as a commandline parameter");
System.exit(1);
}

Sql.loadDriver("oracle.jdbc.driver.OracleDriver")
sql = Sql.newInstance("jdbc:oracle:thin:@localhost:1521:sid", "user", "password")

workbook = new HSSFWorkbook();
sheet = workbook.createSheet("Games");
sheet.setColumnWidth(1.shortValue(), 5600.shortValue())
sheet.setColumnWidth(2.shortValue(), 8000.shortValue())
sheet.setColumnWidth(3.shortValue(), 8000.shortValue())

rowNum = 0
sql.eachRow("select * from tb_game order by name", { game |
skuCount = 0
sql.eachRow("select count(*) from tb_sku where product_id = ${game.id}", { skuCount = it[0] })

row = sheet.createRow(rowNum++)
row.createCell(0.shortValue()).setCellValue(game.id.doubleValue())
row.createCell(1.shortValue()).setCellValue(game.name)
row.createCell(2.shortValue()).setCellValue(game.developer)
row.createCell(3.shortValue()).setCellValue(game.vendor)
row.createCell(4.shortValue()).setCellValue(skuCount.doubleValue())
})

new File(args[0]).withOutputStream({ os | workbook.write(os) })

星期一, 七月 12, 2004

Mysql: return Row number at SELECT statement

select if(isnull(@i),@i:=0,@i:=@i+1) as nid from contact
但是返回的记过将都是0.因为变量的不存在.

Java Applet XUL GUI 语言

http://www.thinlet.com/

星期五, 七月 02, 2004