KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openide > loaders > RuntimeCatalog


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
20 package org.netbeans.modules.openide.loaders;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.openide.xml.EntityCatalog;
27 import org.xml.sax.InputSource JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29
30 /**
31  * Implements non-persistent catalog functionality as EntityResolver.
32  * <p>Registations using this resolver are:
33  * <li>transient
34  * <li>of the hihgest priority
35  * <li>last registration prevails
36  */

37 public final class RuntimeCatalog extends EntityCatalog {
38
39     /** Public constructor for lookup. */
40     public RuntimeCatalog() {}
41     
42     // table mapping public IDs to (local) URIs
43
private Map JavaDoc<String JavaDoc,String JavaDoc> id2uri;
44     
45     // tables mapping public IDs to resources and classloaders
46
private Map JavaDoc<String JavaDoc,String JavaDoc> id2resource;
47     private Map JavaDoc<String JavaDoc,ClassLoader JavaDoc> id2loader;
48     
49     /** SAX entity resolver */
50     public InputSource JavaDoc resolveEntity(String JavaDoc name, String JavaDoc uri) throws IOException JavaDoc, SAXException JavaDoc {
51         
52         InputSource JavaDoc retval;
53         String JavaDoc mappedURI = name2uri(name);
54         InputStream JavaDoc stream = mapResource(name);
55         
56         // prefer explicit URI mappings, then bundled resources...
57
if (mappedURI != null) {
58             retval = new InputSource JavaDoc(mappedURI);
59             retval.setPublicId(name);
60             return retval;
61             
62         } else if (stream != null) {
63             // XXX unused var, what is it for?
64
uri = "java:resource:" + id2resource.get(name); // NOI18N
65
retval = new InputSource JavaDoc(stream);
66             retval.setPublicId(name);
67             return retval;
68             
69         } else {
70             return null;
71         }
72     }
73     
74     public void registerCatalogEntry(String JavaDoc publicId, String JavaDoc uri) {
75         if (id2uri == null) {
76             id2uri = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
77         }
78         id2uri.put(publicId, uri);
79     }
80     
81     /** Map publicid to a resource accessible by a classloader. */
82     public void registerCatalogEntry(String JavaDoc publicId, String JavaDoc resourceName, ClassLoader JavaDoc loader) {
83         if (id2resource == null) {
84             id2resource = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
85         }
86         id2resource.put(publicId, resourceName);
87         
88         if (loader != null) {
89             if (id2loader == null) {
90                 id2loader = new HashMap JavaDoc<String JavaDoc,ClassLoader JavaDoc>();
91             }
92             id2loader.put(publicId, loader);
93         }
94     }
95     
96     // maps the public ID to an alternate URI, if one is registered
97
private String JavaDoc name2uri(String JavaDoc publicId) {
98         
99         if (publicId == null || id2uri == null) {
100             return null;
101         }
102         return id2uri.get(publicId);
103     }
104     
105     
106     // return the resource as a stream
107
private InputStream JavaDoc mapResource(String JavaDoc publicId) {
108         if (publicId == null || id2resource == null) {
109             return null;
110         }
111         
112         String JavaDoc resourceName = id2resource.get(publicId);
113         ClassLoader JavaDoc loader = null;
114         
115         if (resourceName == null) {
116             return null;
117         }
118         
119         if (id2loader != null) {
120             loader = id2loader.get(publicId);
121         }
122         
123         if (loader == null) {
124             return ClassLoader.getSystemResourceAsStream(resourceName);
125         }
126         return loader.getResourceAsStream(resourceName);
127     }
128     
129 }
130
Popular Tags