KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > client > DefaultNamespaceContext


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.client;
18
19 import javax.xml.xpath.*;
20 import javax.xml.namespace.NamespaceContext JavaDoc;
21
22 import java.util.*;
23
24 /**
25  * An implementation of {@link NamespaceContext} which uses a simple Map where
26  * the keys are the prefixes and the values are the URIs
27  *
28  * @version $Revision: $
29  */

30 public class DefaultNamespaceContext implements NamespaceContext JavaDoc {
31
32     private final Map map;
33     private final NamespaceContext JavaDoc parent;
34
35     public DefaultNamespaceContext() {
36         this.map = new HashMap();
37         XPathFactory factory = XPathFactory.newInstance();
38         this.parent = factory.newXPath().getNamespaceContext();
39     }
40
41     public DefaultNamespaceContext(NamespaceContext JavaDoc parent, Map map) {
42         this.parent = parent;
43         this.map = map;
44     }
45
46     /**
47      * A helper method to make it easy to create newly populated instances
48      */

49     public DefaultNamespaceContext add(String JavaDoc prefix, String JavaDoc uri) {
50         map.put(prefix, uri);
51         return this;
52     }
53     
54     public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
55         String JavaDoc answer = (String JavaDoc) map.get(prefix);
56         if (answer == null && parent != null) {
57             return parent.getNamespaceURI(prefix);
58         }
59         return answer;
60     }
61
62     public String JavaDoc getPrefix(String JavaDoc namespaceURI) {
63         for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
64             Map.Entry entry = (Map.Entry) iter.next();
65             if (namespaceURI.equals(entry.getValue())) {
66                 return (String JavaDoc) entry.getKey();
67             }
68         }
69         if (parent != null) {
70             return parent.getPrefix(namespaceURI);
71         }
72         return null;
73     }
74
75     public Iterator getPrefixes(String JavaDoc namespaceURI) {
76         Set set = new HashSet();
77         for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
78             Map.Entry entry = (Map.Entry) iter.next();
79             if (namespaceURI.equals(entry.getValue())) {
80                 set.add(entry.getKey());
81             }
82         }
83         if (parent != null) {
84             Iterator iter = parent.getPrefixes(namespaceURI);
85             while (iter.hasNext()) {
86                 set.add(iter.next());
87             }
88         }
89         return set.iterator();
90     }
91 }
92
Popular Tags