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

org.daisy.common.transform.StringWithNamespaceContext Maven / Gradle / Ivy

The newest version!
package org.daisy.common.transform;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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

public class StringWithNamespaceContext extends InputValue implements NamespaceContext {

	private final String value;
	private final Map namespaces;
	private Map prefixes;

	public StringWithNamespaceContext(String value, Map bindings) {
		super(value);
		this.value = value;
		namespaces = bindings == null ? null : Collections.unmodifiableMap(bindings);
	}

	@Override
	public String toString() {
		return value;
	}

	@Override
	public String getNamespaceURI(String prefix) {
		return namespaces != null ? namespaces.get(prefix) : XMLConstants.NULL_NS_URI;
	}

	@Override
	public String getPrefix(String namespaceURI) {
		if (prefixes == null && namespaces != null) {
			prefixes = new HashMap();
			for (Map.Entry e : namespaces.entrySet())
				prefixes.put(e.getValue(), e.getKey());
		}
		return prefixes != null ? prefixes.get(namespaceURI) : null;
	}

	@Override
	public Iterator getPrefixes(String namespaceURI) {
		if (namespaces == null)
			return Collections.emptyIterator();
		List prefixes = new ArrayList();
		for (String prefix : namespaces.keySet())
			if (namespaces.get(prefix).equals(namespaceURI))
				prefixes.add(prefix);
		return prefixes.iterator();
	}

	public Iterator getPrefixes() {
		return namespaces.keySet().iterator();
	}

	public Map getNamespaceBindingsAsMap() {
		return namespaces;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy