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
030 /**
031 * An implementation of {@link NamespaceContext} which uses a simple Map where
032 * the keys are the prefixes and the values are the URIs
033 *
034 * @version $Revision: 747062 $
035 */
036 public 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<String, String>();
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 (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
073 Map.Entry entry = (Map.Entry) iter.next();
074 if (namespaceURI.equals(entry.getValue())) {
075 return (String) entry.getKey();
076 }
077 }
078 if (parent != null) {
079 return parent.getPrefix(namespaceURI);
080 }
081 return null;
082 }
083
084 @SuppressWarnings("unchecked")
085 public Iterator getPrefixes(String namespaceURI) {
086 Set set = new HashSet();
087 for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
088 Map.Entry entry = (Map.Entry) iter.next();
089 if (namespaceURI.equals(entry.getValue())) {
090 set.add(entry.getKey());
091 }
092 }
093 if (parent != null) {
094 Iterator iter = 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 }