1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/internalizer/NamespaceContextImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,106 @@ 1.4 +/* 1.5 + * reserved comment block 1.6 + * DO NOT REMOVE OR ALTER! 1.7 + */ 1.8 + /* 1.9 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.10 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.11 + * 1.12 + * This code is free software; you can redistribute it and/or modify it 1.13 + * under the terms of the GNU General Public License version 2 only, as 1.14 + * published by the Free Software Foundation. Oracle designates this 1.15 + * particular file as subject to the "Classpath" exception as provided 1.16 + * by Oracle in the LICENSE file that accompanied this code. 1.17 + * 1.18 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.19 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.20 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.21 + * version 2 for more details (a copy is included in the LICENSE file that 1.22 + * accompanied this code). 1.23 + * 1.24 + * You should have received a copy of the GNU General Public License version 1.25 + * 2 along with this work; if not, write to the Free Software Foundation, 1.26 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.27 + * 1.28 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.29 + * or visit www.oracle.com if you need additional information or have any 1.30 + * questions. 1.31 + * 1.32 + * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. 1.33 + */ 1.34 + 1.35 +package com.sun.tools.internal.xjc.reader.internalizer; 1.36 + 1.37 +import java.util.Iterator; 1.38 +import javax.xml.XMLConstants; 1.39 +import javax.xml.namespace.NamespaceContext; 1.40 +import org.w3c.dom.Element; 1.41 +import org.w3c.dom.NamedNodeMap; 1.42 +import org.w3c.dom.Node; 1.43 + 1.44 +/** 1.45 + * Implements {@link NamespaceContext} by looking at the in-scope 1.46 + * namespace binding of a DOM element. 1.47 + * 1.48 + * @author Kohsuke Kawaguchi 1.49 + */ 1.50 +final class NamespaceContextImpl implements NamespaceContext { 1.51 + private final Element e; 1.52 + 1.53 + public NamespaceContextImpl(Element e) { 1.54 + this.e = e; 1.55 + } 1.56 + 1.57 + public String getNamespaceURI(String prefix) { 1.58 + Node parent = e; 1.59 + String namespace = null; 1.60 + final String prefixColon = prefix + ':'; 1.61 + 1.62 + if (prefix.equals("xml")) { 1.63 + namespace = XMLConstants.XML_NS_URI; 1.64 + } else { 1.65 + int type; 1.66 + 1.67 + while ((null != parent) && (null == namespace) 1.68 + && (((type = parent.getNodeType()) == Node.ELEMENT_NODE) 1.69 + || (type == Node.ENTITY_REFERENCE_NODE))) { 1.70 + if (type == Node.ELEMENT_NODE) { 1.71 + if (parent.getNodeName().startsWith(prefixColon)) 1.72 + return parent.getNamespaceURI(); 1.73 + NamedNodeMap nnm = parent.getAttributes(); 1.74 + 1.75 + for (int i = 0; i < nnm.getLength(); i++) { 1.76 + Node attr = nnm.item(i); 1.77 + String aname = attr.getNodeName(); 1.78 + boolean isPrefix = aname.startsWith("xmlns:"); 1.79 + 1.80 + if (isPrefix || aname.equals("xmlns")) { 1.81 + int index = aname.indexOf(':'); 1.82 + String p = isPrefix ? aname.substring(index + 1) : ""; 1.83 + 1.84 + if (p.equals(prefix)) { 1.85 + namespace = attr.getNodeValue(); 1.86 + 1.87 + break; 1.88 + } 1.89 + } 1.90 + } 1.91 + } 1.92 + 1.93 + parent = parent.getParentNode(); 1.94 + } 1.95 + } 1.96 + 1.97 + if(prefix.equals("")) 1.98 + return ""; // default namespace 1.99 + return namespace; 1.100 + } 1.101 + 1.102 + public String getPrefix(String namespaceURI) { 1.103 + throw new UnsupportedOperationException(); 1.104 + } 1.105 + 1.106 + public Iterator getPrefixes(String namespaceURI) { 1.107 + throw new UnsupportedOperationException(); 1.108 + } 1.109 +}