package org.jxmpp.stringprep.simple;

import java.util.Locale;
import org.jxmpp.stringprep.XmppStringPrepUtil;
import org.jxmpp.stringprep.XmppStringprep;
import org.jxmpp.stringprep.XmppStringprepException;

public class SimpleXmppStringprep implements XmppStringprep {
   private static SimpleXmppStringprep instance;
   private static final char[] LOCALPART_FURTHER_EXCLUDED_CHARACTERS = new char[]{'"', '&', '\'', '/', ',', '<', '>', '@', ' '};

   public static void setup() {
      XmppStringPrepUtil.setXmppStringprep(getInstance());
   }

   public static SimpleXmppStringprep getInstance() {
      if (instance == null) {
         instance = new SimpleXmppStringprep();
      }

      return instance;
   }

   private SimpleXmppStringprep() {
   }

   public String localprep(String string) throws XmppStringprepException {
      string = simpleStringprep(string);
      char[] arr$ = string.toCharArray();
      int len$ = arr$.length;

      for(int i$ = 0; i$ < len$; ++i$) {
         char charFromString = arr$[i$];
         char[] arr$ = LOCALPART_FURTHER_EXCLUDED_CHARACTERS;
         int len$ = arr$.length;

         for(int i$ = 0; i$ < len$; ++i$) {
            char forbiddenChar = arr$[i$];
            if (charFromString == forbiddenChar) {
               throw new XmppStringprepException(string, "Localpart must not contain '" + forbiddenChar + "'");
            }
         }
      }

      return string;
   }

   public String domainprep(String string) throws XmppStringprepException {
      return simpleStringprep(string);
   }

   public String resourceprep(String string) throws XmppStringprepException {
      return string;
   }

   private static String simpleStringprep(String string) {
      String res = string.toLowerCase(Locale.US);
      return res;
   }
}
