Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    private static final String GENERIC_BRANCH_COVERAGE = "generic-branch-coverage";
    private static final List<String> genericBranchCoverageTypes = new ArrayList<String>(6);

    /**
     * Qualification of every element in a DOM document. Previous namespaces are
     * ignored.
     * 
     * @param document
     *            The document to modify.
     * @param namespaces
     *            A map of prefix/URL where prefix are expected to match the
     *            local name of the elements they qualify (if an element has no
     *            corresponding prefix, it uses the namespace of its parent).
     */
    public static void autoQualify(Document document, Map<String, String> namespaces) {
        Element root = document.getDocumentElement();
        for (Map.Entry<String, String> entry : namespaces.entrySet()) {
            String prefix = entry.getKey();
            String url = entry.getValue();
            StringBuilder attributName = new StringBuilder("xmlns");
            if (prefix != null) {
                attributName.append(':').append(prefix);
            }
            root.setAttribute(attributName.toString(), url);
        }
        autoQualify(document, root, namespaces, null);
    }

    private static void autoQualify(Document document, Element element, Map<String, String> namespaces,
            String parentPrefix) {
        String localName = element.getLocalName();
        element = renameElement(document, element, localName, namespaces.get(parentPrefix));
        String prefix = parentPrefix;
        if (namespaces.containsKey(localName.toLowerCase())) {
            prefix = localName;
        } else if (genericBranchCoverageTypes.contains(localName.toLowerCase())) {
            prefix = GENERIC_BRANCH_COVERAGE;
        }
        NodeList nodes = element.getChildNodes();
        for (int i = 0, iEnd = nodes.getLength(); i < iEnd; ++i) {
            Node node = nodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                autoQualify(document, (Element) node, namespaces, prefix);
            }
        }
    }

    /**
     * Rename an element in a DOM document. It happens to involves a node
     * replication.
     * 
     * @param document
     *            The document containing the element (some way to verify
     *            that?).
     */
    public static Element renameElement(Document document, Element element, String newName, String namespace) {
        if (namespace == null) {
            throw new IllegalArgumentException("No namespace provided for element " + element);
        }
        Element newElement = document.createElementNS(namespace, newName);
        NamedNodeMap attributes = element.getAttributes();
        for (int i = 0, iEnd = attributes.getLength(); i < iEnd; i++) {
            Attr attr2 = (Attr) document.importNode(attributes.item(i), true);
            newElement.getAttributes().setNamedItem(attr2);
        }
        while (element.hasChildNodes()) {
            newElement.appendChild(element.getFirstChild());
        }
        element.getParentNode().replaceChild(newElement, element);
        return newElement;
    }
}