01: /*
02: * reserved comment block
03: * DO NOT REMOVE OR ALTER!
04: */
05: package com.sun.tools.internal.xjc.reader.internalizer;
06:
07: import java.util.Iterator;
08:
09: import javax.xml.namespace.NamespaceContext;
10:
11: import com.sun.xml.internal.bind.v2.WellKnownNamespace;
12:
13: import org.w3c.dom.Element;
14: import org.w3c.dom.NamedNodeMap;
15: import org.w3c.dom.Node;
16:
17: /**
18: * Implements {@link NamespaceContext} by looking at the in-scope
19: * namespace binding of a DOM element.
20: *
21: * @author Kohsuke Kawaguchi
22: */
23: final class NamespaceContextImpl implements NamespaceContext {
24: private final Element e;
25:
26: public NamespaceContextImpl(Element e) {
27: this .e = e;
28: }
29:
30: /*
31: * Copyright 1999-2004 The Apache Software Foundation.
32: *
33: * Licensed under the Apache License, Version 2.0 (the "License");
34: * you may not use this file except in compliance with the License.
35: * You may obtain a copy of the License at
36: *
37: * http://www.apache.org/licenses/LICENSE-2.0
38: *
39: * Unless required by applicable law or agreed to in writing, software
40: * distributed under the License is distributed on an "AS IS" BASIS,
41: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42: * See the License for the specific language governing permissions and
43: * limitations under the License.
44: */
45: public String getNamespaceURI(String prefix) {
46: Node parent = e;
47: String namespace = null;
48:
49: if (prefix.equals("xml")) {
50: namespace = WellKnownNamespace.XML_NAMESPACE_URI;
51: } else {
52: int type;
53:
54: while ((null != parent)
55: && (null == namespace)
56: && (((type = parent.getNodeType()) == Node.ELEMENT_NODE) || (type == Node.ENTITY_REFERENCE_NODE))) {
57: if (type == Node.ELEMENT_NODE) {
58: if (parent.getNodeName().indexOf(prefix + ':') == 0)
59: return parent.getNamespaceURI();
60: NamedNodeMap nnm = parent.getAttributes();
61:
62: for (int i = 0; i < nnm.getLength(); i++) {
63: Node attr = nnm.item(i);
64: String aname = attr.getNodeName();
65: boolean isPrefix = aname.startsWith("xmlns:");
66:
67: if (isPrefix || aname.equals("xmlns")) {
68: int index = aname.indexOf(':');
69: String p = isPrefix ? aname
70: .substring(index + 1) : "";
71:
72: if (p.equals(prefix)) {
73: namespace = attr.getNodeValue();
74:
75: break;
76: }
77: }
78: }
79: }
80:
81: parent = parent.getParentNode();
82: }
83: }
84:
85: return namespace;
86: }
87:
88: public String getPrefix(String namespaceURI) {
89: throw new UnsupportedOperationException();
90: }
91:
92: public Iterator getPrefixes(String namespaceURI) {
93: throw new UnsupportedOperationException();
94: }
95: }
|