package com.ericsson.ims.mtas.xdms.common.namespace;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.xml.bind.marshaller.NamespacePrefixMapper;

public class CustomNameSpacePrefixMapper extends NamespacePrefixMapper
{
	public static Logger logger = LoggerFactory.getLogger(CustomNameSpacePrefixMapper.class);

	private Map<String, String> nameSpacePrefix = new HashMap<String, String>();

	private String[] preDeclaredNamespaceUris;

	public CustomNameSpacePrefixMapper()
	{
		nameSpacePrefix.put("http://schemas.ericsson.com/cai3g1.2/", "cai3g");
		nameSpacePrefix.put("http://schemas.ericsson.com/mtas/mmtel/cai3g", "mc");
	}

	@Override
	public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix)
	{
		if (nameSpacePrefix.get(namespaceUri) != null)
			// && namespaceUri.equalsIgnoreCase(getPreDeclaredNamespaceUris()[0]))
			return nameSpacePrefix.get(namespaceUri);
		else
		{
			if (namespaceUri.equalsIgnoreCase(getPreDeclaredNamespaceUris()[0])
					|| namespaceUri.equalsIgnoreCase("http://www.w3.org/XML/1998/namespace")
					|| namespaceUri.equalsIgnoreCase("http://www.w3.org/2001/XMLSchema-instance"))
				logger.debug("Default namespace uri used -> " + getPreDeclaredNamespaceUris()[0]);
			else
				logger.warn("No name space prefix found for name space " + namespaceUri);
			return null;
		}
	}

	public void setPreDeclaredNamespaceUris(String[] preDeclaredNamespaceUris)
	{
		this.preDeclaredNamespaceUris = preDeclaredNamespaceUris;
	}

	public String[] getPreDeclaredNamespaceUris()
	{
		return preDeclaredNamespaceUris;
	}
}
