X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=utils%2FHelpLinksChecker.java;h=1279b314403927dd2511864ea19afb90091800d4;hb=b1e2aea95b3839a1a53f809c579666075cd3d348;hp=a0853a93016f12614657e72ee83682cabd47ac77;hpb=07f9d0ec428f22b9df1da4e3ad68d7c4f99961bf;p=jalview.git
diff --git a/utils/HelpLinksChecker.java b/utils/HelpLinksChecker.java
index a0853a9..1279b31 100644
--- a/utils/HelpLinksChecker.java
+++ b/utils/HelpLinksChecker.java
@@ -1,5 +1,23 @@
-
-
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see .
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
@@ -16,7 +34,7 @@ import java.util.Map;
* @author gmcarstairs
*
*/
-public class HelpLinksChecker
+public class HelpLinksChecker implements BufferedLineReader.LineCleaner
{
private static final String HELP_HS = "help.hs";
@@ -64,7 +82,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;
}
@@ -84,11 +102,11 @@ public class HelpLinksChecker
*/
void checkLinks(String helpDirectoryPath) throws IOException
{
- System.out.println("Checking help file links");
+ 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;
}
@@ -159,9 +177,8 @@ public class HelpLinksChecker
unusedTargets.remove(image);
if (!tocTargets.containsKey(image))
{
- System.out.println(String.format(
- "Invalid image '%s' at line %d of %s", image, lineNo,
- HELP_HS));
+ log(String.format("Invalid image '%s' at line %d of %s", image,
+ lineNo, HELP_HS));
invalidImageCount++;
}
}
@@ -177,30 +194,25 @@ 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,
- unusedTargets.get(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)",
+ log(invalidMapUrlCount + " invalid map urls");
+ log(invalidImageCount + " invalid image attributes");
+ log(String.format("%d internal href links (%d with anchors)",
internalHrefCount, anchorRefCount));
- System.out.println(invalidInternalHrefCount
- + " invalid internal href links");
- System.out.println(invalidAnchorRefCount
- + " invalid internal anchor links");
- System.out.println(externalHrefCount + " external href links");
+ log(invalidInternalHrefCount + " invalid internal href links");
+ log(invalidAnchorRefCount + " invalid internal anchor links");
+ log(externalHrefCount + " external href links");
if (internetAvailable)
{
- System.out.println(invalidExternalHrefCount
- + " invalid external href links");
+ log(invalidExternalHrefCount + " invalid external href links");
}
else
{
@@ -210,8 +222,18 @@ public class HelpLinksChecker
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);
}
/**
@@ -262,7 +284,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++;
@@ -274,9 +296,8 @@ public class HelpLinksChecker
{
if (!checkAnchorExists(hrefFile, anchor))
{
- System.out.println(String.format(
- "Invalid anchor: %s at line %d of %s", anchor,
- lineNo, getPath(htmlFile)));
+ log(String.format("Invalid anchor: %s at line %d of %s",
+ anchor, lineNo, getPath(htmlFile)));
invalidAnchorRefCount++;
}
}
@@ -284,9 +305,8 @@ public class HelpLinksChecker
}
if (badLink)
{
- System.out.println(String.format(
- "Invalid href %s at line %d of %s", href, lineNo,
- getPath(htmlFile)));
+ log(String.format("Invalid href %s at line %d of %s", href,
+ lineNo, getPath(htmlFile)));
}
}
data = br.readLine();
@@ -295,6 +315,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(File.separator);
+ String expectedFileName = slashPos == -1 ? href : href
+ .substring(slashPos + 1);
+ String cp = hrefFile.getCanonicalPath();
+ slashPos = cp.lastIndexOf(File.separator);
+ 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
@@ -309,7 +358,8 @@ public class HelpLinksChecker
try
{
BufferedReader br = new BufferedReader(new FileReader(hrefFile));
- String data = br.readLine();
+ BufferedLineReader blr = new BufferedLineReader(br, 3, this);
+ String data = blr.read();
while (data != null)
{
if (data.contains(nameAnchor) || data.contains(idAnchor))
@@ -317,7 +367,7 @@ public class HelpLinksChecker
found = true;
break;
}
- data = br.readLine();
+ data = blr.read();
}
br.close();
} catch (IOException e)
@@ -392,7 +442,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));
}
@@ -416,9 +466,8 @@ public class HelpLinksChecker
}
if (!new File(helpFolder, url).exists())
{
- System.out.println(String.format(
- "Invalid url path '%s' at line %d of %s", url, lineNo,
- HELP_JHM));
+ log(String.format("Invalid url path '%s' at line %d of %s", url,
+ lineNo, HELP_JHM));
invalidMapUrlCount++;
}
}
@@ -459,9 +508,8 @@ public class HelpLinksChecker
unusedTargets.remove(target);
if (!tocTargets.containsKey(target))
{
- System.out.println(String.format(
- "Invalid target '%s' at line %d of %s", target, lineNo,
- HELP_TOC_XML));
+ log(String.format("Invalid target '%s' at line %d of %s", target,
+ lineNo, HELP_TOC_XML));
invalidTargetCount++;
}
}
@@ -497,4 +545,14 @@ public class HelpLinksChecker
}
return value;
}
+
+ /**
+ * Trim whitespace from concatenated lines but preserve one space for valid
+ * parsing
+ */
+ @Override
+ public String cleanLine(String l)
+ {
+ return l.trim() + " ";
+ }
}