KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > xml > EntityCatalog


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 package org.openide.xml;
20
21 import org.openide.util.*;
22 import org.xml.sax.*;
23
24 import java.io.*;
25
26 import java.util.*;
27
28 import javax.swing.event.*;
29
30
31 /**
32  * Entity resolver resolving all entities registered by modules.
33  * Use {@link #getDefault} to get the master instance in system. Any parser working with
34  * unknown XML documents should use it to avoid unnecessary Internet
35  * connections.
36  *
37  * <p>You can register your own instances via lookup to add to the resolver pool,
38  * for example using an XML file in the format defined by {@link #PUBLIC_ID},
39  * but for reasons of performance and predictability during startup it is best to provide
40  * the entity (e.g. some DTD you define) as the contents of a file in
41  * the system filesystem, in the <samp>/xml/entities/</samp> folder, where the file path
42  * beneath this folder is based on the public ID as follows:
43  * <ol>
44  * <li>US-ASCII alphanumeric characters and '_' are left as is.
45  * <li>Spaces and various punctuation are converted to '_' (one per character).
46  * <li>Initial '-//' is dropped.
47  * <li>Final '//EN' is dropped.
48  * <li>Exactly two forward slashes in a row are converted to one.
49  * </ol>
50  * Thus for example the public ID <samp>-//NetBeans//Entity&nbsp;Mapping&nbsp;Registration&nbsp;1.0//EN</samp>
51  * would be looked for in the file <samp>/xml/entities/NetBeans/Entity_Mapping_Registration_1_0</samp>.
52  * Naturally this only works if you are defining a fixed number of entities.
53  * <p>It is recommended that the entity file in <samp>/xml/entities/</samp> also be given a file
54  * attribute named <code>hint.originalPublicID</code> with a string value giving the public ID.
55  *
56  * @author Petr Kuzel
57  * @since release 3.2
58  */

59 public abstract class EntityCatalog implements EntityResolver {
60     /**
61      * DOCTYPE public ID defining grammar used for entity registrations.
62      */

63     public static final String JavaDoc PUBLIC_ID = "-//NetBeans//Entity Mapping Registration 1.0//EN"; // NOI18N
64
private static EntityCatalog instance = new Forwarder();
65
66     /** Get a master entity catalog which can delegate to any others that have
67      * been registered via lookup.
68      */

69     public static EntityCatalog getDefault() {
70         return instance;
71     }
72
73     /**
74      * This catalog is forwarding implementation.
75      */

76     private static class Forwarder extends EntityCatalog {
77         private Lookup.Result<EntityCatalog> result;
78
79         Forwarder() {
80         }
81
82         public InputSource resolveEntity(String JavaDoc publicID, String JavaDoc systemID)
83         throws IOException, SAXException {
84             if (result == null) {
85                 Lookup.Template<EntityCatalog> temp = new Lookup.Template<EntityCatalog>(EntityCatalog.class);
86                 result = Lookup.getDefault().lookup(temp);
87             }
88
89             for (EntityCatalog res : result.allInstances()) {
90                 // using resolver's method because EntityCatalog extends EntityResolver
91
InputSource is = res.resolveEntity(publicID, systemID);
92
93                 if (is != null) {
94                     return is;
95                 }
96             }
97
98             return null;
99         }
100     }
101 }
102
Popular Tags