KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > jaxp > W3CNamespaceContext


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.jbi.jaxp;
18
19 /*
20  * This implementation comes from the XFire project
21  * https://svn.codehaus.org/xfire/trunk/xfire/xfire-core/src/main/org/codehaus/xfire/util/stax/
22  */

23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.xml.namespace.NamespaceContext JavaDoc;
29
30 import org.w3c.dom.Attr JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.NamedNodeMap JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34
35 public class W3CNamespaceContext implements NamespaceContext JavaDoc
36 {
37     private Element JavaDoc currentNode;
38     
39     public String JavaDoc getNamespaceURI(String JavaDoc prefix)
40     {
41         String JavaDoc name = prefix;
42         if (name.length() == 0) name = "xmlns";
43         else name = "xmlns:" + prefix;
44         
45         return getNamespaceURI(currentNode, name);
46     }
47
48     private String JavaDoc getNamespaceURI(Element JavaDoc e, String JavaDoc name)
49     {
50         Attr JavaDoc attr = e.getAttributeNode(name);
51         if (attr == null)
52         {
53             Node JavaDoc n = e.getParentNode();
54             if (n instanceof Element JavaDoc && n != e)
55             {
56                 return getNamespaceURI((Element JavaDoc) n, name);
57             }
58         }
59         else
60         {
61             return attr.getValue();
62         }
63         
64         return null;
65     }
66     
67     public String JavaDoc getPrefix(String JavaDoc uri)
68     {
69         return getPrefix(currentNode, uri);
70     }
71
72     private String JavaDoc getPrefix(Element JavaDoc e, String JavaDoc uri)
73     {
74         NamedNodeMap JavaDoc attributes = e.getAttributes();
75         for (int i = 0; i < attributes.getLength(); i++)
76         {
77             Attr JavaDoc a = (Attr JavaDoc) attributes.item(i);
78             
79             String JavaDoc val = a.getValue();
80             if (val != null && val.equals(uri))
81             {
82                 String JavaDoc name = a.getNodeName();
83                 if (name.equals("xmlns")) return "";
84                 else return name.substring(6);
85             }
86         }
87         
88         Node JavaDoc n = e.getParentNode();
89         if (n instanceof Element JavaDoc && n != e)
90         {
91             return getPrefix((Element JavaDoc) n, uri);
92         }
93         
94         return null;
95     }
96
97     public Iterator JavaDoc getPrefixes(String JavaDoc uri)
98     {
99         List JavaDoc prefixes = new ArrayList JavaDoc();
100         
101         String JavaDoc prefix = getPrefix(uri);
102         if (prefix != null) prefixes.add(prefix);
103         
104         return prefixes.iterator();
105     }
106
107     public Element JavaDoc getElement()
108     {
109         return currentNode;
110     }
111
112     public void setElement(Element JavaDoc currentNode)
113     {
114         this.currentNode = currentNode;
115     }
116 }
117
Popular Tags