存档

文章标签 ‘java’

Java获得汉字的首写字母

2009年7月25日 root 2 条评论


package cn.showtrue.tools;

public class Letters {
//字母Z使用了两个标签,这里有27个值
//i, u, v都不做声母, 跟随前面的字母
private static char[] chartable =
{
'啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈',
'击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然',
'撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', '座'
};

private static char[] alphatable =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};

private static int[] table = new int[27];

//主函数,输入字符,得到他的声母,
//英文字母返回对应的大写字母
//其他非简体汉字返回 '0'

public static char Char2Alpha(char ch){
if (ch >= 'a' && ch <= 'z')
return (char) (ch - 'a' + 'A');
if (ch >= 'A' && ch <= 'Z')
return ch;
int gb = gbValue(ch);
if (gb < table[0])
return '0';
int i;
for (i = 0; i < 26; ++i){
if (match(i, gb)) break;
}
if (i >= 26)
return '0';
else
return alphatable[i];
}

//根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串
public static String getFirstLetter(String SourceStr){
for (int i = 0; i < 27; ++i) {
table[i] = gbValue(chartable[i]);
}
String Result = "";
int StrLength = SourceStr.length();
int i;
try{
for (i = 0; i < StrLength; i++){
Result += Char2Alpha(SourceStr.charAt(i));
}
} catch (Exception e){
Result = "";
}
return Result;
}

private static boolean match(int i, int gb){
if (gb < table[i])
return false;
int j = i + 1;
//字母Z使用了两个标签
while (j < 26 && (table[j] == table[i])) ++j;
if (j == 26)
return gb <= table[j];
else
return gb < table[j];
}

//取出汉字的编码
private static int gbValue(char ch){
String str = new String();
str += ch;
try{
byte[] bytes = str.getBytes("GB2312");
if (bytes.length < 2)
return 0;
return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff);
} catch (Exception e){
return 0;
}
}

public static void main(String[] args){
System.out.println(Letters.getFirstLetter("简单"));
return;
}
}

经测试,对简体中文有效,繁体就会出问题。

分类: 程序设计 标签:

FCKeditor 2.4.1 Java压缩版(上传图片自动重命名)

2009年6月22日 root 没有评论

使用FCKeditor java版本的演示程序的时候上传文件名不自动重新命名,如果是中文的话会出现一点小问题。于是从网上搜索自动重新命名的版本,始终没找到!无奈自己编译了下原文件,把设置成了文件上传后重新自动命名。文件名是根据当时时间命名,用的是Data 的getTime()方法!
改变文件默认上传路径是修改fckeditor.properties 添加默认上传路径和上传文件类型的定义。fckeditor.properties的修改参照net.fckeditor.handlers.default.properties文件中的定义修改,fckeditor.properties的配置优先级别高于default.properties中的设置。default.properties中的默认配置如:

#
# FCKeditor – The text editor for Internet – http://www.fckeditor.net
# Copyright (C) 2003-2008 Frederico Caldeira Knabben
#
# == BEGIN LICENSE ==
#
# Licensed under the terms of any of the following licenses at your
# choice:
#
# – GNU General Public License Version 2 or later (the “GPL”)
# http://www.gnu.org/licenses/gpl.html
#
# – GNU Lesser General Public License Version 2.1 or later (the “LGPL”)
# http://www.gnu.org/licenses/lgpl.html
#
# – Mozilla Public License Version 1.1 or later (the “MPL”)
# http://www.mozilla.org/MPL/MPL-1.1.html
#
# == END LICENSE ==
#
# Default properties for FCKeditor.Java
#
# @version $Id: default.properties 1632 2008-02-25 18:11:06Z th-schwarz $

# default allowed extensions settings
connector.resourceType.file.extensions.allowed = 7z|aiff|asf|avi|bmp|csv|doc|fla|flv|gif|gz|gzip|jpeg|jpg|mid|mov|mp3|mp4|mpc|mpeg|mpg|ods|odt|pdf|png|ppt|pxd|qt|ram|rar|rm|rmi|rmvb|rtf|sdc|sitd|swf|sxc|sxw|tar|tgz|tif|tiff|txt|vsd|wav|wma|wmv|xls|xml|zip
connector.resourceType.image.extensions.allowed = bmp|gif|jpeg|jpg|png
connector.resourceType.flash.extensions.allowed = swf|fla
connector.resourceType.media.extensions.allowed = aiff|asf|avi|bmp|fla|flv|gif|jpeg|jpg|mid|mov|mp3|mp4|mpc|mpeg|mpg|png|qt|ram|rm|rmi|rmvb|swf|tif|tiff|wav|wma|wmv

# default resource type paths
connector.resourceType.file.path = /file
connector.resourceType.image.path = /image
connector.resourceType.flash.path = /flash
connector.resourceType.media.path = /media

# Due to security issues with Apache modules, it is recommended to leave this
# setting enabled.
connector.forceSingleExtension = true

# base directory for the user files relative to the context root
connector.userFilesPath = /userfiles

# Instructs the Connector to return the full URL of a file/folder in the XML
# response rather than the absolute URL
connector.fullUrl = false

# Instructs the Connector to check, if the uploaded image is really one
connector.secureImageUploads = true

# directory of the editor relative to the context root
fckeditor.basePath = /fckeditor

# default height of the editor
fckeditor.height = 200

# default toolbar set of the editor
fckeditor.toolbarSet = Default

# default width of the editor
fckeditor.width = 100%

# some messages
message.enabled_tag.compatible_browser.no = Your browser is not compatible
message.enabled_tag.compatible_browser.yes = Your browser is fully compatible
message.enabled_tag.connector.file_browsing.disabled = The Connector is disabled for FileBrowsing
message.enabled_tag.connector.file_browsing.enabled = The Connector is enabled for FileBrowsing
message.enabled_tag.connector.file_upload.disabled = The Connector is disabled for FileUpload
message.enabled_tag.connector.file_upload.enabled = The Connector is enabled for FileUpload

文件默认上传路径修改:connector.userFilesPath = /userfiles

上传文件自动重命名(含精简了的编辑器):fckeditor 2.4.1

FCKeditor 2.4.1 演示程序下载:http://www.6tbl.com/post/155/

jassistant 1.06 JDBDevelop Assistant数据库生成java程序

2009年6月22日 root 没有评论

The assistant is a class and a package browser. It is divided into two parts: the main class browser and the package viewer. A third options window may be opened to control the program behavior. The main goal of this program is to be able to find very quickly a class, its source, its methods, fields, constructors… and to be able to explore the packages. Completion, and class homonyms may be enabled. Concerning the package viewer, well, you have the package hierarchy and the class hierarchy inside a selected package, which is the inheritance hierarchy inside a package. The classes which appear with an icon marked “i” are interface classes and the ones with an “a” are abstract classes. The options window allows to put filters for the lists which are given: definition with long or short names, filter on the scope (public, package, protected, private), filter on the modifiers (native, abstract, static…), the order for the search of the sources (java, jj, decompile or disassemble), sorting options and an uptdate package button to reread all your packages. The assistant was swing free. At the beginning I was happy to use swing, which is very nice, and that’s why I have a swing package too. But as time passed I saw that swing had bugs and that it was not usable in a real program because it is too slow! This is the reason why I have my own tree classes in pure AWT java! It seems now that swing has been improved, specially 1.1.1, so… I came back to it for the code viewer. This explains that the code is in majority pure AWT, and the viewer in Swing.
assist

绿色版本下载:jassistant(RAR)

分类: 程序设计 标签:

Java清除HTML格式函数

2009年4月15日 root 没有评论
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
/**
* 过滤HTML 标签
* @param inputString
* @return
*/
public static String Html2Text(String inputString) {
String htmlStr = inputString; //含html标签的字符串
String textStr ="";
Pattern p_script;
Matcher m_script;
Pattern p_style;
Matcher m_style;
Pattern p_html;
Matcher m_html;
try{
String regEx_script = "&lt;[\\s]*?script[^&gt;]*?&gt;[\\s\\S]*?&lt;[\\s]*?\\/[\\s]*?script[\\s]*?&gt;"; //定义script的正则表达式{或]*?&gt;[\\s\\S]*?&lt;\\/script&gt; }
String regEx_style = "&lt;[\\s]*?style[^&gt;]*?&gt;[\\s\\S]*?&lt;[\\s]*?\\/[\\s]*?style[\\s]*?&gt;"; //定义style的正则表达式{或]*?&gt;[\\s\\S]*?&lt;\\/style&gt; }
String regEx_html = "&lt;[^&gt;]+&gt;"; //定义HTML标签的正则表达式
p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); //过滤script标签
 
p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); //过滤style标签
 
p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); //过滤html标签
 
textStr = htmlStr;
 
}catch(Exception e) {
log.info(e);
}
return textStr;//返回文本字符串
}
分类: 网站设计 标签: ,