- /**
- * Copyright: Copyright (c) 2002-2004
- * Company: JavaResearch(http://www.javaresearch.org)
- */
-
- package org.jr.java2html;
-
-
-
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.net.URL;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.ArrayList;
-
- import org.jr.util.StringUtil;
-
- /**
- * 根据类名得到相应的链接的工具类。 根据以下的规则得到类名是否有对应的源代码:
- * <ol>
- * <li>如果类名前有包名,则根据这个全限定名进行匹配
- * <li>如果导入语句中有以该类名结尾的导入语句,则以该导入声明的全限定名进行匹配
- * <li>如果导入语句中有带*号的通用导入,则以该包加类名逐个进行匹配
- * <li>根据类文件所在的路径加类名进行匹配
- * <li>以java.lang作为包名进行匹配
- * </ol>
- * 最后更新日期:2004年11月30日
- * @author cherami@javaresearch.org
- * @version 0.9
- */
- public class ClassLink {
- HashMap linkMap;
-
- /**
- * 构造一个空的ClassLink对象。
- */
- public ClassLink() {
- }
-
- /**
- * 以指定参数构造一个ClassLink对象。
- * @param linkMap 类名HashMap和URL映射的集合
- */
- public ClassLink(HashMap linkMap) {
- this.linkMap = linkMap;
- }
-
- /**
- * 将指定的类名HashMap和链接添加到映射中。
- * @param classMap 类名HashMap
- * @param link 类的URL
- */
- public void put(HashMap classMap, String link) {
- if (linkMap == null) {
- linkMap = new HashMap();
- }
- linkMap.put(classMap, link);
- }
-
- /**
- * 将指定的ClassLink对象中的所有映射添加到当前的映射中。
- * @param classLink ClassLink对象
- */
- public void putAll(ClassLink classLink) {
- if (linkMap == null) {
- linkMap = new HashMap();
- }
- linkMap.putAll(classLink.linkMap);
- }
-
- /**
- * 根据指定的类名从当前映射中取得类名对应的URL。
- * @param className 当前解析的类名
- * @param packagePath 当前解析的文件的包名
- * @param classPackage 当前解析的类的限定包名
- * @param importedClasses 当前解析的文件的导入声明
- * @return 类名对应的URL,如果不存在返回null。
- */
- public String getLink(String className, String packagePath,
- String classPackage, ArrayList importedClasses) {
- if (linkMap == null || linkMap.size() == 0) {
- return null;
- }
- Iterator classMaps = linkMap.keySet().iterator();
- while (classMaps.hasNext()) {
- HashMap classMap = (HashMap) classMaps.next();
- Object path = classMap.get(className);
- if (path != null) {
- String root = (String) linkMap.get(classMap);
- String classPath = getPath(path, className, packagePath, classPackage,
- importedClasses);
- if (classPath == null) {
- return null;
- } else {
- return getRoot(root, packagePath) + classPath;
- }
- }
- }
- return null;
- }
-
- /**
- * 根据指定的根路径和包路径得到实际的根路径。
- * 如果指定的根路径是空字符串说明是当前转换的项目的类,实际的路径将是根据指定的包路径得到的相对路径;
- * 如果指定的根路径不是以http://或者file:///开头的绝对URL路径说明是本地相对路径,实际的路径将是根据指定的包路径得到的相对路径加指定的根路径;
- * 如果是其它则说明是绝对的URL路径,直接返回指定的根路径。
- * @param root 根路径
- * @param packagePath 当前解析的文件的包路径
- * @return 实际的根路径。
- */
- private String getRoot(String root, String packagePath) {
- if (root.equals("")) {
- if (packagePath != null) {
- int count = StringUtil.getSubtringCount(packagePath, "/");
- if (packagePath.length() > 0) {
- count++;
- }
- if (count > 0) {
- return Utility.fill("../", count);
- }
- return "";
- } else {
- return root;
- }
- } else if (!root.startsWith("http://") && !root.startsWith("file:///")) {
- if (packagePath != null) {
- int count = StringUtil.getSubtringCount(packagePath, "/");
- if (packagePath.length() > 0) {
- count++;
- }
- if (count > 0) {
- return Utility.fill("../", count) + root;
- }
- return root;
- } else {
- return root;
- }
- } else {
- return root;
- }
- }
-
- /**
- * 根据直接取得的可能路径对象得到完全匹配的类路径。
- * @param path 可能路径,String类型或者ArrayList类型
- * @param className 当前解析的类名
- * @param packagePath 当前解析的文件的包名
- * @param classPackage 当前解析的类的限定包名
- * @param importedClasses 当前解析的文件的导入声明
- * @return 完全匹配的类路径。
- */
- private String getPath(Object path, String className, String packagePath,
- String classPackage, ArrayList importedClasses) {
- //如果只有一个匹配的路径
- if (path instanceof String) {
- //如果类名前有限定包名则比较得到的路径是否是以该限定包名开头的,是则返回该路径,否则返回null
- if (classPackage != null) {
- if (((String) path).startsWith(classPackage)) {
- return ((String) path);
- }
- return null;
- }
- //如果文件中有包导入声明
- if (importedClasses.size() > 0) {
- //取完全匹配类名的导入声明和路径中完全匹配的
- for (int i = 0; i < importedClasses.size(); i++) {
- String currentPackage = (String) importedClasses.get(i);
- if (currentPackage.endsWith("/" + className)) {
- if (((String) path).startsWith(currentPackage)) {
- return ((String) path);
- }
- break;
- }
- }
- //取通配形式的导入声明和路径中完全匹配的
- for (int i = 0; i < importedClasses.size(); i++) {
- String currentPackage = (String) importedClasses.get(i);
- if (currentPackage.endsWith("/")) {
- String fullName = currentPackage + className;
- if (((String) path).startsWith(fullName)) {
- return ((String) path);
- }
- }
- }
- }
- //当前文件的包名加类名是否匹配
- if (packagePath != null) {
- if (((String) path).startsWith(packagePath)) {
- return ((String) path);
- }
- }
- //java中默认导入的包java/lang/是否匹配
- if (((String) path).startsWith("java/lang/")) {
- return ((String) path);
- }
- //没有匹配
- return null;
- } else {//有多个可能的匹配路径
- ArrayList pathList = (ArrayList) path;
- //如果类名前有限定包名则查找是否有完全匹配的
- if (classPackage != null) {
- String fullName = classPackage + className;
- for (int j = 0; j < pathList.size(); j++) {
- String currentPath = (String) pathList.get(j);
- if (currentPath.startsWith(fullName)) {
- return currentPath;
- }
- }
- return null;
- }
- //如果有导入声明
- if (importedClasses.size() > 0) {
- //查找导入声明中可以完全匹配的
- for (int i = 0; i < importedClasses.size(); i++) {
- String currentPackage = (String) importedClasses.get(i);
- if (currentPackage.endsWith("/" + className)) {
- for (int j = 0; j < pathList.size(); j++) {
- String currentPath = (String) pathList.get(j);
- if (currentPath.startsWith(currentPackage)) {
- return currentPath;
- }
- }
- break;
- }
- }
- //查找导入声明中通配中可以完全匹配的
- for (int i = 0; i < importedClasses.size(); i++) {
- String currentPackage = (String) importedClasses.get(i);
- if (currentPackage.endsWith("/")) {
- String fullName = currentPackage + className;
- for (int j = 0; j < pathList.size(); j++) {
- String currentPath = (String) pathList.get(j);
- if (currentPath.startsWith(fullName)) {
- return currentPath;
- }
- }
- }
- }
- }
- //用文件包名加类名进行完全匹配
- String fullName = packagePath + "/" + className;
- for (int j = 0; j < pathList.size(); j++) {
- String currentPath = (String) pathList.get(j);
- if (currentPath.startsWith(fullName)) {
- return currentPath;
- }
- }
- //用java缺省导入的java/lang包进行完全匹配
- fullName = "java/lang/" + className;
- for (int j = 0; j < pathList.size(); j++) {
- String currentPath = (String) pathList.get(j);
- if (currentPath.startsWith(fullName)) {
- return currentPath;
- }
- }
- return null;
- }
- }
-
- /**
- * 从指定文件中取得类名URL映射关系。
- * @param mapFilename 类名URL映射关系文件
- * @return 类名URL映射关系对象
- */
- public static ClassLink getClassLink(String mapFilename) {
- try {
- BufferedReader buffer = new BufferedReader(new FileReader(mapFilename));
- String line = buffer.readLine();
- ClassLink classLink = new ClassLink();
- while (line != null) {
- if (line.startsWith("#") || line.length() == 0) {
- line = buffer.readLine();
- continue;
- }
- String[] array = line.split("=");
- String path;
- String link;
- URL url;
- if (array.length == 1) {
- path = line;
- int index = line.lastIndexOf("/");
- if (index >= 0) {
- link = line.substring(0, index + 1);
- } else {
- link = "";
- }
- } else {
- path = array[0];
- link = array[1];
- }
- url = getURL(path);
- HashMap map = (HashMap) org.jr.io.FileReader.readObject(url);
- if (map != null) {
- classLink.put(map, link);
- }
- line = buffer.readLine();
- }
- return classLink;
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
-
- /**
- * 根据指定的路径得到路径的URL。
- * 如果路径是以http://或者file:///开头的则直接生成URL, 否则将路径转换为文件对象后再转换为URL。
- * @param path 路径
- * @return 路径对应的URL
- */
- private static URL getURL(String path) {
- try {
- if (path.startsWith("http://") || path.startsWith("file:///")) {
- return new URL(path);
- } else {
- return new File(path).toURL();
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
-
- }