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 */
017package org.apache.camel.builder.xml;
018
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.Iterator;
022import java.util.Map;
023import java.util.Map.Entry;
024import java.util.Set;
025
026import javax.xml.namespace.NamespaceContext;
027import javax.xml.xpath.XPathFactory;
028
029import org.apache.camel.spi.NamespaceAware;
030import org.apache.camel.util.CastUtils;
031
032/**
033 * An implementation of {@link NamespaceContext} which uses a simple Map where
034 * the keys are the prefixes and the values are the URIs
035 */
036public class DefaultNamespaceContext implements NamespaceContext, NamespaceAware {
037
038    private final Map<String, String> map;
039    private final NamespaceContext parent;
040
041    public DefaultNamespaceContext() {
042        this(XPathFactory.newInstance());
043    }
044
045    public DefaultNamespaceContext(XPathFactory factory) {
046        this.parent = factory.newXPath().getNamespaceContext();
047        this.map = new HashMap<>();
048    }
049
050    public DefaultNamespaceContext(NamespaceContext parent, Map<String, String> map) {
051        this.parent = parent;
052        this.map = map;
053    }
054
055    /**
056     * A helper method to make it easy to create newly populated instances
057     */
058    public DefaultNamespaceContext add(String prefix, String uri) {
059        map.put(prefix, uri);
060        return this;
061    }
062
063    public String getNamespaceURI(String prefix) {
064        String answer = map.get(prefix);
065        if (answer == null && parent != null) {
066            return parent.getNamespaceURI(prefix);
067        }
068        return answer;
069    }
070
071    public String getPrefix(String namespaceURI) {
072        for (Entry<String, String> entry : map.entrySet()) {
073            if (namespaceURI.equals(entry.getValue())) {
074                return entry.getKey();
075            }
076        }
077        if (parent != null) {
078            return parent.getPrefix(namespaceURI);
079        }
080        return null;
081    }
082
083    public Iterator<String> getPrefixes(String namespaceURI) {
084        Set<String> set = new HashSet<>();
085        for (Entry<String, String> entry : map.entrySet()) {
086            if (namespaceURI.equals(entry.getValue())) {
087                set.add(entry.getKey());
088            }
089        }
090        if (parent != null) {
091            Iterator<String> iter = CastUtils.cast(parent.getPrefixes(namespaceURI));
092            while (iter.hasNext()) {
093                set.add(iter.next());
094            }
095        }
096        return set.iterator();
097    }
098
099    public void setNamespaces(Map<String, String> namespaces) {
100        map.putAll(namespaces);
101    }
102
103    public Map<String, String> getNamespaces() {
104        return map;
105    }
106
107    /**
108     * toString() implementation that outputs the namespace mappings with the following format: "[me: {prefix -> value}, {prefix -> value}], [parent: {prefix -> value}, {prefix -> value}].
109     * Recurses up the parent's chain.
110     */
111    @Override
112    public String toString() {
113        StringBuilder sb = new StringBuilder("[me: ");
114        for (Entry<String, String> nsEntry : map.entrySet()) {
115            sb.append("{" + nsEntry.getKey() + " -> " + nsEntry.getValue() + "},");
116        }
117        if (!map.isEmpty()) {
118            // remove the last comma
119            sb.deleteCharAt(sb.length() - 1);
120        }
121        sb.append("]");
122
123        // Get the parent's namespace mappings
124        if (parent != null) {
125            sb.append(", [parent: ");
126            sb.append(parent.toString());
127            sb.append("]");
128        }
129        return sb.toString();
130    }
131
132}