1 5 package org.h2.tools.doc; 6 7 import java.io.File ; 8 import java.io.FileReader ; 9 import java.util.ArrayList ; 10 import java.util.Collections ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 14 import org.h2.util.IOUtils; 15 import org.h2.util.StartBrowser; 16 import org.h2.util.StringUtils; 17 18 public class LinkChecker { 19 20 private static final boolean OPEN_EXTERNAL_LINKS = false; 21 22 public static void main(String [] args) throws Exception { 23 new LinkChecker().run(args); 24 } 25 26 private HashMap targets = new HashMap (); 27 private HashMap links = new HashMap (); 28 29 private void run(String [] args) throws Exception { 30 String dir = "src/docsrc"; 31 for(int i=0; i<args.length; i++) { 32 if("-dir".equals(args[i])) { 33 dir = args[++i]; 34 } 35 } 36 process(dir); 37 listExternalLinks(); 38 listBadLinks(); 39 } 40 41 void listExternalLinks() { 42 for(Iterator it = links.keySet().iterator(); it.hasNext(); ) { 43 String link = (String ) it.next(); 44 if(link.startsWith("http")) { 45 if(link.indexOf("//localhost")>0) { 46 continue; 47 } 48 if(OPEN_EXTERNAL_LINKS) { 49 StartBrowser.openURL(link); 50 } 51 System.out.println("External Link: " + link); 52 } 53 } 54 } 55 56 void listBadLinks() throws Exception { 57 ArrayList errors = new ArrayList (); 58 for(Iterator it = links.keySet().iterator(); it.hasNext(); ) { 59 String link = (String ) it.next(); 60 if(!link.startsWith("http") && !link.endsWith("h2.pdf")) { 61 if(targets.get(link) == null) { 62 errors.add(links.get(link) + ": missing link " + link); 63 } 64 } 65 } 66 for(Iterator it = links.keySet().iterator(); it.hasNext(); ) { 67 String link = (String ) it.next(); 68 if(!link.startsWith("http")) { 69 targets.remove(link); 70 } 71 } 72 for(Iterator it = targets.keySet().iterator(); it.hasNext(); ) { 73 String name = (String ) it.next(); 74 if(targets.get(name).equals("name")) { 75 errors.add("No link to " + name); 76 } 77 } 78 Collections.sort(errors); 79 for(int i=0; i<errors.size(); i++) { 80 System.out.println(errors.get(i)); 81 } 82 if(errors.size() > 0) { 83 throw new Exception ("Problems where found by the Link Checker"); 84 } 85 } 86 87 void process(String path) throws Exception { 88 if(path.endsWith("/CVS") || path.endsWith("/.svn")) { 89 return; 90 } 91 File file = new File (path); 92 if(file.isDirectory()) { 93 String [] list = file.list(); 94 for(int i=0; i<list.length; i++) { 95 process(path + "/" + list[i]); 96 } 97 } else { 98 processFile(path); 99 } 100 } 101 102 void processFile(String path) throws Exception { 103 targets.put(path, "file"); 104 String lower = StringUtils.toLowerEnglish(path); 105 if(!lower.endsWith(".html") && !lower.endsWith(".htm")) { 106 return; 107 } 108 String fileName = new File (path).getName(); 109 String parent = path.substring(0, path.lastIndexOf('/')); 110 String html = IOUtils.readStringAndClose(new FileReader (path), -1); 111 int idx = -1; 112 while(true) { 113 idx = html.indexOf(" id=\"", idx+1); 114 if(idx < 0) { 115 break; 116 } 117 int start = idx + 4; 118 int end = html.indexOf("\"", start + 1); 119 if(end < 0) { 120 error(fileName, "expected \" after id= " + html.substring(idx, idx + 100)); 121 } 122 String ref = html.substring(start+1, end); 123 targets.put(path + "#" + ref, "id"); 124 } 125 idx = -1; 126 while(true) { 127 idx = html.indexOf("<a ", idx+1); 128 if(idx < 0) { 129 break; 130 } 131 int equals = html.indexOf("=", idx); 132 if(equals < 0) { 133 error(fileName, "expected = after <a at " + html.substring(idx, idx + 100)); 134 } 135 String type = html.substring(idx+2, equals).trim(); 136 int start = html.indexOf("\"", idx); 137 if(start < 0) { 138 error(fileName, "expected \" after <a at " + html.substring(idx, idx + 100)); 139 } 140 int end = html.indexOf("\"", start + 1); 141 if(end < 0) { 142 error(fileName, "expected \" after <a at " + html.substring(idx, idx + 100)); 143 } 144 String ref = html.substring(start+1, end); 145 if(type.equals("href")) { 146 if(ref.startsWith("http:") || ref.startsWith("https:")) { 147 } else if(ref.startsWith("#")) { 149 ref = path + ref; 150 } else { 151 String p = parent; 152 while(ref.startsWith(".")) { 153 if(ref.startsWith("./")) { 154 ref = ref.substring(2); 155 } else if(ref.startsWith("../")) { 156 ref = ref.substring(3); 157 p = p.substring(0, p.lastIndexOf('/')); 158 } 159 } 160 ref = p + "/" + ref; 161 } 162 links.put(ref, path); 163 } else if(type.equals("name")) { 164 targets.put(path + "#" + ref, "name"); 165 } else { 166 error(fileName, "unsupported <a xxx: " + html.substring(idx, idx + 100)); 167 } 168 } 169 } 170 171 private void error(String fileName, String string) { 172 System.out.println("ERROR with " + fileName + ": " + string); 173 } 174 175 } 176 | Popular Tags |