Project dom4j 1.5.2 [5/2/05 10:13 PM]
 
Coverage - org/dom4j/tree/QNameCache.java
  /*
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   *
   * This software is open source.
   * See the bottom of this file for the licence.
   *
   * $Id: QNameCache.java,v 1.13 2004/06/25 08:03:41 maartenc Exp $
   */
 
  package org.dom4j.tree;
 
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
 
  import org.dom4j.DocumentFactory;
  import org.dom4j.Namespace;
  import org.dom4j.QName;
 
  /** <p><code>QNameCache</code> caches instances of <code>QName</code>
    * for reuse both across documents and within documents.</p>
    *
    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
    * @version $Revision: 1.13 $
    */
  public class QNameCache {
 
      /** Cache of {@link QName} instances with no namespace */
140x       protected Map noNamespaceCache = Collections.synchronizedMap(new HashMap());
 
      /** Cache of {@link Map} instances indexed by namespace which contain
        * caches of {@link QName} for each name
        */
140x       protected Map namespaceCache = Collections.synchronizedMap(new HashMap());
 
      /** The document factory associated with new QNames instances in this cache
        * or null if no instances should be associated by default
        */
      private DocumentFactory documentFactory;
 
 
12x       public QNameCache() {
12x       }
 
140x       public QNameCache(DocumentFactory documentFactory) {
140x           this.documentFactory = documentFactory;
140x       }
 
      /** Returns a list of all the QName instances currently used
       */
      public List getQNames() {
0x           List answer = new ArrayList();
0x           answer.addAll( noNamespaceCache.values() );
0/2 0x           for ( Iterator iter = namespaceCache.values().iterator(); iter.hasNext(); ) {
0x               Map map = (Map) iter.next();
0x               answer.addAll( map.values() );
          }
0x           return answer;
      }
 
      /** @return the QName for the given name and no namepsace
        */
      public QName get(String name) {
45940x           QName answer = null;
2/2 45940x           if (name!=null) {
45938x             answer=(QName) noNamespaceCache.get(name);
          }
          else {
2x             name="";
          }
2/2 45940x           if (answer == null) {
284x               answer = createQName(name);
284x               answer.setDocumentFactory( documentFactory );
284x               noNamespaceCache.put(name, answer);
          }
45940x           return answer;
      }
 
      /** @return the QName for the given local name and namepsace
        */
      public QName get(String name, Namespace namespace) {
138954x           Map cache = getNamespaceCache(namespace);
138954x           QName answer = null;
1/2 138954x           if (name!=null) {
138954x             answer=(QName) cache.get(name);
          }
          else {
0x             name="";
          }
2/2 138954x           if (answer == null) {
618x               answer = createQName(name, namespace);
618x               answer.setDocumentFactory( documentFactory );
618x               cache.put(name, answer);
          }
138954x           return answer;
      }
 
 
      /** @return the QName for the given local name, qualified name and namepsace
        */
      public QName get(String localName, Namespace namespace, String qualifiedName) {
0x           Map cache = getNamespaceCache(namespace);
0x           QName answer = null;
0/2 0x           if (localName!=null) {
0x             answer=(QName) cache.get(localName);
          }
          else {
0x             localName="";
          }
 
0/2 0x           if (answer == null) {
0x               answer = createQName(localName, namespace, qualifiedName);
0x               answer.setDocumentFactory( documentFactory );
0x               cache.put(localName, answer);
          }
0x           return answer;
      }
 
 
      public QName get(String qualifiedName, String uri) {
8x           int index = qualifiedName.indexOf( ':' );
1/2 8x           if ( index < 0 ) {
8x               return get( qualifiedName, Namespace.get( uri ) );
          }
          else {
0x               String name = qualifiedName.substring( index + 1 );
0x               String prefix = qualifiedName.substring( 0, index );
0x               return get(name, Namespace.get( prefix, uri ));
          }
      }
 
 
      /** @return the cached QName instance if there is one or adds the given
        * qname to the cache if not
         */
      public QName intern(QName qname) {
0x           return get(qname.getName(), qname.getNamespace(), qname.getQualifiedName());
      }
 
      /** @return the cache for the given namespace. If one does not
        * currently exist it is created.
        */
      protected Map getNamespaceCache(Namespace namespace) {
2/2 138954x           if (namespace == Namespace.NO_NAMESPACE) {
105432x               return noNamespaceCache;
          }
33522x           Map answer = null;
1/2 33522x           if (namespace!=null) {
33522x             answer=(Map) namespaceCache.get(namespace);
          }
2/2 33522x           if (answer == null) {
92x               answer = createMap();
92x               namespaceCache.put(namespace, answer);
          }
33522x           return answer;
      }
 
      /** A factory method
        * @return a newly created {@link Map} instance.
        */
      protected Map createMap() {
92x           return Collections.synchronizedMap(new HashMap());
      }
 
      /** Factory method to create a new QName object
        * which can be overloaded to create derived QName instances
        */
      protected QName createQName(String name) {
284x           return new QName(name);
      }
 
      /** Factory method to create a new QName object
        * which can be overloaded to create derived QName instances
        */
      protected QName createQName(String name, Namespace namespace) {
618x           return new QName(name, namespace);
      }
 
      /** Factory method to create a new QName object
        * which can be overloaded to create derived QName instances
        */
      protected QName createQName(String name, Namespace namespace, String qualifiedName) {
0x           return new QName(name, namespace, qualifiedName);
      }
  }
 
 
 
 
  /*
   * Redistribution and use of this software and associated documentation
   * ("Software"), with or without modification, are permitted provided
   * that the following conditions are met:
   *
   * 1. Redistributions of source code must retain copyright
   *    statements and notices.  Redistributions must also contain a
   *    copy of this document.
   *
   * 2. Redistributions in binary form must reproduce the
   *    above copyright notice, this list of conditions and the
   *    following disclaimer in the documentation and/or other
   *    materials provided with the distribution.
   *
   * 3. The name "DOM4J" must not be used to endorse or promote
   *    products derived from this Software without prior written
   *    permission of MetaStuff, Ltd.  For written permission,
   *    please contact dom4j-info@metastuff.com.
   *
   * 4. Products derived from this Software may not be called "DOM4J"
   *    nor may "DOM4J" appear in their names without prior written
   *    permission of MetaStuff, Ltd. DOM4J is a registered
   *    trademark of MetaStuff, Ltd.
   *
   * 5. Due credit should be given to the DOM4J Project - 
   *    http://www.dom4j.org
   *
   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
   * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   *
   * $Id: QNameCache.java,v 1.13 2004/06/25 08:03:41 maartenc Exp $
   */