Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2010 Sun Microsystems, Inc. |
41 |
*/ |
42 |
|
43 |
package org.netbeans.modules.apisupport.hints; |
44 |
|
45 |
import java.util.ArrayList; |
46 |
import java.util.HashMap; |
47 |
import java.util.LinkedHashSet; |
48 |
import java.util.List; |
49 |
import java.util.Map; |
50 |
import java.util.Set; |
51 |
import javax.swing.text.Document; |
52 |
import javax.xml.parsers.SAXParser; |
53 |
import javax.xml.parsers.SAXParserFactory; |
54 |
import org.netbeans.api.editor.mimelookup.MimeRegistration; |
55 |
import org.netbeans.api.project.FileOwnerQuery; |
56 |
import org.netbeans.api.project.Project; |
57 |
import org.netbeans.modules.apisupport.project.api.LayerHandle; |
58 |
import org.netbeans.modules.apisupport.project.spi.NbModuleProvider; |
59 |
import org.netbeans.spi.editor.errorstripe.UpToDateStatus; |
60 |
import org.netbeans.spi.editor.errorstripe.UpToDateStatusProvider; |
61 |
import org.netbeans.spi.editor.errorstripe.UpToDateStatusProviderFactory; |
62 |
import org.netbeans.spi.editor.hints.ErrorDescription; |
63 |
import org.netbeans.spi.editor.hints.HintsController; |
64 |
import org.openide.filesystems.FileChangeAdapter; |
65 |
import org.openide.filesystems.FileChangeListener; |
66 |
import org.openide.filesystems.FileEvent; |
67 |
import org.openide.filesystems.FileObject; |
68 |
import org.openide.filesystems.FileSystem; |
69 |
import org.openide.filesystems.FileUtil; |
70 |
import org.openide.loaders.DataObject; |
71 |
import org.openide.loaders.DataObjectNotFoundException; |
72 |
import org.openide.util.Exceptions; |
73 |
import org.openide.util.Lookup; |
74 |
import org.openide.util.NbCollections; |
75 |
import org.openide.util.RequestProcessor; |
76 |
import org.xml.sax.Attributes; |
77 |
import org.xml.sax.InputSource; |
78 |
import org.xml.sax.Locator; |
79 |
import org.xml.sax.SAXException; |
80 |
import org.xml.sax.ext.DefaultHandler2; |
81 |
|
82 |
@MimeRegistration(mimeType="text/xml", service=UpToDateStatusProviderFactory.class) |
83 |
public class ConvertToAnnotationHint implements UpToDateStatusProviderFactory { |
84 |
|
85 |
private static final RequestProcessor RP = new RequestProcessor(ConvertToAnnotationHint.class); |
86 |
|
87 |
public @Override UpToDateStatusProvider createUpToDateStatusProvider(Document doc) { |
88 |
Object sdp = doc.getProperty(Document.StreamDescriptionProperty); // avoid dep on NbEditorUtilities.getFileObject if possible |
89 |
DataObject xml; |
90 |
if (sdp instanceof DataObject) { |
91 |
xml = (DataObject) sdp; |
92 |
} else if (sdp instanceof FileObject) { |
93 |
try { |
94 |
xml = DataObject.find((FileObject) sdp); |
95 |
} catch (DataObjectNotFoundException x) { |
96 |
Exceptions.printStackTrace(x); |
97 |
return null; |
98 |
} |
99 |
} else { |
100 |
return null; |
101 |
} |
102 |
LayerHandle handle = xml.getNodeDelegate().getLookup().lookup(LayerHandle.class); |
103 |
if (handle == null) { |
104 |
return null; |
105 |
} |
106 |
Project project = FileOwnerQuery.getOwner(xml.getPrimaryFile()); |
107 |
if (project == null || project.getLookup().lookup(NbModuleProvider.class) == null) { |
108 |
return null; |
109 |
} |
110 |
return new Prov(doc, xml, handle); |
111 |
} |
112 |
|
113 |
private static class Prov extends UpToDateStatusProvider implements Runnable { |
114 |
|
115 |
private final Document doc; |
116 |
private final LayerHandle handle; |
117 |
private boolean processed; |
118 |
private final FileChangeListener listener = new FileChangeAdapter() { |
119 |
public @Override void fileChanged(FileEvent fe) { |
120 |
processed = false; |
121 |
RP.post(Prov.this); // XXX use schedule on a Task |
122 |
firePropertyChange(PROP_UP_TO_DATE, null, null); |
123 |
} |
124 |
}; |
125 |
|
126 |
Prov(Document doc, DataObject xml, LayerHandle handle) { |
127 |
this.doc = doc; |
128 |
this.handle = handle; |
129 |
xml.getPrimaryFile().addFileChangeListener(FileUtil.weakFileChangeListener(listener, xml.getPrimaryFile())); |
130 |
RP.post(this); |
131 |
} |
132 |
|
133 |
public @Override UpToDateStatus getUpToDate() { |
134 |
if (processed) { |
135 |
return UpToDateStatus.UP_TO_DATE_OK; |
136 |
} |
137 |
return processed ? UpToDateStatus.UP_TO_DATE_OK : UpToDateStatus.UP_TO_DATE_PROCESSING; |
138 |
} |
139 |
|
140 |
public @Override void run() { |
141 |
FileSystem fs = handle.layer(false); |
142 |
if (fs == null) { |
143 |
return; |
144 |
} |
145 |
Set<FileObject> instances = new LinkedHashSet<FileObject>(); |
146 |
// Compare AbstractRefactoringPlugin.checkFileObject: |
147 |
FILE: for (FileObject f : NbCollections.iterable(fs.getRoot().getData(true))) { |
148 |
if (!f.hasExt("instance")) { |
149 |
continue; // not supporting *.settings etc. for now |
150 |
} |
151 |
instances.add(f); |
152 |
} |
153 |
List<ErrorDescription> errors = new ArrayList<ErrorDescription>(); |
154 |
if (!instances.isEmpty()) { |
155 |
final Map<String,Integer> lines = new HashMap<String,Integer>(); |
156 |
try { // Adapted from OpenLayerFilesAction.openLayerFileAndFind: |
157 |
InputSource in = new InputSource(handle.getLayerFile().getURL().toExternalForm()); |
158 |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
159 |
SAXParser parser = factory.newSAXParser(); |
160 |
class Handler extends DefaultHandler2 { |
161 |
private Locator locator; |
162 |
private String path; |
163 |
public @Override void setDocumentLocator(Locator l) { |
164 |
locator = l; |
165 |
} |
166 |
public @Override void startElement(String uri, String localname, String qname, Attributes attr) throws SAXException { |
167 |
if (!qname.matches("file|folder")) { // NOI18N |
168 |
return; |
169 |
} |
170 |
String n = attr.getValue("name"); // NOI18N |
171 |
path = path == null ? n : path + '/' + n; |
172 |
lines.put(path, locator.getLineNumber()); |
173 |
} |
174 |
public @Override void endElement(String uri, String localname, String qname) throws SAXException { |
175 |
if (!qname.matches("file|folder")) { // NOI18N |
176 |
return; |
177 |
} |
178 |
int slash = path.lastIndexOf('/'); |
179 |
path = slash == -1 ? null : path.substring(0, slash); |
180 |
} |
181 |
} |
182 |
DefaultHandler2 handler = new Handler(); |
183 |
parser.getXMLReader().setProperty("http://xml.org/sax/properties/lexical-handler", handler); // NOI18N |
184 |
parser.parse(in, handler); |
185 |
} catch (Exception x) { |
186 |
Exceptions.printStackTrace(x); |
187 |
} |
188 |
for (FileObject instance : instances) { |
189 |
Integer line = lines.get(instance.getPath()); |
190 |
if (line == null) { |
191 |
System.err.println("XXX no line for " + instance.getPath()); |
192 |
continue; |
193 |
} |
194 |
for (Hinter hinter : Lookup.getDefault().lookupAll(Hinter.class)) { |
195 |
try { |
196 |
hinter.process(new Hinter.Context(doc, handle, instance, line, errors)); |
197 |
} catch (Exception x) { |
198 |
Exceptions.printStackTrace(x); |
199 |
} |
200 |
} |
201 |
} |
202 |
} |
203 |
HintsController.setErrors(doc, ConvertToAnnotationHint.class.getName(), errors); |
204 |
processed = true; |
205 |
firePropertyChange(PROP_UP_TO_DATE, null, null); |
206 |
} |
207 |
|
208 |
} |
209 |
|
210 |
} |