KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > catalog > impl > sun > Catalog


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.xml.catalog.impl.sun;
21
22 import java.awt.Image JavaDoc;
23 import java.io.*;
24 import java.beans.*;
25 import java.util.*;
26 import java.net.*;
27
28 import org.xml.sax.*;
29
30
31 import org.netbeans.modules.xml.catalog.spi.*;
32
33 import org.apache.xml.resolver.tools.CatalogResolver;
34 import org.apache.xml.resolver.CatalogException;
35 import org.apache.xml.resolver.CatalogManager;
36
37 /**
38  * SPI implementation that bridges to Sun's Resolvers 1.1.
39  * <p>
40  * It uses heavily lazy initialization to eliminate differences between an
41  * instance constructed by the contructor <b>or</b> by deserialization process.
42  * The approach also speeds up setup time.
43  *
44  * @author Petr Kuzel
45  */

46 public final class Catalog
47     implements org.netbeans.modules.xml.catalog.spi.CatalogReader, CatalogDescriptor, Serializable, EntityResolver{
48
49     private static final long serialVersionUID = 123659121L;
50         
51     private transient PropertyChangeSupport pchs;
52
53     private transient EntityResolver peer;
54
55     private transient String JavaDoc desc;
56     
57     // a catalog source location
58
private String JavaDoc location;
59     
60     // a public preference
61
private boolean preference = true;
62     
63     private static final String JavaDoc PROP_LOCATION = "cat-loc";
64     
65     private static final String JavaDoc PROP_PREF_PUBLIC = "cat-pref";
66
67     private static final String JavaDoc PROP_DESC = CatalogDescriptor.PROP_CATALOG_DESC;
68     
69     /** Creates a new instance of Catalog */
70     public Catalog() {
71     }
72
73     /**
74      * Deserialization 'constructor'.
75      */

76     public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException JavaDoc {
77         in.defaultReadObject();
78         
79         // lazy init transient fields, see getPCHS() and getPeer() methods
80
setShortDescription(Util.THIS.getString("MSG_prepared", location));
81     }
82     
83     /**
84      * Set Catalog source (a URL).
85      */

86     public synchronized void setLocation(String JavaDoc location) {
87         String JavaDoc old = this.location;
88         this.location = location;
89         peer = null; // lazy init
90
getPCHS().firePropertyChange(PROP_LOCATION, old, location);
91         updateDisplayName();
92     }
93
94     /**
95      * Access the location value.
96      */

97     public String JavaDoc getLocation() {
98         return location;
99     }
100     
101     /**
102      * Set public resolving preference.
103      */

104     public void setPreferPublic(boolean val) {
105         boolean old = preference;
106         this.preference = val;
107         getPCHS().firePropertyChange(PROP_LOCATION, old, val);
108     }
109
110     /**
111      * Access the public ID preference flag.
112      */

113     public boolean isPreferPublic() {
114         return preference;
115     }
116     
117     /**
118      * Optional operation allowing to listen at catalog for changes.
119      * @throws UnsupportedOpertaionException if not supported by the implementation.
120      */

121     public void addCatalogListener(CatalogListener l) {
122         throw new UnsupportedOperationException JavaDoc();
123     }
124     
125     /** Registers new listener. */
126     public void addPropertyChangeListener(PropertyChangeListener l) {
127         getPCHS().addPropertyChangeListener(l);
128     }
129     
130     /**
131      * @return I18N display name
132      */

133     public String JavaDoc getDisplayName() {
134         String JavaDoc src = location;
135         if (src == null || "".equals(src.trim())) {
136             return Util.THIS.getString("PROP_missing_location");
137         } else {
138             return Util.THIS.getString("TITLE_catalog", location);
139         }
140     }
141
142     public String JavaDoc getName() {
143         return getClass() + location + preference;
144     }
145     
146     /**
147      * Notify listeners that display name have changed.
148      */

149     public void updateDisplayName() {
150         String JavaDoc name = getDisplayName();
151         getPCHS().firePropertyChange(CatalogDescriptor.PROP_CATALOG_NAME, null, name);
152     }
153     
154     /**
155      * Return visuaized state of given catalog.
156      * @param type of icon defined by JavaBeans specs
157      * @return icon representing current state or null
158      */

159     public Image JavaDoc getIcon(int type) {
160         return null;
161     }
162     
163     /**
164      * Get String iterator representing all public IDs registered in catalog.
165      * @return null if cannot proceed, try later.
166      */

167     public Iterator getPublicIDs() {
168         Object JavaDoc p = getPeer();
169         if (p instanceof org.apache.xml.resolver.tools.CatalogResolver) {
170             org.apache.xml.resolver.Catalog cat = ((org.apache.xml.resolver.tools.CatalogResolver) p).getCatalog();
171             return cat.getPublicIDs();
172         }
173         return null;
174     }
175     
176     /**
177      * @return I18N short description
178      */

179     public String JavaDoc getShortDescription() {
180         return desc;
181     }
182     
183     public void setShortDescription(String JavaDoc desc) {
184         String JavaDoc old = this.desc;
185         this.desc = desc;
186         getPCHS().firePropertyChange(PROP_DESC, old, desc);
187     }
188     
189     /**
190      * Get registered systemid for given public Id or null if not registered.
191      * @return null if not registered
192      */

193     public String JavaDoc getSystemID(String JavaDoc publicId) {
194         Object JavaDoc p = getPeer();
195         if (p instanceof org.apache.xml.resolver.tools.CatalogResolver)
196             try {
197                 return ((org.apache.xml.resolver.tools.CatalogResolver) p).getCatalog().resolveSystem(publicId);
198             } catch (java.net.MalformedURLException JavaDoc ex) {}
199               catch (java.io.IOException JavaDoc ex) {}
200         return null;
201     }
202     
203     /**
204      * Refresh content according to content of mounted catalog.
205      */

206     public synchronized void refresh() {
207         peer = createPeer(location, preference);
208     }
209     
210     /**
211      * Optional operation couled with addCatalogListener.
212      * @throws UnsupportedOpertaionException if not supported by the implementation.
213      */

214     public void removeCatalogListener(CatalogListener l) {
215         throw new UnsupportedOperationException JavaDoc();
216     }
217     
218     /** Unregister the listener. */
219     public void removePropertyChangeListener(PropertyChangeListener l) {
220         getPCHS().removePropertyChangeListener(l);
221     }
222
223     /**
224      * Delegate entity resution process to peer if exists.
225      */

226     public InputSource resolveEntity(String JavaDoc publicID, String JavaDoc systemID) throws SAXException, IOException {
227         return getPeer().resolveEntity(publicID, systemID);
228     }
229     
230 /** We are a key and must retain equals immutability
231     public boolean equals(Object obj) {
232         if (obj instanceof Catalog) {
233             Catalog cat = (Catalog) obj;
234             if (this.location == null && cat.location != null) return false;
235             if ((this.location != null && this.location.equals(cat.location)) == false) return false;
236             return (this.preference == cat.preference);
237         }
238         return false;
239     }
240     
241     
242     public int hashCode() {
243         return (location != null ? location.hashCode() : 0) ^ (preference?13:7);
244     }
245 */

246     /**
247      * Factory new peer and load data into it.
248      * As a side effect set short description.
249      * @return EntityResolver never <code>null</code>
250      */

251     private EntityResolver createPeer(String JavaDoc location, boolean pref) {
252         try {
253             CatalogManager manager = new CatalogManager(null);
254             manager.setUseStaticCatalog(false);
255             manager.setPreferPublic(pref);
256
257             CatalogResolver catalogResolver = new CatalogResolver(manager);
258             org.apache.xml.resolver.Catalog cat = catalogResolver.getCatalog();
259             cat.parseCatalog(new URL(location));
260             setShortDescription(Util.THIS.getString("DESC_loaded"));
261             return catalogResolver;
262         } catch (IOException ex) {
263             setShortDescription(Util.THIS.getString("DESC_error_loading", ex.getLocalizedMessage()));
264             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("I/O error loading catalog " + location, ex);
265         }
266         
267         // return dumb peer
268
return new EntityResolver () {
269             public InputSource resolveEntity(String JavaDoc p, String JavaDoc s) {
270                 return null;
271             }
272         };
273     }
274     
275     /**
276      * Lazy init PropertyChangeSupport and return it.
277      */

278     private synchronized PropertyChangeSupport getPCHS() {
279         if (pchs == null) pchs = new PropertyChangeSupport(this);
280         return pchs;
281     }
282
283     /**
284      * Lazy init peer and return it.
285      */

286     private synchronized EntityResolver getPeer() {
287         
288         if (peer == null) peer = createPeer(location, preference);
289         return peer;
290     }
291
292     /**
293      * Get registered URI for the given name or null if not registered.
294      * @return null if not registered
295      */

296     public String JavaDoc resolveURI(String JavaDoc name) {
297         Object JavaDoc p = getPeer();
298         if (p instanceof org.apache.xml.resolver.tools.CatalogResolver)
299             try {
300                 return ((org.apache.xml.resolver.tools.CatalogResolver) p).getCatalog().resolveURI(name);
301             } catch (java.net.MalformedURLException JavaDoc ex) {}
302               catch (java.io.IOException JavaDoc ex) {}
303         return null;
304     }
305     /**
306      * Get registered URI for the given publicId or null if not registered.
307      * @return null if not registered
308      */

309     public String JavaDoc resolvePublic(String JavaDoc publicId) {
310         Object JavaDoc p = getPeer();
311         if (p instanceof org.apache.xml.resolver.tools.CatalogResolver)
312             try {
313                 return ((org.apache.xml.resolver.tools.CatalogResolver) p).getCatalog().resolvePublic(publicId,null);
314             } catch (java.net.MalformedURLException JavaDoc ex) {}
315               catch (java.io.IOException JavaDoc ex) {}
316         return null;
317     }
318     
319 }
320
Popular Tags