What this is
This file is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.xml.catalog.impl;
import java.awt.*;
import java.io.*;
import java.beans.*;
import java.util.*;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.openide.util.*;
import org.netbeans.modules.xml.catalog.spi.*;
import org.netbeans.modules.xml.catalog.lib.*;
/**
* An abstract catalog implementation featuring firing support and
* properties describing common catalog content.
*
* It implements but does not declare that implements varios methods
* from EntityResolver, CatalogDescriptor and CatalogReader interfaces.
*
* @author Petr Kuzel
*/
public abstract class AbstractCatalog {
/** Public identifier mappings. */
private Map publicMap = new HashMap();
/** System identifier mappings (aliases). */
private Map systemMap = new HashMap();
private String location;
private Vector listeners;
// catalog delegation and chaining
/** Delegates. */
private Map delegate = new HashMap();
/** Delegates ordering. */
private Vector delegateOrder = new Vector();
/** Contains patch catalogs. */
protected Vector extenders = new Vector();
//
// Public methods
//
/**
* Set catalog location URI.
*/
public void setLocation(String location) {
this.location = location;
}
public String getLocation() {
return location;
}
/**
* Optional operation allowing to listen at catalog for changes.
* @throws UnsupportedOpertaionException if not supported by the implementation.
*/
public synchronized void addCatalogListener(CatalogListener l) {
if (listeners == null) listeners = new Vector(2);
listeners.add(l);
}
/**
* Optional operation couled with addCatalogListener.
* @see addCatalogListener
*/
public synchronized void removeCatalogListener(CatalogListener l) {
if (listeners == null) return;
if (listeners != null) listeners.remove(l);
if (listeners.isEmpty()) listeners = null;
}
protected void notifyInvalidate() {
CatalogListener[] lis = null;
synchronized (this) {
if (listeners == null || listeners.isEmpty()) return;
lis = (CatalogListener[]) listeners.toArray(new CatalogListener[0]);
}
for (int i = 0; i system id
InputSource ret = resolvePublicId(publicId);
if (ret != null) return ret;
// system id(1) -> system id(2)
return resolveSystemId(systemId);
}
protected InputSource resolvePublicId(String publicId) {
// public id -> system id
if (publicId != null) {
String value = getPublicMapping(publicId);
if (value != null) {
InputSource input = new InputSource(value);
input.setPublicId(publicId);
return input;
}
}
return null;
}
protected InputSource resolveSystemId(String systemId) {
if (systemId != null) {
String value = getSystemMapping(systemId);
if (value == null) {
value = systemId; //??? is it good
}
return new InputSource(value);
}
return null;
}
// Catalog delegation stuff ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* Adds a delegate mapping. If the prefix of a public identifier
* matches a delegate prefix, then the delegate catalog is
* searched in order to resolve the identifier.
*
* This method makes sure that prefixes that match each other
* are inserted into the delegate list in order of longest prefix
* length first.
*
* @param prefix The delegate prefix.
* @param catalog The delegate catalog.
*/
public void addDelegateCatalog(String prefix, AbstractCatalog catalog) {
synchronized (delegate) {
// insert prefix in proper order
if (!delegate.containsKey(prefix)) {
int size = delegateOrder.size();
boolean found = false;
for (int i = 0; i < size; i++) {
String element = (String)delegateOrder.elementAt(i);
if (prefix.startsWith(element) || prefix.compareTo(element) < 0) {
delegateOrder.insertElementAt(prefix, i);
found = true;
break;
}
}
if (!found) {
delegateOrder.addElement(prefix);
}
}
// replace (or add new) prefix mapping
delegate.put(prefix, catalog);
}
}
/**
* Removes a delegate.
*
* @param prefix The delegate prefix to remove.
*/
public void removeDelegateCatalog(String prefix) {
synchronized (delegate) {
delegate.remove(prefix);
delegateOrder.removeElement(prefix);
}
} // removeDelegateCatalog(String)
/** Returns an enumeration of delegate prefixes. */
public Enumeration getDelegateCatalogKeys() {
return delegateOrder.elements();
}
/** Returns the catalog for the given delegate prefix. */
public AbstractCatalog getDelegateCatalog(String prefix) {
return (AbstractCatalog)delegate.get(prefix);
}
// Listeners ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private PropertyChangeSupport support = new PropertyChangeSupport(this);
/**
* Should provide callbacks on PROP_CATALOG_ICON, PROP_CATALOG_DESC
* and PROP_CATALOG_NAME changes as defined in CatalogDescriptor.
*/
public void addPropertyChangeListener(PropertyChangeListener l) {
support.addPropertyChangeListener(l);
}
public void removePropertyChangeListener(PropertyChangeListener l) {
support.removePropertyChangeListener(l);
}
protected void firePropertyChange(String prop, Object val1, Object val2) {
support.firePropertyChange(prop, val1, val2);
}
/** Get icon from bean info or null. */
protected Image getDefaultIcon(int type) {
try {
BeanInfo info = Utilities.getBeanInfo(getClass());
return info.getIcon(type);
} catch (IntrospectionException ex) {
return null;
}
}
/**
* Badge catalog icon with error sign.
* @return null
*/
protected Image getDefaultErrorIcon(int type) {
if (getDefaultIcon(type) == null) return null;
return null;
}
}
|