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

net.sf.xolite.impl.MapPrefixResolver Maven / Gradle / Ivy

Go to download

This project provides a lightweight framework to serialize/deserialize (or marshall/unmarshall) java objects into XML. The implementation is based on standard SAX (Simple Api for Xml) but it follows a original approach based on the strong data encapsulation paradigm of Object Oriented (OO) programming.

The newest version!
/*-------------------------------------------------------------------------
 Copyright 2006 Olivier Berlanger

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 -------------------------------------------------------------------------*/
package net.sf.xolite.impl;


import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.xml.namespace.NamespaceContext;


/**
 * Default (Map based) implementation of the NamespaceContext interface.
 */
public class MapPrefixResolver implements NamespaceContext {


    /** Map from key=prefix to URI. */
    private Map uriMappings;
    /** Map from key=URI to set of prefixes. */
    private Map> prefixMappings;


    public MapPrefixResolver() {
        uriMappings = new HashMap();
        prefixMappings = new HashMap>();
    }


    public void addAll(MapPrefixResolver other) {
        if (other != null) {
            String uri;
            String prefix;
            for (Iterator uris = other.prefixMappings.keySet().iterator(); uris.hasNext();) {
                uri = uris.next();
                for (Iterator prefixes = other.getPrefixes(uri); prefixes.hasNext();) {
                    prefix = prefixes.next();
                    addPrefixMapping(prefix, uri);
                }
            }
        }
    }


    public void addPrefixMapping(String prefix, String namespaceURI) {
        if (prefix == null) throw new IllegalArgumentException("Added prefix cannot be null");
        if (namespaceURI == null) throw new IllegalArgumentException("Added Namespace URI cannot be null");
        if (uriMappings.containsKey(prefix)) {
            throw new IllegalArgumentException("MapPrefixResolver doesn't support prefix redefinition: " + prefix + "="
                    + namespaceURI + " WAS " + prefix + "=" + uriMappings.get(prefix));
        }
        uriMappings.put(prefix, namespaceURI);
        Set prefixs = prefixMappings.get(namespaceURI);
        if (prefixs == null) {
            prefixs = new HashSet();
            prefixMappings.put(namespaceURI, prefixs);
        }
        prefixs.add(prefix);
    }


    public void removePrefixMapping(String prefix, String namespaceURI) {
        if (prefix == null) throw new IllegalArgumentException("Removed prefix cannot be null");
        if (namespaceURI == null) throw new IllegalArgumentException("Removed Namespace URI cannot be null");
        uriMappings.remove(prefix);
        Set prefixs = prefixMappings.get(namespaceURI);
        if (prefixs != null) prefixs.remove(prefix);
    }


    public void clear() {
        uriMappings.clear();
        prefixMappings.clear();
    }


    /**
     * Get namespace URI corresponding to the given prefix.
     * 
     * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
     */
    public String getNamespaceURI(String prefix) {
        return uriMappings.get(prefix);
    }


    /**
     * Get the first prefix mapped to the given namespace.
     * 
     * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
     */
    public String getPrefix(String namespaceURI) {
        Set prefixs = prefixMappings.get(namespaceURI);
        if (prefixs == null) return null;
        return (prefixs.isEmpty()) ? null : (String) prefixs.iterator().next();
    }


    /**
     * Get all the prefixes mapped to the given namespace.
     * 
     * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String)
     */
    public Iterator getPrefixes(String namespaceURI) {
        Set prefixs = prefixMappings.get(namespaceURI);
        if (prefixs == null) prefixs = new HashSet();
        return prefixs.iterator();
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy