Example usage for org.w3c.dom Element getAttributes

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

Introduction

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

Prototype

public NamedNodeMap getAttributes();

Source Link

Document

A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.

Usage

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 ww w  .j  a  v a 2  s  . 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) {/*from   ww w.  ja  v  a 2 s  .com*/
    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:DomUtils.java

/**
 * Rename element./*from  w w  w . j  a  v a 2 s  .  c  o m*/
 * @param element The element to be renamed.
 * @param replacementElement The tag name of the replacement element.
 * @param keepChildContent <code>true</code> if the target element's child content
 * is to be copied to the replacement element, false if not. Default <code>true</code>.
 * @param keepAttributes <code>true</code> if the target element's attributes
 * are to be copied to the replacement element, false if not. Default <code>true</code>.
 * @return The renamed element.
 */
public static Element renameElement(Element element, String replacementElement, boolean keepChildContent,
        boolean keepAttributes) {

    Element replacement = element.getOwnerDocument().createElement(replacementElement);
    if (keepChildContent) {
        DomUtils.copyChildNodes(element, replacement);
    }
    if (keepAttributes) {
        NamedNodeMap attributes = element.getAttributes();
        int attributeCount = attributes.getLength();

        for (int i = 0; i < attributeCount; i++) {
            Attr attribute = (Attr) attributes.item(i);
            replacement.setAttribute(attribute.getName(), attribute.getValue());
        }
    }
    DomUtils.replaceNode(replacement, element);

    return replacement;
}

From source file:de.betterform.xml.dom.DOMUtil.java

/**
 * copies all attributes from one Element to another
 *
 * @param from   - the Element which the source attributes
 * @param to     - the target Element for the Attributes
 * @param filter - a NodeFilter to apply during copy
 */// w  w w .j  a v a  2  s . c om
public static void copyAttributes(Element from, Element to, NodeFilter filter) {
    if ((from != null) && (to != null)) {
        NamedNodeMap map = from.getAttributes();

        /* if filter is null use our own default filter, which accepts
           everything (this saves us from always check if filter is
           null */
        if (filter == null) {
            filter = new NodeFilter() {
                public short acceptNode(Node n) {
                    return NodeFilter.FILTER_ACCEPT;
                }
            };
        }

        if (map != null) {
            int len = map.getLength();

            for (int i = 0; i < len; i++) {
                Node attr = map.item(i);

                if (attr.getNodeType() == Node.ATTRIBUTE_NODE) {
                    if (filter.acceptNode(attr) == NodeFilter.FILTER_ACCEPT) {
                        to.setAttributeNS(attr.getNamespaceURI(), attr.getNodeName(), attr.getNodeValue());
                    }
                }
            }
        }
    }
}

From source file:TreeDumper2.java

private void dumpElement(Element node, String indent) {
    System.out.println(indent + "ELEMENT: " + node.getTagName());
    NamedNodeMap nm = node.getAttributes();
    for (int i = 0; i < nm.getLength(); i++)
        dumpLoop(nm.item(i), indent + "  ");
}

From source file:org.focusns.common.web.page.config.xml.XmlPageFactory.java

private Map<String, String> getParameters(Element pageEle) {
    Map<String, String> parameters = new HashMap<String, String>();
    NamedNodeMap nnm = pageEle.getAttributes();
    for (int i = 0; i < nnm.getLength(); i++) {
        Node node = nnm.item(i);/*w  w  w  .  j av  a2 s. c  o m*/
        String paramName = node.getNodeName();
        String paramValue = node.getNodeValue();
        parameters.put(paramName, paramValue);
    }
    return parameters;
}

From source file:com.krawler.esp.utils.mime.MimeTypesReader.java

/** Read Element named mime-type. */
private MimeType readMimeType(Element element) {
    String name = null;//from w ww .  j  a v a  2s.  c om
    String description = null;
    MimeType type = null;
    NamedNodeMap attrs = element.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr = (Attr) attrs.item(i);
        if (attr.getName().equals("name")) {
            name = attr.getValue();
        } else if (attr.getName().equals("description")) {
            description = attr.getValue();
        }
    }
    if ((name == null) || (name.trim().equals(""))) {
        return null;
    }

    try {
        type = new MimeType(name);
    } catch (MimeTypeException mte) {
        // Mime Type not valid... just ignore it
        if (logger.isInfoEnabled()) {
            logger.info(mte.toString() + " ... Ignoring!");
        }
        return null;
    }
    type.setDescription(description);

    NodeList nodes = element.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element nodeElement = (Element) node;
            if (nodeElement.getTagName().equals("ext")) {
                readExt(nodeElement, type);
            } else if (nodeElement.getTagName().equals("magic")) {
                readMagic(nodeElement, type);
            }
        }
    }
    return type;
}

From source file:XMLDocumentWriter.java

/**
 * Output the specified DOM Node object, printing it using the specified
 * indentation string/* w  ww .j  a va 2s .com*/
 */
public void write(Node node, String indent) {
    // The output depends on the type of the node
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE: { // If its a Document node
        Document doc = (Document) node;
        out.println(indent + "<?xml version='1.0'?>"); // Output header
        Node child = doc.getFirstChild(); // Get the first node
        while (child != null) { // Loop 'till no more nodes
            write(child, indent); // Output node
            child = child.getNextSibling(); // Get next node
        }
        break;
    }
    case Node.DOCUMENT_TYPE_NODE: { // It is a <!DOCTYPE> tag
        DocumentType doctype = (DocumentType) node;
        // Note that the DOM Level 1 does not give us information about
        // the the public or system ids of the doctype, so we can't output
        // a complete <!DOCTYPE> tag here. We can do better with Level 2.
        out.println("<!DOCTYPE " + doctype.getName() + ">");
        break;
    }
    case Node.ELEMENT_NODE: { // Most nodes are Elements
        Element elt = (Element) node;
        out.print(indent + "<" + elt.getTagName()); // Begin start tag
        NamedNodeMap attrs = elt.getAttributes(); // Get attributes
        for (int i = 0; i < attrs.getLength(); i++) { // Loop through them
            Node a = attrs.item(i);
            out.print(" " + a.getNodeName() + "='" + // Print attr. name
                    fixup(a.getNodeValue()) + "'"); // Print attr. value
        }
        out.println(">"); // Finish start tag

        String newindent = indent + "    "; // Increase indent
        Node child = elt.getFirstChild(); // Get child
        while (child != null) { // Loop
            write(child, newindent); // Output child
            child = child.getNextSibling(); // Get next child
        }

        out.println(indent + "</" + // Output end tag
                elt.getTagName() + ">");
        break;
    }
    case Node.TEXT_NODE: { // Plain text node
        Text textNode = (Text) node;
        String text = textNode.getData().trim(); // Strip off space
        if ((text != null) && text.length() > 0) // If non-empty
            out.println(indent + fixup(text)); // print text
        break;
    }
    case Node.PROCESSING_INSTRUCTION_NODE: { // Handle PI nodes
        ProcessingInstruction pi = (ProcessingInstruction) node;
        out.println(indent + "<?" + pi.getTarget() + " " + pi.getData() + "?>");
        break;
    }
    case Node.ENTITY_REFERENCE_NODE: { // Handle entities
        out.println(indent + "&" + node.getNodeName() + ";");
        break;
    }
    case Node.CDATA_SECTION_NODE: { // Output CDATA sections
        CDATASection cdata = (CDATASection) node;
        // Careful! Don't put a CDATA section in the program itself!
        out.println(indent + "<" + "![CDATA[" + cdata.getData() + "]]" + ">");
        break;
    }
    case Node.COMMENT_NODE: { // Comments
        Comment c = (Comment) node;
        out.println(indent + "<!--" + c.getData() + "-->");
        break;
    }
    default: // Hopefully, this won't happen too much!
        System.err.println("Ignoring node: " + node.getClass().getName());
        break;
    }
}

From source file:com.krawler.esp.utils.mime.MimeTypesReader.java

/** Read Element named magic. */
private void readMagic(Element element, MimeType mimeType) {
    // element.getValue();
    String offset = null;/*from  w  w  w  .  j  av  a  2s .c o m*/
    String content = null;
    String type = null;
    NamedNodeMap attrs = element.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr = (Attr) attrs.item(i);
        if (attr.getName().equals("offset")) {
            offset = attr.getValue();
        } else if (attr.getName().equals("type")) {
            type = attr.getValue();
        } else if (attr.getName().equals("value")) {
            content = attr.getValue();
        }
    }
    if ((offset != null) && (content != null)) {
        mimeType.addMagic(Integer.parseInt(offset), type, content);
    }
}

From source file:com.ryantenney.metrics.spring.reporter.AbstractReporterElementParser.java

protected void addDefaultProperties(Element element, BeanDefinitionBuilder beanDefBuilder) {
    final Map<String, String> properties = new HashMap<String, String>();
    final NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        final Node attribute = attributes.item(i);
        final String name = attribute.getNodeName();
        if (name.equals(METRIC_REGISTRY_REF) || name.equals(ID) || name.equals(TYPE)) {
            continue;
        }//from  w w w.  j a v  a  2s .  c om
        properties.put(name, attribute.getNodeValue());
    }

    validate(properties);

    beanDefBuilder.addPropertyReference("metricRegistry", element.getAttribute(METRIC_REGISTRY_REF));
    beanDefBuilder.addPropertyValue("properties", properties);
}