X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=utils%2FHelpLinksChecker.java;h=94637582c64e9c33558d31db0a61a24793799504;hb=048c16087dcab837f2ef3d8dbebbc112b91320ce;hp=1f666a4a1a0968b9843c0aaecb2be299f0fbd69c;hpb=b58f3fc059df3b857f9e7047b0b7fba578387399;p=jalview.git diff --git a/utils/HelpLinksChecker.java b/utils/HelpLinksChecker.java index 1f666a4..9463758 100644 --- a/utils/HelpLinksChecker.java +++ b/utils/HelpLinksChecker.java @@ -34,6 +34,8 @@ public class HelpLinksChecker private int anchorRefCount = 0; + private int invalidAnchorRefCount = 0; + private int externalHrefCount = 0; private int invalidMapUrlCount = 0; @@ -62,7 +64,7 @@ public class HelpLinksChecker if (args.length == 0 || args.length > 2 || (args.length == 2 && !args[1].equals("-nointernet"))) { - System.out.println("Usage: [-nointernet]"); + log("Usage: [-nointernet]"); return; } @@ -82,15 +84,15 @@ public class HelpLinksChecker */ void checkLinks(String helpDirectoryPath) throws IOException { - System.out.println("Checking help file links"); - File helpFolder = new File(helpDirectoryPath); + log("Checking help file links"); + File helpFolder = new File(helpDirectoryPath).getCanonicalFile(); if (!helpFolder.exists()) { - System.out.println("Can't find " + helpDirectoryPath); + log("Can't find " + helpDirectoryPath); return; } - internetAvailable &= connectToUrl("http://www.example.com"); + internetAvailable &= connectToUrl("http://www.example.org"); Map tocTargets = checkHelpMappings(helpFolder); @@ -157,7 +159,7 @@ public class HelpLinksChecker unusedTargets.remove(image); if (!tocTargets.containsKey(image)) { - System.out.println(String.format( + log(String.format( "Invalid image '%s' at line %d of %s", image, lineNo, HELP_HS)); invalidImageCount++; @@ -175,27 +177,29 @@ public class HelpLinksChecker */ private void reportResults(Map unusedTargets) { - System.out.println("\nResults:"); - System.out.println(targetCount + " distinct help targets"); - System.out.println(mapCount + " help mappings"); - System.out.println(invalidTargetCount + " invalid targets"); - System.out.println(unusedTargets.size() + " unused targets"); + log("\nResults:"); + log(targetCount + " distinct help targets"); + log(mapCount + " help mappings"); + log(invalidTargetCount + " invalid targets"); + log(unusedTargets.size() + " unused targets"); for (String target : unusedTargets.keySet()) { - System.out.println(String.format(" %s: %s", target, + log(String.format(" %s: %s", target, unusedTargets.get(target))); } - System.out.println(invalidMapUrlCount + " invalid map urls"); - System.out.println(invalidImageCount + " invalid image attributes"); - System.out.println(String.format( - "%d internal href links (%d with anchors - not checked)", - internalHrefCount, anchorRefCount)); - System.out.println(invalidInternalHrefCount + log(invalidMapUrlCount + " invalid map urls"); + log(invalidImageCount + " invalid image attributes"); + log(String.format( + "%d internal href links (%d with anchors)", internalHrefCount, + anchorRefCount)); + log(invalidInternalHrefCount + " invalid internal href links"); - System.out.println(externalHrefCount + " external href links"); + log(invalidAnchorRefCount + + " invalid internal anchor links"); + log(externalHrefCount + " external href links"); if (internetAvailable) { - System.out.println(invalidExternalHrefCount + log(invalidExternalHrefCount + " invalid external href links"); } else @@ -203,7 +207,21 @@ public class HelpLinksChecker System.out .println("External links not verified as internet not available"); } + if (invalidInternalHrefCount > 0 || invalidExternalHrefCount > 0 + || invalidImageCount > 0 || invalidAnchorRefCount > 0) + { + log("*** Failed ***"); + System.exit(1); + } + log("*** Success ***"); + } + /** + * @param s + */ + static void log(String s) + { + System.out.println(s); } /** @@ -254,7 +272,7 @@ public class HelpLinksChecker internalHrefCount++; File hrefFile = href.equals("") ? htmlFile : new File(htmlFolder, href); - if (!hrefFile.exists()) + if (hrefFile != htmlFile && !fileExists(hrefFile, href)) { badLink = true; invalidInternalHrefCount++; @@ -266,16 +284,17 @@ public class HelpLinksChecker { if (!checkAnchorExists(hrefFile, anchor)) { - System.out.println(String.format( + log(String.format( "Invalid anchor: %s at line %d of %s", anchor, lineNo, getPath(htmlFile))); + invalidAnchorRefCount++; } } } } if (badLink) { - System.out.println(String.format( + log(String.format( "Invalid href %s at line %d of %s", href, lineNo, getPath(htmlFile))); } @@ -286,6 +305,35 @@ public class HelpLinksChecker } /** + * Performs a case-sensitive check that the href'd file exists + * + * @param hrefFile + * @return + * @throws IOException + */ + boolean fileExists(File hrefFile, String href) throws IOException + { + if (!hrefFile.exists()) + { + return false; + } + + /* + * On Mac or Windows, file.exists() is not case sensitive, so do an + * additional check with case sensitivity + */ + int slashPos = href.lastIndexOf("/"); + String expectedFileName = slashPos == -1 ? href : href + .substring(slashPos + 1); + String cp = hrefFile.getCanonicalPath(); + slashPos = cp.lastIndexOf("/"); + String actualFileName = slashPos == -1 ? cp : cp + .substring(slashPos + 1); + + return expectedFileName.equals(actualFileName); + } + + /** * Reads the file and checks for the presence of the given html anchor * * @param hrefFile @@ -383,7 +431,7 @@ public class HelpLinksChecker mapCount++; if (targets.containsKey(target)) { - System.out.println(String.format( + log(String.format( "Duplicate target mapping to %s at line %d of %s", target, lineNo, HELP_JHM)); } @@ -407,7 +455,7 @@ public class HelpLinksChecker } if (!new File(helpFolder, url).exists()) { - System.out.println(String.format( + log(String.format( "Invalid url path '%s' at line %d of %s", url, lineNo, HELP_JHM)); invalidMapUrlCount++; @@ -450,7 +498,7 @@ public class HelpLinksChecker unusedTargets.remove(target); if (!tocTargets.containsKey(target)) { - System.out.println(String.format( + log(String.format( "Invalid target '%s' at line %d of %s", target, lineNo, HELP_TOC_XML)); invalidTargetCount++;