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 192595
Collapse All | Expand All

(-)a/openide.filesystems/src/org/netbeans/modules/openide/filesystems/EntityCatalogRegistrationProcessor.java (+111 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.modules.openide.filesystems;
44
45
import java.io.FileNotFoundException;
46
import java.io.IOException;
47
import java.util.Arrays;
48
import java.util.HashSet;
49
import java.util.Set;
50
import javax.annotation.processing.Processor;
51
import javax.annotation.processing.RoundEnvironment;
52
import javax.annotation.processing.SupportedSourceVersion;
53
import javax.lang.model.SourceVersion;
54
import javax.lang.model.element.Element;
55
import javax.lang.model.element.PackageElement;
56
import javax.lang.model.element.TypeElement;
57
import javax.tools.StandardLocation;
58
import org.openide.filesystems.EntityCatalogRegistration;
59
import org.openide.filesystems.EntityCatalogRegistrations;
60
import org.openide.filesystems.annotations.LayerGeneratingProcessor;
61
import org.openide.filesystems.annotations.LayerGenerationException;
62
import org.openide.util.lookup.ServiceProvider;
63
import org.openide.xml.EntityCatalog;
64
65
@ServiceProvider(service=Processor.class)
66
@SupportedSourceVersion(SourceVersion.RELEASE_6)
67
public class EntityCatalogRegistrationProcessor extends LayerGeneratingProcessor {
68
69
    public @Override Set<String> getSupportedAnnotationTypes() {
70
        return new HashSet<String>(Arrays.asList(
71
                EntityCatalogRegistration.class.getCanonicalName(),
72
                EntityCatalogRegistrations.class.getCanonicalName()));
73
    }
74
75
    protected @Override boolean handleProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) throws LayerGenerationException {
76
        if (roundEnv.processingOver()) {
77
            return false;
78
        }
79
        for (Element e : roundEnv.getElementsAnnotatedWith(EntityCatalogRegistration.class)) {
80
            register(e, e.getAnnotation(EntityCatalogRegistration.class));
81
        }
82
        for (Element e : roundEnv.getElementsAnnotatedWith(EntityCatalogRegistrations.class)) {
83
            for (EntityCatalogRegistration r :e.getAnnotation(EntityCatalogRegistrations.class).value()) {
84
                register(e, r);
85
            }
86
        }
87
        return true;
88
    }
89
90
    private void register(Element e, EntityCatalogRegistration r) throws LayerGenerationException {
91
        String url = r.entity();
92
        if (!url.startsWith("/")) {
93
            url = "/" + ((PackageElement) e).getQualifiedName().toString().replace('.', '/') + "/" + url; // XXX also handle ../something.dtd sequences?
94
        }
95
        try { // XXX should there be some method in LayerGeneratingProcessor such as loadResource(String name)?
96
            try {
97
                processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", url.substring(1)).openInputStream().close();
98
            } catch (FileNotFoundException x) { // #181355
99
                try {
100
                    processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", url.substring(1)).openInputStream().close();
101
                } catch (IOException x2) {
102
                    throw x;
103
                }
104
            }
105
        } catch (IOException x) {
106
            throw new LayerGenerationException("Could not open " + url + ": " + x, e);
107
        }
108
        layer(e).file(EntityCatalog.ENTITIES_FOLDER + "/" + EntityCatalog.convertPublicId(r.publicId())).url(url).stringvalue(EntityCatalog.ORIGINAL_PUBLIC_ID, r.publicId()).write();
109
    }
110
111
}
(-)a/openide.filesystems/src/org/netbeans/modules/openide/filesystems/LayerEntityCatalog.java (+87 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.modules.openide.filesystems;
43
44
import java.io.IOException;
45
import java.net.URL;
46
import org.openide.filesystems.FileObject;
47
import org.openide.filesystems.FileUtil;
48
import org.openide.util.lookup.ServiceProvider;
49
import org.openide.xml.EntityCatalog;
50
import org.xml.sax.InputSource;
51
import org.xml.sax.SAXException;
52
53
/**
54
 * Implements standard entity catalog registration.
55
 */
56
@ServiceProvider(service = EntityCatalog.class)
57
public class LayerEntityCatalog extends EntityCatalog {
58
59
    public @Override InputSource resolveEntity(String publicID, String systemID) throws SAXException, IOException {
60
        if (publicID == null) {
61
            return null;
62
        }
63
        String id = EntityCatalog.convertPublicId(publicID);
64
        StringBuilder sb = new StringBuilder(200);
65
        sb.append(EntityCatalog.ENTITIES_FOLDER);
66
        sb.append('/');
67
        sb.append(id);
68
        FileObject fo = FileUtil.getConfigFile(sb.toString());
69
        if (fo != null) {
70
            InputSource in = new InputSource(fo.getInputStream());
71
            try {
72
                Object myPublicID = fo.getAttribute(EntityCatalog.ORIGINAL_PUBLIC_ID);
73
                if (myPublicID instanceof String) {
74
                    in.setPublicId((String) myPublicID);
75
                }
76
                URL url = fo.getURL();
77
                in.setSystemId(url.toString());
78
            } catch (IOException ex) {
79
                // do no care just no system id
80
            }
81
            return in;
82
        } else {
83
            return null;
84
        }
85
    }
86
87
}
(-)a/openide.filesystems/src/org/openide/filesystems/EntityCatalogRegistration.java (+71 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.openide.filesystems;
44
45
import java.lang.annotation.ElementType;
46
import java.lang.annotation.Retention;
47
import java.lang.annotation.RetentionPolicy;
48
import java.lang.annotation.Target;
49
import org.openide.xml.EntityCatalog;
50
51
/**
52
 * Registers an entity for use in {@link EntityCatalog}.
53
 * Normally used to register XML DTDs.
54
 * @see EntityCatalogRegistrations
55
 * @since XXX
56
 */
57
@Retention(RetentionPolicy.SOURCE)
58
@Target(ElementType.PACKAGE)
59
public @interface EntityCatalogRegistration {
60
61
    /**
62
     * Public ID of the entity.
63
     */
64
    String publicId();
65
    
66
    /**
67
     * Resource location of the entity (absolute, or relative to this package).
68
     */
69
    String entity();
70
71
}
(-)a/openide.filesystems/src/org/openide/filesystems/EntityCatalogRegistrations.java (+62 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.openide.filesystems;
44
45
import java.lang.annotation.ElementType;
46
import java.lang.annotation.Retention;
47
import java.lang.annotation.RetentionPolicy;
48
import java.lang.annotation.Target;
49
50
/**
51
 * Permits multiple {@link EntityCatalogRegistration}s on a single package.
52
 */
53
@Retention(RetentionPolicy.SOURCE)
54
@Target(ElementType.PACKAGE)
55
public @interface EntityCatalogRegistrations {
56
57
    /**
58
     * Registrations.
59
     */
60
    public EntityCatalogRegistration[] value();
61
62
}
(-)a/openide.loaders/src/org/netbeans/modules/openide/loaders/FileEntityResolver.java (-122 / +6 lines)
Lines 66-72 Link Here
66
import org.openide.filesystems.FileRenameEvent;
66
import org.openide.filesystems.FileRenameEvent;
67
import org.openide.filesystems.FileEvent;
67
import org.openide.filesystems.FileEvent;
68
import org.openide.filesystems.FileAttributeEvent;
68
import org.openide.filesystems.FileAttributeEvent;
69
import java.net.URL;
70
import java.util.logging.Level;
69
import java.util.logging.Level;
71
import java.util.logging.Logger;
70
import java.util.logging.Logger;
72
import org.openide.filesystems.FileUtil;
71
import org.openide.filesystems.FileUtil;
Lines 74-143 Link Here
74
73
75
74
76
/** 
75
/** 
77
 * Entity resolver which loads entities (typically DTDs) from fixed
76
 * Entity resolver which attaches <tt>Environment</tt> according to registrations
78
 * locations in the system file system, according to public ID.
79
 * <p>
80
 * It expects that PUBLIC has at maximum three "//" parts 
81
 * (standard // vendor // entity name // language). It is basically
82
 * converted to <tt>"/xml/entities/{vendor}/{entity_name}"</tt> resource name.
83
 * <p>
84
 * It also attaches <tt>Environment</tt> according to registrations
85
 * at <tt>/xml/lookups/</tt> area. There can be registered:
77
 * at <tt>/xml/lookups/</tt> area. There can be registered:
86
 * <tt>Environment.Provider</tt> or deprecated <tt>XMLDataObject.Processor</tt>
78
 * <tt>Environment.Provider</tt> or deprecated <tt>XMLDataObject.Processor</tt>
87
 * and <tt>XMLDataObject.Info</tt> instances.
79
 * and <tt>XMLDataObject.Info</tt> instances.
88
 * <p>
89
 * All above are core implementation features.
90
 *
91
 * @author  Jaroslav Tulach
80
 * @author  Jaroslav Tulach
92
 */
81
 */
93
@ServiceProviders({@ServiceProvider(service=Environment.Provider.class), @ServiceProvider(service=EntityCatalog.class)})
82
@ServiceProvider(service=Environment.Provider.class)
94
public final class FileEntityResolver extends EntityCatalog implements Environment.Provider {
83
public final class FileEntityResolver implements Environment.Provider {
95
    private static final String ENTITY_PREFIX = "/xml/entities"; // NOI18N
84
    private static final String LOOKUP_PREFIX = "/xml/lookups/"; // NOI18N
96
    private static final String LOOKUP_PREFIX = "/xml/lookups"; // NOI18N
97
85
98
    static final Logger ERR = Logger.getLogger(FileEntityResolver.class.getName());
86
    static final Logger ERR = Logger.getLogger(FileEntityResolver.class.getName());
99
    
87
    
100
    /** Constructor
101
     */
102
    public FileEntityResolver() {
103
    }
104
    
105
    /** Tries to find the entity on system file system.
106
     */
107
    public InputSource resolveEntity(String publicID, String systemID) throws IOException, SAXException {
108
        if (publicID == null) {
109
            return null;
110
        }
111
112
113
        String id = convertPublicId (publicID);
114
        
115
        StringBuffer sb = new StringBuffer (200);
116
        sb.append (ENTITY_PREFIX);
117
        sb.append (id);
118
        
119
        FileObject fo = FileUtil.getConfigFile (sb.toString ());
120
        if (fo != null) {
121
            
122
            // fill in InputSource instance
123
            
124
            InputSource in = new InputSource (fo.getInputStream ());
125
            try {
126
                Object myPublicID = fo.getAttribute("hint.originalPublicID");  //NOI18N
127
                if (myPublicID instanceof String) {
128
                    in.setPublicId((String)myPublicID);
129
                }                
130
                URL url = fo.getURL();
131
                in.setSystemId(url.toString());  // we get nasty nbfs: instead nbres: but it is enough                
132
            } catch (IOException ex) {
133
                // do no care just no system id
134
            }
135
            return in;
136
        } else {
137
            return null;
138
        }
139
    }
140
    
141
    /** A method that tries to find the correct lookup for given XMLDataObject.
88
    /** A method that tries to find the correct lookup for given XMLDataObject.
142
     * @return the lookup
89
     * @return the lookup
143
     */
90
     */
Lines 161-167 Link Here
161
                return null;
108
                return null;
162
            }
109
            }
163
            
110
            
164
            id = convertPublicId (id);
111
            id = EntityCatalog.convertPublicId(id);
165
            
112
            
166
            return new Lkp (id, xml);
113
            return new Lkp (id, xml);
167
        } else if (obj instanceof InstanceDataObject) {
114
        } else if (obj instanceof InstanceDataObject) {
Lines 175-181 Link Here
175
        parser.parse();
122
        parser.parse();
176
        String id = parser.getPublicId();
123
        String id = parser.getPublicId();
177
        if (id == null) return null;
124
        if (id == null) return null;
178
        id = convertPublicId (id);
125
        id = EntityCatalog.convertPublicId(id);
179
        return new Lkp (id, ido);
126
        return new Lkp (id, ido);
180
    }
127
    }
181
    
128
    
Lines 253-321 Link Here
253
        }
200
        }
254
    }
201
    }
255
202
256
    /** Converts the publicID into filesystem friendly name.
257
     * <p>
258
     * It expects that PUBLIC has at maximum three "//" parts 
259
     * (standard // vendor // entity name // language). It is basically
260
     * converted to "vendor/entity_name" resource name.
261
     *
262
     * @see EntityCatalog
263
     */
264
    @SuppressWarnings("fallthrough")
265
    private static String convertPublicId (String publicID) {
266
        char[] arr = publicID.toCharArray ();
267
268
269
        int numberofslashes = 0;
270
        int state = 0;
271
        int write = 0;
272
        OUT: for (int i = 0; i < arr.length; i++) {
273
            char ch = arr[i];
274
275
            switch (state) {
276
            case 0:
277
                // initial state 
278
                if (ch == '+' || ch == '-' || ch == 'I' || ch == 'S' || ch == 'O') {
279
                    // do not write that char
280
                    continue;
281
                }
282
                // switch to regular state
283
                state = 1;
284
                // fallthru
285
            case 1:
286
                // regular state expecting any character
287
                if (ch == '/') {
288
                    state = 2;
289
                    if (++numberofslashes == 3) {
290
                        // last part of the ID, exit
291
                        break OUT;
292
                    }
293
                    arr[write++] = '/';
294
                    continue;
295
                }
296
                break;
297
            case 2:
298
                // previous character was /
299
                if (ch == '/') {
300
                    // ignore second / and write nothing
301
                    continue;
302
                }
303
                state = 1;
304
                break;
305
            }
306
307
            // write the char into the array
308
            if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9') {
309
                arr[write++] = ch;
310
            } else {
311
                arr[write++] = '_';
312
            }
313
        }
314
315
        return new String (arr, 0, write);
316
    }
317
        
318
    
319
    /** Finds a fileobject for given ID.
203
    /** Finds a fileobject for given ID.
320
     * @param id string id
204
     * @param id string id
321
     * @param last[0] will be filled with last file object we should listen on
205
     * @param last[0] will be filled with last file object we should listen on
(-)a/openide.loaders/src/org/netbeans/modules/openide/loaders/layer.xml (-12 lines)
Lines 2-19 Link Here
2
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN" "http://www.netbeans.org/dtds/filesystem-1_2.dtd">
2
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN" "http://www.netbeans.org/dtds/filesystem-1_2.dtd">
3
<filesystem>
3
<filesystem>
4
    <folder name="xml">
4
    <folder name="xml">
5
        <folder name="entities">
6
            <folder name="NetBeans">
7
                <file name="Entity_Mapping_Registration_1_0" url="EntityCatalog.dtd" >
8
                    <attr name="hint.originalPublicID" stringvalue="-//NetBeans//Entity Mapping Registration 1.0//EN"/>
9
                </file>
10
            </folder>
11
            <folder name="NetBeans_IDE">
12
                <file name="DTD_xmlinfo" url="xmlinfo.dtd">
13
                    <attr name="hint.originalPublicID" stringvalue="-//NetBeans IDE//DTD xmlinfo//EN"/>
14
                </file>
15
            </folder>
16
        </folder>
17
        <folder name="lookups">
5
        <folder name="lookups">
18
            <folder name="NetBeans">
6
            <folder name="NetBeans">
19
                <file name="Entity_Mapping_Registration_1_0.instance">
7
                <file name="Entity_Mapping_Registration_1_0.instance">
(-)a/openide.loaders/src/org/netbeans/modules/openide/loaders/package-info.java (+50 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
@EntityCatalogRegistrations({
44
    @EntityCatalogRegistration(publicId="-//NetBeans//Entity Mapping Registration 1.0//EN", entity="EntityCatalog.dtd"),
45
    @EntityCatalogRegistration(publicId="-//NetBeans IDE//DTD xmlinfo//EN", entity="xmlinfo.dtd")
46
})
47
package org.netbeans.modules.openide.loaders;
48
49
import org.openide.filesystems.EntityCatalogRegistration;
50
import org.openide.filesystems.EntityCatalogRegistrations;
(-)a/openide.util/src/org/openide/xml/EntityCatalog.java (-12 / +88 lines)
Lines 62-82 Link Here
62
 * <p>You can register your own instances via lookup to add to the resolver pool,
62
 * <p>You can register your own instances via lookup to add to the resolver pool,
63
 * but for reasons of performance and predictability during startup it is best to provide
63
 * but for reasons of performance and predictability during startup it is best to provide
64
 * the entity (e.g. some DTD you define) as the contents of a file in
64
 * the entity (e.g. some DTD you define) as the contents of a file in
65
 * the system filesystem, in the <samp>/xml/entities/</samp> folder, where the file path
65
 * the system filesystem, in {@link #ENTITIES_FOLDER}, where the file path
66
 * beneath this folder is based on the public ID as follows:
66
 * beneath this folder is based on the public ID as per {@link #convertPublicId}.
67
 * <ol>
68
 * <li>US-ASCII alphanumeric characters and '_' are left as is.
69
 * <li>Spaces and various punctuation are converted to '_' (one per character).
70
 * <li>Initial '-//' is dropped.
71
 * <li>Final '//EN' is dropped.
72
 * <li>Exactly two forward slashes in a row are converted to one.
73
 * </ol>
74
 * Thus for example the public ID <samp>-//NetBeans//Entity&nbsp;Mapping&nbsp;Registration&nbsp;1.0//EN</samp>
67
 * Thus for example the public ID <samp>-//NetBeans//Entity&nbsp;Mapping&nbsp;Registration&nbsp;1.0//EN</samp>
75
 * would be looked for in the file <samp>/xml/entities/NetBeans/Entity_Mapping_Registration_1_0</samp>.
68
 * would be looked for in the file <samp>xml/entities/NetBeans/Entity_Mapping_Registration_1_0</samp>.
76
 * Naturally this only works if you are defining a fixed number of entities.
69
 * Naturally this only works if you are defining a fixed number of entities.
77
 * <p>It is recommended that the entity file in <samp>/xml/entities/</samp> also be given a file
70
 * <p>It is recommended that the entity file in <samp>/xml/entities/</samp> also be given a file
78
 * attribute named <code>hint.originalPublicID</code> with a string value giving the public ID.
71
 * attribute named {@link #ORIGINAL_PUBLIC_ID}.
79
 * This permits {@code org.netbeans.modules.xml.catalog} to display the entry properly.
80
 * @author  Petr Kuzel
72
 * @author  Petr Kuzel
81
 */
73
 */
82
public abstract class EntityCatalog implements EntityResolver {
74
public abstract class EntityCatalog implements EntityResolver {
Lines 102-107 Link Here
102
    }
94
    }
103
95
104
    /**
96
    /**
97
     * Location where entities are declared.
98
     * @since XXX
99
     */
100
    public static final String ENTITIES_FOLDER = "xml/entities"; // NOI18N
101
102
    /**
103
     * File attribute giving the String public ID for a registration, since {@link #convertPublicId} is not reversible.
104
     * This permits {@code org.netbeans.modules.xml.catalog} to display the entry properly.
105
     * @since XXX
106
     */
107
    public static final String ORIGINAL_PUBLIC_ID = "hint.originalPublicID"; // NOI18N
108
109
    /**
110
     * Converts a public ID into a layer-friendly name.
111
     * It expects that the ID has at most three "//" divisions
112
     * (standard // vendor // entity name // language). It is basically
113
     * converted to "vendor/entity_name" resource name. Specifically:
114
     * <ol>
115
     * <li>US-ASCII alphanumeric characters and '_' are left as is.
116
     * <li>Spaces and various punctuation are converted to '_' (one per character).
117
     * <li>Initial '-//' is dropped.
118
     * <li>Final '//EN' is dropped.
119
     * <li>Exactly two forward slashes in a row are converted to one.
120
     * </ol>
121
     * @param publicID e.g. {@code -//NetBeans//Entity Mapping Registration 1.0//EN}
122
     * @return e.g. {@code NetBeans/Entity_Mapping_Registration_1_0}
123
     * @since XXX
124
     */
125
    @SuppressWarnings("fallthrough")
126
    public static String convertPublicId(String publicID) {
127
        char[] arr = publicID.toCharArray ();
128
129
130
        int numberofslashes = 0;
131
        int state = 0;
132
        int write = 0;
133
        OUT: for (int i = 0; i < arr.length; i++) {
134
            char ch = arr[i];
135
136
            switch (state) {
137
            case 0:
138
                // initial state
139
                if (ch == '+' || ch == '-' || ch == 'I' || ch == 'S' || ch == 'O') {
140
                    // do not write that char
141
                    continue;
142
                }
143
                // switch to regular state
144
                state = 1;
145
                // fallthru
146
            case 1:
147
                // regular state expecting any character
148
                if (ch == '/') {
149
                    state = 2;
150
                    if (++numberofslashes == 3) {
151
                        // last part of the ID, exit
152
                        break OUT;
153
                    }
154
                    arr[write++] = '/';
155
                    continue;
156
                }
157
                break;
158
            case 2:
159
                // previous character was /
160
                if (ch == '/') {
161
                    // ignore second / and write nothing
162
                    continue;
163
                }
164
                state = 1;
165
                break;
166
            }
167
168
            // write the char into the array
169
            if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9') {
170
                arr[write++] = ch;
171
            } else {
172
                arr[write++] = '_';
173
            }
174
        }
175
176
        boolean startsWithSlash = arr[0] == '/';
177
        return new String (arr, startsWithSlash ? 1 : 0, startsWithSlash ? write - 1 : write);
178
    }
179
180
    /**
105
     * This catalog is forwarding implementation.
181
     * This catalog is forwarding implementation.
106
     */
182
     */
107
    private static class Forwarder extends EntityCatalog {
183
    private static class Forwarder extends EntityCatalog {
(-)a/openide.util/test/unit/src/org/openide/xml/EntityCatalogTest.java (+58 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.openide.xml;
44
45
import org.netbeans.junit.NbTestCase;
46
47
public class EntityCatalogTest extends NbTestCase {
48
49
    public EntityCatalogTest(String n) {
50
        super(n);
51
    }
52
53
    public void testConvertPublicId() throws Exception {
54
        assertEquals("NetBeans/Entity_Mapping_Registration_1_0", EntityCatalog.convertPublicId("-//NetBeans//Entity Mapping Registration 1.0//EN"));
55
        assertEquals("DTD_XMLCatalog/EN", EntityCatalog.convertPublicId("-//DTD XMLCatalog//EN")); // weird but needed for compat
56
    }
57
58
}
(-)a/xml.catalog/src/org/netbeans/modules/xml/catalog/impl/SystemCatalogReader.java (-2 / +2 lines)
Lines 84-95 Link Here
84
        
84
        
85
        // inspect system/xml/entities
85
        // inspect system/xml/entities
86
        
86
        
87
        FileObject root = FileUtil.getConfigFile("xml/entities");
87
        FileObject root = FileUtil.getConfigFile(EntityCatalog.ENTITIES_FOLDER);
88
        Enumeration en = root.getChildren(true);
88
        Enumeration en = root.getChildren(true);
89
        while (en.hasMoreElements()) {
89
        while (en.hasMoreElements()) {
90
            FileObject next = (FileObject) en.nextElement();
90
            FileObject next = (FileObject) en.nextElement();
91
            if (next.isData()) {
91
            if (next.isData()) {
92
                Object hint = next.getAttribute("hint.originalPublicID");
92
                Object hint = next.getAttribute(EntityCatalog.ORIGINAL_PUBLIC_ID);
93
                if (hint instanceof String) {
93
                if (hint instanceof String) {
94
                    set.add(hint);
94
                    set.add(hint);
95
                    found = true;
95
                    found = true;
(-)a/xml.catalog/src/org/netbeans/modules/xml/catalog/impl/package-info.java (+50 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
@EntityCatalogRegistrations({
44
    @EntityCatalogRegistration(publicId="-//DTD XMLCatalog//EN", entity="XCatalog-0_4.dtd"),
45
    @EntityCatalogRegistration(publicId="-//DTD XCatalog//EN", entity="XCatalog-0_2.dtd")
46
})
47
package org.netbeans.modules.xml.catalog.impl;
48
49
import org.openide.filesystems.EntityCatalogRegistration;
50
import org.openide.filesystems.EntityCatalogRegistrations;
(-)a/xml.catalog/src/org/netbeans/modules/xml/catalog/resources/mf-layer.xml (-13 / +1 lines)
Lines 92-111 Link Here
92
92
93
    <!-- register entities with system entity resolver -->
93
    <!-- register entities with system entity resolver -->
94
    <folder name="xml">
94
    <folder name="xml">
95
        <folder name="entities">
96
            <folder name="DTD_XMLCatalog">
97
                <file name="EN" url="nbres:/org/netbeans/modules/xml/catalog/impl/XCatalog-0_4.dtd">
98
                    <attr name="hint.originalPublicID" stringvalue="-//DTD XMLCatalog//EN"/>
99
                </file>
100
            </folder>
101
            <folder name="DTD_XCatalog">
102
                <file name="EN" url="nbres:/org/netbeans/modules/xml/catalog/impl/XCatalog-0_2.dtd">
103
                    <attr name="hint.originalPublicID" stringvalue="-//DTD XCatalog//EN"/>
104
                </file>
105
            </folder>
106
        </folder>
107
        <folder name="catalogs">
95
        <folder name="catalogs">
108
            <file name="UserXMLCatalog.xml" url="nbres:/org/netbeans/modules/xml/catalog/resources/UserXMLCatalog.xml"/>
96
            <file name="UserXMLCatalog.xml" url="UserXMLCatalog.xml"/>
109
        </folder>
97
        </folder>
110
    </folder>
98
    </folder>
111
99

Return to bug 192595