package com.airlock.iam.saml2.infrastructure.plugin;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import javax.xml.namespace.NamespaceContext;

public class SimpleNamespaceContext implements NamespaceContext {
   private final BiMap<String, String> namespaceByPrefix = HashBiMap.create();

   public SimpleNamespaceContext(Map<String, String> namespaceByPrefix) {
      this.namespaceByPrefix.put("", "");
      this.namespaceByPrefix.put("xml", "http://www.w3.org/XML/1998/namespace");
      this.namespaceByPrefix.put("xmlns", "http://www.w3.org/2000/xmlns/");
      this.namespaceByPrefix.putAll(namespaceByPrefix);
   }

   @Override
   public String getNamespaceURI(String prefix) {
      if (prefix == null) {
         throw new IllegalArgumentException("prefix argument was null");
      } else {
         return (String)this.namespaceByPrefix.getOrDefault(prefix, "");
      }
   }

   @Override
   public String getPrefix(String namespaceURI) {
      if (namespaceURI == null) {
         throw new IllegalArgumentException("namespaceURI was null");
      } else {
         return (String)this.namespaceByPrefix.inverse().get(namespaceURI);
      }
   }

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