3 import java.io.BufferedReader;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.io.InputStream;
9 import java.util.HashMap;
13 * A class to check help file cross-references, and external URLs if internet
19 public class HelpLinksChecker
21 private static final String HELP_HS = "help.hs";
23 private static final String HELP_TOC_XML = "helpTOC.xml";
25 private static final String HELP_JHM = "help.jhm";
27 private static boolean internetAvailable = true;
29 private int targetCount = 0;
31 private int mapCount = 0;
33 private int internalHrefCount = 0;
35 private int anchorRefCount = 0;
37 private int invalidAnchorRefCount = 0;
39 private int externalHrefCount = 0;
41 private int invalidMapUrlCount = 0;
43 private int invalidTargetCount = 0;
45 private int invalidImageCount = 0;
47 private int invalidInternalHrefCount = 0;
49 private int invalidExternalHrefCount = 0;
52 * The only parameter should be a path to the root of the help directory in
56 * [0] path to the /html folder in the workspace
58 * [1] (optional) -nointernet to suppress external link checking for
59 * a fast check of internal links only
62 public static void main(String[] args) throws IOException
64 if (args.length == 0 || args.length > 2
65 || (args.length == 2 && !args[1].equals("-nointernet")))
67 System.out.println("Usage: <pathToHelpFolder> [-nointernet]");
73 internetAvailable = false;
76 new HelpLinksChecker().checkLinks(args[0]);
80 * Checks help links and reports results
82 * @param helpDirectoryPath
85 void checkLinks(String helpDirectoryPath) throws IOException
87 System.out.println("Checking help file links");
88 File helpFolder = new File(helpDirectoryPath).getCanonicalFile();
89 if (!helpFolder.exists())
91 System.out.println("Can't find " + helpDirectoryPath);
95 internetAvailable &= connectToUrl("http://www.example.org");
97 Map<String, String> tocTargets = checkHelpMappings(helpFolder);
99 Map<String, String> unusedTargets = new HashMap<String, String>(
102 checkTableOfContents(helpFolder, tocTargets, unusedTargets);
104 checkHelpSet(helpFolder, tocTargets, unusedTargets);
106 checkHtmlFolder(new File(helpFolder, "html"));
108 reportResults(unusedTargets);
112 * Checks all html files in the given directory or its sub-directories
115 * @throws IOException
117 private void checkHtmlFolder(File folder) throws IOException
119 File[] files = folder.listFiles();
128 if (f.getAbsolutePath().endsWith(".html"))
130 checkHtmlFile(f, folder);
137 * Checks that any image attribute in help.hs is a valid target
141 * @param unusedTargets
142 * used targets are removed from here
144 private void checkHelpSet(File helpFolder,
145 Map<String, String> tocTargets, Map<String, String> unusedTargets)
148 BufferedReader br = new BufferedReader(new FileReader(new File(
149 helpFolder, HELP_HS)));
150 String data = br.readLine();
156 String image = getAttribute(data, "image");
159 unusedTargets.remove(image);
160 if (!tocTargets.containsKey(image))
162 System.out.println(String.format(
163 "Invalid image '%s' at line %d of %s", image, lineNo,
168 data = br.readLine();
174 * Print counts to sysout
176 * @param unusedTargets
178 private void reportResults(Map<String, String> unusedTargets)
180 System.out.println("\nResults:");
181 System.out.println(targetCount + " distinct help targets");
182 System.out.println(mapCount + " help mappings");
183 System.out.println(invalidTargetCount + " invalid targets");
184 System.out.println(unusedTargets.size() + " unused targets");
185 for (String target : unusedTargets.keySet())
187 System.out.println(String.format(" %s: %s", target,
188 unusedTargets.get(target)));
190 System.out.println(invalidMapUrlCount + " invalid map urls");
191 System.out.println(invalidImageCount + " invalid image attributes");
192 System.out.println(String.format(
193 "%d internal href links (%d with anchors - not checked)",
194 internalHrefCount, anchorRefCount));
195 System.out.println(invalidInternalHrefCount
196 + " invalid internal href links");
197 System.out.println(invalidAnchorRefCount
198 + " invalid internal anchor links");
199 System.out.println(externalHrefCount + " external href links");
200 if (internetAvailable)
202 System.out.println(invalidExternalHrefCount
203 + " invalid external href links");
208 .println("External links not verified as internet not available");
210 if (invalidInternalHrefCount > 0 || invalidExternalHrefCount > 0
211 || invalidImageCount > 0 || invalidAnchorRefCount > 0)
218 * Reads the given html file and checks any href attibute values are either
220 * <li>a valid relative file path, or</li>
221 * <li>a valid absolute URL (if external link checking is enabled)</li>
226 * the parent folder (for validation of relative paths)
228 private void checkHtmlFile(File htmlFile, File htmlFolder)
231 BufferedReader br = new BufferedReader(new FileReader(htmlFile));
232 String data = br.readLine();
237 String href = getAttribute(data, "href");
240 String anchor = null;
241 int anchorPos = href.indexOf("#");
244 anchor = href.substring(anchorPos + 1);
245 href = href.substring(0, anchorPos);
247 boolean badLink = false;
248 if (href.startsWith("http"))
251 if (internetAvailable)
253 if (!connectToUrl(href))
256 invalidExternalHrefCount++;
263 File hrefFile = href.equals("") ? htmlFile : new File(htmlFolder,
265 if (!hrefFile.exists())
268 invalidInternalHrefCount++;
275 if (!checkAnchorExists(hrefFile, anchor))
277 System.out.println(String.format(
278 "Invalid anchor: %s at line %d of %s", anchor,
279 lineNo, getPath(htmlFile)));
280 invalidAnchorRefCount++;
287 System.out.println(String.format(
288 "Invalid href %s at line %d of %s", href, lineNo,
292 data = br.readLine();
298 * Reads the file and checks for the presence of the given html anchor
302 * @return true if anchor is found else false
304 private boolean checkAnchorExists(File hrefFile, String anchor)
306 String nameAnchor = "<a name=\"" + anchor + "\"";
307 String idAnchor = "<a id=\"" + anchor + "\"";
308 boolean found = false;
311 BufferedReader br = new BufferedReader(new FileReader(hrefFile));
312 String data = br.readLine();
315 if (data.contains(nameAnchor) || data.contains(idAnchor))
320 data = br.readLine();
323 } catch (IOException e)
331 * Returns the part of the file path starting from /help/
336 private String getPath(File helpFile)
338 String path = helpFile.getPath();
339 int helpPos = path.indexOf("/help/");
340 return helpPos == -1 ? path : path.substring(helpPos);
344 * Returns true if the URL returns an input stream, or false if the URL
345 * returns an error code or we cannot connect to it (e.g. no internet
351 private boolean connectToUrl(String url)
355 URL u = new URL(url);
356 InputStream connection = u.openStream();
359 } catch (Throwable t)
366 * Reads file help.jhm and checks that
368 * <li>each target attribute is in tocTargets</li>
369 * <li>each url attribute is a valid relative file link</li>
374 private Map<String, String> checkHelpMappings(File helpFolder)
377 Map<String, String> targets = new HashMap<String, String>();
378 BufferedReader br = new BufferedReader(new FileReader(new File(
379 helpFolder, HELP_JHM)));
380 String data = br.readLine();
387 * record target, check for duplicates
389 String target = getAttribute(data, "target");
393 if (targets.containsKey(target))
395 System.out.println(String.format(
396 "Duplicate target mapping to %s at line %d of %s",
397 target, lineNo, HELP_JHM));
408 String url = getAttribute(data, "url");
411 targets.put(target, url);
412 int anchorPos = url.indexOf("#");
415 url = url.substring(0, anchorPos);
417 if (!new File(helpFolder, url).exists())
419 System.out.println(String.format(
420 "Invalid url path '%s' at line %d of %s", url, lineNo,
422 invalidMapUrlCount++;
425 data = br.readLine();
432 * Reads file helpTOC.xml and reports any invalid targets
436 * @param unusedTargets
437 * used targets are removed from this map
440 * @throws IOException
442 private void checkTableOfContents(File helpFolder,
443 Map<String, String> tocTargets, Map<String, String> unusedTargets)
446 BufferedReader br = new BufferedReader(new FileReader(new File(
447 helpFolder, HELP_TOC_XML)));
448 String data = br.readLine();
454 * assuming no more than one "target" per line of file here
456 String target = getAttribute(data, "target");
459 unusedTargets.remove(target);
460 if (!tocTargets.containsKey(target))
462 System.out.println(String.format(
463 "Invalid target '%s' at line %d of %s", target, lineNo,
465 invalidTargetCount++;
468 data = br.readLine();
474 * Returns the value of an attribute if found in the data, else null
480 private static String getAttribute(String data, String attName)
483 * make a partial attempt at ignoring within <!-- html comments -->
484 * (doesn't work if multi-line)
486 int commentStartPos = data.indexOf("<!--");
487 int commentEndPos = commentStartPos == -1 ? -1 : data.substring(
488 commentStartPos + 4).indexOf("-->");
490 String match = attName + "=\"";
491 int attPos = data.indexOf(match);
493 && (commentStartPos == -1 || attPos < commentStartPos || attPos > commentEndPos))
495 data = data.substring(attPos + match.length());
496 value = data.substring(0, data.indexOf("\""));