1. /**
  2. * Copyright: Copyright (c) 2002-2004
  3. * Company: JavaResearch(http://www.javaresearch.org)
  4. */
  5. package org.jr.java2html;
  6. import java.io.File;
  7. import java.util.ArrayList;
  8. import java.util.Enumeration;
  9. import java.util.HashMap;
  10. import java.util.ResourceBundle;
  11. import java.util.TreeMap;
  12. import org.jr.swing.filter.CombineFileFilter;
  13. import org.jr.swing.filter.DirectoryFilter;
  14. import org.jr.swing.filter.FileTypeFilter;
  15. /**
  16. * 本软件需要用到的工具方法。
  17. * <br>最后更新日期:2004年8月31日
  18. * @author cherami@javaresearch.org
  19. * @version 0.9
  20. */
  21. public class Utility {
  22. /**
  23. * 目录过滤器。
  24. */
  25. static DirectoryFilter dirFilter = new DirectoryFilter();
  26. /**
  27. * java文件名过滤器。
  28. */
  29. static FileTypeFilter fileFilter = new FileTypeFilter("java",
  30. CombineFileFilter.IOLIST);
  31. /**
  32. * 解析目录时用于存放全部的类名的关键字。
  33. */
  34. static final String ALL_CLASSES = " allclasses";
  35. private static boolean debugable = true;
  36. private Utility() {
  37. }
  38. /**
  39. * 设置debug方法是否可以输出。
  40. * @param debugable 是否可以输出调试信息
  41. */
  42. public static void setDebugable(boolean debugable) {
  43. Utility.debugable = debugable;
  44. }
  45. /**
  46. * 输出调试信息。
  47. * @param message 调试信息
  48. */
  49. public static void debug(String message) {
  50. if (debugable) {
  51. System.out.println(message);
  52. }
  53. }
  54. /**
  55. * 输出调试对象的信息。
  56. * @param message 调试对象
  57. */
  58. public static void debug(Object message) {
  59. if (debugable) {
  60. System.out.println(message);
  61. }
  62. }
  63. /**
  64. * 判断一个字符串中的字母是否全部是大写。
  65. * @param string 字符串
  66. * @return 全部是大写(没有大小写差别的也算做大写)时返回true,否则返回false
  67. */
  68. public static boolean isWholeUppercase(String string) {
  69. if (string == null || string.length() == 0) {
  70. return false;
  71. }
  72. String upperString = string.toUpperCase();
  73. if (!upperString.equals(string)) {
  74. return false;
  75. }
  76. return true;
  77. }
  78. /**
  79. * 将资源束的内容转换为模版属性映射表。
  80. * @param resource 资源束
  81. * @return 转换得到的模版属性映射表
  82. */
  83. public static TemplatePropertyMap convertTo(ResourceBundle resource) {
  84. TemplatePropertyMap result = new TemplatePropertyMap();
  85. Enumeration keys = resource.getKeys();
  86. while (keys.hasMoreElements()) {
  87. String key = (String) keys.nextElement();
  88. result.put(key, resource.getString(key));
  89. }
  90. return result;
  91. }
  92. /**
  93. * 解析指定目录下的java类包的结构。
  94. * @param pathName 目录名
  95. * @param classMap 类名-HTML源代码路径映射
  96. * @return 解析得到的类包结构
  97. */
  98. public static TreeMap analyseDirectory(String pathName,HashMap classMap) {
  99. TreeMap packages = new TreeMap();
  100. ArrayList allClasses = new ArrayList();
  101. packages.put(ALL_CLASSES, allClasses);
  102. File path = new File(pathName);
  103. File[] files;
  104. files = path.listFiles(fileFilter);
  105. for (int i = 0; i < files.length; i++) {
  106. allClasses.add(getNamePart(files[i].getName()));
  107. }
  108. if (classMap!=null) {
  109. for (int i = 0; i < files.length; i++) {
  110. put(classMap,getNamePart(files[i].getName()),files[i].getName()+JavaToHTMLCreator.HTML_EXT);
  111. }
  112. }
  113. files = path.listFiles(dirFilter);
  114. for (int i = 0; i < files.length; i++) {
  115. analyseDirectory("", files[i], packages,classMap);
  116. }
  117. return packages;
  118. }
  119. /**
  120. * 解析制定目录下的java类包的结构。
  121. * @param pathName 目录名
  122. * @return 解析得到的类包结构
  123. */
  124. public static TreeMap analyseDirectory(String pathName) {
  125. return analyseDirectory(pathName,null);
  126. }
  127. private static void analyseDirectory(String parentPackage, File path,
  128. TreeMap packages,HashMap classMap) {
  129. String packageName = "";
  130. if (parentPackage.length() == 0) {
  131. packageName = path.getName();
  132. }
  133. else {
  134. packageName = parentPackage + "." + path.getName();
  135. }
  136. ArrayList allClasses = (ArrayList) packages.get(ALL_CLASSES);
  137. ArrayList classes = new ArrayList();
  138. File[] files;
  139. files = path.listFiles(fileFilter);
  140. if (files.length > 0) {
  141. packages.put(packageName, classes);
  142. for (int i = 0; i < files.length; i++) {
  143. String fileName = files[i].getName();
  144. classes.add(getNamePart(fileName));
  145. String className = getNamePart(fileName);
  146. if (packageName.length() == 0) {
  147. allClasses.add(className);
  148. }
  149. else {
  150. allClasses.add(packageName + "." + className);
  151. }
  152. }
  153. if (classMap!=null) {
  154. for (int i = 0; i < files.length; i++) {
  155. String className=getNamePart(files[i].getName());
  156. put(classMap,className,getPath(packageName)+"/"+className+JavaToHTMLCreator.HTML_EXT);
  157. }
  158. }
  159. }
  160. files = path.listFiles(dirFilter);
  161. for (int i = 0; i < files.length; i++) {
  162. analyseDirectory(packageName, files[i], packages,classMap);
  163. }
  164. }
  165. private static String getPath(String packageName) {
  166. return packageName.replace('.','/');
  167. }
  168. /**
  169. * 得到文件的名称部分,实际上就是得到字符串中最后一个'.'号前的部分。
  170. * @param name 全文件名
  171. * @return 最后一个'.'号前的部分
  172. */
  173. public static String getNamePart(String name) {
  174. String result = name;
  175. int lastIndex = result.lastIndexOf(".");
  176. if (lastIndex > 0) {
  177. return result.substring(0, lastIndex);
  178. }
  179. else {
  180. return result;
  181. }
  182. }
  183. /**
  184. * 将指定字符串重复合并指定次数。
  185. * @param str 原字符串
  186. * @param times 重复次数
  187. * @return 合并以后的字符串
  188. */
  189. public static String fill(String str, int times) {
  190. StringBuffer buffer = new StringBuffer();
  191. for (int i = 0; i < times; i++) {
  192. buffer.append(str);
  193. }
  194. return buffer.toString();
  195. }
  196. /**
  197. * 去掉字符串的最后一个字符。
  198. * 如果字符串长度为0返回原字符串
  199. * @param string 原字符串
  200. * @return 截取以后的结果
  201. */
  202. public static String trimLastChar(String string) {
  203. if (string.length() == 0) {
  204. return string;
  205. }
  206. else {
  207. return string.substring(0, string.length() - 1);
  208. }
  209. }
  210. /**
  211. * 将指定的类名和包路径添加到类映射中。
  212. * 如果映射中不存在类名的映射则直接添加,
  213. * 如果存在并且只有一个,则将路径转换为ArrayList后添加
  214. * 如果存在多个则添加到ArrayList中。
  215. * @param classMap
  216. * @param className
  217. * @param path
  218. */
  219. public static void put(HashMap classMap,String className,String path) {
  220. if (classMap.containsKey(className)) {
  221. Object oldPath=classMap.get(className);
  222. if (oldPath instanceof String) {
  223. ArrayList pathList=new ArrayList();
  224. pathList.add(oldPath);
  225. pathList.add(path);
  226. classMap.put(className,pathList);
  227. } else {
  228. ((ArrayList)oldPath).add(path);
  229. }
  230. } else {
  231. classMap.put(className,path);
  232. }
  233. }
  234. }