KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > CheckHelpSets


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 // See #13931.
21

22 package org.netbeans.nbbuild;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.Set JavaDoc;
37 import javax.help.HelpSet;
38 import javax.help.IndexItem;
39 import javax.help.IndexView;
40 import javax.help.NavigatorView;
41 import javax.help.TOCItem;
42 import javax.help.TOCView;
43 import javax.help.TreeItem;
44 import javax.help.TreeItemFactory;
45 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
46 import javax.xml.parsers.SAXParser JavaDoc;
47 import javax.xml.parsers.SAXParserFactory JavaDoc;
48 import org.apache.tools.ant.BuildException;
49 import org.apache.tools.ant.FileScanner;
50 import org.apache.tools.ant.Location;
51 import org.apache.tools.ant.Project;
52 import org.apache.tools.ant.Task;
53 import org.apache.tools.ant.types.FileSet;
54 import org.apache.tools.ant.types.Mapper;
55 import org.xml.sax.Attributes JavaDoc;
56 import org.xml.sax.InputSource JavaDoc;
57 import org.xml.sax.SAXException JavaDoc;
58 import org.xml.sax.helpers.DefaultHandler JavaDoc;
59
60 /** Task to check various aspects of JavaHelp helpsets.
61  * <ol>
62  * <li>General parsability as far as JavaHelp is concerned.
63  * <li>Map IDs are not duplicated.
64  * <li>Map IDs point to real HTML files (and anchors where specified).
65  * <li>TOC/Index navigators refer to real map IDs.
66  * <li>HTML links in reachable HTML files point to valid places (including anchors).
67  * </ol>
68  * @author Jesse Glick
69  */

70 public class CheckHelpSets extends Task {
71     
72     private List JavaDoc<FileSet> filesets = new ArrayList JavaDoc<FileSet>();
73     
74     /** Add a fileset with one or more helpsets in it.
75      * <strong>Only</strong> the <samp>*.hs</samp> should match!
76      * All other files will be found from it.
77      */

78     public void addFileset(FileSet fs) {
79         filesets.add(fs);
80     }
81     
82     public void execute() throws BuildException {
83         Iterator JavaDoc it = filesets.iterator();
84         while (it.hasNext()) {
85             FileSet fs = (FileSet)it.next();
86             FileScanner scanner = fs.getDirectoryScanner(getProject());
87             File JavaDoc dir = scanner.getBasedir();
88             String JavaDoc[] files = scanner.getIncludedFiles();
89             for (int i = 0; i < files.length; i++) {
90                 File JavaDoc helpset = new File JavaDoc(dir, files[i]);
91                 try {
92                     checkHelpSet(helpset);
93                 } catch (BuildException be) {
94                     throw be;
95                 } catch (Exception JavaDoc e) {
96                     throw new BuildException("Error checking helpset", e, new Location(helpset.getAbsolutePath()));
97                 }
98             }
99         }
100     }
101     
102     private void checkHelpSet(File JavaDoc hsfile) throws Exception JavaDoc {
103         log("Checking helpset: " + hsfile);
104         HelpSet hs = new HelpSet(null, hsfile.toURI().toURL());
105         javax.help.Map map = hs.getCombinedMap();
106         log("Parsed helpset, checking map IDs in TOC/Index navigators...");
107         NavigatorView[] navs = hs.getNavigatorViews();
108         for (int i = 0; i < navs.length; i++) {
109             String JavaDoc name = navs[i].getName();
110             File JavaDoc navfile = new File JavaDoc(hsfile.getParentFile(), (String JavaDoc)navs[i].getParameters().get("data"));
111             if (! navfile.exists()) throw new BuildException("Navigator " + name + " not found", new Location(navfile.getAbsolutePath()));
112             if (navs[i] instanceof IndexView) {
113                 log("Checking index navigator " + name, Project.MSG_VERBOSE);
114                 IndexView.parse(navfile.toURI().toURL(), hs, Locale.getDefault(), new VerifyTIFactory(hs, map, navfile, false));
115             } else if (navs[i] instanceof TOCView) {
116                 log("Checking TOC navigator " + name, Project.MSG_VERBOSE);
117                 TOCView.parse(navfile.toURI().toURL(), hs, Locale.getDefault(), new VerifyTIFactory(hs, map, navfile, true));
118             } else {
119                 log("Skipping non-TOC/Index view: " + name, Project.MSG_VERBOSE);
120             }
121         }
122         log("Checking for duplicate map IDs...");
123         HelpSet.parse(hsfile.toURI().toURL(), null, new VerifyHSFactory());
124         log("Checking links from help map and between HTML files...");
125         Enumeration JavaDoc e = map.getAllIDs();
126         Set JavaDoc<URI JavaDoc> okurls = new HashSet JavaDoc<URI JavaDoc>(1000);
127         Set JavaDoc<URI JavaDoc> badurls = new HashSet JavaDoc<URI JavaDoc>(1000);
128         Set JavaDoc<URI JavaDoc> cleanurls = new HashSet JavaDoc<URI JavaDoc>(1000);
129         while (e.hasMoreElements()) {
130             javax.help.Map.ID id = (javax.help.Map.ID)e.nextElement();
131             URL JavaDoc u = map.getURLFromID(id);
132             if (u == null) {
133                 throw new BuildException("Bogus map ID: " + id.id, new Location(hsfile.getAbsolutePath()));
134             }
135             log("Checking ID " + id.id, Project.MSG_VERBOSE);
136             CheckLinks.scan(this, id.id, "", new URI JavaDoc(u.toExternalForm()), okurls, badurls, cleanurls, false, false, false, 2, Collections.<Mapper>emptyList());
137         }
138     }
139     
140     private final class VerifyTIFactory implements TreeItemFactory {
141         
142         private final HelpSet hs;
143         private final javax.help.Map map;
144         private final File JavaDoc navfile;
145         private final boolean toc;
146         public VerifyTIFactory(HelpSet hs, javax.help.Map map, File JavaDoc navfile, boolean toc) {
147             this.hs = hs;
148             this.map = map;
149             this.navfile = navfile;
150             this.toc = toc;
151         }
152         
153         // The useful method:
154

155         public TreeItem createItem(String JavaDoc str, Hashtable JavaDoc hashtable, HelpSet helpSet, Locale JavaDoc locale) {
156             String JavaDoc target = (String JavaDoc)hashtable.get("target");
157             if (target != null) {
158                 if (! map.isValidID(target, hs)) {
159                     log(navfile + ": invalid map ID: " + target, Project.MSG_WARN);
160                 } else {
161                     log("OK map ID: " + target, Project.MSG_VERBOSE);
162                 }
163             }
164             return createItem();
165         }
166         
167         // Filler methods:
168

169         public Enumeration JavaDoc listMessages() {
170             return Collections.enumeration(Collections.<String JavaDoc>emptyList());
171         }
172         
173         public void processPI(HelpSet helpSet, String JavaDoc str, String JavaDoc str2) {
174         }
175         
176         public void reportMessage(String JavaDoc str, boolean param) {
177             log(str, param ? Project.MSG_VERBOSE : Project.MSG_WARN);
178         }
179         
180         public void processDOCTYPE(String JavaDoc str, String JavaDoc str1, String JavaDoc str2) {
181         }
182         
183         public void parsingStarted(URL JavaDoc uRL) {
184         }
185         
186         public DefaultMutableTreeNode JavaDoc parsingEnded(DefaultMutableTreeNode JavaDoc defaultMutableTreeNode) {
187             return defaultMutableTreeNode;
188         }
189         
190         public TreeItem createItem() {
191             if (toc) {
192                 return new TOCItem();
193             } else {
194                 return new IndexItem();
195             }
196         }
197         
198     }
199     
200     private final class VerifyHSFactory extends HelpSet.DefaultHelpSetFactory {
201         
202         private Set JavaDoc<String JavaDoc> ids = new HashSet JavaDoc<String JavaDoc>(1000);
203         
204         public void processMapRef(HelpSet hs, Hashtable JavaDoc attrs) {
205             try {
206                 URL JavaDoc map = new URL JavaDoc(hs.getHelpSetURL(), (String JavaDoc)attrs.get("location"));
207                 SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
208                 factory.setValidating(false);
209                 factory.setNamespaceAware(false);
210                 SAXParser JavaDoc parser = factory.newSAXParser();
211                 parser.parse(new InputSource JavaDoc(map.toExternalForm()), new Handler JavaDoc(map.getFile()));
212             } catch (Exception JavaDoc e) {
213                 e.printStackTrace();
214             }
215         }
216         
217         private final class Handler extends DefaultHandler JavaDoc {
218             
219             private final String JavaDoc map;
220             public Handler(String JavaDoc map) {
221                 this.map = map;
222             }
223             
224             public void startElement(String JavaDoc uri, String JavaDoc lname, String JavaDoc name, Attributes JavaDoc attributes) throws SAXException JavaDoc {
225                 if (name.equals("mapID")) {
226                     String JavaDoc target = attributes.getValue("target");
227                     if (target != null) {
228                         if (ids.add(target)) {
229                             log("Found map ID: " + target, Project.MSG_DEBUG);
230                         } else {
231                             log(map + ": duplicated ID: " + target, Project.MSG_WARN);
232                         }
233                     }
234                 }
235             }
236             
237             public InputSource JavaDoc resolveEntity(String JavaDoc pub, String JavaDoc sys) throws SAXException JavaDoc {
238                 if (pub.equals("-//Sun Microsystems Inc.//DTD JavaHelp Map Version 1.0//EN") ||
239                         pub.equals("-//Sun Microsystems Inc.//DTD JavaHelp Map Version 2.0//EN")) {
240                     // Ignore.
241
return new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(new byte[0]));
242                 } else {
243                     return null;
244                 }
245             }
246             
247         }
248         
249     }
250     
251 }
252
Popular Tags