214 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			214 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Text;
 | 
						|
using System.Xml;
 | 
						|
using System.Xml.Serialization;
 | 
						|
using System.Xml.Schema;
 | 
						|
using System.IO;
 | 
						|
using VEPROMS.CSLA.Library;
 | 
						|
 | 
						|
namespace DataLoader
 | 
						|
{
 | 
						|
	[XmlRoot("DataLoaderGlitches")]
 | 
						|
	//[Serializable()]
 | 
						|
	public class DataLoaderGlitches
 | 
						|
	{
 | 
						|
		private Glitches _Glitches;
 | 
						|
 | 
						|
		public Glitches Glitches
 | 
						|
		{
 | 
						|
			get 
 | 
						|
			{
 | 
						|
				if (_Glitches == null) _Glitches = new Glitches();
 | 
						|
				return _Glitches; 
 | 
						|
			}
 | 
						|
			set {	_Glitches = value; }
 | 
						|
		}
 | 
						|
		public void Add(string cause, string originalText, string newText)
 | 
						|
		{
 | 
						|
			Glitches.Add(cause, originalText, newText);
 | 
						|
		}
 | 
						|
		public void Save(string fileName)
 | 
						|
		{
 | 
						|
			XmlSerializer<DataLoaderGlitches>.WriteFile(this, fileName);
 | 
						|
		}
 | 
						|
		public static DataLoaderGlitches LoadProblems(string fileName)
 | 
						|
		{
 | 
						|
			return XmlSerializer<DataLoaderGlitches>.ReadFile(fileName);
 | 
						|
		}
 | 
						|
	}
 | 
						|
	public class Glitches : List<Glitch>
 | 
						|
	{
 | 
						|
		public void Add(string cause, string originalText, string newText)
 | 
						|
		{
 | 
						|
			base.Add(new Glitch(cause, originalText, newText));
 | 
						|
		}
 | 
						|
	}
 | 
						|
	public class Glitch
 | 
						|
	{
 | 
						|
		public Glitch()
 | 
						|
		{
 | 
						|
		}
 | 
						|
		public Glitch(string cause, string originalText, string newText)
 | 
						|
		{
 | 
						|
			_Cause = cause;
 | 
						|
			_OriginalText = originalText;
 | 
						|
			_NewText = newText;
 | 
						|
		}
 | 
						|
		private string _Cause;
 | 
						|
		[XmlAttribute("Cause")]
 | 
						|
		public string Cause
 | 
						|
		{
 | 
						|
			get { return _Cause; }
 | 
						|
			set { _Cause = value; }
 | 
						|
		}
 | 
						|
		private string _OriginalText;
 | 
						|
		[XmlAttribute("OriginalText")]
 | 
						|
		public string OriginalText
 | 
						|
		{
 | 
						|
			get { return _OriginalText; }
 | 
						|
			set { _OriginalText = value; }
 | 
						|
		}
 | 
						|
		private string _NewText;
 | 
						|
		[XmlAttribute("NewText")]
 | 
						|
		public string NewText
 | 
						|
		{
 | 
						|
			get { return _NewText; }
 | 
						|
			set { _NewText = value; }
 | 
						|
		}
 | 
						|
		private string _Comparison;
 | 
						|
		[XmlIgnore]
 | 
						|
		public string Comparison
 | 
						|
		{
 | 
						|
			get 
 | 
						|
			{
 | 
						|
				if (_Comparison == null)
 | 
						|
					_Comparison = GetComparison(_OriginalText, _NewText);
 | 
						|
				return _Comparison; 
 | 
						|
			}
 | 
						|
		}
 | 
						|
		private string GetComparison(string str1, string str2)
 | 
						|
		{
 | 
						|
			if(str1 == str2)return str1;
 | 
						|
			int endMatch = 0;
 | 
						|
			int l1 = str1.Length;
 | 
						|
			int l2 = str2.Length;
 | 
						|
			int min = l1 > l2 ? l2 : l1;
 | 
						|
			int startMatch = min;
 | 
						|
			for(int i = 0; i< min; i++)
 | 
						|
				if (str1[i] != str2[i])
 | 
						|
				{
 | 
						|
					startMatch = i;
 | 
						|
					break;
 | 
						|
				}
 | 
						|
			for (int i = 1; i < min; i++)
 | 
						|
				if (str1[l1-i] != str2[l2-i])
 | 
						|
				{
 | 
						|
					endMatch = i-1;
 | 
						|
					break;
 | 
						|
				}
 | 
						|
			string prefix = str1.Substring(0, startMatch);
 | 
						|
			string part1 = str1.Substring(startMatch, l1 - (startMatch + endMatch));
 | 
						|
			string part2 = str2.Substring(startMatch, l2 - (startMatch + endMatch));
 | 
						|
			string suffix = str1.Substring(l1 - endMatch, endMatch);
 | 
						|
			_Delta = "<<<" + part1 + "|||" + part2 + ">>>";
 | 
						|
			return prefix + "\r\n<<<" + part1 + "|||" + part2 + ">>>\r\n" + suffix;
 | 
						|
		}
 | 
						|
		private string _Delta = null;
 | 
						|
		public override string ToString()
 | 
						|
		{
 | 
						|
			if (Comparison == null) { ;} // This gets the Comparsion and sets _Delta
 | 
						|
			return _Delta;
 | 
						|
		}
 | 
						|
	}
 | 
						|
	public static class XmlSerializer<T> where T : class
 | 
						|
	{
 | 
						|
		public static string StringSerialize(T t)
 | 
						|
		{
 | 
						|
			string strOutput = string.Empty;
 | 
						|
			XmlSerializer xs = new XmlSerializer(typeof(T), "http://www.volian.com");
 | 
						|
			using (MemoryStream ms = new MemoryStream())
 | 
						|
			{
 | 
						|
				xs.Serialize(new NonXsiTextWriter(ms, Encoding.UTF8), t);
 | 
						|
				//xs.Serialize(ms, t);
 | 
						|
				ms.Position = 0;
 | 
						|
				StreamReader sr = new StreamReader(ms);
 | 
						|
				strOutput = sr.ReadToEnd();
 | 
						|
				ms.Close();
 | 
						|
			}
 | 
						|
			return strOutput;
 | 
						|
		}
 | 
						|
		public static T StringDeserialize(string s)
 | 
						|
		{
 | 
						|
			T t;
 | 
						|
			XmlSerializer xs = new XmlSerializer(typeof(T), "http://www.volian.com");
 | 
						|
			UTF8Encoding enc = new UTF8Encoding();
 | 
						|
			Byte[] arrBytData = enc.GetBytes(s);
 | 
						|
			using (MemoryStream ms = new MemoryStream(arrBytData))
 | 
						|
			{
 | 
						|
				t = (T)xs.Deserialize(ms);
 | 
						|
			}
 | 
						|
			return t;
 | 
						|
		}
 | 
						|
		public static void WriteFile(T t, string fileName)
 | 
						|
		{
 | 
						|
			string strOutput = string.Empty;
 | 
						|
			XmlSerializer xs = new XmlSerializer(typeof(T), "http://www.volian.com");
 | 
						|
			using (FileStream fs = new FileStream(fileName, FileMode.Create))
 | 
						|
			{
 | 
						|
				xs.Serialize(new NonXsiTextWriter(fs, Encoding.UTF8), t);
 | 
						|
				fs.Close();
 | 
						|
			}
 | 
						|
		}
 | 
						|
		public static T ReadFile(string fileName)
 | 
						|
		{
 | 
						|
			T t;
 | 
						|
			XmlSerializer xs = new XmlSerializer(typeof(T), "http://www.volian.com");
 | 
						|
			using (FileStream fs = new FileStream(fileName, FileMode.Open))
 | 
						|
			{
 | 
						|
				t = (T)xs.Deserialize(fs);
 | 
						|
			}
 | 
						|
			return t;
 | 
						|
		}
 | 
						|
	}
 | 
						|
	public class NonXsiTextWriter : XmlTextWriter
 | 
						|
	{
 | 
						|
		public NonXsiTextWriter(TextWriter w) : base(w) { }
 | 
						|
		public NonXsiTextWriter(Stream w, Encoding encoding)
 | 
						|
			: base(w, encoding)
 | 
						|
		{
 | 
						|
			this.Formatting = Formatting.Indented;
 | 
						|
		}
 | 
						|
		public NonXsiTextWriter(string filename, Encoding encoding) : base(filename, encoding) { }
 | 
						|
		bool _skip = false;
 | 
						|
		public override void WriteStartAttribute(string prefix, string localName, string ns)
 | 
						|
		{
 | 
						|
			if ((prefix == "xmlns" && (localName == "xsd" || localName == "xsi")) || // Omits XSD and XSI declarations. 
 | 
						|
				ns == XmlSchema.InstanceNamespace) // Omits all XSI attributes. 
 | 
						|
			{
 | 
						|
				_skip = true;
 | 
						|
				return;
 | 
						|
			}
 | 
						|
			if (localName == "xlink_href")
 | 
						|
				base.WriteStartAttribute(prefix, "xlink:href", ns);
 | 
						|
			else
 | 
						|
				base.WriteStartAttribute(prefix, localName, ns);
 | 
						|
		}
 | 
						|
		public override void WriteString(string text)
 | 
						|
		{
 | 
						|
			if (_skip) return;
 | 
						|
			base.WriteString(text);
 | 
						|
		}
 | 
						|
		public override void WriteEndAttribute()
 | 
						|
		{
 | 
						|
			if (_skip)
 | 
						|
			{ // Reset the flag, so we keep writing. 
 | 
						|
				_skip = false;
 | 
						|
				return;
 | 
						|
			}
 | 
						|
			base.WriteEndAttribute();
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
}
 |