Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.*;

public class Main {
    /**
     * Sets a named attribute of a Node, or removes the attribute if the value is null;
     * @param node The node to search
     * @param attr The name of the attribute to set or remove
     * @param value The value to assign, or NULL to remove
     */
    public synchronized static void setOrRemoveAttribute(Node node, String attr, String value) {
        if (value == null) {
            removeAttribute(node, attr);
        } else {
            setAttribute(node, attr, value);
        }
    }

    /**
     * Sets a named attribute of an Element, or removes the attribute if the value is null;
     * @param element The element to search
     * @param attr The name of the attribute to set or remove
     * @param value The value to assign, or NULL to remove
     */
    public synchronized static void setOrRemoveAttribute(Element element, String attr, String value) {
        if (value == null) {
            element.removeAttribute(attr);
        } else {
            element.setAttribute(attr, value);
        }
    }

    /**
     *  Removes a named attribute of a Node<p>
     *  @param node The node to search
     *  @param attr The name of the attribute to remove
     */
    public synchronized static void removeAttribute(Node node, String attr) {
        if (node == null)
            throw new IllegalArgumentException("Node argument cannot be null");
        if (attr == null)
            throw new IllegalArgumentException("Node attribute argument cannot be null");

        NamedNodeMap map = node.getAttributes();
        if (map != null) {
            Node an = map.getNamedItem(attr);
            if (an != null)
                map.removeNamedItem(attr);
        }
    }

    /**
     *  Sets a named attribute of a Node
     *  @param node The node
     *  @param attr The name of the attribute to set
     *  @param value The value to assign to the attribute
     */
    public synchronized static void setAttribute(Node node, String attr, String value) {
        if (node == null)
            throw new IllegalArgumentException("Node argument cannot be null");
        if (attr == null)
            throw new IllegalArgumentException("Node attribute argument cannot be null");
        if (value == null)
            throw new IllegalArgumentException("Node attribute value argument cannot be null");

        Node attrN = null;
        NamedNodeMap map = node.getAttributes();
        if (map != null)
            attrN = map.getNamedItem(attr);

        if (attrN == null) {
            attrN = node.getOwnerDocument().createAttribute(attr);
            map.setNamedItem(attrN);
        }

        attrN.setNodeValue(value);
    }
}