KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22 import java.io.*;
23 import java.beans.*;
24 import java.util.*;
25
26 import org.xml.sax.EntityResolver JavaDoc;
27 import org.xml.sax.InputSource JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29
30 import org.openide.util.*;
31
32 import org.netbeans.modules.xml.catalog.spi.*;
33 import org.netbeans.modules.xml.catalog.lib.*;
34
35 /**
36  * An abstract catalog implementation featuring firing support and
37  * properties describing common catalog content.
38  * <p>
39  * It implements but does not declare that implements varios methods
40  * from EntityResolver, CatalogDescriptor and CatalogReader interfaces.
41  *
42  * @author Petr Kuzel
43  */

44 public abstract class AbstractCatalog {
45
46     /** Public identifier mappings. */
47     private Map publicMap = new HashMap();
48
49     /** System identifier mappings (aliases). */
50     private Map systemMap = new HashMap();
51
52     private String JavaDoc location;
53     
54     private Vector listeners;
55     
56     // catalog delegation and chaining
57

58     /** Delegates. */
59     private Map delegate = new HashMap();
60     
61     /** Delegates ordering. */
62     private Vector delegateOrder = new Vector();
63
64     /** Contains patch catalogs. */
65     protected Vector extenders = new Vector();
66     
67     //
68
// Public methods
69
//
70

71     /**
72      * Set catalog location URI.
73      */

74     public void setLocation(String JavaDoc location) {
75         this.location = location;
76     }
77     
78     public String JavaDoc getLocation() {
79         return location;
80     }
81     
82     
83     /**
84      * Optional operation allowing to listen at catalog for changes.
85      * @throws UnsupportedOpertaionException if not supported by the implementation.
86      */

87     public synchronized void addCatalogListener(CatalogListener l) {
88         if (listeners == null) listeners = new Vector(2);
89         listeners.add(l);
90     }
91     
92     /**
93      * Optional operation couled with addCatalogListener.
94      * @see addCatalogListener
95      */

96     public synchronized void removeCatalogListener(CatalogListener l) {
97         if (listeners == null) return;
98         if (listeners != null) listeners.remove(l);
99         if (listeners.isEmpty()) listeners = null;
100     }
101     
102
103     protected void notifyInvalidate() {
104         
105         CatalogListener[] lis = null;
106         
107         synchronized (this) {
108             if (listeners == null || listeners.isEmpty()) return;
109             lis = (CatalogListener[]) listeners.toArray(new CatalogListener[0]);
110         }
111         
112         for (int i = 0; i<lis.length; i++) {
113             lis[i].notifyInvalidate();
114         }
115     }
116     
117     /**
118      * Adds a public to system identifier mapping.
119      *
120      * @param publicId The public identifier, or "key".
121      * @param systemId The system identifier, or "value".
122      */

123     public void addPublicMapping(String JavaDoc publicId, String JavaDoc systemId) {
124         publicMap.put(publicId, systemId);
125     }
126
127     /**
128      * Removes a public identifier mapping.
129      *
130      * @param publicId The public identifier to remove.
131      */

132     public void removePublicMapping(System JavaDoc publicId) {
133         publicMap.remove(publicId);
134     }
135
136     /** Returns an enumeration of public identifier mapping keys. */
137     public Iterator getPublicMappingKeys() {
138         return publicMap.keySet().iterator();
139     }
140
141     /**
142      * Returns a public identifier mapping.
143      *
144      * @param publicId The public identifier, or "key".
145      *
146      * @return Returns the system identifier value or null if there
147      * is no mapping defined.
148      */

149     public String JavaDoc getPublicMapping(String JavaDoc publicId) {
150         return (String JavaDoc)publicMap.get(publicId);
151     }
152
153     /**
154      * Adds a system identifier alias.
155      *
156      * @param publicId The system identifier "key".
157      * @param systemId The system identifier "value".
158      */

159     public void addSystemMapping(String JavaDoc systemId1, String JavaDoc systemId2) {
160         systemMap.put(systemId1, systemId2);
161     }
162
163     /**
164      * Removes a system identifier alias.
165      *
166      * @param systemId The system identifier to remove.
167      */

168     public void removeSystemMapping(String JavaDoc systemId) {
169         systemMap.remove(systemId);
170     }
171
172     /** Returns an enumeration of system identifier mapping keys. */
173     public Iterator getSystemMappingKeys() {
174         return systemMap.keySet().iterator();
175     }
176
177     /**
178      * Clean content of all internal structures
179      */

180     protected void clearAll() {
181         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("AbstractCatalog: clearing maps"); // NOI18N
182

183         publicMap.clear();
184         systemMap.clear();
185         delegate.clear();
186         delegateOrder.clear();
187         extenders.clear();
188     }
189     
190     /**
191      * Returns a system identifier alias.
192      *
193      * @param systemId The system identifier "key".
194      *
195      * @return Returns the system identifier alias value or null if there
196      * is no alias defined.
197      */

198     public String JavaDoc getSystemMapping(String JavaDoc systemId) {
199         return (String JavaDoc)systemMap.get(systemId);
200     }
201
202     
203     /**
204      * Get String iterator representing all public IDs registered in catalog.
205      * @return null if cannot proceed, try later.
206      */

207     public Iterator getPublicIDs() {
208         return getPublicIDs(""); // NOI18N
209
}
210
211     /**
212      * Obtain public IDs that starts with given prefix.
213      */

214     private Iterator getPublicIDs(String JavaDoc prefix) {
215
216         if (prefix == null) throw new IllegalArgumentException JavaDoc();
217
218         IteratorIterator set = new IteratorIterator();
219         set.add(getPublicMappingKeys());
220         
221         Iterator it = extenders.iterator();
222         while (it.hasNext()) {
223             set.add(((AbstractCatalog) it.next()).getPublicIDs());
224         }
225
226         Enumeration en = getDelegateCatalogKeys();
227         while (en.hasMoreElements()) {
228             String JavaDoc _prefix = (String JavaDoc) en.nextElement();
229             AbstractCatalog delegee = (AbstractCatalog) delegate.get(_prefix);
230             set.add(delegee.getPublicIDs(_prefix));
231         }
232
233         return new FilterIterator(set, new PrefixFilter(prefix));
234     }
235     
236     private class PrefixFilter implements FilterIterator.Filter {
237         
238         private final String JavaDoc prefix;
239         
240         PrefixFilter(String JavaDoc prefix) {
241             this.prefix = prefix;
242         }
243         
244         public boolean accept(Object JavaDoc obj) {
245             return ((String JavaDoc)obj).startsWith(prefix);
246         }
247     }
248     
249
250     /**
251      * Get registered systemid for given public Id or null if not registered.
252      * @return null if not registered
253      */

254     public String JavaDoc getSystemID(String JavaDoc publicId) {
255         return getPublicMapping(publicId);
256     }
257     
258     /**
259      * Resolves external entities.
260      *
261      * @param publicId The public identifier used for entity resolution.
262      * @param systemId If the publicId is not null, this systemId is
263      * to be considered the default system identifier;
264      * else a system identifier alias mapping is
265      * requested.
266      *
267      * @return Returns the input source of the resolved entity or null
268      * if no resolution is possible.
269      *
270      * @exception org.xml.sax.SAXException Exception thrown on SAX error.
271      * @exception java.io.IOException Exception thrown on i/o error.
272      */

273     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException
274     {
275         
276         // public id -> system id
277
InputSource JavaDoc ret = resolvePublicId(publicId);
278         if (ret != null) return ret;
279
280         // system id(1) -> system id(2)
281
return resolveSystemId(systemId);
282     }
283     
284     protected InputSource JavaDoc resolvePublicId(String JavaDoc publicId) {
285
286         // public id -> system id
287
if (publicId != null) {
288             String JavaDoc value = getPublicMapping(publicId);
289             if (value != null) {
290                 InputSource JavaDoc input = new InputSource JavaDoc(value);
291                 input.setPublicId(publicId);
292                 return input;
293             }
294         }
295         
296         return null;
297     }
298     
299     protected InputSource JavaDoc resolveSystemId(String JavaDoc systemId) {
300
301         if (systemId != null) {
302             String JavaDoc value = getSystemMapping(systemId);
303             if (value == null) {
304                 value = systemId; //??? is it good
305
}
306
307             return new InputSource JavaDoc(value);
308         }
309         
310         return null;
311     }
312     
313     // Catalog delegation stuff ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
314

315     
316     /**
317      * Adds a delegate mapping. If the prefix of a public identifier
318      * matches a delegate prefix, then the delegate catalog is
319      * searched in order to resolve the identifier.
320      * <p>
321      * This method makes sure that prefixes that match each other
322      * are inserted into the delegate list in order of longest prefix
323      * length first.
324      *
325      * @param prefix The delegate prefix.
326      * @param catalog The delegate catalog.
327      */

328     public void addDelegateCatalog(String JavaDoc prefix, AbstractCatalog catalog) {
329         
330         synchronized (delegate) {
331             // insert prefix in proper order
332
if (!delegate.containsKey(prefix)) {
333                 int size = delegateOrder.size();
334                 boolean found = false;
335                 for (int i = 0; i < size; i++) {
336                     String JavaDoc element = (String JavaDoc)delegateOrder.elementAt(i);
337                     if (prefix.startsWith(element) || prefix.compareTo(element) < 0) {
338                         delegateOrder.insertElementAt(prefix, i);
339                         found = true;
340                         break;
341                     }
342                 }
343                 if (!found) {
344                     delegateOrder.addElement(prefix);
345                 }
346             }
347             
348             // replace (or add new) prefix mapping
349
delegate.put(prefix, catalog);
350         }
351         
352     }
353     
354     /**
355      * Removes a delegate.
356      *
357      * @param prefix The delegate prefix to remove.
358      */

359     public void removeDelegateCatalog(String JavaDoc prefix) {
360         
361         synchronized (delegate) {
362             delegate.remove(prefix);
363             delegateOrder.removeElement(prefix);
364         }
365         
366     } // removeDelegateCatalog(String)
367

368     /** Returns an enumeration of delegate prefixes. */
369     public Enumeration getDelegateCatalogKeys() {
370         return delegateOrder.elements();
371     }
372     
373     /** Returns the catalog for the given delegate prefix. */
374     public AbstractCatalog getDelegateCatalog(String JavaDoc prefix) {
375         return (AbstractCatalog)delegate.get(prefix);
376     }
377     
378     
379     
380     // Listeners ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
381

382     
383     
384     private PropertyChangeSupport support = new PropertyChangeSupport(this);
385
386     /**
387      * Should provide callbacks on PROP_CATALOG_ICON, PROP_CATALOG_DESC
388      * and PROP_CATALOG_NAME changes as defined in CatalogDescriptor.
389      */

390     public void addPropertyChangeListener(PropertyChangeListener l) {
391         support.addPropertyChangeListener(l);
392     }
393     
394     public void removePropertyChangeListener(PropertyChangeListener l) {
395         support.removePropertyChangeListener(l);
396     }
397
398     protected void firePropertyChange(String JavaDoc prop, Object JavaDoc val1, Object JavaDoc val2) {
399         support.firePropertyChange(prop, val1, val2);
400     }
401     
402     /** Get icon from bean info or null. */
403     protected Image getDefaultIcon(int type) {
404         try {
405             BeanInfo info = Utilities.getBeanInfo(getClass());
406             return info.getIcon(type);
407         } catch (IntrospectionException ex) {
408             return null;
409         }
410     }
411     
412     /**
413      * Badge catalog icon with error sign.
414      * @return null
415      */

416     protected Image getDefaultErrorIcon(int type) {
417         if (getDefaultIcon(type) == null) return null;
418         
419         return null;
420     }
421     
422     /**
423      * Get registered URI for the given name or null if not registered.
424      * @return null if not registered
425      */

426     public String JavaDoc resolveURI(String JavaDoc name) {
427         return null;
428     }
429     /**
430      * Get registered URI for the given publicId or null if not registered.
431      * @return null if not registered
432      */

433     public String JavaDoc resolvePublic(String JavaDoc publicId) {
434         return null;
435     }
436     
437 }
438
Popular Tags