📄 utility.java
字号:
/**
* Copyright: Copyright (c) 2002-2003
* Company: JavaResearch(http://www.javaresearch.org)
*/
package org.jr.java2html;
import java.io.*;
import java.util.*;
import org.jr.swing.filter.*;
/**
* 本软件需要用到的工具方法。
* <br>最后更新日期:2003年5月22日
* @author cherami@javaresearch.org
* @version 0.8
*/
public class Utility {
/**
* 目录过滤器。
*/
static DirectoryFilter dirFilter = new DirectoryFilter();
/**
* java文件名过滤器。
*/
static FileTypeFilter fileFilter = new FileTypeFilter("java",
CombineFileFilter.IOLIST);
/**
* 解析目录时用于存放全部的类名的关键字。
*/
static final String ALL_CLASSES = " allclasses";
private static boolean debugable = true;
private Utility() {
}
/**
* 设置debug方法是否可以输出。
* @param debugable 是否可以输出调试信息
*/
public static void setDebugable(boolean debugable) {
Utility.debugable = debugable;
}
/**
* 输出调试信息。
* @param message 调试信息
*/
public static void debug(String message) {
if (debugable) {
System.out.println(message);
}
}
/**
* 输出调试对象的信息。
* @param message 调试对象
*/
public static void debug(Object message) {
if (debugable) {
System.out.println(message);
}
}
/**
* 判断一个字符串中的字母是否全部是大写。
* @param string 字符串
* @return 全部是大写(没有大小写差别的也算做大写)时返回true,否则返回false
*/
public static boolean isWholeUppercase(String string) {
if (string == null || string.length() == 0) {
return false;
}
String upperString = string.toUpperCase();
if (!upperString.equals(string)) {
return false;
}
return true;
}
/**
* 将资源束的内容转换为模版属性映射表。
* @param resource 资源束
* @return 转换得到的模版属性映射表
*/
public static TemplatePropertyMap convertTo(ResourceBundle resource) {
TemplatePropertyMap result = new TemplatePropertyMap();
Enumeration keys = resource.getKeys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
result.put(key, resource.getString(key));
}
return result;
}
/**
* 解析制定目录下的java类包的结构。
* @param pathName 目录名
* @return 解析得到的类包结构
*/
public static TreeMap analyseDirectory(String pathName) {
TreeMap packages = new TreeMap();
ArrayList allClasses = new ArrayList();
packages.put(ALL_CLASSES, allClasses);
File path = new File(pathName);
File[] files;
files = path.listFiles(fileFilter);
for (int i = 0; i < files.length; i++) {
allClasses.add(getNamePart(files[i].getName()));
}
files = path.listFiles(dirFilter);
for (int i = 0; i < files.length; i++) {
analyseDirectory("", files[i], packages);
}
return packages;
}
private static void analyseDirectory(String parentPackage, File path,
TreeMap packages) {
String packageName = "";
if (parentPackage.length() == 0) {
packageName = path.getName();
}
else {
packageName = parentPackage + "." + path.getName();
}
ArrayList allClasses = (ArrayList) packages.get(ALL_CLASSES);
ArrayList classes = new ArrayList();
File[] files;
files = path.listFiles(fileFilter);
if (files.length > 0) {
packages.put(packageName, classes);
for (int i = 0; i < files.length; i++) {
String fileName = files[i].getName();
classes.add(getNamePart(fileName));
String className = getNamePart(fileName);
if (packageName.length() == 0) {
allClasses.add(className);
}
else {
allClasses.add(packageName + "." + className);
}
}
}
files = path.listFiles(dirFilter);
for (int i = 0; i < files.length; i++) {
analyseDirectory(packageName, files[i], packages);
}
}
/**
* 得到文件的名称部分,实际上就是得到字符串中最后一个'.'号前的部分。
* @param name 全文件名
* @return 最后一个'.'号前的部分
*/
public static String getNamePart(String name) {
String result = name;
int lastIndex = result.lastIndexOf(".");
if (lastIndex > 0) {
return result.substring(0, lastIndex);
}
else {
return result;
}
}
/**
* 将指定字符串重复合并指定次数。
* @param str 原字符串
* @param times 重复次数
* @return 合并以后的字符串
*/
public static String fill(String str, int times) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < times; i++) {
buffer.append(str);
}
return buffer.toString();
}
/**
* 去掉字符串的最后一个字符。
* 如果字符串长度为0返回原字符串
* @param string 原字符串
* @return 截取以后的结果
*/
public static String trimLastChar(String string) {
if (string.length() == 0) {
return string;
}
else {
return string.substring(0, string.length() - 1);
}
}
/**
* 得到指定字符在字符串中出现的次数。
* @param source 字符串
* @param c 字符
* @return 出现的次数
*/
public static int getCharCount(String source, int c) {
if (source == null || source.length() == 0) {
return 0;
}
int count = 0;
int index = source.indexOf(c);
while (index > 0) {
count++;
index = source.indexOf(c, index + 1);
}
return count;
}
/**
* 读取文件内容并作为一个字符串返回。
* @param file 文件
* @return 文件内容
*/
public static String getFileContent(File file) {
BufferedReader reader = null;
StringBuffer content = new StringBuffer(1024);
try {
reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while (line != null) {
content.append(line);
line = reader.readLine();
}
return content.toString();
}
catch (IOException e) {
System.err.println(e.getMessage());
return "";
}
finally {
if (reader != null) {
try {
reader.close();
}
catch (IOException ioe) {
System.err.println(ioe.getMessage());
return content.toString();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -