KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > catalog > impl > SystemCatalogReader


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.netbeans.modules.xml.catalog.impl;
20
21 import java.awt.Image JavaDoc;
22 import java.lang.reflect.*;
23 import java.util.*;
24 import java.io.Serializable JavaDoc;
25
26 import org.xml.sax.*;
27
28 import org.openide.util.Lookup;
29 import org.openide.xml.EntityCatalog;
30 import org.openide.filesystems.*;
31
32 import org.netbeans.modules.xml.catalog.spi.*;
33 import java.io.IOException JavaDoc;
34
35 /**
36  * Read mapping redistered in IDE system resolver/catalog.
37  * It uses knowledge of IDE catalog implementation.
38  *
39  * @author Petr Kuzel
40  * @version 1.0
41  *
42  */

43 public class SystemCatalogReader implements EntityResolver, CatalogReader, Serializable JavaDoc {
44
45     /** Serial Version UID */
46     private static final long serialVersionUID = -6353123780493006631L;
47     
48     /** Creates new SystemCatalogReader */
49     public SystemCatalogReader() {
50     }
51
52     /**
53      * Get String iterator representing all public IDs registered in catalog.
54      */

55     public Iterator getPublicIDs() {
56         
57         HashSet set = new HashSet();
58         boolean found = false;
59         
60         // inspect system/xml/entities
61

62         FileObject root = Repository.getDefault ().getDefaultFileSystem().findResource("xml/entities");
63         Enumeration en = root.getChildren(true);
64         while (en.hasMoreElements()) {
65             FileObject next = (FileObject) en.nextElement();
66             if (next.isData()) {
67                 Object JavaDoc hint = next.getAttribute("hint.originalPublicID");
68                 if (hint instanceof String JavaDoc) {
69                     set.add(hint);
70                     found = true;
71                 } else {
72                     // we could guess it, BUT it is too dangerous
73
}
74             }
75         }
76                 
77         // get instance of system resolver that contains the catalog
78

79         Lookup.Template templ = new Lookup.Template(EntityCatalog.class);
80         Lookup.Result res = Lookup.getDefault().lookup(templ);
81
82         Iterator it = res.allInstances().iterator();
83         while (it.hasNext()) {
84             EntityCatalog next = (EntityCatalog) it.next();
85
86             try {
87                 
88                 //BACKWARD COMPATABILITY it is explicit knowledge how it worked in NetBeans 3.2
89
Field uriMapF = next.getClass().getDeclaredField("id2uri"); // NOI18N
90
if (uriMapF == null) continue;
91
92                 uriMapF.setAccessible(true);
93                 found = true;
94
95                 Map uris = (Map) uriMapF.get(next);
96                 if (uris != null) {
97                    set.addAll(uris.keySet());
98                 }
99             } catch (NoSuchFieldException JavaDoc ex) {
100                 // ignore unknown implementation
101
} catch (IllegalAccessException JavaDoc ex) {
102                 // ignore unknown implementation
103
} catch (IllegalArgumentException JavaDoc ex) {
104                 // ignore unknown implementation
105
}
106         }
107         
108         return (found == false) ? null : set.iterator();
109     }
110     
111     
112     /**
113      * Get registered systemid for given public Id or null if not registered.
114      */

115     public String JavaDoc getSystemID(String JavaDoc publicId) {
116         
117         try {
118             EntityResolver sysResolver = EntityCatalog.getDefault();
119             
120             if (sysResolver == null) return null;
121
122             InputSource in = sysResolver.resolveEntity(publicId, null);
123             if (in == null) return null;
124             
125             return in.getSystemId();
126             
127         } catch (java.io.IOException JavaDoc ex) {
128             return null;
129         } catch (SAXException ex) {
130             return null;
131         }
132     }
133
134     /**
135      * No refresh is necessary, it is always fresh in RAM.
136      */

137     public void refresh() {
138     }
139     
140    
141     /**
142      * Optional operation allowing to listen at catalog for changes.
143      * @throws UnsupportedOpertaionException if not supported by the implementation.
144      */

145     public void addCatalogListener(CatalogListener l) {
146         throw new UnsupportedOperationException JavaDoc();
147     }
148     
149     /**
150      * Optional operation couled with addCatalogListener.
151      * @see addCatalogListener
152      */

153     public void removeCatalogListener(CatalogListener l) {
154         throw new UnsupportedOperationException JavaDoc();
155     }
156
157     /*
158      * System catalog is singleton.
159      */

160     public boolean equals(Object JavaDoc obj) {
161         if (obj == null) return false;
162         return getClass().equals(obj.getClass());
163     }
164     
165     public int hashCode() {
166         return getClass().hashCode();
167     }
168     
169     /**
170      * Delegate to entity catalog to resolve unlisted elements.
171      */

172     public InputSource resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException, IOException JavaDoc {
173         return EntityCatalog.getDefault().resolveEntity(publicId, systemId);
174     }
175     
176     /**
177      * Get registered URI for the given name or null if not registered.
178      * @return null if not registered
179      */

180     public String JavaDoc resolveURI(String JavaDoc name) {
181         return null;
182     }
183     /**
184      * Get registered URI for the given publicId or null if not registered.
185      * @return null if not registered
186      */

187     public String JavaDoc resolvePublic(String JavaDoc publicId) {
188         return null;
189     }
190     
191 }
192
Popular Tags