Example usage for org.w3c.dom Attr getLocalName

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

Introduction

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

Prototype

public String getLocalName();

Source Link

Document

Returns the local part of the qualified name of this node.

Usage

From source file:DOMUtils.java

public static void compareNodes(Node expected, Node actual) throws Exception {
    if (expected.getNodeType() != actual.getNodeType()) {
        throw new Exception("Different types of nodes: " + expected + " " + actual);
    }/*from   w  w w. ja  v  a  2  s .com*/
    if (expected instanceof Document) {
        Document expectedDoc = (Document) expected;
        Document actualDoc = (Document) actual;
        compareNodes(expectedDoc.getDocumentElement(), actualDoc.getDocumentElement());
    } else if (expected instanceof Element) {
        Element expectedElement = (Element) expected;
        Element actualElement = (Element) actual;

        // compare element names
        if (!expectedElement.getLocalName().equals(actualElement.getLocalName())) {
            throw new Exception("Element names do not match: " + expectedElement.getLocalName() + " "
                    + actualElement.getLocalName());
        }
        // compare element ns
        String expectedNS = expectedElement.getNamespaceURI();
        String actualNS = actualElement.getNamespaceURI();
        if ((expectedNS == null && actualNS != null) || (expectedNS != null && !expectedNS.equals(actualNS))) {
            throw new Exception("Element namespaces names do not match: " + expectedNS + " " + actualNS);
        }

        String elementName = "{" + expectedElement.getNamespaceURI() + "}" + actualElement.getLocalName();

        // compare attributes
        NamedNodeMap expectedAttrs = expectedElement.getAttributes();
        NamedNodeMap actualAttrs = actualElement.getAttributes();
        if (countNonNamespaceAttribures(expectedAttrs) != countNonNamespaceAttribures(actualAttrs)) {
            throw new Exception(elementName + ": Number of attributes do not match up: "
                    + countNonNamespaceAttribures(expectedAttrs) + " "
                    + countNonNamespaceAttribures(actualAttrs));
        }
        for (int i = 0; i < expectedAttrs.getLength(); i++) {
            Attr expectedAttr = (Attr) expectedAttrs.item(i);
            if (expectedAttr.getName().startsWith("xmlns")) {
                continue;
            }
            Attr actualAttr = null;
            if (expectedAttr.getNamespaceURI() == null) {
                actualAttr = (Attr) actualAttrs.getNamedItem(expectedAttr.getName());
            } else {
                actualAttr = (Attr) actualAttrs.getNamedItemNS(expectedAttr.getNamespaceURI(),
                        expectedAttr.getLocalName());
            }
            if (actualAttr == null) {
                throw new Exception(elementName + ": No attribute found:" + expectedAttr);
            }
            if (!expectedAttr.getValue().equals(actualAttr.getValue())) {
                throw new Exception(elementName + ": Attribute values do not match: " + expectedAttr.getValue()
                        + " " + actualAttr.getValue());
            }
        }

        // compare children
        NodeList expectedChildren = expectedElement.getChildNodes();
        NodeList actualChildren = actualElement.getChildNodes();
        if (expectedChildren.getLength() != actualChildren.getLength()) {
            throw new Exception(elementName + ": Number of children do not match up: "
                    + expectedChildren.getLength() + " " + actualChildren.getLength());
        }
        for (int i = 0; i < expectedChildren.getLength(); i++) {
            Node expectedChild = expectedChildren.item(i);
            Node actualChild = actualChildren.item(i);
            compareNodes(expectedChild, actualChild);
        }
    } else if (expected instanceof Text) {
        String expectedData = ((Text) expected).getData().trim();
        String actualData = ((Text) actual).getData().trim();

        if (!expectedData.equals(actualData)) {
            throw new Exception("Text does not match: " + expectedData + " " + actualData);
        }
    }
}

From source file:com.predic8.membrane.core.config.spring.AbstractParser.java

protected void setProperties(String prop, Element e, BeanDefinitionBuilder builder) {
    NamedNodeMap attributes = e.getAttributes();
    HashMap<String, String> attrs = new HashMap<String, String>();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr item = (Attr) attributes.item(i);
        if (item.getLocalName() != null)
            attrs.put(item.getLocalName(), item.getValue());
    }/* w  w w  .ja  va2s . com*/
    builder.addPropertyValue(prop, attrs);
}

From source file:com.apporiented.spring.override.GenericBeanDefinitionParser.java

/**
 * Parse the supplied {@link org.w3c.dom.Element} and populate the supplied
 * {@link org.springframework.beans.factory.support.BeanDefinitionBuilder} as required.
 * <p>This implementation maps any attributes present on the
 * supplied element to {@link org.springframework.beans.PropertyValue}
 * instances, and/*from  w  ww.  j  a  v a 2s  .  c  o  m*/
 * {@link org.springframework.beans.factory.support.BeanDefinitionBuilder#addPropertyValue(String, Object) adds them}
 * to the
 * {@link org.springframework.beans.factory.config.BeanDefinition builder}.
 * <p>The {@link #extractPropertyName(String)} method is used to
 * reconcile the name of an attribute with the name of a JavaBean
 * property.
 * @param element the XML element being parsed
 * @param parserContext the object encapsulating the current state of the parsing process
 * @param builder used to define the <code>BeanDefinition</code>
 * @see #extractPropertyName(String)
 */
protected final void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {

    NamedNodeMap attributes = element.getAttributes();
    for (int x = 0; x < attributes.getLength(); x++) {
        Attr attribute = (Attr) attributes.item(x);
        String name = attribute.getLocalName();
        if (isEligibleAttribute(name, parserContext)) {
            String propertyName = extractPropertyName(name);
            Assert.state(StringUtils.hasText(propertyName),
                    "Illegal property name returned from 'extractPropertyName(String)': cannot be null or empty.");

            Object value;
            if (references.contains(propertyName)) {
                value = new RuntimeBeanReference(attribute.getValue());
            } else {
                value = attribute.getValue();
            }
            builder.addPropertyValue(propertyName, value);
        }
    }
    postProcess(builder, parserContext, element);
}

From source file:com.predic8.membrane.annot.parser.BlueprintElementParser.java

protected void setProperties(ParserContext context, String springPropertyName, Element element,
        MutableBeanMetadata mcm) {/*  w w  w .  ja  v  a 2 s.  c o m*/
    NamedNodeMap attributes = element.getAttributes();
    HashMap<String, String> attrs = new HashMap<String, String>();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr item = (Attr) attributes.item(i);
        if (item.getLocalName() != null)
            attrs.put(item.getLocalName(), item.getValue());
    }
    MutablePassThroughMetadata pt = context.createMetadata(MutablePassThroughMetadata.class);
    pt.setObject(attrs);
    mcm.addProperty(springPropertyName, pt);
}

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 w  w . j  a  va 2 s  .  c o  m
                }
            }
            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 ava  2 s  . c o  m
        QName aQname = new QName(aAttr.getNamespaceURI(), aAttr.getLocalName());
        if (aQname.equals(qname)) {
            return aAttr;
        }
    }
    return null;
}

From source file:org.solmix.runtime.support.spring.AbstractBeanDefinitionParser.java

/**?Element,?Container*/
protected boolean parseAttributes(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
    NamedNodeMap atts = element.getAttributes();
    boolean setBus = false;
    for (int i = 0; i < atts.getLength(); i++) {
        Attr node = (Attr) atts.item(i);
        String val = node.getValue();
        String pre = node.getPrefix();
        String name = node.getLocalName();
        String prefix = node.getPrefix();

        // Don't process namespaces
        if (isNamespace(name, prefix)) {
            continue;
        }/*w  w  w .ja  v a2  s.  c om*/
        if ("createdFromAPI".equals(name)) {
            bean.setAbstract(true);
        } else if ("abstract".equals(name)) {
            bean.setAbstract(true);
        } else if ("depends-on".equals(name)) {
            bean.addDependsOn(val);
        } else if ("name".equals(name)) {
            parseNameAttribute(element, ctx, bean, val);
        } else if ("container".equals(name)) {
            setBus = parseContainerAttribute(element, ctx, bean, val);
        } else if ("id".equals(name)) {
            parseIdAttribute(bean, element, name, val, ctx);
        } else if (isAttribute(pre, name)) {
            parseAttribute(bean, element, name, val, ctx);
        }
    }
    return setBus;
}

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;
        }/*from  w  ww  .j  a  v a2 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/*  w w w.j  av a  2  s  .c o 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   ww  w  .ja  va2 s . 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());
}