001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.builder.xml;
018    
019    import java.util.HashMap;
020    import java.util.HashSet;
021    import java.util.Iterator;
022    import java.util.Map;
023    import java.util.Set;
024    
025    import javax.xml.namespace.NamespaceContext;
026    import javax.xml.xpath.XPathFactory;
027    
028    import org.apache.camel.spi.NamespaceAware;
029    import org.apache.camel.util.CastUtils;
030    
031    /**
032     * An implementation of {@link NamespaceContext} which uses a simple Map where
033     * the keys are the prefixes and the values are the URIs
034     *
035     * @version $Revision: 881690 $
036     */
037    public class DefaultNamespaceContext implements NamespaceContext, NamespaceAware {
038    
039        private final Map<String, String> map;
040        private final NamespaceContext parent;
041    
042        public DefaultNamespaceContext() {
043            this(XPathFactory.newInstance());
044        }
045    
046        public DefaultNamespaceContext(XPathFactory factory) {
047            this.parent = factory.newXPath().getNamespaceContext();
048            this.map = new HashMap<String, String>();
049        }
050    
051        public DefaultNamespaceContext(NamespaceContext parent, Map<String, String> map) {
052            this.parent = parent;
053            this.map = map;
054        }
055    
056        /**
057         * A helper method to make it easy to create newly populated instances
058         */
059        public DefaultNamespaceContext add(String prefix, String uri) {
060            map.put(prefix, uri);
061            return this;
062        }
063    
064        public String getNamespaceURI(String prefix) {
065            String answer = map.get(prefix);
066            if (answer == null && parent != null) {
067                return parent.getNamespaceURI(prefix);
068            }
069            return answer;
070        }
071    
072        public String getPrefix(String namespaceURI) {
073            for (Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator(); iter.hasNext();) {
074                Map.Entry<String, String> entry = iter.next();
075                if (namespaceURI.equals(entry.getValue())) {
076                    return (String) entry.getKey();
077                }
078            }
079            if (parent != null) {
080                return parent.getPrefix(namespaceURI);
081            }
082            return null;
083        }
084    
085        public Iterator<String> getPrefixes(String namespaceURI) {
086            Set<String> set = new HashSet<String>();
087            for (Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator(); iter.hasNext();) {
088                Map.Entry<String, String> entry = iter.next();
089                if (namespaceURI.equals(entry.getValue())) {
090                    set.add(entry.getKey());
091                }
092            }
093            if (parent != null) {
094                Iterator<String> iter = CastUtils.cast(parent.getPrefixes(namespaceURI));
095                while (iter.hasNext()) {
096                    set.add(iter.next());
097                }
098            }
099            return set.iterator();
100        }
101    
102        public void setNamespaces(Map<String, String> namespaces) {
103            map.putAll(namespaces);
104        }
105    }