Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2008-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 |
* Contributor(s): |
28 |
* |
29 |
* Portions Copyrighted 2008-2010 Sun Microsystems, Inc. |
30 |
*/ |
31 |
|
32 |
package org.netbeans.modules.editor.mimelookup; |
33 |
|
34 |
import java.util.Collection; |
35 |
import java.util.Collections; |
36 |
import java.util.LinkedList; |
37 |
import java.util.List; |
38 |
import java.util.Map.Entry; |
39 |
import java.util.Set; |
40 |
import javax.annotation.processing.Completion; |
41 |
import javax.annotation.processing.Processor; |
42 |
import javax.annotation.processing.RoundEnvironment; |
43 |
import javax.annotation.processing.SupportedAnnotationTypes; |
44 |
import javax.annotation.processing.SupportedSourceVersion; |
45 |
import javax.lang.model.SourceVersion; |
46 |
import javax.lang.model.element.AnnotationMirror; |
47 |
import javax.lang.model.element.AnnotationValue; |
48 |
import javax.lang.model.element.Element; |
49 |
import javax.lang.model.element.ExecutableElement; |
50 |
import javax.lang.model.element.Name; |
51 |
import javax.lang.model.element.TypeElement; |
52 |
import javax.lang.model.type.TypeKind; |
53 |
import javax.lang.model.type.TypeMirror; |
54 |
import org.openide.filesystems.annotations.LayerGeneratingProcessor; |
55 |
import org.openide.filesystems.annotations.LayerGenerationException; |
56 |
import org.openide.util.lookup.ServiceProvider; |
57 |
|
58 |
/** |
59 |
* |
60 |
* @author Jan Lahoda |
61 |
*/ |
62 |
@SupportedAnnotationTypes({"org.netbeans.api.editor.mimelookup.MimeRegistration", "org.netbeans.api.editor.mimelookup.MimeRegistrations"}) |
63 |
@SupportedSourceVersion(SourceVersion.RELEASE_6) |
64 |
@ServiceProvider(service=Processor.class) |
65 |
public class CreateRegistrationProcessor extends LayerGeneratingProcessor { |
66 |
|
67 |
@Override |
68 |
protected boolean handleProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) throws LayerGenerationException { |
69 |
TypeElement mimeRegistration = processingEnv.getElementUtils().getTypeElement("org.netbeans.api.editor.mimelookup.MimeRegistration"); |
70 |
|
71 |
for (Element el : roundEnv.getElementsAnnotatedWith(mimeRegistration)) { |
72 |
for (AnnotationMirror am : el.getAnnotationMirrors()) { |
73 |
if (!mimeRegistration.equals(am.getAnnotationType().asElement())) { |
74 |
continue; |
75 |
} |
76 |
|
77 |
process(el, am); |
78 |
} |
79 |
} |
80 |
|
81 |
TypeElement mimeRegistrations = processingEnv.getElementUtils().getTypeElement("org.netbeans.api.editor.mimelookup.MimeRegistrations"); |
82 |
|
83 |
for (Element el : roundEnv.getElementsAnnotatedWith(mimeRegistrations)) { |
84 |
for (AnnotationMirror am : el.getAnnotationMirrors()) { |
85 |
if (!mimeRegistrations.equals(am.getAnnotationType().asElement())) { |
86 |
continue; |
87 |
} |
88 |
|
89 |
for (Entry<? extends ExecutableElement, ? extends AnnotationValue> e : am.getElementValues().entrySet()) { |
90 |
if (!e.getKey().getSimpleName().contentEquals("value")) continue; |
91 |
|
92 |
for (AnnotationMirror r : (AnnotationMirror[]) e.getValue().getValue()) { |
93 |
process(el, r); |
94 |
} |
95 |
} |
96 |
} |
97 |
} |
98 |
|
99 |
return true; |
100 |
} |
101 |
|
102 |
private void process(Element toRegister, AnnotationMirror mimeRegistration) throws LayerGenerationException { |
103 |
TypeMirror service = null; |
104 |
String mimeType = null; |
105 |
int position = Integer.MAX_VALUE; |
106 |
String[] supersedes = new String[0]; |
107 |
for (Entry<? extends ExecutableElement, ? extends AnnotationValue> e : mimeRegistration.getElementValues().entrySet()) { |
108 |
Name simpleName = e.getKey().getSimpleName(); |
109 |
if (simpleName.contentEquals("service")) { |
110 |
service = (TypeMirror) e.getValue().getValue(); |
111 |
continue; |
112 |
} |
113 |
if (simpleName.contentEquals("mimeType")) { |
114 |
mimeType = (String) e.getValue().getValue(); |
115 |
continue; |
116 |
} |
117 |
if (simpleName.contentEquals("position")) { |
118 |
position = (Integer) e.getValue().getValue(); |
119 |
continue; |
120 |
} |
121 |
if (simpleName.contentEquals("supersedes")) { |
122 |
position = (Integer) e.getValue().getValue(); |
123 |
continue; |
124 |
} |
125 |
} |
126 |
|
127 |
if (mimeType != null) { |
128 |
if (mimeType.length() != 0) mimeType = "/" + mimeType; |
129 |
|
130 |
String folder = ""; |
131 |
TypeElement apiTE = (TypeElement) processingEnv.getTypeUtils().asElement(service); |
132 |
TypeElement location = processingEnv.getElementUtils().getTypeElement("org.netbeans.spi.editor.mimelookup.MimeLocation"); |
133 |
|
134 |
OUTER: for (AnnotationMirror am : apiTE.getAnnotationMirrors()) { |
135 |
if (!location.equals(am.getAnnotationType().asElement())) continue; |
136 |
|
137 |
for (Entry<? extends ExecutableElement, ? extends AnnotationValue> e : am.getElementValues().entrySet()) { |
138 |
if (e.getKey().getSimpleName().contentEquals("subfolderName")) { |
139 |
folder = "/" + (String) e.getValue().getValue(); |
140 |
break OUTER; |
141 |
} |
142 |
} |
143 |
} |
144 |
|
145 |
String fqn = (apiTE).getQualifiedName().toString(); |
146 |
Class<?> apiClass; |
147 |
|
148 |
try { |
149 |
apiClass = Class.forName(fqn); |
150 |
} catch (ClassNotFoundException ex) { |
151 |
//error/warning? |
152 |
apiClass = null; |
153 |
} |
154 |
|
155 |
layer(toRegister).instanceFile("Editors" + mimeType + folder, null, apiClass).position(position).write(); |
156 |
|
157 |
for (String superseded : supersedes) { |
158 |
layer(toRegister).file("Editors" + mimeType + folder + "/" + superseded + "_hidden").write(); |
159 |
} |
160 |
} |
161 |
} |
162 |
|
163 |
@Override |
164 |
public Iterable<? extends Completion> getCompletions(Element annotated, AnnotationMirror annotation, ExecutableElement attr, String userText) { |
165 |
if (processingEnv == null || annotated == null || !annotated.getKind().isClass()) { |
166 |
return Collections.emptyList(); |
167 |
} |
168 |
|
169 |
if ( annotation == null |
170 |
|| !"org.netbeans.api.editor.mimelookup.MimeRegistration".contentEquals(((TypeElement) annotation.getAnnotationType().asElement()).getQualifiedName())) { |
171 |
return Collections.emptyList(); |
172 |
} |
173 |
|
174 |
if (!"service".contentEquals(attr.getSimpleName())) { |
175 |
return Collections.emptyList(); |
176 |
} |
177 |
|
178 |
TypeElement jlObject = processingEnv.getElementUtils().getTypeElement("java.lang.Object"); |
179 |
|
180 |
if (jlObject == null) { |
181 |
return Collections.emptyList(); |
182 |
} |
183 |
|
184 |
Collection<Completion> result = new LinkedList<Completion>(); |
185 |
List<TypeElement> toProcess = new LinkedList<TypeElement>(); |
186 |
|
187 |
toProcess.add((TypeElement) annotated); |
188 |
|
189 |
while (!toProcess.isEmpty()) { |
190 |
TypeElement c = toProcess.remove(0); |
191 |
|
192 |
result.add(new TypeCompletion(c.getQualifiedName().toString() + ".class")); |
193 |
|
194 |
List<TypeMirror> parents = new LinkedList<TypeMirror>(); |
195 |
|
196 |
parents.add(c.getSuperclass()); |
197 |
parents.addAll(c.getInterfaces()); |
198 |
|
199 |
for (TypeMirror tm : parents) { |
200 |
if (tm == null || tm.getKind() != TypeKind.DECLARED) { |
201 |
continue; |
202 |
} |
203 |
|
204 |
TypeElement type = (TypeElement) processingEnv.getTypeUtils().asElement(tm); |
205 |
|
206 |
if (!jlObject.equals(type)) { |
207 |
toProcess.add(type); |
208 |
} |
209 |
} |
210 |
} |
211 |
|
212 |
return result; |
213 |
} |
214 |
|
215 |
private static final class TypeCompletion implements Completion { |
216 |
|
217 |
private final String type; |
218 |
|
219 |
public TypeCompletion(String type) { |
220 |
this.type = type; |
221 |
} |
222 |
|
223 |
public String getValue() { |
224 |
return type; |
225 |
} |
226 |
|
227 |
public String getMessage() { |
228 |
return null; |
229 |
} |
230 |
|
231 |
} |
232 |
} |