KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > tools > doc > LinkChecker


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.tools.doc;
6
7 import java.io.File JavaDoc;
8 import java.io.FileReader JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
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 JavaDoc[] args) throws Exception JavaDoc {
23         new LinkChecker().run(args);
24     }
25     
26     private HashMap JavaDoc targets = new HashMap JavaDoc();
27     private HashMap JavaDoc links = new HashMap JavaDoc();
28
29     private void run(String JavaDoc[] args) throws Exception JavaDoc {
30         String JavaDoc 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 JavaDoc it = links.keySet().iterator(); it.hasNext(); ) {
43             String JavaDoc link = (String JavaDoc) 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 JavaDoc {
57         ArrayList JavaDoc errors = new ArrayList JavaDoc();
58         for(Iterator JavaDoc it = links.keySet().iterator(); it.hasNext(); ) {
59             String JavaDoc link = (String JavaDoc) 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 JavaDoc it = links.keySet().iterator(); it.hasNext(); ) {
67             String JavaDoc link = (String JavaDoc) it.next();
68             if(!link.startsWith("http")) {
69                 targets.remove(link);
70             }
71         }
72         for(Iterator JavaDoc it = targets.keySet().iterator(); it.hasNext(); ) {
73             String JavaDoc name = (String JavaDoc) 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 JavaDoc("Problems where found by the Link Checker");
84         }
85     }
86     
87     void process(String JavaDoc path) throws Exception JavaDoc {
88         if(path.endsWith("/CVS") || path.endsWith("/.svn")) {
89             return;
90         }
91         File JavaDoc file = new File JavaDoc(path);
92         if(file.isDirectory()) {
93             String JavaDoc[] 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 JavaDoc path) throws Exception JavaDoc {
103         targets.put(path, "file");
104         String JavaDoc lower = StringUtils.toLowerEnglish(path);
105         if(!lower.endsWith(".html") && !lower.endsWith(".htm")) {
106             return;
107         }
108         String JavaDoc fileName = new File JavaDoc(path).getName();
109         String JavaDoc parent = path.substring(0, path.lastIndexOf('/'));
110         String JavaDoc html = IOUtils.readStringAndClose(new FileReader JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc ref = html.substring(start+1, end);
145             if(type.equals("href")) {
146                 if(ref.startsWith("http:") || ref.startsWith("https:")) {
147                     // ok
148
} else if(ref.startsWith("#")) {
149                     ref = path + ref;
150                 } else {
151                     String JavaDoc 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 JavaDoc fileName, String JavaDoc string) {
172         System.out.println("ERROR with " + fileName + ": " + string);
173     }
174     
175 }
176
Popular Tags