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: */package org.apache.cxf.staxutils;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import javax.xml.namespace.NamespaceContext;
025:
026: import org.w3c.dom.Attr;
027: import org.w3c.dom.Element;
028: import org.w3c.dom.NamedNodeMap;
029: import org.w3c.dom.Node;
030:
031: public class W3CNamespaceContext implements NamespaceContext {
032: private Element currentNode;
033:
034: public String getNamespaceURI(String prefix) {
035: String name = prefix;
036: if (name.length() == 0) {
037: name = "xmlns";
038: } else {
039: name = "xmlns:" + prefix;
040: }
041:
042: return getNamespaceURI(currentNode, name);
043: }
044:
045: private String getNamespaceURI(Element e, String name) {
046: Attr attr = e.getAttributeNode(name);
047: if (attr == null) {
048: Node n = e.getParentNode();
049: if (n instanceof Element && n != e) {
050: return getNamespaceURI((Element) n, name);
051: }
052: } else {
053: return attr.getValue();
054: }
055:
056: return null;
057: }
058:
059: public String getPrefix(String uri) {
060: return getPrefix(currentNode, uri);
061: }
062:
063: private String getPrefix(Element e, String uri) {
064: if (e == null) {
065: return null;
066: }
067: NamedNodeMap attributes = e.getAttributes();
068: if (attributes != null) {
069: for (int i = 0; i < attributes.getLength(); i++) {
070: Attr a = (Attr) attributes.item(i);
071:
072: String val = a.getValue();
073: if (val != null && val.equals(uri)) {
074: String name = a.getNodeName();
075: if ("xmlns".equals(name)) {
076: return "";
077: } else {
078: return name.substring(6);
079: }
080: }
081: }
082: }
083:
084: Node n = e.getParentNode();
085: if (n instanceof Element && n != e) {
086: return getPrefix((Element) n, uri);
087: }
088:
089: return null;
090: }
091:
092: public Iterator getPrefixes(String uri) {
093: List<String> prefixes = new ArrayList<String>();
094:
095: String prefix = getPrefix(uri);
096: if (prefix != null) {
097: prefixes.add(prefix);
098: }
099:
100: return prefixes.iterator();
101: }
102:
103: public Element getElement() {
104: return currentNode;
105: }
106:
107: public void setElement(Element node) {
108: this.currentNode = node;
109: }
110: }
|