|
1 /* |
|
2 * reserved comment block |
|
3 * DO NOT REMOVE OR ALTER! |
|
4 */ |
|
5 /* |
|
6 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
|
7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
8 * |
|
9 * This code is free software; you can redistribute it and/or modify it |
|
10 * under the terms of the GNU General Public License version 2 only, as |
|
11 * published by the Free Software Foundation. Oracle designates this |
|
12 * particular file as subject to the "Classpath" exception as provided |
|
13 * by Oracle in the LICENSE file that accompanied this code. |
|
14 * |
|
15 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
18 * version 2 for more details (a copy is included in the LICENSE file that |
|
19 * accompanied this code). |
|
20 * |
|
21 * You should have received a copy of the GNU General Public License version |
|
22 * 2 along with this work; if not, write to the Free Software Foundation, |
|
23 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
24 * |
|
25 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
26 * or visit www.oracle.com if you need additional information or have any |
|
27 * questions. |
|
28 * |
|
29 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
|
30 */ |
|
31 |
|
32 package com.sun.tools.internal.xjc.reader.internalizer; |
|
33 |
|
34 import java.util.Iterator; |
|
35 import javax.xml.XMLConstants; |
|
36 import javax.xml.namespace.NamespaceContext; |
|
37 import org.w3c.dom.Element; |
|
38 import org.w3c.dom.NamedNodeMap; |
|
39 import org.w3c.dom.Node; |
|
40 |
|
41 /** |
|
42 * Implements {@link NamespaceContext} by looking at the in-scope |
|
43 * namespace binding of a DOM element. |
|
44 * |
|
45 * @author Kohsuke Kawaguchi |
|
46 */ |
|
47 final class NamespaceContextImpl implements NamespaceContext { |
|
48 private final Element e; |
|
49 |
|
50 public NamespaceContextImpl(Element e) { |
|
51 this.e = e; |
|
52 } |
|
53 |
|
54 public String getNamespaceURI(String prefix) { |
|
55 Node parent = e; |
|
56 String namespace = null; |
|
57 final String prefixColon = prefix + ':'; |
|
58 |
|
59 if (prefix.equals("xml")) { |
|
60 namespace = XMLConstants.XML_NS_URI; |
|
61 } else { |
|
62 int type; |
|
63 |
|
64 while ((null != parent) && (null == namespace) |
|
65 && (((type = parent.getNodeType()) == Node.ELEMENT_NODE) |
|
66 || (type == Node.ENTITY_REFERENCE_NODE))) { |
|
67 if (type == Node.ELEMENT_NODE) { |
|
68 if (parent.getNodeName().startsWith(prefixColon)) |
|
69 return parent.getNamespaceURI(); |
|
70 NamedNodeMap nnm = parent.getAttributes(); |
|
71 |
|
72 for (int i = 0; i < nnm.getLength(); i++) { |
|
73 Node attr = nnm.item(i); |
|
74 String aname = attr.getNodeName(); |
|
75 boolean isPrefix = aname.startsWith("xmlns:"); |
|
76 |
|
77 if (isPrefix || aname.equals("xmlns")) { |
|
78 int index = aname.indexOf(':'); |
|
79 String p = isPrefix ? aname.substring(index + 1) : ""; |
|
80 |
|
81 if (p.equals(prefix)) { |
|
82 namespace = attr.getNodeValue(); |
|
83 |
|
84 break; |
|
85 } |
|
86 } |
|
87 } |
|
88 } |
|
89 |
|
90 parent = parent.getParentNode(); |
|
91 } |
|
92 } |
|
93 |
|
94 if(prefix.equals("")) |
|
95 return ""; // default namespace |
|
96 return namespace; |
|
97 } |
|
98 |
|
99 public String getPrefix(String namespaceURI) { |
|
100 throw new UnsupportedOperationException(); |
|
101 } |
|
102 |
|
103 public Iterator getPrefixes(String namespaceURI) { |
|
104 throw new UnsupportedOperationException(); |
|
105 } |
|
106 } |