KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > html > TidySuggester


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 package org.netbeans.modules.tasklist.html;
21
22
23 import javax.swing.text.*;
24 import javax.swing.event.*;
25 import java.awt.*;
26 import java.awt.event.*;
27 import javax.swing.*;
28 import java.io.*;
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import org.openide.ErrorManager;
32 import org.openide.explorer.view.*;
33 import org.openide.util.Utilities;
34
35 import org.openide.loaders.DataObject;
36 import org.openide.loaders.DataObjectNotFoundException;
37 import org.openide.text.Line;
38
39 import org.netbeans.modules.html.*;
40
41 import org.w3c.tidy.*;
42
43 import org.netbeans.modules.tasklist.core.TLUtils;
44 import org.netbeans.modules.tasklist.client.Suggestion;
45 import org.netbeans.modules.tasklist.client.SuggestionManager;
46 import org.netbeans.modules.tasklist.client.SuggestionPriority;
47 import org.netbeans.modules.tasklist.client.SuggestionAgent;
48 import org.netbeans.modules.tasklist.providers.DocumentSuggestionProvider;
49 import org.netbeans.modules.tasklist.providers.SuggestionContext;
50
51
52 /**
53  * This class lists problems in HTML documents (based on a
54  * doc scan by the Tidy utility)
55  * <p>
56  *
57  * @author Tor Norbye
58  */

59 public class TidySuggester extends DocumentSuggestionProvider
60     implements ErrorReporter {
61
62     final private static String JavaDoc TYPE = "nb-html-errors"; // NOI18N
63
private SuggestionContext env;
64
65     public String JavaDoc getType() {
66         return TYPE;
67     }
68     
69     static boolean isHTML(DataObject dobj) {
70          // XXX instanceof not good - I've heard data object
71
// instancing like this is going away. Look for
72
// some kind of HTML related cookie instead?
73
return dobj instanceof HtmlDataObject;
74     }
75
76     static boolean isJSP(DataObject dobj) {
77         String JavaDoc file = dobj.getPrimaryFile().getNameExt();
78         return file.endsWith(".jsp") || // NOI18N
79
file.endsWith(".JSP") || // NOI18N
80
// There are several data objects in web/core/.../jsploader
81
// so just look for the jsploader package instead of
82
// and actual classname
83
(dobj.getClass().getName().indexOf("jsploader") != -1); // NOI18N
84
}
85
86     static boolean isXML(DataObject dobj) {
87         String JavaDoc file = dobj.getPrimaryFile().getNameExt();
88         return file.endsWith(".xml") || // NOI18N
89
file.endsWith(".XML") || // NOI18N
90
(dobj.getClass().getName().indexOf("XMLDataObject") != -1); // NOI18N
91
}
92                          
93     public List JavaDoc scan(SuggestionContext env) {
94
95         DataObject dobj = null;
96         try {
97             dobj = DataObject.find(env.getFileObject());
98         } catch (DataObjectNotFoundException e) {
99             return null;
100         }
101
102         // XXX instanceof not good - I've heard data object
103
// instancing like this is going away. Look for
104
// some kind of HTML related cookie instead?
105
boolean isHTML = isHTML(dobj);
106          boolean isJSP = false;
107          boolean isXML = false;
108          if (!isHTML) {
109              isJSP = isJSP(dobj);
110              if (!isJSP) {
111                  isXML = isXML(dobj);
112              }
113          }
114          if (!(isHTML || isJSP || isXML)) {
115              return null;
116          }
117         SuggestionManager manager = SuggestionManager.getDefault();
118         
119         parseTasks = null;
120         parseObject = dobj;
121         if (manager.isEnabled(TYPE)) {
122             InputStream input = null;
123             try {
124                 input = env.getFileObject().getInputStream();
125             } catch (FileNotFoundException e) {
126                 return null;
127             }
128
129             if (tidy == null) {
130                 tidy = new Tidy();
131             }
132             tidy.setOnlyErrors(true);
133             tidy.setShowWarnings(true);
134             tidy.setQuiet(true);
135             // XXX Apparently JSP pages (at least those involving
136
// JSF) need XML handling in order for JTidy not to choke on them
137
tidy.setXmlTags(isXML || isJSP);
138
139             PrintWriter output = new ReportWriter(this);
140             tidy.setErrout(output);
141             // Where do I direct its output? If it really obeys
142
// setQuiet(true) it shouldn't matter...
143
tidy.parse(input, System.err);
144         }
145         return parseTasks;
146     }
147
148     /** The list of tasks we're currently showing in the tasklist */
149     private List JavaDoc showingTasks = null;
150
151     /** List being built during a scan */
152     private List JavaDoc parseTasks = null;
153     private DataObject parseObject = null;
154
155     public void reportError(int line, int col, boolean error, String JavaDoc message) {
156         //System.err.println("reportError(" + line + ", " + col + ", " + error + ", " + message + ")");
157

158         SuggestionManager manager = SuggestionManager.getDefault();
159         SuggestionAgent s = manager.createSuggestion(TYPE,
160                                                 message,
161                                                 null,
162                                                 this);
163         if (line != -1) {
164             Line l = TLUtils.getLineByNumber(parseObject, line);
165             s.setLine(l);
166         }
167         if (error) {
168             Image taskIcon = Utilities.loadImage("org/netbeans/modules/tasklist/html/error.gif"); // NOI18N
169
s.setIcon(taskIcon);
170             s.setPriority(SuggestionPriority.HIGH);
171         }
172         if (parseTasks == null) {
173             parseTasks = new ArrayList JavaDoc(30);
174         }
175         parseTasks.add(s.getSuggestion());
176     }
177
178     private Object JavaDoc request = null;
179     private Tidy tidy = null;
180 }
181
Popular Tags