//
// FlowNamespaceContext.java - Allow XPaths to use namespace prefixes
//
// The default namespace is the null namespace.
// Users are expected to add their own mappings.
// We rely on our users not changing the default prefix->NS mappings.
//

package nyangau.jmsadapter;

//...simports:0:
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
//...e

public class FlowNamespaceContext implements NamespaceContext
  {
  protected Map<String,String> map;
//...sclass RoIterator:2:
protected class RoIterator implements Iterator<String>
  {
  protected Iterator<String> iter;
  public RoIterator(Iterator<String> i)
    { iter = i; }
  public boolean hasNext()
    { return iter.hasNext(); };
  public String next()
    { return iter.next(); }
  public void remove()
    throws UnsupportedOperationException
    { throw new UnsupportedOperationException(); }
  }
//...e
//...sFlowNamespaceContext:2:
// Likely to be followed by addPrefix() calls
public FlowNamespaceContext()
  {
  map = new HashMap<String,String>();
  map.put(XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.NULL_NS_URI           );
  map.put(XMLConstants.XML_NS_PREFIX    , XMLConstants.XML_NS_URI            );
  map.put(XMLConstants.XMLNS_ATTRIBUTE  , XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
  }

// Typical use
public FlowNamespaceContext(Map<String,String> m)
  {
  this();
  map.putAll(m);
  }
//...e
//...saddPrefix:2:
public void addPrefix(String prefix, String namespaceURI)
  {
  map.put(prefix, namespaceURI);
  }
//...e
//...sgetNamespaceURI:2:
public String getNamespaceURI(String prefix)
  {
  if ( prefix == null )
    throw new IllegalArgumentException();
  return map.get(prefix);
  }
//...e
//...sgetPrefix:2:
public String getPrefix(String namespaceURI)
  {
  if ( namespaceURI == null )
    throw new IllegalArgumentException();
  for ( String k : map.keySet() )
    if ( map.get(k).equals(namespaceURI) )
      return k;
  return null;
  }
//...e
//...sgetPrefixes:2:
public Iterator getPrefixes(String namespaceURI)
  {
  if ( namespaceURI == null )
    throw new IllegalArgumentException();
  Set<String> prefixes = new HashSet<String>();
  for ( String k : map.keySet() )
    if ( map.get(k).equals(namespaceURI) )
      prefixes.add(k);
  return new RoIterator(prefixes.iterator());
  }
//...e
  }
