JSPWiki文件名转换问题

80酷酷网    80kuku.com

  js|文件名|问题|转换 这几天研究了一下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 dengbergmail.com
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 dengbergmail.com
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 dengbergmail.com
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编码。而windows2000或以上的系统的默认编码,一般是unicode,这两者不太一样。我不知道你试过直接编辑jspwiki的数据文件没,我试过,发现只能利用UltraEdit之类的可以支持UTF-8的编辑器,才可以成功编写,而如果利用windows自带的notepad来编辑的话,其文件头部会自动被加上3个字节。> > 另外,当在浏览器里面,鼠标指向一个含中文字符的链接的时候,会被自动转换成可能试URL所要求的表示形式,就是上述的两个加了百分号的数字来表示一个汉字的方式。> > 所以我想,要想解决这个问题,是不是需要解决上述两个问题。> > 不知道你是否研究过这个,请不吝赐教。  


分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: