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: /**
020: *
021: */package org.apache.cxf.aegis.util.stax;
022:
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import javax.xml.namespace.NamespaceContext;
028:
029: import org.w3c.dom.Attr;
030: import org.w3c.dom.Element;
031: import org.w3c.dom.NamedNodeMap;
032: import org.w3c.dom.Node;
033:
034: public class W3CNamespaceContext implements NamespaceContext {
035: private Element currentNode;
036:
037: public String getNamespaceURI(String prefix) {
038: String name = prefix;
039: if (name.length() == 0) {
040: name = "xmlns";
041: } else {
042: name = "xmlns:" + prefix;
043: }
044:
045: return getNamespaceURI(currentNode, name);
046: }
047:
048: private String getNamespaceURI(Element e, String name) {
049: Attr attr = e.getAttributeNode(name);
050: if (attr == null) {
051: Node n = e.getParentNode();
052: if (n instanceof Element && n != e) {
053: return getNamespaceURI((Element) n, name);
054: }
055: } else {
056: return attr.getValue();
057: }
058:
059: return null;
060: }
061:
062: public String getPrefix(String uri) {
063: return getPrefix(currentNode, uri);
064: }
065:
066: private String getPrefix(Element e, String uri) {
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<String> 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 cn) {
108: this.currentNode = cn;
109: }
110: }
|