001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.jaxws.message.util.impl;
020:
021: import org.w3c.dom.Attr;
022: import org.w3c.dom.Element;
023: import org.w3c.dom.NamedNodeMap;
024: import org.w3c.dom.Node;
025:
026: import javax.xml.namespace.NamespaceContext;
027: import java.util.ArrayList;
028: import java.util.Iterator;
029:
030: /**
031: * Namespace information available at the current scope. Utility class for XMLStreamReaderFromDOM
032: *
033: * @see XMLStreamReaderFromDOM
034: */
035: public class NamespaceContextFromDOM implements NamespaceContext {
036:
037: private Element element;
038:
039: /** @param element representing the current scope */
040: NamespaceContextFromDOM(Element element) {
041: this .element = element;
042: }
043:
044: /* (non-Javadoc)
045: * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
046: */
047: public String getPrefix(String namespaceURI) {
048: return element.lookupPrefix(namespaceURI);
049: }
050:
051: /* (non-Javadoc)
052: * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String)
053: */
054: public Iterator getPrefixes(String namespaceURI) {
055: if (element instanceof javax.xml.soap.SOAPElement) {
056: Iterator it = ((javax.xml.soap.SOAPElement) element)
057: .getVisibleNamespacePrefixes();
058: ArrayList list = new ArrayList();
059: while (it.hasNext()) {
060: String prefix = (String) it.next();
061: if (getNamespaceURI(prefix).equals(namespaceURI)) {
062: if (prefix != null && prefix.length() == 0) {
063: prefix = null;
064: }
065: list.add(prefix);
066: }
067: }
068: return list.iterator();
069: } else {
070: ArrayList list = new ArrayList();
071: Node node = element;
072: while (node != null) {
073: if (node instanceof Element) {
074: // Walk the attributes looking for namespace declarations
075: NamedNodeMap attrs = ((Element) node)
076: .getAttributes();
077: for (int i = 0; i < attrs.getLength(); i++) {
078: Attr attr = (Attr) attrs.item(i);
079: if (attr.getNodeValue().equals(namespaceURI)) {
080: String name = attr.getNodeName();
081:
082: if (name.startsWith("xmlns")) {
083: String prefix = "";
084: if (name.startsWith("xmlns:")) {
085: prefix = name.substring(6);
086: }
087: // Found a namespace declaration with the prefix.
088: // Make sure this is not overridden by a declaration
089: // in a closer scope.
090: if (!list.contains(prefix)
091: && getNamespaceURI(prefix)
092: .equals(namespaceURI)) {
093: list.add(prefix);
094: }
095: }
096: }
097: }
098: }
099: // Pop up to the parent node
100: node = node.getParentNode();
101: }
102: return list.iterator();
103: }
104: }
105:
106: public String getNamespaceURI(String prefix) {
107: return element.lookupNamespaceURI(prefix);
108: }
109:
110: }
|