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);
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");
214 * Reads the given html file and checks any href attibute values are either
216 * <li>a valid relative file path, or</li>
217 * <li>a valid absolute URL (if external link checking is enabled)</li>
222 * the parent folder (for validation of relative paths)
224 private void checkHtmlFile(File htmlFile, File htmlFolder)
227 BufferedReader br = new BufferedReader(new FileReader(htmlFile));
228 String data = br.readLine();
233 String href = getAttribute(data, "href");
236 String anchor = null;
237 int anchorPos = href.indexOf("#");
240 anchor = href.substring(anchorPos + 1);
241 href = href.substring(0, anchorPos);
243 boolean badLink = false;
244 if (href.startsWith("http"))
247 if (internetAvailable)
249 if (!connectToUrl(href))
252 invalidExternalHrefCount++;
259 File hrefFile = href.equals("") ? htmlFile : new File(htmlFolder,
261 if (!hrefFile.exists())
264 invalidInternalHrefCount++;
271 if (!checkAnchorExists(hrefFile, anchor))
273 System.out.println(String.format(
274 "Invalid anchor: %s at line %d of %s", anchor,
275 lineNo, getPath(htmlFile)));
276 invalidAnchorRefCount++;
283 System.out.println(String.format(
284 "Invalid href %s at line %d of %s", href, lineNo,
288 data = br.readLine();
294 * Reads the file and checks for the presence of the given html anchor
298 * @return true if anchor is found else false
300 private boolean checkAnchorExists(File hrefFile, String anchor)
302 String nameAnchor = "<a name=\"" + anchor + "\"";
303 String idAnchor = "<a id=\"" + anchor + "\"";
304 boolean found = false;
307 BufferedReader br = new BufferedReader(new FileReader(hrefFile));
308 String data = br.readLine();
311 if (data.contains(nameAnchor) || data.contains(idAnchor))
316 data = br.readLine();
319 } catch (IOException e)
327 * Returns the part of the file path starting from /help/
332 private String getPath(File helpFile)
334 String path = helpFile.getPath();
335 int helpPos = path.indexOf("/help/");
336 return helpPos == -1 ? path : path.substring(helpPos);
340 * Returns true if the URL returns an input stream, or false if the URL
341 * returns an error code or we cannot connect to it (e.g. no internet
347 private boolean connectToUrl(String url)
351 URL u = new URL(url);
352 InputStream connection = u.openStream();
355 } catch (Throwable t)
362 * Reads file help.jhm and checks that
364 * <li>each target attribute is in tocTargets</li>
365 * <li>each url attribute is a valid relative file link</li>
370 private Map<String, String> checkHelpMappings(File helpFolder)
373 Map<String, String> targets = new HashMap<String, String>();
374 BufferedReader br = new BufferedReader(new FileReader(new File(
375 helpFolder, HELP_JHM)));
376 String data = br.readLine();
383 * record target, check for duplicates
385 String target = getAttribute(data, "target");
389 if (targets.containsKey(target))
391 System.out.println(String.format(
392 "Duplicate target mapping to %s at line %d of %s",
393 target, lineNo, HELP_JHM));
404 String url = getAttribute(data, "url");
407 targets.put(target, url);
408 int anchorPos = url.indexOf("#");
411 url = url.substring(0, anchorPos);
413 if (!new File(helpFolder, url).exists())
415 System.out.println(String.format(
416 "Invalid url path '%s' at line %d of %s", url, lineNo,
418 invalidMapUrlCount++;
421 data = br.readLine();
428 * Reads file helpTOC.xml and reports any invalid targets
432 * @param unusedTargets
433 * used targets are removed from this map
436 * @throws IOException
438 private void checkTableOfContents(File helpFolder,
439 Map<String, String> tocTargets, Map<String, String> unusedTargets)
442 BufferedReader br = new BufferedReader(new FileReader(new File(
443 helpFolder, HELP_TOC_XML)));
444 String data = br.readLine();
450 * assuming no more than one "target" per line of file here
452 String target = getAttribute(data, "target");
455 unusedTargets.remove(target);
456 if (!tocTargets.containsKey(target))
458 System.out.println(String.format(
459 "Invalid target '%s' at line %d of %s", target, lineNo,
461 invalidTargetCount++;
464 data = br.readLine();
470 * Returns the value of an attribute if found in the data, else null
476 private static String getAttribute(String data, String attName)
479 * make a partial attempt at ignoring within <!-- html comments -->
480 * (doesn't work if multi-line)
482 int commentStartPos = data.indexOf("<!--");
483 int commentEndPos = commentStartPos == -1 ? -1 : data.substring(
484 commentStartPos + 4).indexOf("-->");
486 String match = attName + "=\"";
487 int attPos = data.indexOf(match);
489 && (commentStartPos == -1 || attPos < commentStartPos || attPos > commentEndPos))
491 data = data.substring(attPos + match.length());
492 value = data.substring(0, data.indexOf("\""));