001/*
002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
003 * 
004 * This software is open source. 
005 * See the bottom of this file for the licence.
006 * 
007 * $Id: NamespaceCache.java,v 1.4 2001/03/21 00:53:57 jstrachan Exp $
008 */
009
010package org.dom4j.tree;
011
012import java.util.HashMap;
013import java.util.Map;
014
015import org.dom4j.Namespace;
016
017/** <p><code>NamespaceCache</code> caches instances of <code>DefaultNamespace</code> 
018  * for reuse both across documents and within documents.</p>
019  *
020  * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
021  * @version $Revision: 1.4 $
022  */
023public class NamespaceCache {
024
025    /** Cache of {@link Map} instances indexed by URI which contain 
026      * caches of {@link Namespace} for each prefix
027      */ 
028    protected static Map cache;
029
030    /** Cache of {@link Namespace} instances indexed by URI 
031      * for default namespaces with no prefixes
032      */ 
033    protected static Map noPrefixCache;
034
035
036    /** @return the name model for the given name and namepsace 
037      */
038    public synchronized Namespace get(String prefix, String uri) {
039        Map cache = getURICache(uri);
040        Namespace answer = (Namespace) cache.get(prefix);
041        if (answer == null) {
042            answer = createNamespace(prefix, uri);
043            cache.put(prefix, answer);
044        }
045        return answer;
046    }
047    
048
049    /** @return the name model for the given name and namepsace 
050      */
051    public synchronized Namespace get(String uri) {
052        if ( noPrefixCache == null ) {
053            noPrefixCache = createURIMap();
054        }
055        Namespace answer = (Namespace) noPrefixCache.get(uri);
056        if (answer == null) {
057            answer = createNamespace("", uri);
058            noPrefixCache.put(uri, answer);
059        }
060        return answer;
061    }
062    
063
064    /** @return the cache for the given namespace URI. If one does not
065      * currently exist it is created.
066      */
067    protected Map getURICache(String uri) {
068        if (cache == null) {
069            cache = createURIMap();
070        }
071        Map answer = (Map) cache.get(uri);
072        if (answer == null) {
073            answer = createPrefixMap();
074            cache.put(uri, answer);
075        }
076        return answer;
077    }
078    
079    /** A factory method to create {@link Namespace} instance
080      * @return a newly created {@link Namespace} instance.
081      */
082    protected Namespace createNamespace(String prefix, String uri) {
083        return new Namespace(prefix, uri);
084    }
085    /** A factory method to create prefix caches
086      * @return a newly created {@link Map} instance.
087      */
088    protected Map createPrefixMap() {
089        return new HashMap();
090    }
091    
092    /** A factory method to create URI caches
093      * @return a newly created {@link Map} instance.
094      */
095    protected Map createURIMap() {
096        return new HashMap();
097    }
098}
099
100
101
102
103/*
104 * Redistribution and use of this software and associated documentation
105 * ("Software"), with or without modification, are permitted provided
106 * that the following conditions are met:
107 *
108 * 1. Redistributions of source code must retain copyright
109 *    statements and notices.  Redistributions must also contain a
110 *    copy of this document.
111 *
112 * 2. Redistributions in binary form must reproduce the
113 *    above copyright notice, this list of conditions and the
114 *    following disclaimer in the documentation and/or other
115 *    materials provided with the distribution.
116 *
117 * 3. The name "DOM4J" must not be used to endorse or promote
118 *    products derived from this Software without prior written
119 *    permission of MetaStuff, Ltd.  For written permission,
120 *    please contact dom4j-info@metastuff.com.
121 *
122 * 4. Products derived from this Software may not be called "DOM4J"
123 *    nor may "DOM4J" appear in their names without prior written
124 *    permission of MetaStuff, Ltd. DOM4J is a registered
125 *    trademark of MetaStuff, Ltd.
126 *
127 * 5. Due credit should be given to the DOM4J Project
128 *    (http://dom4j.org/).
129 *
130 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
131 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
132 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
133 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
134 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
135 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
136 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
137 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
138 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
139 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
140 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
141 * OF THE POSSIBILITY OF SUCH DAMAGE.
142 *
143 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
144 *
145 * $Id: NamespaceCache.java,v 1.4 2001/03/21 00:53:57 jstrachan Exp $
146 */