Example usage for org.w3c.dom Element isDefaultNamespace

List of usage examples for org.w3c.dom Element isDefaultNamespace

Introduction

In this page you can find the example usage for org.w3c.dom Element isDefaultNamespace.

Prototype

public boolean isDefaultNamespace(String namespaceURI);

Source Link

Document

This method checks if the specified namespaceURI is the default namespace or not.

Usage

From source file:com.marklogic.dom.DocumentImpl.java

@Override
public boolean isDefaultNamespace(String namespaceURI) {
    Element e = getDocumentElement();
    return e != null ? e.isDefaultNamespace(namespaceURI) : false;
}

From source file:com.evolveum.midpoint.util.DOMUtil.java

/**
 * @param element Element, on which the namespace declaration is evaluated
 * @param namespaceUri Namespace URI to be assigned to a prefix
 * @param preferredPrefix Preferred prefix
 * @param definitionElement Element, on which namespace declaration will be created (there should not be any redefinitions between definitionElement and element in order for this to work...)
 * @param allowUseOfDefaultNamespace If we are allowed to use default namespace (i.e. return empty prefix). This is important for QNames, see setQNameValue
 * @return prefix that is really used//from   w  w  w.j av a2 s .co  m
 *
 * Returned prefix is never null nor "" if allowUseOfDefaultNamespace is false.
 */

public static String lookupOrCreateNamespaceDeclaration(Element element, String namespaceUri,
        String preferredPrefix, Element definitionElement, boolean allowUseOfDefaultNamespace) {
    // We need to figure out correct prefix. We have namespace URI, but we
    // need a prefix to specify in the xsi:type or element name
    if (!StringUtils.isBlank(preferredPrefix)) {
        String namespaceForPreferredPrefix = element.lookupNamespaceURI(preferredPrefix);
        if (namespaceForPreferredPrefix == null) {
            // preferred prefix is not yet bound
            setNamespaceDeclaration(definitionElement, preferredPrefix, namespaceUri);
            return preferredPrefix;
        } else {
            if (namespaceForPreferredPrefix.equals(namespaceUri)) {
                return preferredPrefix;
            } else {
                // Prefix conflict, we need to create different prefix
                // Just going on will do that 
            }
        }
    }
    if (allowUseOfDefaultNamespace && element.isDefaultNamespace(namespaceUri)) {
        // Namespace URI is a default namespace. Return empty prefix;
        return "";
    }
    // We DO NOT WANT to use default namespace for QNames. QNames without prefix are NOT considered by midPoint to belong to the default namespace.
    String prefix = element.lookupPrefix(namespaceUri);
    if (prefix == null) {
        // generate random prefix
        boolean gotIt = false;
        for (int i = 0; i < RANDOM_ATTR_PREFIX_MAX_ITERATIONS; i++) {
            prefix = generatePrefix();
            if (element.lookupNamespaceURI(prefix) == null) {
                // the prefix is free
                gotIt = true;
                break;
            }
        }
        if (!gotIt) {
            throw new IllegalStateException("Unable to generate unique prefix for namespace " + namespaceUri
                    + " even after " + RANDOM_ATTR_PREFIX_MAX_ITERATIONS + " attempts");
        }
        setNamespaceDeclaration(definitionElement, prefix, namespaceUri);
    }
    return prefix;
}