Beispiel #1
0
        //---------------------------------------------------------------------
        // Private methods
        //---------------------------------------------------------------------
        private string Encode(UTF32String pSource, IEncodingOption pOption)
        {
            bool        bAllAscii = false;
            string      encoded   = null;
            UTF32String prepared  = null;

            // Step #1: set the flag, all ascii?
            bAllAscii = Converter.IsAllAscii(pSource);

            // Step #2
            if (!bAllAscii)
            {
                // check if we need to prepare the string
                if (null != m_preparer)
                {
                    prepared = m_preparer.Prepare(pSource, pOption);
                }
                else
                {
                    prepared = pSource;
                }
            }

            // Step #3: check if we need to apply the rules
            if (pOption.IsOptionSet(EncodingOption.USE_STD3_RULES))
            {
                // failed on Dns compatible?
                if (!Converter.IsAllDnsCompatible(prepared))
                {
                    throw new Std3RuleCodePointException(string.Format("The input does not conform to the STD 3 ASCII rules(DNS Compatible): {0}", prepared.ToString()));
                }

                if (0 < prepared.Length)
                {
                    // first char is hyphen?
                    if (prepared[0] == Converter.CHAR_HYPHEN)
                    {
                        throw new Std3RuleCodePointException(string.Format("The input does not conform to the STD 3 ASCII rules(Hyphen at the beginning): {0}", prepared.ToString()));
                    }

                    // last char is hyphen?
                    if (prepared[prepared.Length - 1] == Converter.CHAR_HYPHEN)
                    {
                        throw new Std3RuleCodePointException(string.Format("The input does not conform to the STD 3 ASCII rules(Hyphen at the end): {0}", prepared.ToString()));
                    }
                }
            }

            //Step #4: check if it's all ascii already
            if (!bAllAscii)
            {
                // Step #5: check if it begin with the 'prefix'
                if (m_converter.IsBeginWithPrefix(prepared))
                {
                    throw new ACEException(string.Format("The input can't begin with an ACE prefix: {0}", pSource.ToString()));
                }

                // Step #6:
                encoded = m_converter.Encode(prepared, new bool[prepared.Length]);

                //Step #7: insert the prefix
                encoded = encoded.Insert(0, m_converter.Prefix);
            }
            else
            {
                encoded = pSource.ToUTF16();
            }

            // Step #8
            if (encoded.Length > Converter.LABEL_MAX_LENGTH)
            {
                throw new ACEException(string.Format("Encoded name too long: {0}", encoded.Length));
            }

            // Done
            return(encoded);
        }
Beispiel #2
0
 /// <summary>
 /// Calls method to write out drink to console
 /// </summary>
 /// <param name="drink">
 /// Drink to be written out
 /// </param>
 public void Prepare(IDrink drink)
 {
     Preparer.Prepare(drink);
 }