Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
 package org.jboss.xb.binding;
 
 
 import java.util.Map;
 import java.util.List;
A simple namespace registry. It assignes namespace prefixes of the form 'ns?' where ? is an incrementing integer. registerURI(String,String) [TODO] cleanup the api

Author(s):
Thomas.Diesler@jboss.org
Alexey Loubyansky
Anil Saldhana
Since:
08-June-2004
 
 public class NamespaceRegistry implements NamespaceContextSerializable
 {
    private static final long serialVersionUID = 8435680858785550261L;
 
    // The index of the last assigned prefix
    private int namespaceIndex;
 
    private final Map prefixByUri = new HashMap();
    private final Map uriByPrefix = new HashMap();
 
    public NamespaceRegistry()
    {
    }

   
Register a QName and return a QName that is guarantied to have a prefix
 
    public QName registerQName(QName qname)
    {
       if (qname == null)
          return null;
 
       String nsURI = qname.getNamespaceURI();
       String prefix = getPrefix(nsURI);
       if (prefix == null)
       {
          prefix = qname.getPrefix();
          if (prefix.length() == 0)
             prefix = registerURI(nsURInull);
          else
             prefix = registerURI(nsURIprefix);
       }
 
       qname = new QName(nsURIqname.getLocalPart(), prefix);
       return qname;
    }

   
Register the given nsURI/prefix combination. In case the prefix is null, it will be assigend.

Parameters:
nsURI The nsURI
prefix The corresponding prefix, maybe null
Returns:
A prefix, never null
 
    public String registerURI(String nsURIString prefix)
    {
       if (prefix == null)
       {
          prefix = "ns" + (++);
       }
 
       addPrefixMapping(prefixnsURI);
       return prefix;
   }

   
Adds prefix mapping.

Parameters:
prefix prefix to map
nsURI the URI to prefix to
   public void addPrefixMapping(String prefixString nsURI)
   {
      if (nsURI == null)
         throw new IllegalArgumentException("Cannot add mapping for null namespace URI");
      Object obj = .get(prefix);
      if (nsURI.equals(obj) == false)
      {
         if (obj == null)
         {
            .put(prefixnsURI);
         }
         else if (obj instanceof String)
         {
            List list = new ArrayList();
            list.add(obj);
            list.add(nsURI);
            .put(prefixlist);
         }
         else if (obj instanceof List)
         {
            ((List)obj).add(nsURI);
         }
         else
         {
            throwUnexpectedEntryException(obj);
         }
         obj = .get(nsURI);
         if (obj == null)
         {
            .put(nsURIprefix);
         }
         else if (obj instanceof String)
         {
            List list = new ArrayList();
            list.add(obj);
            list.add(prefix);
            .put(nsURIlist);
         }
         else if (obj instanceof List)
         {
            ((List)obj).add(prefix);
         }
         else
         {
            throwUnexpectedEntryException(obj);
         }
      }
   }

   
Removes the last mapping for the given prefix.

Parameters:
prefix the prefix to remove mapping for
   public void removePrefixMapping(String prefix)
   {
      Object obj = .get(prefix);
      if (obj != null)
      {
         String uri = null;
         if (obj instanceof String)
         {
            uri = (String)obj;
            .remove(prefix);
         }
         else if (obj instanceof List)
         {
            List list = (List)obj;
            uri = (String)list.remove(list.size() - 1);
            if (list.isEmpty())
            {
               .remove(prefix);
            }
         }
         else
         {
            throwUnexpectedEntryException(obj);
         }
         if (uri != null)
         {
            obj = .get(uri);
            if (obj instanceof String)
            {
               if (!prefix.equals(obj))
               {
                  throw new IllegalStateException("Inconsistent mapping: prefix=" + prefix + ", found=" + obj);
               }
               .remove(uri);
            }
            else if (obj instanceof List)
            {
               List list = (ArrayList)obj;
               list.remove(prefix);
               if (list.isEmpty())
               {
                  .remove(uri);
               }
            }
            else
            {
               throwUnexpectedEntryException(obj);
            }
         }
      }
   }

   
Unregisters all prefix mappings for the given URI, not just the last one added. todo what is this used for?

Parameters:
nsURI the URI to unregister
   public void unregisterURI(String nsURI)
   {
      Object obj = .get(nsURI);
      if (obj != null)
      {
         String prefix = null;
         if (obj instanceof String)
         {
            prefix = (String)obj;
            .remove(nsURI);
            removePrefixMappingOnly(prefixnsURI);
         }
         else if (obj instanceof List)
         {
            List list = (List)obj;
            for (int i = 0; i < list.size(); ++i)
            {
               removePrefixMappingOnly((String)list.get(i), nsURI);
            }
            .remove(nsURI);
         }
         else
         {
            throwUnexpectedEntryException(obj);
         }
      }
   }

   
True if the given nsURI is registered.
   public boolean isRegistered(String nsURI)
   {
      return .containsKey(nsURI);
   }

   
Return an iterator over all registered nsURIs.
   {
      return .keySet().iterator();
   }

   
Return an iterator over all registered nsURIs.
   {
      return .keySet().iterator();
   }

   

Returns:
the number of registered URIs
   public int size()
   {
      return .size();
   }
   // NamespaceContext implementation

   
Returns the last mapping for the prefix or null if the prefix was not mapped.
   public String getPrefix(String nsURI)
   {
      Object obj = .get(nsURI);
      String prefix = null;
      if (obj != null)
      {
         if (obj instanceof String)
         {
            prefix = (String)obj;
         }
         else if (obj instanceof List)
         {
            List list = (List)obj;
            prefix = (String)list.get(list.size() - 1);
         }
         else
         {
            throwUnexpectedEntryException(obj);
         }
      }
      return prefix;
   }

   
Returns all prefixes for the given URI.

Parameters:
namespaceURI the URI to return prefixes for
Returns:
prefixes mapped to the URI
   public Iterator getPrefixes(String namespaceURI)
   {
      Object obj = .get(namespaceURI);
      Iterator result = null;
      if (obj == null)
      {
         result = ..iterator();
      }
      else if (obj instanceof String)
      {
         result = Collections.singletonList(obj).iterator();
      }
      else if (obj instanceof List)
      {
         result = ((List)obj).iterator();
      }
      else
      {
         throwUnexpectedEntryException(obj);
      }
      return result;
   }

   
Get the nsURI for a given prefix, maybe null.
   public String getNamespaceURI(String prefix)
   {
      Object obj = .get(prefix);
      String uri = null;
      if (obj != null)
      {
         if (obj instanceof String)
         {
            uri = (String)obj;
         }
         else if (obj instanceof List)
         {
            List list = (List)obj;
            uri = (String)list.get(list.size() - 1);
         }
         else
         {
            throwUnexpectedEntryException(obj);
         }
      }
      return uri;
   }
   // Private
   private void removePrefixMappingOnly(String prefixString nsURI)
   {
      Object obj = .get(prefix);
      if (obj instanceof String)
      {
         if (!obj.equals(nsURI))
         {
            throw new IllegalStateException("Inconsistent mapping: uri=" + nsURI + ", found=" + obj);
         }
         .remove(prefix);
      }
      else if (obj instanceof List)
      {
         List list = (List)obj;
         list.remove(prefix);
         if (list.isEmpty())
         {
            .remove(prefix);
         }
      }
   }
   private void throwUnexpectedEntryException(Object entry)
   {
      throw new IllegalStateException("Unexpected entry type: expected java.lang.String or java.util.List but got " + entry.getClass());
   }
New to GrepCode? Check out our FAQ X