예제 #1
0
파일: BaseProperty.cs 프로젝트: nhaberl/PDI
        /// <summary>
        /// This method is used to encode a value for output to a PDI data stream
        /// </summary>
        /// <param name="data">The data string to encode</param>
        /// <returns>The encoded data</returns>
        /// <remarks><para>By default, it can handle Base 64 and Quoted-Printable encoding.  For 7-bit and 8-bit
        /// encoding, it will escape characters as needed.  The method used depends on the current setting of the
        /// <see cref="Encoding"/> property.  It can be overridden to support other types of encoding.</para>
        ///
        /// <para>For vCard 2.1 and vCalendar 1.0 properties, if a <see cref="CharacterSet"/> parameter has been
        /// specified, the value is converted from the one specified by the <see cref="DefaultEncoding"/>
        /// property to the one specified by the <c>CharacterSet</c> property before encoding it.</para></remarks>
        public virtual string Encode(string data)
        {
            string encoded;

            // It's only done if the value is inline, text, or binary
            if (this.ValueLocation != ValLocValue.Inline && this.ValueLocation != ValLocValue.Text &&
                this.ValueLocation != ValLocValue.Binary)
            {
                return(data);
            }

            if ((this.Version == SpecificationVersions.vCard21 || this.Version == SpecificationVersions.vCalendar10) &&
                String.Compare(this.CharacterSet, CharSetValue.ASCII, StringComparison.OrdinalIgnoreCase) != 0)
            {
                Encoding destEnc   = Encoding.GetEncoding(this.CharacterSet);
                byte[]   destBytes = Encoding.Convert(defaultEncoding, destEnc, defaultEncoding.GetBytes(data));

                // Use the literal bytes.  If we get the string, the encoder gives us the decoded values which is
                // not what we want to write out.
                StringBuilder sb = new StringBuilder(destBytes.Length);

                for (int nIdx = 0; nIdx < destBytes.Length; nIdx++)
                {
                    sb.Append((char)destBytes[nIdx]);
                }

                data = sb.ToString();
            }

            switch (this.EncodingMethod)
            {
            case EncodingType.SevenBit:
            case EncodingType.EightBit:
                // Escape characters for these if necessary.  vCard 2.1 and vCalendar 1.0 do not escape
                // commas and semi-colons.
                if (this.Version == SpecificationVersions.vCard21 || this.Version == SpecificationVersions.vCalendar10)
                {
                    encoded = EncodingUtils.RestrictedEscape(data);
                }
                else
                {
                    encoded = EncodingUtils.Escape(data);
                }
                break;

            case EncodingType.QuotedPrintable:
                encoded = EncodingUtils.ToQuotedPrintable(data, 75);
                break;

            case EncodingType.Base64:
            case EncodingType.BEncoding:
                // Base 64 encoding
                encoded = EncodingUtils.ToBase64(data, 75, (this.EncodingMethod == EncodingType.Base64));
                break;

            default:        // Custom, can't do anything with it
                encoded = data;
                break;
            }

            return(encoded);
        }