Example usage for org.w3c.dom Attr getNamespaceURI

List of usage examples for org.w3c.dom Attr getNamespaceURI

Introduction

In this page you can find the example usage for org.w3c.dom Attr getNamespaceURI.

Prototype

public String getNamespaceURI();

Source Link

Document

The namespace URI of this node, or null if it is unspecified (see ).

Usage

From source file:edu.mayo.cts2.framework.core.xml.PatchedCastorMarshaller.java

protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException {

    Node node = domSource.getNode();
    if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;

        Node parent = node.getParentNode();
        while (parent != null) {
            NamedNodeMap atts = parent.getAttributes();
            if (atts != null) {
                for (int i = 0, j = atts.getLength(); i < j; i++) {

                    Attr att = (Attr) atts.item(i);
                    if (XMLNS_NS.equals(att.getNamespaceURI())) {
                        String name = att.getName();
                        String value = att.getValue();
                        if (!element.hasAttributeNS(XMLNS_NS, name)) {
                            element.setAttributeNS(XMLNS_NS, name, value);
                        }//from  w  w w.  java  2  s. c om
                    }

                }
            }
            parent = parent.getParentNode();
        }
    }

    return super.unmarshalDomSource(domSource);
}

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

private static boolean isApplicationAttribute(Attr attr) {
    String namespaceURI = attr.getNamespaceURI();
    if (StringUtils.isEmpty(attr.getNamespaceURI()) && HACKED_XSI_TYPE.equals(attr.getName())) {
        return false;
    }/*  w  w  w.j  ava2s  .c  o m*/
    if (namespaceURI == null) {
        return true;
    }
    if (W3C_XML_SCHEMA_XMLNS_URI.equals(namespaceURI)) {
        return false;
    }
    if (W3C_XML_XML_URI.equals(namespaceURI)) {
        return false;
    }
    if (W3C_XML_SCHEMA_INSTANCE_NS_URI.equals(namespaceURI)) {
        return false;
    }
    return true;
}

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

public static boolean isNamespaceDefinition(Attr attr) {
    if (W3C_XML_SCHEMA_XMLNS_URI.equals(attr.getNamespaceURI())) {
        return true;
    }//from  w w  w  . j a v  a 2  s  .c  o  m
    if (attr.getName().startsWith("xmlns:") || "xmlns".equals(attr.getName())) {
        return true;
    }
    return false;
}

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

public static String getNamespaceDeclarationNamespace(Attr attr) {
    if (!W3C_XML_SCHEMA_XMLNS_URI.equals(attr.getNamespaceURI())) {
        throw new IllegalStateException(
                "Attempt to get namespace from a attribute that is not a namespace declaration, it has namespace "
                        + attr.getNamespaceURI());
    }//from  ww w  .  j a  va 2s . c o  m
    String attrName = attr.getName();
    if (!attrName.startsWith("xmlns:") && !"xmlns".equals(attr.getName())) {
        throw new IllegalStateException(
                "Attempt to get namespace from a attribute that is not a namespace declaration, it is "
                        + attrName);
    }
    return attr.getValue();
}

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

public static String getNamespaceDeclarationPrefix(Attr attr) {
    if (!W3C_XML_SCHEMA_XMLNS_URI.equals(attr.getNamespaceURI())) {
        throw new IllegalStateException(
                "Attempt to get prefix from a attribute that is not a namespace declaration, it has namespace "
                        + attr.getNamespaceURI());
    }/*from  www  .  j  a  v  a 2s . com*/
    String attrName = attr.getName();
    if (attrName.startsWith("xmlns:")) {
        return attrName.substring(6);
    }
    if ("xmlns".equals(attrName)) {
        return null;
    }
    throw new IllegalStateException(
            "Attempt to get prefix from a attribute that is not a namespace declaration, it is " + attrName);
}

From source file:net.di2e.ecdr.search.transform.atom.security.impl.XmlMetadataSecurityMarkingHandler.java

@Override
public SecurityData getSecurityData(Metacard metacard) {
    String metadata = metacard.getMetadata();
    if (StringUtils.isNotBlank(metadata)) {
        XPathHelper helper = new XPathHelper(metacard.getMetadata());
        Document document = helper.getDocument();
        NodeList nodeList = document.getElementsByTagNameNS("*", "security");
        if (nodeList != null && nodeList.getLength() > 0) {
            Element element = (Element) nodeList.item(0);
            NamedNodeMap nodeNameMap = element.getAttributes();
            int length = nodeNameMap.getLength();

            Map<String, List<String>> securityProps = new HashMap<String, List<String>>();
            String securityNamespace = null;
            for (int i = 0; i < length; i++) {
                Attr attr = (Attr) nodeNameMap.item(i);
                String value = attr.getValue();
                if (!attr.getName().startsWith(XMLNS_PREFIX) && StringUtils.isNotBlank(value)) {
                    securityProps.put(attr.getLocalName(), SecurityMarkingParser.getValues(value));
                    if (securityNamespace == null) {
                        securityNamespace = attr.getNamespaceURI();
                    }//from w ww.j  ava2  s.  c om
                }
            }
            if (!securityProps.isEmpty()) {
                return new SecurityDataImpl(securityProps, securityNamespace);
            }
        }
    }
    return null;
}

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

private static Attr findAttributeByQName(NamedNodeMap attrs, QName qname) {
    for (int i = 0; i < attrs.getLength(); i++) {
        Node aItem = attrs.item(i);
        Attr aAttr = (Attr) aItem;
        if (aAttr.getLocalName() == null) {
            continue;
        }/*w  w  w.j  a  v  a  2s . c om*/
        QName aQname = new QName(aAttr.getNamespaceURI(), aAttr.getLocalName());
        if (aQname.equals(qname)) {
            return aAttr;
        }
    }
    return null;
}

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

private static boolean compareAttributesIsSubset(NamedNodeMap subset, NamedNodeMap superset,
        boolean considerNamespacePrefixes) {
    for (int i = 0; i < subset.getLength(); i++) {
        Node aItem = subset.item(i);
        Attr aAttr = (Attr) aItem;
        if (!considerNamespacePrefixes && isNamespaceDefinition(aAttr)) {
            continue;
        }//  ww w.ja  va  2  s  .c  o m
        if (StringUtils.isBlank(aAttr.getLocalName())) {
            // this is strange, but it can obviously happen
            continue;
        }
        QName aQname = new QName(aAttr.getNamespaceURI(), aAttr.getLocalName());
        Attr bAttr = findAttributeByQName(superset, aQname);
        if (bAttr == null) {
            return false;
        }
        if (!StringUtils.equals(aAttr.getTextContent(), bAttr.getTextContent())) {
            return false;
        }
    }
    return true;
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test//  ww w  .  j  av a  2  s  .co  m
public void testAddAttributeFromQNameWithoutNamespace() throws Exception {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    SOAPElement retValue = element.addAttribute(new QName("attr"), "value");
    assertSame(element, retValue);
    Attr attr = element.getAttributeNodeNS(null, "attr");
    assertNull(attr.getNamespaceURI());
    String localName = attr.getLocalName();
    // The RI creates a namespace unaware attribute node in this case; we believe
    // it's better to create a namespace aware node.
    if (localName != null) {
        assertEquals("attr", attr.getLocalName());
    }
    assertNull(attr.getPrefix());
    assertEquals("value", attr.getValue());
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test//from  www.j a  va 2s.c o  m
public void testAddAttributeFromNameWithoutNamespace() throws Exception {
    SOAPEnvelope envelope = saajUtil.createSOAP11Envelope();
    SOAPBody body = envelope.addBody();
    SOAPElement element = body.addChildElement(new QName("urn:test", "test"));
    SOAPElement retValue = element.addAttribute(envelope.createName("attr"), "value");
    assertSame(element, retValue);
    Attr attr = element.getAttributeNodeNS(null, "attr");
    assertNull(attr.getNamespaceURI());
    String localName = attr.getLocalName();
    if (localName != null) {
        assertEquals("attr", attr.getLocalName());
    }
    assertNull(attr.getPrefix());
    assertEquals("value", attr.getValue());
}