Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * This file is part of experimaestro.
 * Copyright (c) 2014 B. Piwowarski <benjamin@bpiwowar.net>
 *
 * experimaestro is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * experimaestro is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with experimaestro.  If not, see <http://www.gnu.org/licenses/>.
 */

import org.w3c.dom.*;

import java.util.Map.Entry;

import java.util.TreeMap;

public class Main {
    /**
     * Gather all the namespaces defined on a node
     *
     * @return
     */
    public static Iterable<Entry<String, String>> getNamespaces(Element element) {
        TreeMap<String, String> map = new TreeMap<String, String>();
        do {
            NamedNodeMap attributes = element.getAttributes();
            for (int i = 0; i < attributes.getLength(); i++) {
                Attr attr = (Attr) attributes.item(i);
                final String name = attr.getLocalName();

                if (attr.getPrefix() != null) {
                    if ("xmlns".equals(attr.getPrefix()))
                        if (!map.containsKey(name))
                            map.put(name, attr.getValue());
                } else if ("xmlns".equals(name)) {
                    if (!map.containsKey(""))
                        map.put("", attr.getValue());
                }
            }
            if (element.getParentNode() == null || element.getParentNode().getNodeType() != Node.ELEMENT_NODE)
                break;
            element = (Element) element.getParentNode();
        } while (true);
        return map.entrySet();
    }
}