- /**
- * Copyright: Copyright (c) 2002-2004
- * Company: JavaResearch(http://www.javaresearch.org)
- */
- package org.jr.java2html;
-
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.HashMap;
- import java.util.ResourceBundle;
- import java.util.TreeMap;
-
- import org.jr.swing.filter.CombineFileFilter;
- import org.jr.swing.filter.DirectoryFilter;
- import org.jr.swing.filter.FileTypeFilter;
-
-
- /**
- * 本软件需要用到的工具方法。
- * <br>最后更新日期:2004年8月31日
- * @author cherami@javaresearch.org
- * @version 0.9
- */
-
- 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 目录名
- * @param classMap 类名-HTML源代码路径映射
- * @return 解析得到的类包结构
- */
- public static TreeMap analyseDirectory(String pathName,HashMap classMap) {
- 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()));
- }
- if (classMap!=null) {
- for (int i = 0; i < files.length; i++) {
- put(classMap,getNamePart(files[i].getName()),files[i].getName()+JavaToHTMLCreator.HTML_EXT);
- }
- }
- files = path.listFiles(dirFilter);
- for (int i = 0; i < files.length; i++) {
- analyseDirectory("", files[i], packages,classMap);
- }
- return packages;
- }
-
-
- /**
- * 解析制定目录下的java类包的结构。
- * @param pathName 目录名
- * @return 解析得到的类包结构
- */
- public static TreeMap analyseDirectory(String pathName) {
- return analyseDirectory(pathName,null);
- }
-
- private static void analyseDirectory(String parentPackage, File path,
- TreeMap packages,HashMap classMap) {
- 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);
- }
- }
- if (classMap!=null) {
- for (int i = 0; i < files.length; i++) {
- String className=getNamePart(files[i].getName());
- put(classMap,className,getPath(packageName)+"/"+className+JavaToHTMLCreator.HTML_EXT);
- }
- }
- }
- files = path.listFiles(dirFilter);
- for (int i = 0; i < files.length; i++) {
- analyseDirectory(packageName, files[i], packages,classMap);
- }
- }
-
- private static String getPath(String packageName) {
- return packageName.replace('.','/');
- }
-
-
- /**
- * 得到文件的名称部分,实际上就是得到字符串中最后一个'.'号前的部分。
- * @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);
- }
- }
-
- /**
- * 将指定的类名和包路径添加到类映射中。
- * 如果映射中不存在类名的映射则直接添加,
- * 如果存在并且只有一个,则将路径转换为ArrayList后添加
- * 如果存在多个则添加到ArrayList中。
- * @param classMap
- * @param className
- * @param path
- */
- public static void put(HashMap classMap,String className,String path) {
- if (classMap.containsKey(className)) {
- Object oldPath=classMap.get(className);
- if (oldPath instanceof String) {
- ArrayList pathList=new ArrayList();
- pathList.add(oldPath);
- pathList.add(path);
- classMap.put(className,pathList);
- } else {
- ((ArrayList)oldPath).add(path);
- }
-
- } else {
- classMap.put(className,path);
- }
- }
-
- }