Pinyin4j

[不指定 2008/12/11 16:22 | by ipaddr ]


Source: http://pinyin4j.sourceforge.net/

Pinyin4j is a Java library supporting convertion between Chinese characters and most popular Pinyin systems. Moreover, the output format of pinyin could be customized.


Features

  1. Convert Chinese (both Simplified and Tranditional) to most popular pinyin systems. Supporting pinyin system are listed below.
  2. Support multiple pronounciations of a single Chinese character. For example, taking the Chinese character '和' as input, you will get eight Hanyu Pinyin results (hé hè huó huò huo hāi he hú), depeding on different contexts.
  3. Multiple options for output format (details can be found here)
    • All uppercase or lowercase
    • Can output Unicode ü or v or u:
    • With tone numbers (lü3) or tone marks (lǚ) or without tone (lü)

当项目完成后接下来的就是打包发行了,应用程序(Application)项目和Eclipse插件项目(Plugin)的打包是不同的,本章将分别介绍两者的打包方法,并给出实际的打包例子。

7.1 应用程序项目的打包与发行

7.1.1 简介

Java应用程序项目完成后是可以脱离Eclipse运行的,要运行程序先要打它打成一个JAR包,它打包的大部份方法和标准Java的AWT/SWING的打包方法一样,主要有以下几个要点

l MANIFEST.MF - 打包清单。它是打包的关键性文件,主要是设置执行入口类和支持库的路径,在运行Java应用程序时是要根据此文件中给出的信息来查找入口类和支持库。

l 支持包 -如果Java应用程序用到了一些Eclipse包,那么就必须将这些包也复制到程序运行目录,否则程序将无法运行。如swt组件支持包swt.jar,jface组件支持包jface.jar。这些包都要在MANIFEST.MF文件中设置好。

l 本地化文件 - 如果用到了SWT组件,则还需要将SWT的本地化文件swt-win32-3063.dll(3063是版本号)复制到程序运行目录,否则程序将无法运行。

7.1.2 打包的具体操作步骤

本节将用前几章开发的SWT/JFace项目"myswt"的打包为例,来介绍打包应用程序项目的方法。

1、编辑清单MANIFEST.MF

(1)Eclipse提供了用于打包项目的"导出"向导,但本例运行此向导之前先需要创建一个MANIFEST.MF清单文件,其内容如下:

Manifest-Version: 1.0

Main-Class: book.chapter_4.wizard_dialog.WizardDialog1

Class-Path: ./lib/swt.jar ./lib/jface.jar ./lib/runtime.jar

说明:

l Manifest-Version - 指定清单文件的版本号

l Main-Class - 指定程序运行的入口类。本例设为运行4.5.2节开发的向导式对话框。注意:类名后不要加class扩展名

l Class-Path - 指定支持库的路径。"."指程序运行目录,即导出的JAR包所在目录。程序运行时依据Class-Path项的设置路径来查找支持库。每一个支持库之间用空格隔开。在这里jface.jar需要用到runtime.jar包,所以runtime.jar包也要加入到Class-Path中。

l 除了入口类的包名和类名之外,其他设置项都不分大小写,比如:Class-Path写成class-path或CLASS-PATH也可以,swt.jar写成SWT.JAR也行。

(2)将清单文件保存下来,建议放在myswt项目的根目录下。它的文件名可以任意取,本例取名为manifes.txt,Eclipse向导在打包时会自动的将manifes.txt的内容复制到JAR包的META-INF目录下的MANIFEST.MF文件中。

2、使用Eclipse"导出"向导来打包项目

(1)右键单击myswt项目的项目名,在弹出菜单中选择"导出"。在弹出的如下图7.1所示的对话框中,选择"JAR文件",单击"下一步"。

图7.1 导出对话框

(2)如下图7.2所示,将右边不需要的文件都取消勾选。在"选择导出目标"项文本框中设置JAR包的输出路径和包名(可以任意取名)为"D:\myswt_application\myswt.jar"。接受其他的默认设置不变,单击"下一步"。

附注:左边虽然选择了src目录,但源文件并不会导出到包中,除非勾选了"导出Java源代码文件和资源"项。

图7.2 选择导入文件

(3)如下图7.3所示,接受默认设置不变,单击"下一步"。

图7.3 导出类的选项

(4)这一步较关键。如下图7.4所示,选择"从工作空间中使用现有清单"项,将创建的清单文件输入,也可以通过旁边的"浏览"按钮来选择清单文件。输入清单文件后,单击"完成",Eclipse开始将项目打包。

图7.4 清单文件设置

经过以上四步后,在"D:\myswt_application"路径下生成了一个名为"myswt.jar"的文件。myswt.jar是一个ZIP格式的压缩文件,可以用WinRAR或WinZip软件打开,也就是说用这两个软件也可以替代Eclipse向导来打包文件。如果用WinRAR来打包文件,则压缩格式要选择ZIP格式而非RAR格式,压缩率倒可以任意选。

用WinRAR打开myswt.jar文件后其内部的目录结构如下图7.5所示:

图7.5 myswt.jar文件的内部目录结构

在myswt.jar文件的内部目录META-INF中仅一个文件:MANIFEST.MF,它和以前创建的清单文件manifest.txt的内容是一样的,如下:

Manifest-Version: 1.0

Class-Path: ./lib/swt.jar ./lib/jface.jar ./lib/runtime.jar

Main-Class: book.chapter_4.wizard_dialog.WizardDialog1

3、复制Java应用程序的支持包及本地化文件

在MANIFEST.MF文件中的Class-Path项设置了三个包,从Eclipse的plugins目录中将此三个支持包复制到D:\myswt_application\lib目录,本地化文件swt-win32-3063.dll复制到D:\myswt_application目录中。此三个文件在Eclipse中的路径为:

plugins\org.eclipse.swt.win32_3.0.1\ws\win32\swt.jar

plugins\org.eclipse.jface_3.0.0\jface.jar

plugins\org.eclipse.core.runtime_3.0.1\runtime.jar

plugins\org.eclipse.swt.win32_3.0.1\os\win32\x86\swt-win32-3063.dll

复制完成后的目录结构如下图7.6所示:

图7.6 目录结构图

4、编写运行myswt.jar包的批处理程序"run.bat"

在myswt_application目录下创建一个批处理程序run.bat(名字任取,扩展名必须是bat),其内容仅一句语句,如下:

javaw -jar myswt.jar

说明:

l javaw对应c:\jdk\jre\bin\javaw.exe文件,如果windows提示命令未发现,则需要将c:\jdk\jre\bin路径加入到windows环境变量path中。

l 在运行程序的时候有一个讨厌的黑色命令行窗口,要去掉它,可以将run.bat内容更改如下:"start javaw -jar myswt.jar",start是指调用了windows的"运行"命令。

l 如果想将swt-win32-3063.dll也放在单独的目录中,如"D:\myswt_application\native"目录,则需将run.bat内容更改为:

start javaw -Djava.library.path=./native/ -jar myswt.jar

5、运行程序

双击run.bat文件,得到如下图7.7所示的程序界面。

图7.7 程序运行效果图

6、注意事项

本例只需要三个支持包,但你的程序也许会需要更多的支持包才能运行。如果你想一次到位,则可以将"Java构建路径"的"库"选项卡中所有引用的包都复制到lib目录中。如果你喜欢用到什么包才加入什么包,希望维持打包文件的简洁,则需要自己一步步的去试:如果缺少某支持包,运行程序时会输出的未找到类的错误信息,从信息中的包名可得知程序缺少哪一个支持包。比如"Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jface/wizard/IWizard",从错误信息中很明显的就能知道程序缺少jface包

7.1.3 其他得到JAR包的方式

要得到JAR包除了以上所说的用Eclipse"导出"向导、用WinZip和WinRAR,另外还能用Java自带的命令行式打包软件jar.exe(位于c:\jdk\bin目录),其打包命令为:

c:\jdk\bin\jar cvfm myswt.jar C:\eclipse3.0.1\eclipse\workspace\myswt\manifest.txt -C C:\eclipse3.0.1\eclipse\workspace\myswt\bin .

说明:

l c:\jdk\bin\jar - 由于本书没有把c:\jdk\bin加入到windows环境变量path中,所以手工指定jar.exe的路径

l cvfm - jar.exe的参数,"c"创建新的jar包;"v"将调试信息打印在屏幕上;"f"指定生成的jar文件名;"m"使用清单文件。注意它们都是小写

l myswt.jar - 打包后的JAR包名

l 在前面是把清单文件manifest.txt放在C:\eclipse3.0.1\eclipse\workspace\myswt\目录下。如果将它和批处理文件放在一个目录就不必指定长长的路径了。

l "-C 路径 . "指将路径下(包括子目录)的所有文件打包,由于class文件是输出在项目的bin目录下,所以路径指定到项目的bin目录,注意三者之间是用空格隔开,并且最后一个字符是小数点。

这种方式的优点是没有Eclipse导出向导的操作那么麻烦,适合经常需要导出JAR包的情况。

7.1.4 使用第三方插件对项目打包

开源组织(http://sourceforge.net/)中有一款可将Eclipse支持包和项目编译文件一起打到一个包中的插件,叫"Fat Jar",它的下载地址是"http://fjep.sourceforge.net/",具体下载不再说明,安装步骤参阅第1章SWT Designer的安装。

Fat Jar的使用步骤如下:

(1)右键单击myswt项目的项目名,可见菜单中多了一项"Build Fat Jar",如下图7.8所示,选择"Build Fat Jar"项。

图7.8 右键菜单

(2)在下图7.9所示的对话框中,"Jar-Name"项填入JAR包的输出路径。文件清单"Manifest"项不用填,默认会自动创建一个。"Main-Class"项填入程序的入口类。其他都接受默认值,单击"下一步"。

图7.9 输出配置

(3)如下图7.10所示,窗口中将myswt项目所用到的支持包都列了出来。我们仅勾选图中runtime.jar、swt.jar、jface.jar这三项即可,当然全选也并尝不可,只是最后得到的JAR包会更大一些,因为Fat Jar会将所有支持包合并在一个JAR包中。

图7.10 选择要打包的文件

单击图7.10的"完成"按钮后, JAR包myswt.jar将输出到D:\myswt_applicationh目录中。和以前一样,要运行此JAR包需要一个批处理文件以及本地化文件swt-win32-3063.dll,唯一不同的是不再需要Eclipse支持包,其目录结构如下图7.11所示:

图7.11 目录结构

为什么不需要Eclipse支持包了呢?那是因为支持包已经在myswt.jar文件中了,从下图7.12可以看到swt.jar等都被拆散成目录,并包含在myswt.jar包中。

图7.12 myswt.jar的内部目录结构

其中META-INF目录的MANIFEST.MF文件内容如下,可以发现和以前不同的地方是:Class-Path项没有了。

Manifest-Version: 1.0

Created-By: Fat Jar Eclipse Plug-In

Main-Class: book.chapter_4.wizard_dialog.WizardDialog1

7.1.4 让用户电脑不必安装JRE环境

通常运行Java程序有个前提条件:用户电脑必须先安装JRE环境。虽然安装JRE环境非常简单,但毕竟多了一步,算是有一点点的瑕疵。这里给出一个不必让用户安装JRE环境的方法,其实现步骤如下:

(1)将原JDK中的"jre"目录复制到"D:\myswt_application\java1.4.2"目录下(java1.4.2也可换成其他名称)。

(2)将JDK和JRE从本机卸载掉,这样表示本机没有安装JAVA运行环境。

(3)修改批处理文件run.bat中的命令为"start java1.4.2\jre\bin\javaw -jar myswt.jar",仅仅是在javaw前加上了一个相对应路径。

双击run.bat即可在不安装JRE环境的电脑运行此Java应用程序。

7.1.5 更进一步的完善

1、抛弃批处理文件(*.bat)

用批处理文件运行程序似乎不够专业,虽然它足以完成运行任务。但习惯就象一种毒药一旦染上就很难摆脱它的影响,Windows统治下的人们早已经习惯运行扩展名是EXE的程序,用*.bat他们就会感觉别扭。

我们可以用一个叫JavaLauncher的免费小程序来代替批处理文件去运行Java程序。JavaLauncher的下载网址是:

http://www.rolemaker.dk/nonRoleMaker/javalauncher/marner_java_launcher.htm

下载下来的文件是一个名JavaLauncher.zip的压缩包,解压后的目录结构如下图7.13所示:

图7.13 JavaLauncher.zip目录结构

在上图的目录中

l source目录包含了JavaLauncher的源程序,是用C语言写的

l changes.txt是新版的修改说明

l launch.exe是主程序

l launcher.cfg是配置文件

l readme.txt是一些说明和示例

我们只需要launch.exe、launcher.cfg两个文件,将这两个文件复制到打包文件所在的目录。launcher.cfg是一个仅三行内容的文本文件,将它修改如下:

.

.\java1.4.2\jre\bin\javaw.exe

-jar myswt.jar

l 第一行设置指向JAR包myswt.jar的目录,由于launch.exe和myswt.jar同在一个目录,所以用"."即当前目录。

l 第二行设置指向jre\bin\javaw.exe的路径。在上一小节(7.1.4节)已将jre目录复制到了java1.4.2子目录中

配置好launcher.cfg后,双击launch.exe即可运行java应用程序。

如果仔佃研究eclipse的启动方式,发现eclipse和JavaLauncher的原理一样:eclipse.exe相当于launch.exe,startup.jar相当于myswt.jar。只不过eclipse.exe不象launch.exe要具有通用性,所以它没有*.cfg这样的配置文件,而是将启动信息固化在eclipse.exe中。

2、美化图标

launch.exe文件的图标太单调了,让我们给它换个好看点的。换程序的图标需要用到一个免费的软件:Resource Hacker,它有中文版,下载网址是:

http://www.users.on.net/johnson/resourcehacker/

用Resource Hacker来替换launch.exe的图标的步骤如下:

(1)运行Resource Hacker,得到如下图7.14所示的窗口。

图7.14 Resource Hacker的主界面

(2)单击主菜单"文件→打开",将launch.exe载入到程序中,结果如下图7.15所示。

图7.15 载入Lanunch.exe之后的界面

(3)如上图,选择左边的"图标→1→1030",然后右键单击"1030"项,选择"替换资源..."。如下图7.16所示,在弹出窗口中单击"打开新图标文件",选择一个满意的图标,然后单击"替换"按钮。

附注:图标文件可以是exe、dll、res、ico,该软件可以从exe、dll、res抽取出图标,本例选择的是java的一个图标文件JavaCup.ico。

图7.16 选择图标文件

(4)如下图7.17所示,选择"文件→另存为",取名myswt.exe。

附注:按理说选择"保存"也是可以的,这时Resource Hacker会将老的launch.exe备份成launch_original.exe。但也许是刷新上有问题,用"保存"方式有时launch.exe无法显示出新图标,但有时又可以。

图7.17 保存修改

(5)最后的目录如下图7.18所示,可见myswt.exe(也就是launch.exe改了个名称)的图标换成了Java的图标。双击myswt.exe即可运行Java应用程序。

图7.18 最后的效果

3、最后的打包

发送给用户之前,通常要用WinZip或WinRAR将所有的文件全部打成一个压缩包,然后用户得到这个压缩包后,将其解压缩后即可运行程序,Eclipse软件就是这种方式。

另一种方式是用InstallShield、InstallAnyWhere这样的安装程序制作软件来创建一个单一的setup.exe文件,它具有向导式的安装界面,而且还可以往windows的程序栏插入菜单项,关于这些安装程序制作软件的具体使用请参阅相关书籍

--------------------------------------
以上内容来自互联网,上面的几个软件,可以本博客下载:
[file]attachment/200812/fatjar.rar[/file]
[file]attachment/200812/reshack.zip[/file]
[file]attachment/200812/javalauncher.rar[/file]

IE7 support IPv6

[不指定 2008/12/03 18:07 | by ipaddr ]

Starting with with IE7 and above, WinINet supports IPv6 literals in the hostname, and the authority component of the URI. WinINet also supports the use of IPv6 literals in relevant portions of the HTTP protocol, such as in the Location header.

Hostname IPv6 Literals and URI Components

WinINet implements IPv6 literals according to the specifications in RFC 3513. As specified in this RFC, IPv6 literals in a URI must be enclosed in brackets. For example, http://[::1]/ is a valid IPv6 URI; the form without brackets (http://::1/) is not valid. Hostname IPv6 literals that are not part of the URI, however, do not need to be enclosed in the brackets; either form is acceptable to WinINet. For example, both "::1" and "[::1]" are acceptable forms of IPv6 hostname literals. Other APIs, such as the WinSock API, will also accept both forms. Thus applications should be prepared to handle both forms of IPv6 hostname literals.

Scope ID

The IPv6 literal address in the URI may include a scope ID. A scope ID can be an interface ID such as [FE80::1%1]. The URI standard, documented in RFC 3986, does not define the syntax for the scope ID, and the URI is considered non-uniform when the scope ID is present. However, WinINet accepts a scope ID in the authority component of the URI, and in the hostname IPv6 literal.

The percent character (%) in the IPv6 literal address must be percent escaped when present in the URI. For example, the scope ID FE80::2%3, must appear in the URI as "http://[FE80::2%253]/", where %25 is the hex encoded percent character (%). If the application retrieves the URI from a Unicode API, such as the Winsock WSAAddressToString API, the application must add the escaped version of the percent character (%) in the hostname of the URI. To create the escaped version of the URI, applications call InternetCreateUrl with the dwFlags parameter set to ICU_ESCAPE_AUTHORITY, and the IPv6 hostname specified in the URL components structure specified in the lpUrlComponents parameter.

For all sockets operations, WinINet uses the scope ID. However, because the scope ID has only local host significance, it is not sent as part of the HTTP protocol headers in the request. For example, the call to InternetOpenUrl is called with the following URL in the lpszUrl parameter.

http://[fec0::2%251]:80/path.htm

The scope ID portion of the URL is removed by WinINet when the HTTP request is sent for this URL. The request contains the following headers:

GET path.htm HTTP/1.1
Host: [fec0::2]

IPv6 URIs in IE7

[不指定 2008/12/03 18:01 | by ipaddr ]

One of the benefits of creating a new URI parsing API for IE7 was that we were able to more easily add support for IPv6 addresses in URIs throughout IE7. This blog post will describe the use of IPv6 URIs in IE7.

IPv6 Syntax

One of the advantages of IPv6 over IPv4 is that IPv6 addresses are four times longer than IPv4 addresses (128 bits vs. 32 bits) allowing for about 3.402 * 1038 unique IPv6 addresses. Due to the longer length, a new and more compact textual representation is used for IPv6 addresses. IPv6 addresses are written as groups of four hexadecimal digits delimited by colons. For the full details on IPv6 addresses and their textual representation, see the IPv6 Addressing standard. The following is an example of an IPv6 address:

fec0::abcd

The old URI syntax and the IPv6 address syntax conflicted so in accommodation, the URI syntax was modified. According to the latest URI standard, when you put an IPv6 address in the host, you must enclose it in square brackets. Thus, the previous example IPv6 address could appear as follows in a URI:

http://[fec0::abcd]/

URIs containing IPv6 addresses are not considered “special” in IE and they may be used anywhere IE accepts or displays a URI.

Scope-IDs

One explicit omission from the latest URI standard concerning IPv6 addresses is IPv6 scope-ids. IPv6 scope-ids specify which zone (a group of network connections) on a computer should be used when resolving a particular IPv6 address. See the IPv6 Scoped Address Architecture proposed standard for a full description of scope-ids. The scope-id appears after the IPv6 address delimited by a percent character. The previous IPv6 address example with a scope-id of 1 looks like this:

fec0::abcd%1

Non-encoded percents are not permitted in URIs, so in order to support scope-ids in IPv6 URIs, IE7 allows scope-ids following the IPv6 address when delimited by a percent-encoded percent character. Therefore, the previous example IPv6 address could appear in a URI in IE7 as follows:

http://[fec0::abcd%251]/

There is currently an IETF document describing an update to the URI standard to include scope-ids, however this is still in draft stage and so is not supported by IE7. MSDN has more details on IE7’s support for IPv6 scope-ids in URIs.

Trying It Out

If you’re interested in trying this out yourself with your own web server, but aren’t connected to an IPv6 network, you can enable the IPv6 stack in Windows, and open the URI http://[::1]/ in IE7. The IPv6 address ::1 is the loopback address, similar to the IPv4 address 127.0.0.1. This will allow you to connect to your own computer’s web server via an IPv6 address.

Conclusion

I’ve described the use of and syntax of IPv6 URIs in IE7 at a high level, but if you’re interested in the nitty-gritty details, check out the provided links to documents that describe IPv6 addressing and URIs in greater detail and try out IPv6 URIs on your own. Additionally, I should mention that although support for IPv6 URIs is new to IE7 and not available in IE6, IE6 does support DNS names backed by IPv6 addresses. Please leave me any IPv6 URI related comments or questions.

IPv6地址方案

[不指定 2008/12/03 16:11 | by ipaddr ]
   和IPv4相比,IPv6的主要改变就是地址的长度为128位,也就是说可以有2的128次方的IP地址,相当于10的后面有38个零。这么庞大的地址空间,足以保证地球上的每个人拥有一个或多个IP地址。

IPv6地址类型

    在RFC1884中指出了三种类型的IPv6地址,他们分别占用不同的地址空间:
  • 单点传送:这种类型的地址是单个接口的地址。发送到一个单点传送地址的信息包只会送到地址为这个地址的接口。
  • 任意点传送:这种类型的地址是一组接口的地址,发送到一个任意点传送地址的信息包只会发送到这组地址中的一个(根据路由距离的远近来选择)
  • 多点传送:这种类型的地址是一组接口的地址,发送到一个多点传送地址的信息包会发送到属于这个组的全部接口。
    和IPv4不同的是,IPv6中出现了任意点传送地址,并以多点传送地址代替了IPv4中的广播地址。


IPv6地址表示


    我们知道,IPv4地址长度为32位(4个字节)。书写IPv4的地址是用一个字节来代表一个无符号十进制整数,四个字节写成由3个点分开的四个十进制数,例如:
     10.1.123.56

对于128位的IPv6地址,定义相似的表示方法是必要的。 考虑到IPv6地址的长度是原来的四倍,RFC1884规定的标准语法建议把IPv6地址的128位(16个字节)写成8个16位的无符号整数,每个整数用四个十六进制位表示,这些数之间用冒号(:)分开,例如:
    3ffe:3201:1401:1:280:c8ff:fe4d:db39

    从上面的例子我们看到了手工管理IPv6地址的难度,也看到了DHCP和DNS的必要性。为了进一步简化IPv6的地址表示,可以用0来表示0000,用1来表示0001,用20来表示0020, 用300来表示0300,只要保证数值不便,就可以将前面的0省略。比如:
.
    1080:0000:0000:0000:0008:0800:200C:417A
    0000:0000:0000:0000:0000:0000:0A00:0001

    可以简写为:

    1080:0:0:0:8:800:200C:417A
    0:0:0:0:0:0:A00:1

    另外,还规定可以用符号::表示一系列的0。那么上面的地址又可以简化为:
    1080::0:8:800:200C:417A
    ::A00:1

    IPv6地址的前缀(FP, Format Prefix)的表示和IPv4地址前缀在CIDR中的表示方法类似。比如 0020:0250:f002::/48表示一个前缀为48位的网络地址空间。

IPv6地址分配


    RFC1881规定, IPv6地址空间的管理必须符合Internet团体的利益,必须是通过一个中心权威机构来分配。目前这个权威机构就是IANA(Internet Assigned Numbers Authority,Internet分配号码权威机构)。 IANA会根据IAB(Internet Architecture Board)和IEGS的建议来进行IPv6地址的分配.

    目前IANA已经委派三个地方组织来执行IPv6地址分配的任务:

  • 欧洲的RIPE-NCC (www.ripe.net)
  • 北美的INTERNIC (www.internic.net)
  • 亚太平洋地区的APNIC(www.apnic.net)
分页: 1/1 第一页 1 最后页 [ 显示模式: 摘要 | 列表 ]