コード例 #1
0
        //---------------------------------------------------------------------
        // Public methods
        //---------------------------------------------------------------------
        public string Encode(string pSource)
        {
            string         encoded = null;
            StringBuilder  buffer  = null;
            EncodingOption option  = null;
            UTF32String    source  = null;
            UTF32String    label   = null;

            UTF32String[] labelArray = null;


            // check if we have encoder or not
            if (null == m_converter)
            {
                throw new ACEException("No ace converter defined");
            }

            pSource = StringUtil.Normalize(pSource);
            if (null == pSource)
            {
                throw new ACEException("Encoding error, invalid input(null, Encode)");
            }

            // Initializes
            buffer     = new StringBuilder();
            source     = new UTF32String(pSource);
            option     = new EncodingOption();
            labelArray = source.Split(Converter.SEPERATORS);

            // for each label do the encoding
            for (int index = 0; index < labelArray.Length; index++)
            {
                label = StringUtil.Normalize(labelArray[index]);
                if (null == label)
                {
                    throw new ACEException(string.Format("Encoding error, empty label: {0}", pSource.ToString()));
                }

                // encode each label
                encoded = this.Encode(label, option);

                // append the encoded
                buffer.Append(encoded);
                if (index < (labelArray.Length - 1))
                {
                    // Based on RFC3492, only allow FULL_STOP in
                    // encoded string as seperator
                    buffer.Append(".");
                }
            }

            // return the encoded string
            return(buffer.ToString());
        }
コード例 #2
0
        public string Decode(string pSource)
        {
            string label = null;

            string[]       labelArray = null;
            StringBuilder  buffer     = null;
            EncodingOption option     = null;
            UTF32String    decoded    = null;

            // check if we have encoder or not
            if (null == m_converter)
            {
                return(null);
            }

            if (null == pSource)
            {
                return(null);
            }

            // Initializes
            buffer = new StringBuilder();
            option = new EncodingOption();

            // SHOULD ONLY contain FULL_STOP as seperator(RFC3492)
            labelArray = pSource.Split('.');

            // for each label do the decoding
            for (int index = 0; index < labelArray.Length; index++)
            {
                label   = labelArray[index];
                decoded = StringUtil.Normalize(this.Decode(label, option));
                if (null == decoded)
                {
                    throw new ACEException(string.Format("Decoding error, empty label: {0}", pSource));
                }


                // append the decoded
                buffer.Append(decoded.ToUTF16());
                if (index < (labelArray.Length - 1))
                {
                    // Based on RFC3492, only allow FULL_STOP in
                    // string as seperator after decoding
                    buffer.Append(".");
                }
            }

            // return the decoded string
            return(buffer.ToString());
        }