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