using System;
using System.Collections.Generic;
using System.Text;
namespace Paulb
{
///
/// Helper class to encode string values for use in BizTalk.
///
public static class PropertyEncoder
{
///
/// Encodes the filename with the SharePoint filename rules, all illegal charaters are
/// removed.
/// The following characters are not valid in a SharePoint documentlibrary:
/// "/ \ : * ? < > | " # { } % & ~ \t ,"
///
/// The filename to encode.
/// The encoded filename.
public static string EncodeFileName(string fileName)
{
if( string.IsNullOrEmpty(fileName)){ return fileName; }
int index = 0;
while( (index = fileName.IndexOfAny(new char[]{'/', '\\',':', '*', '?' ,'<', '>', '|', '"', '#', '{', '}', '%', '&', '~', '\t', ','})) > 0 ){
fileName = fileName.Remove(index, 1);
}
return System.Web.HttpUtility.HtmlEncode(fileName);
}
///
/// Encodes the supplied value for use in XML and the BizTalk XLANG language.
///
/// The value to encode.
/// The encoded string.
public static string Encode(string value)
{
if (string.IsNullOrEmpty(value))
{
return value;
}
string result = System.Web.HttpUtility.HtmlEncode(value);
return BizTalkEncode(result);
}
// Encodes BizTalk specific characters.
private static string BizTalkEncode(string value)
{
string tmp1 = value.Replace(@"\", @"\\");
string tmp2 = tmp1.Replace(@"%", @"\%");
return tmp2;
}
}
}