All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.buschmais.jqassistant.plugin.graphml.report.impl.GraphMLNamespaceContext Maven / Gradle / Ivy

There is a newer version: 1.12.2
Show newest version
package com.buschmais.jqassistant.plugin.graphml.report.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;

class GraphMLNamespaceContext implements NamespaceContext {

    private static final String XSI_NS_PREFIX = "xsi";

    private Map namespaceByPrefix;

    private Map prefixByNameSpace;

    private Map schemaLocations;

    public GraphMLNamespaceContext(Map additionalNamespaces, Map schemaLocations) {
        this.namespaceByPrefix = createDefaultNamespaces();
        this.namespaceByPrefix.putAll(additionalNamespaces);
        this.schemaLocations = schemaLocations;
        this.prefixByNameSpace = new HashMap<>();
        for (Map.Entry entry : this.namespaceByPrefix.entrySet()) {
            this.prefixByNameSpace.put(entry.getValue(), entry.getKey());
        }
    }

    private Map createDefaultNamespaces() {
        LinkedHashMap defaultNS = new LinkedHashMap<>();
        defaultNS.put(XSI_NS_PREFIX, XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
        return defaultNS;
    }

    @Override
    public String getNamespaceURI(String prefix) {
        return getOrDefault(namespaceByPrefix, prefix, XMLConstants.NULL_NS_URI);
    }

    @Override
    public String getPrefix(String namespaceURI) {
        return getOrDefault(prefixByNameSpace, namespaceURI, XMLConstants.DEFAULT_NS_PREFIX);
    }

    @Override
    public Iterator getPrefixes(String namespaceURI) {
        return Collections.singletonList(getPrefix(namespaceURI)).iterator();
    }

    /**
     * Return all registered namespaces and their prefixes.
     *
     * @return The registered namespaces.
     */
    Map getNamespaces() {
        return namespaceByPrefix;
    }

    /**
     * Return namespaces and their schema locatons.
     *
     * @return The schema locations.
     */
    public Map getSchemaLocations() {
        return schemaLocations;
    }

    private  V getOrDefault(Map map, K key, V defaultValue) {
        if (map.containsKey(key)) {
            return map.get(key);
        }
        return defaultValue;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy