This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 190606
Collapse All | Expand All

(-)a/editor.mimelookup/apichanges.xml (+13 lines)
Lines 108-113 Link Here
108
108
109
    <changes>
109
    <changes>
110
110
111
        <change id="MimeLookupRegistration">
112
            <summary>@MimeLocation, @MimeRegistration and @MimeRegistrations added</summary>
113
            <version major="1" minor="19"/>
114
            <date day="4" month="10" year="2010"/>
115
            <author login="jlahoda"/>
116
            <compatibility binary="compatible" source="compatible" semantic="compatible" addition="yes" deletion="no" modification="no" />
117
            <description>
118
                Added <code>@MimeLocation</code>, <code>@MimeRegistration</code> and <code>@MimeRegistrations</code>
119
                that allow to register services to the MimeLookup.
120
            </description>
121
            <issue number="190606"/>
122
        </change>
123
111
        <change id="MimeLookup.getLookup-mimePath-as-String-added">
124
        <change id="MimeLookup.getLookup-mimePath-as-String-added">
112
            <summary>MimeLookup.getLookup(String mimePath) method added</summary>
125
            <summary>MimeLookup.getLookup(String mimePath) method added</summary>
113
            <version major="1" minor="8"/>
126
            <version major="1" minor="8"/>
(-)a/editor.mimelookup/manifest.mf (-1 / +1 lines)
Lines 1-7 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.modules.editor.mimelookup/1
2
OpenIDE-Module: org.netbeans.modules.editor.mimelookup/1
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/editor/mimelookup/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/editor/mimelookup/Bundle.properties
4
OpenIDE-Module-Specification-Version: 1.18
4
OpenIDE-Module-Specification-Version: 1.19
5
OpenIDE-Module-Recommends: org.netbeans.spi.editor.mimelookup.MimeDataProvider
5
OpenIDE-Module-Recommends: org.netbeans.spi.editor.mimelookup.MimeDataProvider
6
AutoUpdate-Essential-Module: true
6
AutoUpdate-Essential-Module: true
7
7
(-)a/editor.mimelookup/nbproject/project.xml (+5 lines)
Lines 101-106 Link Here
101
                        <recursive/>
101
                        <recursive/>
102
                        <compile-dependency/>
102
                        <compile-dependency/>
103
                    </test-dependency>
103
                    </test-dependency>
104
                    <test-dependency>
105
                        <code-name-base>org.openide.util.lookup</code-name-base>
106
                        <compile-dependency/>
107
                        <test/>
108
                    </test-dependency>
104
                </test-type>
109
                </test-type>
105
            </test-dependencies>
110
            </test-dependencies>
106
            <public-packages>
111
            <public-packages>
(-)a/editor.mimelookup/src/org/netbeans/api/editor/mimelookup/MimeRegistration.java (+77 lines)
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
package org.netbeans.api.editor.mimelookup;
43
44
import java.lang.annotation.ElementType;
45
import java.lang.annotation.Target;
46
47
/**Register an implementation of a service to the mime lookup under given mime-type.
48
 * This annotation can be used either to annotate implementation class, in which case
49
 * it must have a public non-arg constructor, or a public static factory method returning
50
 * the service.
51
 *
52
 * @author Jan Lahoda
53
 * @since 1.19
54
 */
55
@Target({ElementType.TYPE, ElementType.METHOD})
56
public @interface MimeRegistration {
57
58
    /**
59
     * The API service.
60
     */
61
    public Class<?> service();
62
    
63
    /**
64
     * Mime type to which should be the given provider registered.
65
     */
66
    public String mimeType();
67
68
    /**
69
     * Position of the provider in the list of providers.
70
     */
71
    public int position() default Integer.MAX_VALUE;
72
73
    /**
74
     * Superseded providers.
75
     */
76
    public String[] supersedes() default {};
77
}
(-)a/editor.mimelookup/src/org/netbeans/api/editor/mimelookup/MimeRegistrations.java (+53 lines)
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
package org.netbeans.api.editor.mimelookup;
43
44
/**Allows to specify more that one {@link MimeRegistration} for one element.
45
 *
46
 * @author lahvac
47
 * @since 1.19
48
 */
49
public @interface MimeRegistrations {
50
51
    public MimeRegistration[] value();
52
    
53
}
(-)a/editor.mimelookup/src/org/netbeans/modules/editor/mimelookup/CreateRegistrationProcessor.java (+232 lines)
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
}
(-)a/editor.mimelookup/src/org/netbeans/spi/editor/mimelookup/MimeLocation.java (+69 lines)
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.spi.editor.mimelookup;
44
45
import java.lang.annotation.Documented;
46
import java.lang.annotation.ElementType;
47
import java.lang.annotation.Retention;
48
import java.lang.annotation.RetentionPolicy;
49
import java.lang.annotation.Target;
50
51
/**Should be used for services that are to be registered to the MimeLookup and that
52
 * need a specific subfolder to be used for the registration (optional, mime-type root
53
 * will be searched if this annotation is missing)
54
 *
55
 * @author Jan Lahoda
56
 * @since 1.19
57
 */
58
@Documented
59
@Retention(RetentionPolicy.RUNTIME)
60
@Target(ElementType.TYPE)
61
public @interface MimeLocation {
62
63
    /**
64
     * Folder under which the services should be registered in the system filesystem.
65
     * The full path for registering the services will then be <code>Editors/&lt;mime-type&gt;/&lt;subfolderName&gt;</code>
66
     */
67
    public String subfolderName();
68
69
}

Return to bug 190606