Fixed bug in relative path computation (and made that testable alone, too, though I did not commit the test since we don't have unit testing in place)

This commit is contained in:
Andrea Aime 2006-06-11 15:28:05 +00:00
parent 10d62dabb8
commit 0f3c2cf511
2 changed files with 70 additions and 64 deletions

View File

@ -869,43 +869,46 @@ class ClassGraph {
// building relative path for context and package diagrams
if(contextDoc != null && rootClass) {
// determine the context path, relative to the root
String[] contextClassPath = null;
if(contextDoc instanceof ClassDoc) {
contextClassPath = ((ClassDoc) contextDoc).containingPackage().name().split("\\.");
} else if(contextDoc instanceof PackageDoc) {
contextClassPath = ((PackageDoc) contextDoc).name().split("\\.");
} else {
return classToUrl(cd.qualifiedName());
}
// path, relative to the root, of the destination class
String[] currClassPath = cd.containingPackage().name().split("\\.");
// compute relative path between the context and the destination
// ... first, compute common part
int i = 0;
while(i < contextClassPath.length && i < currClassPath.length
&& contextClassPath[i].equals(currClassPath[i]))
i++;
// ... go up with ".." to reach the common root
StringBuffer buf = new StringBuffer();
if(i == contextClassPath.length) {
buf.append(".").append(FILE_SEPARATOR);
} else {
for (int j = i; j < currClassPath.length; j++) {
buf.append("..").append(FILE_SEPARATOR);
String packageName = null;
if (contextDoc instanceof ClassDoc) {
packageName = ((ClassDoc) contextDoc).containingPackage().name();
} else if (contextDoc instanceof PackageDoc) {
packageName = ((PackageDoc) contextDoc).name();
} else {
return classToUrl(cd.qualifiedName());
}
}
// ... go down from the common root to the destination
for (int j = i; j < currClassPath.length; j++) {
buf.append(currClassPath[j]).append(FILE_SEPARATOR);
}
buf.append(cd.name()).append(".html");
return buf.toString();
return buildRelativePath(packageName, cd.containingPackage().name()) + cd.name() + ".html";
} else {
return classToUrl(cd.qualifiedName());
}
}
protected static String buildRelativePath(String contextPackageName, String classPackageName) {
// path, relative to the root, of the destination class
String[] contextClassPath = contextPackageName.split("\\.");
String[] currClassPath = classPackageName.split("\\.");
// compute relative path between the context and the destination
// ... first, compute common part
int i = 0;
while (i < contextClassPath.length && i < currClassPath.length
&& contextClassPath[i].equals(currClassPath[i]))
i++;
// ... go up with ".." to reach the common root
StringBuffer buf = new StringBuffer();
if (i == contextClassPath.length) {
buf.append(".").append(FILE_SEPARATOR);
} else {
for (int j = i; j < contextClassPath.length; j++) {
buf.append("..").append(FILE_SEPARATOR);
}
}
// ... go down from the common root to the destination
for (int j = i; j < currClassPath.length; j++) {
buf.append(currClassPath[j]).append(FILE_SEPARATOR);
}
return buf.toString();
}
/** Convert the class name into a corresponding URL */
public String classToUrl(String className) {

View File

@ -869,43 +869,46 @@ class ClassGraph {
// building relative path for context and package diagrams
if(contextDoc != null && rootClass) {
// determine the context path, relative to the root
String[] contextClassPath = null;
if(contextDoc instanceof ClassDoc) {
contextClassPath = ((ClassDoc) contextDoc).containingPackage().name().split("\\.");
} else if(contextDoc instanceof PackageDoc) {
contextClassPath = ((PackageDoc) contextDoc).name().split("\\.");
} else {
return classToUrl(cd.qualifiedName());
}
// path, relative to the root, of the destination class
String[] currClassPath = cd.containingPackage().name().split("\\.");
// compute relative path between the context and the destination
// ... first, compute common part
int i = 0;
while(i < contextClassPath.length && i < currClassPath.length
&& contextClassPath[i].equals(currClassPath[i]))
i++;
// ... go up with ".." to reach the common root
StringBuffer buf = new StringBuffer();
if(i == contextClassPath.length) {
buf.append(".").append(FILE_SEPARATOR);
} else {
for (int j = i; j < currClassPath.length; j++) {
buf.append("..").append(FILE_SEPARATOR);
String packageName = null;
if (contextDoc instanceof ClassDoc) {
packageName = ((ClassDoc) contextDoc).containingPackage().name();
} else if (contextDoc instanceof PackageDoc) {
packageName = ((PackageDoc) contextDoc).name();
} else {
return classToUrl(cd.qualifiedName());
}
}
// ... go down from the common root to the destination
for (int j = i; j < currClassPath.length; j++) {
buf.append(currClassPath[j]).append(FILE_SEPARATOR);
}
buf.append(cd.name()).append(".html");
return buf.toString();
return buildRelativePath(packageName, cd.containingPackage().name()) + cd.name() + ".html";
} else {
return classToUrl(cd.qualifiedName());
}
}
protected static String buildRelativePath(String contextPackageName, String classPackageName) {
// path, relative to the root, of the destination class
String[] contextClassPath = contextPackageName.split("\\.");
String[] currClassPath = classPackageName.split("\\.");
// compute relative path between the context and the destination
// ... first, compute common part
int i = 0;
while (i < contextClassPath.length && i < currClassPath.length
&& contextClassPath[i].equals(currClassPath[i]))
i++;
// ... go up with ".." to reach the common root
StringBuffer buf = new StringBuffer();
if (i == contextClassPath.length) {
buf.append(".").append(FILE_SEPARATOR);
} else {
for (int j = i; j < contextClassPath.length; j++) {
buf.append("..").append(FILE_SEPARATOR);
}
}
// ... go down from the common root to the destination
for (int j = i; j < currClassPath.length; j++) {
buf.append(currClassPath[j]).append(FILE_SEPARATOR);
}
return buf.toString();
}
/** Convert the class name into a corresponding URL */
public String classToUrl(String className) {