Java XML QName getStringForQName(QName qname, Element e)

Here you can find the source of getStringForQName(QName qname, Element e)

Description

Return a string for a particular QName, mapping a new prefix if necessary.

License

Apache License

Declaration

public static String getStringForQName(QName qname, Element e) 

Method Source Code


//package com.java2s;
/*//from  w  ww . j a v a2  s.com
 * Copyright 1999-2006 University of Chicago
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.w3c.dom.Attr;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import javax.xml.namespace.QName;

public class Main {
    public static final String NS_URI_XMLNS = "http://www.w3.org/2000/xmlns/";

    /**
     * Return a string for a particular QName, mapping a new prefix
     * if necessary.
     */
    public static String getStringForQName(QName qname, Element e) {
        String uri = qname.getNamespaceURI();
        String prefix = getPrefix(uri, e);
        if (prefix == null) {
            int i = 1;
            prefix = "ns" + i;
            while (getNamespace(prefix, e) != null) {
                i++;
                prefix = "ns" + i;
            }
            e.setAttributeNS(NS_URI_XMLNS, "xmlns:" + prefix, uri);
        }
        return prefix + ":" + qname.getLocalPart();
    }

    public static String getPrefix(String uri, Node e) {
        while (e != null && (e.getNodeType() == Element.ELEMENT_NODE)) {
            NamedNodeMap attrs = e.getAttributes();
            for (int n = 0; n < attrs.getLength(); n++) {
                Attr a = (Attr) attrs.item(n);
                String name;
                if ((name = a.getName()).startsWith("xmlns:") && a.getNodeValue().equals(uri)) {
                    return name.substring(6);
                }
            }
            e = e.getParentNode();
        }
        return null;
    }

    public static String getNamespace(String prefix, Node e, Node stopNode) {
        while (e != null && (e.getNodeType() == Node.ELEMENT_NODE)) {
            Attr attr = null;
            if (prefix == null) {
                attr = ((Element) e).getAttributeNode("xmlns");
            } else {
                attr = ((Element) e).getAttributeNodeNS(NS_URI_XMLNS, prefix);
            }
            if (attr != null)
                return attr.getValue();
            if (e == stopNode)
                return null;
            e = e.getParentNode();
        }
        return null;
    }

    public static String getNamespace(String prefix, Node e) {
        return getNamespace(prefix, e, null);
    }
}

Related

  1. getPort(Service service, Class seiClass, QName port)
  2. getPrefix(QName qName)
  3. getQualifiedName(QName qName)
  4. getServiceCode(SOAPMessage soap, QName requestElementQName)
  5. getString(QName qName)
  6. getThreddsCatalogAttributeQName(String localName)
  7. getTypeFromQName(final QName name)
  8. getTypeQName(Class type)
  9. getUniquePOAName(QName serviceName, String portName, String poaName)