JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:有關zhcode繁簡中文轉碼工具套件的使用問題 [Re:liminjun]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2003-11-16 20:15 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
以下是我測試用的servlet,使用zhcode類別,可以正確編譯成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
Sample359.java
 
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.*;
import java.util.*;
 
class Encoding {
    // Supported Encoding Types
    public static int GB2312        = 0;
    public static int GBK           = 1;
    public static int HZ            = 2;
    public static int BIG5          = 3;
    public static int CNS11643      = 4;
    public static int UTF8          = 5;
    public static int UNICODE       = 6;
    public static int UNICODET      = 7;
    public static int UNICODES      = 8;
    public static int ISO2022CN     = 9;
    public static int ISO2022CN_CNS = 10;
    public static int ISO2022CN_GB  = 11;
 
    public static int ASCII         = 12;
    public static int OTHER         = 13;
 
    public static int TOTALTYPES    = 14;
 
    // Names of the encodings as understood by Java
    public static String[] javaname;
    // Names of the encodings for human viewing
    public static String[] nicename;
    // Names of charsets as used in charset parameter of HTML Meta tag
    public static String[] htmlname;
 
    // Constructor
    public Encoding() {
  javaname = new String[TOTALTYPES];
  nicename = new String[TOTALTYPES];
  htmlname = new String[TOTALTYPES];
 
  // Assign encoding names
  javaname[GB2312] = "GB2312";
  javaname[HZ] = "ASCII";  // What to put here?  Sun doesn't support HZ
  javaname[GBK] = "GBK";
  javaname[ISO2022CN_GB] = "ISO2022CN_GB";
  javaname[BIG5] = "BIG5";
  javaname[CNS11643] = "EUC-TW";
  javaname[ISO2022CN_CNS] = "ISO2022CN_CNS";
  javaname[ISO2022CN] = "ISO2022CN";
  javaname[UTF8] = "UTF8";
  javaname[UNICODE] = "Unicode";
  javaname[UNICODET] = "Unicode";
  javaname[UNICODES] = "Unicode";
  javaname[ASCII] = "ASCII";
  javaname[OTHER] = "ISO8859_1";
 
  // Assign encoding names
  htmlname[GB2312] = "GB2312";
  htmlname[HZ] = "HZ-GB-2312";  
  htmlname[GBK] = "GB2312";
  htmlname[ISO2022CN_GB] = "ISO-2022-CN-EXT";
  htmlname[BIG5] = "BIG5";
  htmlname[CNS11643] = "EUC-TW";
  htmlname[ISO2022CN_CNS] = "ISO-2022-CN-EXT";
  htmlname[ISO2022CN] = "ISO-2022-CN";
  htmlname[UTF8] = "UTF-8";
  htmlname[UNICODE] = "UTF-16";
  htmlname[UNICODET] = "UTF-16";
  htmlname[UNICODES] = "UTF-16";
  htmlname[ASCII] = "ASCII";
  htmlname[OTHER] = "ISO8859-1";
 
  // Assign Human readable names
  nicename[GB2312] = "GB-2312";
  nicename[HZ] = "HZ";
  nicename[GBK] = "GBK";
  nicename[ISO2022CN_GB] = "ISO2022CN-GB";
  nicename[BIG5] = "Big5";
  nicename[CNS11643] = "CNS11643";
  nicename[ISO2022CN_CNS] = "ISO2022CN-CNS";
  nicename[ISO2022CN] = "ISO2022 CN";
  nicename[UTF8] = "UTF-8";
  nicename[UNICODE] = "Unicode";
  nicename[UNICODET] = "Unicode (Trad)";
  nicename[UNICODES] = "Unicode (Simp)";
  nicename[ASCII] = "ASCII";
  nicename[OTHER] = "OTHER";
 
    }
 
    /*    System.out.println("Usage:  java zhcode -[gbc8ui2nk][gbc8uts2nk] in_file out_file");
     System.out.println("  g = GB2312, b = Big5, c = CNS11643, 8 = UTF-8, u = Unicode,");
     System.out.println("  t = Unicode (traditional characters),");
    System.out.println("  s = Unicode (simplified characters),");
     System.out.println("  i = ISO-2022-CN, 2 = ISO-2022-CN-GB, n = ISO-2022-CN-CNS,");
     System.out.println("  k = GBK");
    */
    
}
 
class zhcode extends Encoding {
    // Simplfied/Traditional character equivalence hashes
    protected Hashtable s2thash, t2shash;
 
    // Constructor
    public zhcode() {
  super();
  String dataline;
 
  // Initialize and load in the simplified/traditional character hashses
  s2thash = new Hashtable();
  t2shash = new Hashtable();
 
  try {
      InputStream pydata = getClass().getResourceAsStream("hcutf8.txt");
      BufferedReader in = new BufferedReader(new InputStreamReader(pydata, "UTF8"));
      while ((dataline = in.readLine()) != null) {
    // Skip empty and commented lines
    if (dataline.length() == 0 || dataline.charAt(0) == '#') {
        continue;
    }
    
    // Simplified to Traditional, (one to many, but pick only one)
    s2thash.put(dataline.substring(0,1).intern(), dataline.substring(1,2));
 
    // Traditional to Simplified, (many to one)
    for (int i = 1; i < dataline.length(); i++) {
        t2shash.put(dataline.substring(i,i+1).intern(), dataline.substring(0,1));
    }
      }
  }
  catch (Exception e) {
      System.err.println( e );
  }
 
    }
 
    public String convertString(String dataline, int source_encoding, int target_encoding) {
  StringBuffer outline = new StringBuffer();
  int lineindex;
  
  if (source_encoding == HZ) {
      dataline = hz2gb(dataline);
  }
  for (lineindex = 0; lineindex < dataline.length(); lineindex++) {
      if ((source_encoding == GB2312 || source_encoding == GBK || source_encoding == ISO2022CN_GB ||
     source_encoding == HZ || 
     source_encoding == UNICODE || source_encoding == UNICODES || source_encoding == UTF8) 
    &&
    (target_encoding == BIG5 || target_encoding == CNS11643 || target_encoding == UNICODET ||
     target_encoding == ISO2022CN_CNS)) {
    if (s2thash.containsKey(dataline.substring(lineindex, lineindex+1)) == true) {
        outline.append(s2thash.get(dataline.substring(lineindex, lineindex+1).intern()));
    } else {
        outline.append(dataline.substring(lineindex, lineindex+1));
    }
      } else if ((source_encoding == BIG5 || source_encoding == CNS11643 || source_encoding == UNICODET ||
      source_encoding == UTF8 ||
      source_encoding == ISO2022CN_CNS || source_encoding == GBK || source_encoding == UNICODE) 
           &&
           (target_encoding == GB2312 || target_encoding == UNICODES || target_encoding == ISO2022CN_GB ||
      target_encoding == HZ)) {
    if (t2shash.containsKey(dataline.substring(lineindex, lineindex+1)) == true) {
        outline.append(t2shash.get(dataline.substring(lineindex, lineindex+1).intern()));
    } else {
        outline.append(dataline.substring(lineindex, lineindex+1));
    }
      } else {
    outline.append(dataline.substring(lineindex, lineindex+1));
      }
  }
 
  if (target_encoding == HZ) {
      // Convert to look like HZ
      return gb2hz(outline.toString());
  }
 
  return outline.toString();
    }
 
    public String hz2gb(String hzstring) {
  byte[] hzbytes = new byte[2];
  byte[] gbchar = new byte[2];
  int byteindex = 0;
  StringBuffer gbstring = new StringBuffer("");
 
  try {
      hzbytes = hzstring.getBytes("8859_1");
  } 
  catch (Exception usee) { System.err.println("Exception " + usee.toString()); return hzstring; } 
 
  // Convert to look like equivalent Unicode of GB
  for (byteindex = 0; byteindex < hzbytes.length; byteindex++) {
      if (hzbytes[byteindex] == 0x7e) {
    if (hzbytes[byteindex+1] == 0x7b) {
        byteindex+=2;
        while (byteindex < hzbytes.length) {
      if (hzbytes[byteindex] == 0x7e && hzbytes[byteindex+1] == 0x7d) {
          byteindex++;
          break;
      } else if (hzbytes[byteindex] == 0x0a || hzbytes[byteindex] == 0x0d) {
          gbstring.append((char)hzbytes[byteindex]);
          break;
      }
      gbchar[0] = (byte)(hzbytes[byteindex] + 0x80);
      gbchar[1] = (byte)(hzbytes[byteindex+1] + 0x80);
      try {
          gbstring.append(new String(gbchar, "GB2312"));
      }  catch (Exception usee) { System.err.println("Exception " + usee.toString()); } 
      byteindex+=2;
        } 
    } else if (hzbytes[byteindex+1] == 0x7e) { // ~~ becomes ~
        gbstring.append('~');
    } else {  // false alarm
        gbstring.append((char)hzbytes[byteindex]);  
    }
      } else {
    gbstring.append((char)hzbytes[byteindex]);
      }
  }
  return gbstring.toString();
    }
 
    public String gb2hz(String gbstring) {
  StringBuffer hzbuffer;
  byte[] gbbytes = new byte[2];
  int i;
  boolean terminated = false;
 
  hzbuffer = new StringBuffer("");
  try {
      gbbytes = gbstring.getBytes("GB2312");
  }
  catch (Exception usee) { System.err.println(usee.toString()); return gbstring; }
 
  for (i = 0; i < gbbytes.length; i++) {
      if (gbbytes[i] < 0) {
    hzbuffer.append("~{");
    terminated = false;
    while (i < gbbytes.length) {
        if (gbbytes[i] == 0x0a || gbbytes[i] == 0x0d) {
      hzbuffer.append("~}" + (char)gbbytes[i]);
      terminated = true;
      break;
        } else if (gbbytes[i] >= 0) {
      hzbuffer.append("~}" + (char)gbbytes[i]);
      terminated = true;
      break;
        }
        hzbuffer.append((char)(gbbytes[i] + 256 - 0x80));
        hzbuffer.append((char)(gbbytes[i+1] + 256 - 0x80));
        i+=2;        
    }
    if (terminated == false) {
        hzbuffer.append("~}");
    }
      } else {
    if (gbbytes[i] == 0x7e) {
        hzbuffer.append("~~");
    } else {
        hzbuffer.append((char)gbbytes[i]);
    }
      }
  }
  return new String(hzbuffer);
    }
 
}
 
public class Sample359 extends HttpServlet {
  public void doGet(HttpServletRequest req,HttpServletResponse res)
  throws IOException, ServletException {
    res.setContentType("text/html; charset=big5");
    PrintWriter out = res.getWriter();
    
    
    String word = "繁體中文轉簡體中文測試";
    zhcode mycode = new zhcode();
    String Big5ToGb = mycode.convertString(word,mycode.BIG5,mycode.GB2312);
    
    
    
    out.println("<html><head>");
    out.println("<title>Servlet!</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("word =>"+word);
    out.println("<br>");
    out.println("convert word =>"+Big5ToGb);
    out.println("<br>");
    out.println("<h1>Java Servlet!</h1>");
    out.println("</body></html>");
    
 
    /*
    zhcode mycode=new zhcode(); 
    //繁體轉簡體 
    String Big5ToGb=mycode.convertString("繁體轉簡體",mycode.BIG5,mycode.GB2312); 
    */
 
  }
}


但是放在伺服器之後,開啟IE卻出現以下的錯誤訊息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
HTTP Status 500 - 
 
--------------------------------------------------------------------------------
 
type Exception report
 
message 
 
description The server encountered an internal error () that prevented it from fulfilling this request.
 
exception 
 
javax.servlet.ServletException: Invoker service() exception
  org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:524)
  org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
 
root cause 
 
java.lang.NoClassDefFoundError: zhcode  <<Sample359.java已經有包含這個CLASS為什麼還會有這種錯誤訊息?
  Sample359.doGet(Sample359.java:292)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
  org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:466)
  org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
 
note The full stack trace of the root cause is available in the Tomcat logs.
 
--------------------------------------------------------------------------------
 
Apache Tomcat/5.0.14


請問這是什麼原因造成的?該如何解決?
如果把紅色部分的程式碼去掉可以正常執行,所以問題可能是出在zhcode
but 是什麼問題呢?
如果把servlet部分去掉改寫成public static void main(String argc[]) {...}這種client的JAVA程式又可以正常執行...

真的頭暈了 不知道該怎麼處理
麻煩有使用過zhcode繁簡轉碼的大大分享一下使用經驗和方法

以上Sample359.java程式碼可以正確編譯,附加一個hcutf8.txt繁簡對照表也要下載
我使用WINXP+TOMCAT5


hcutf8.txt (53.43k)


liminjun edited on 2003-11-16 20:27
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
17438 [精華] 有關zhcode繁簡中文轉碼工具套件的使用問題 liminjun 876 2003-11-16 15:19
15108 Re:有關zhcode繁簡中文轉碼工具套件的使用問題 liminjun 15422 2003-11-16 20:15
12330 Re:有關zhcode繁簡中文轉碼工具套件的使用問題 Duncan 59 2003-11-16 21:38
12266 Re:有關zhcode繁簡中文轉碼工具套件的使用問題 liminjun 270 2003-11-16 22:46
12055 Re:有關zhcode繁簡中文轉碼工具套件的使用問題 Duncan 261 2003-11-16 22:53
12188 Re:有關zhcode繁簡中文轉碼工具套件的使用問題 liminjun 327 2003-11-16 23:08
12562 Re:有關zhcode繁簡中文轉碼工具套件的使用問題 liminjun 1507 2003-11-17 08:09
13537 Re:有關zhcode繁簡中文轉碼工具套件的使用問題 chuang5089 333 2004-02-11 21:30
11989 Re:有關zhcode繁簡中文轉碼工具套件的使用問題 liminjun 430 2004-02-11 21:38
11826 Re:有關zhcode繁簡中文轉碼工具套件的使用問題 chuang5089 857 2004-02-12 21:57
12400 Re:有關zhcode繁簡中文轉碼工具套件的使用問題 liminjun 288 2004-02-13 00:52
7194 Re:有關zhcode繁簡中文轉碼工具套件的使用問題 ying0515 105 2008-02-21 18:27
7527 Re:有關zhcode繁簡中文轉碼工具套件的使用問題 ying0515 4 2008-02-22 15:26
» JWorld@TW »  Java SE 討論區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8