//
//
//
//
// $Revision$
//
using System;
using System.IO;
using System.Text;
using System.Xml;
namespace ICSharpCode.XmlEditor
{
///
/// Utility class that will encode special XML characters.
///
public sealed class XmlEncoder
{
XmlEncoder()
{
}
///
/// Encodes any special characters in the xml string.
///
public static string Encode(string xml, char quoteCharacter)
{
XmlEncoderTextWriter encoderWriter = new XmlEncoderTextWriter();
using (XmlTextWriter writer = new XmlTextWriter(encoderWriter)) {
writer.WriteStartElement("root");
writer.WriteStartAttribute("attribute");
writer.QuoteChar = quoteCharacter;
encoderWriter.BeginMarkup();
writer.WriteString(xml);
return encoderWriter.Markup;
}
}
///
/// Special XmlTextWriter that will return the last item written to
/// it from a certain point. This is used by the XmlEncoder to
/// get the encoded attribute string so the XmlEncoder does not
/// have to do the special character encoding itself, but can
/// use the .NET framework to do the work.
///
class XmlEncoderTextWriter : EncodedStringWriter
{
StringBuilder markup = new StringBuilder();
public XmlEncoderTextWriter() : base(Encoding.UTF8)
{
}
///
/// Sets the point from which we are interested in
/// saving the string written to the text writer.
///
public void BeginMarkup()
{
markup = new StringBuilder();
}
public void EndMarkup()
{
BeginMarkup();
}
///
/// Returns the string written to this text writer after the
/// BeginMarkup method was called.
///
public string Markup {
get {
return markup.ToString();
}
}
public override void Write(string text)
{
base.Write(text);
markup.Append(text);
}
public override void Write(char value)
{
base.Write(value);
markup.Append(value);
}
}
}
}