首页 > 编程 > JSP > 正文

JSPWiki文件名转换问题

2024-09-05 00:19:20
字体:
来源:转载
供稿:网友
国内最大的酷站演示中心!
 这几天研究了一下jspwiki把网页存到硬盘上的代码,想出了一个解决的办法.
jspwiki把文件名转换成%xx的格式是为了能在硬盘上存储各个国家语言的文件,
如果我们只在中文环境下使用,则没有必要进行这样的转换.
 
  jspwiki里的pageprovider和attachmentprovider负责了上面的转换,
而这两个provider可以在jspwiki.properties里面设置,所以我们继承一下
jspwiki里提供的类,再在配置文件里设置一下,很容易就让jspwiki不进行文
件名转换了.
 
  我是通过继承nativefilenameversioningfileprovider和basicattachmentprovider
类实现子类的.可惜basicattachmentprovider中负责文件名转换的方法是static的,我在
子类里没办法把它重载掉,这个问题已经向jspwiki的开发者提出修改意见了.现在page名
可以存为中文了,附件名还要等到jspwiki的basicattachmentprovider修改了以后才能
实现.
 
  附件里是编译完的代码,把它拷贝到jspwiki的lib目录里,再在jspwiki.properties里设置
"jspwiki.pageprovider =net.sf.startemplate.nativefilenameversioningfileprovider"
就可以把page存为中文名的文件了.这个provider和已有的%xx形式的page不能正常工作,
需要把这些page的名称改成未编码的中文格式.
 
  至于utf8编码的文件用notepad修改后会在最前面添加3个字符,那都是微软做的孽,不用微软的
或者基于微软的文本编辑器就行了.(微软为了识别unicode的编码,在文件最开始加了标识符,
而其他公司并不承认这个标准,所以才导致这些标识符会被显示出来)
 
  浏览器对网址会转换成%xx形式后再向服务器,服务器收到以后也都有相应的逆向转换,这个应该
没有什么问题.
 
  很高兴和你交流!!
 
  以下是我的代码:两个provider和一个工具类
 
 
   1:/**
2: * created on 2005-9-8 by dengber.
3: */
4:package net.sf.startemplate;
5:
6:import org.apache.log4j.logger;
7:
8:import com.ecyrd.jspwiki.providers.versioningfileprovider;
9:
10:/**
11: * a privider for jspwiki.
12: * this class behaves the same with <code>versioningfileprovider</code> provided
13: * by jspwiki,excepting it saves wiki pages into harddisk with the name inputted
14: * by the user. this is different with <code>versioningfileprovider</code>, it
15: * will save wiki pages with a encoded name, such as %e6%b5%8b%e8%af%95.
16: * though it is a good solution to save files whos name may contains any
17: * charachers in the world, it is not readable.
18: * <br /><br />
19: * for many people, they use jspwiki only in a intranet, the character problem
20: * above is nothing to them. saving wiki pages with the native charachter names
21: * may working well to them. so this class is a choice to that people: the wiki
22: * pages in harddisk have a readable name.
23: * <br /><br />
24: * danger: if the jspwiki user can input a character that can not be proccessed
25: * by your server, not use this class.
26: * <br /><br />
27: * how to use:copy the jar file that contains this class to <b>lib</b>
28: * directory of your jspwiki webapp, and set the following value to
29: * jspwiki.properties:
30: * jspwiki.pageprovider=net.sf.startemplate.nativefilenameversioningfileprovider
31: *
32: * @author dengber [email protected]
33: *
34: */
35:public class nativefilenameversioningfileprovider extends
36: versioningfileprovider {
37: private static final logger log = logger
38: .getlogger(nativefilenameversioningfileprovider.class);
39:
40: /**
41: * convert string to the formatting that can be as file name.
42: *
43: * @param pagename
44: * @return
45: */
46: protected string manglename(string pagename) {
47: final string[] windows_device_names = { "con", "prn", "nul",
48: "aux", "lpt1", "lpt2", "lpt3", "lpt4", "lpt5", "lpt6", "lpt7",
49: "lpt8", "lpt9", "com1", "com2", "com3", "com4", "com5", "com6",
50: "com7", "com8", "com9" };
51:
52: log.debug("manglename(" + pagename + ")");
53: pagename = textutil.encodefilename(pagename);
54:
55: boolean windowshackneeded = false;
56: string os = system.getproperty("os.name").tolowercase();
57:
58: if (os.startswith("windows") || os.equals("nt")) {
59: windowshackneeded = true;
60: }
61:
62: if (windowshackneeded) {
63: string pn = pagename.tolowercase();
64: for (int i = 0; i < windows_device_names.length; i++) {
65: if (windows_device_names[i].equals(pn)) {
66: pagename = "$$$" + pagename;
67: }
68: }
69: }
70:
71: log.debug("manglename()=" + pagename);
72: return pagename;
73: }
74:
75: /**
76: * convert string converted by <code>manglename()</code> to its orginal
77: * value.
78: *
79: * @param pagename
80: * @return
81: */
82: protected string unmanglename(string pagename) {
83: log.debug("unmanglename(" + pagename + ")");
84: boolean windowshackneeded = false;
85: string os = system.getproperty("os.name").tolowercase();
86:
87: if (os.startswith("windows") || os.equals("nt")) {
88: windowshackneeded = true;
89: }
90:
91: if (windowshackneeded && pagename.startswith("$$$")
92: && pagename.length() > 3) {
93: pagename = pagename.substring(3);
94: }
95:
96: log.debug("unmanglename()=" + pagename);
97: return textutil.decodefilename(pagename);
98: }
99:
100:}
101:
 
 
   1:/**
2: * created on 2005-9-11 by dengber.
3: */
4:package net.sf.startemplate;
5:
6:import org.apache.log4j.logger;
7:
8:import com.ecyrd.jspwiki.providers.basicattachmentprovider;
9:
10:/**
11: * a privider for jspwiki.
12: * this class behaves the same with <code>versioningfileprovider</code> provided
13: * by jspwiki,excepting it saves wiki pages into harddisk with the name inputted
14: * by the user. this is different with <code>versioningfileprovider</code>, it
15: * will save wiki pages with a encoded name, such as %e6%b5%8b%e8%af%95.
16: *
17: * @see net.sf.startemplate.nativefilenameversioningfileprovider
18: *
19: * @author dengber [email protected]
20: *
21: */
22:public class nativefilenameattachmentprovider extends basicattachmentprovider {
23: private static final logger log = logger.getlogger(nativefilenameattachmentprovider.class);
24:
25: /**
26: * convert string to the formatting that can be as file name.
27: *
28: * @param pagename
29: * @return
30: */
31: protected string manglename(string pagename) {
32: log.debug("manglename(" + pagename + "");
33: return textutil.encodefilename(pagename);
34: }
35:
36: /**
37: * convert string converted by <code>manglename()</code> to its orginal
38: * value.
39: *
40: * @param pagename
41: * @return
42: */
43: protected string unmanglename(string pagename) {
44: log.debug("unmanglename(" + pagename + "");
45: return textutil.decodefilename(pagename);
46: }
47:}
48:
 
 
   1:/**
2: * created on 2005-9-11 by dengber.
3: */
4:package net.sf.startemplate;
5:
6:import org.apache.log4j.logger;
7:
8:/**
9: * a utility class for string processing.
10: *
11: * @author dengber [email protected]
12: *
13: */
14:public class textutil {
15:
16: /**
17: * encode //:*?<>|% to %nn%nn formatting.
18: *
19: * @param filename
20: * @return
21: */
22: public static string encodefilename(string filename) {
23: stringbuffer result = new stringbuffer();
24: final string hex_digits = "0123456789abcdef";
25:
26: for (int i = 0; i < filename.length(); i++) {
27: char c = filename.charat(i);
28: switch (c) {
29: case '//':
30: case '/':
31: case ':':
32: case '*':
33: case '?':
34: case '/"':
35: case '<':
36: case '>':
37: case '|':
38: case '%':
39: result.append('%');
40: //the character above is all one-byte character,
41: //so we only need to encode the most right 8 bit.
42: result.append(hex_digits.charat((c & 0xf0) >> 4));
43: result.append(hex_digits.charat(c & 0x0f));
44: break;
45: default:
46: result.append(c);
47: }
48: }
49:
50: return result.tostring();
51: }
52:
53: /**
54: * decode %nn%nn formatting string to its orginal value .
55: *
56: * @param filename
57: * @return
58: */
59: public static string decodefilename(string filename) {
60: stringbuffer result = new stringbuffer();
61:
62: for (int i = 0; i < filename.length(); i++) {
63: char c = filename.charat(i);
64: switch (c) {
65: case '%':
66: char upper = filename.charat(++i);
67: char lower = filename.charat(++i);
68: char ch = (char) integer.parseint("" + upper + lower, 16);
69: result.append(ch);
70: break;
71:
72: default:
73: result.append(c);
74: break;
75: }
76: }
77:
78: return result.tostring();
79: }
80:
81:}
82:
 
 
 
======= 2005-09-07 22:26:40 您在来信中写道:=======
 
> dengber,您好!
>
> 我从sf.net上面下载了你的startemplate,看看不错。
>
> 我最近正在将jspwiki部署到我公司上,作为一种工作日志、工作总结、技术文章的共享平台。有个问题想问你一下,不知道你是
否了解,如何让jspwiki里面用中文写的超级链接,所形成的实际文件名,可以成为中文编码呢?因为目前都会变成%xx%xx的形式
来表示中文。
>
> 我知道jspwiki是利用java写的,它的中文处理机制,完全就是java的机制,就是将所有编码,统一成utf-8编码。而windows200
0或以上的系统的默认编码,一般是unicode,这两者不太一样。我不知道你试过直接编辑jspwiki的数据文件没,我试过,发现只
能利用ultraedit之类的可以支持utf-8的编辑器,才可以成功编写,而如果利用windows自带的notepad来编辑的话,其文件头部
会自动被加上3个字节。
>
> 另外,当在浏览器里面,鼠标指向一个含中文字符的链接的时候,会被自动转换成可能试url所要求的表示形式,就是上述的两个
加了百分号的数字来表示一个汉字的方式。
>
> 所以我想,要想解决这个问题,是不是需要解决上述两个问题。
>
> 不知道你是否研究过这个,请不吝赐教。  

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表