using System;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Data;
using System.Collections;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
namespace TdoCodeGenerator.TdoGeneratorDom{
[Serializable()]
public class TdoSolutionClass : IXmlSerializable
{
#region Fields
[NonSerialized()]
private string fName;
[NonSerialized()]
private string fPath;
[NonSerialized()]
internal string fOldPath;
[NonSerialized()]
private TdoProjectCollection fTdoProjects;
internal bool fModified;
#endregion Fields
#region Constructors
public TdoSolutionClass()
{
this.fName = String.Empty;
this.fPath = String.Empty;
this.fTdoProjects = new TdoProjectCollection();
this.fModified = false;
}
#endregion Constructors
#region Properties
public string Name
{
get
{
return this.fName;
}
set
{
if (this.fName != value)
{
this.fModified = true;
this.fName = value;
this.Path = String.Empty;
}
}
}
[XmlIgnore()]
public TdoProjectCollection TdoProjects
{
get
{
return this.fTdoProjects;
}
set
{
if (this.fTdoProjects != value) this.fModified = true;
this.fTdoProjects = value;
}
}
public bool Modified
{
get
{
bool modified = this.fModified;
modified |= this.TdoProjects.Modified;
return modified;
}
}
[XmlIgnore()]
public string Path
{
get
{
return this.fPath;
}
set
{
if (this.fPath != value)
{
this.fModified = true;
this.fOldPath = this.fPath;
this.fPath = value;
}
}
}
#endregion Properties
#region Methods
public void Rename(string newName)
{
this.Name = newName;
}
public void Save()
{
if (!this.Modified) return;
//Make all TdoProject path relative
foreach (TdoProjectClass prj in this.TdoProjects)
{
string absPath = prj.Settings.Destination.OutputFolder;
prj.Settings.Destination.OutputFolder = TdoCodeGenerator.Generator.TdoDiscovery.getRelativePath(System.IO.Path.GetDirectoryName(prj.Path), absPath);
prj.Path = TdoCodeGenerator.Generator.TdoDiscovery.getRelativePath(this.Path, System.IO.Path.Combine(System.IO.Path.GetDirectoryName(this.Path), prj.Path));
}
XmlSerializer ser = new XmlSerializer(typeof(TdoSolutionClass));
StreamWriter streamWriter = new StreamWriter(this.fPath);
ser.Serialize(streamWriter, this);
streamWriter.Close();
this.fModified = false;
foreach (TdoProjectClass prj in this.TdoProjects)
{
prj.Path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(this.fPath), prj.Path);
prj.Settings.Destination.OutputFolder = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(prj.Path), prj.Settings.Destination.OutputFolder);
if (prj.Settings.Destination.OutputFolder.EndsWith("\\.\\"))
prj.Settings.Destination.OutputFolder = prj.Settings.Destination.OutputFolder.Substring(0, prj.Settings.Destination.OutputFolder.Length - 3);
prj.fModified = false;
prj.Data.fModified = false;
prj.Settings.fModified = false;
prj.Data.Tables.fModified = false;
prj.Data.Views.fModified = false;
prj.Data.StoredProcedures.fModified = false;
prj.Data.TableValuedFunctions.fModified = false;
prj.Data.ScalarValuedFunctions.fModified = false;
prj.Settings.Destination.fModified = false;
prj.Settings.Source.fModified = false;
}
this.fModified = false;
this.TdoProjects.fModified = false;
}
#endregion Methods
#region Static Fields
internal static string basePath;
#endregion Static Fields
#region Static Methods
public static TdoSolutionClass Open(string path)
{
XmlSerializer ser = new XmlSerializer(typeof(TdoSolutionClass));
StreamReader streamReader = new StreamReader(path);
TdoSolutionClass.basePath = System.IO.Path.GetDirectoryName(path);
TdoSolutionClass result = (TdoSolutionClass)ser.Deserialize(streamReader);
streamReader.Close();
result.Path = path;
foreach (TdoProjectClass prj in result.TdoProjects)
{
prj.Path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), prj.Path);
prj.Settings.Destination.OutputFolder = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(prj.Path), prj.Settings.Destination.OutputFolder);
if (prj.Settings.Destination.OutputFolder.EndsWith("\\.\\"))
prj.Settings.Destination.OutputFolder = prj.Settings.Destination.OutputFolder.Substring(0, prj.Settings.Destination.OutputFolder.Length - 3);
prj.fModified = false;
prj.Data.fModified = false;
prj.Settings.fModified = false;
prj.Data.Tables.fModified = false;
prj.Data.Views.fModified = false;
prj.Data.StoredProcedures.fModified = false;
prj.Data.TableValuedFunctions.fModified = false;
prj.Data.ScalarValuedFunctions.fModified = false;
prj.Settings.Destination.fModified = false;
prj.Settings.Source.fModified = false;
}
result.fModified = false;
result.TdoProjects.fModified = false;
return result;
}
#endregion Static Methods
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
reader.ReadStartElement();
reader.ReadStartElement("Name"); this.fName = reader.ReadString(); reader.ReadEndElement();
XmlDocument xmlTdoProjects = new XmlDocument();
xmlTdoProjects.LoadXml(reader.ReadOuterXml());
XmlNodeList xmlTdoProject = xmlTdoProjects.GetElementsByTagName("TdoProject");
XmlSerializer ser=new XmlSerializer(typeof(TdoProjectClass));
foreach (XmlNode xmlprj in xmlTdoProject)
{
string path = xmlprj["Path"].InnerText;
TdoProjectClass prj = (TdoProjectClass)ser.Deserialize(new StreamReader(System.IO.Path.Combine(TdoSolutionClass.basePath, path)));
prj.Path = path;
prj.fModified = false;
this.fTdoProjects.Add(prj);
}
reader.ReadEndElement();
}
public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement("Name"); writer.WriteString(this.fName); writer.WriteEndElement();
writer.WriteStartElement("TdoProjects");
foreach (TdoProjectClass prj in this.fTdoProjects)
{
writer.WriteStartElement("TdoProject");
writer.WriteStartElement("Path"); writer.WriteString(prj.Path); writer.WriteEndElement();
writer.WriteEndElement();
}
writer.WriteEndElement();
}
[OnDeserialized()]
internal void OnDeserializedMethod(StreamingContext context)
{
this.fModified = false;
}
#endregion
}
[Serializable()]
public class TdoProjectCollection : System.Collections.Generic.List<TdoProjectClass>
{
#region Fields
[NonSerialized()]
internal bool fModified;
#endregion Fields
#region Constructors
public TdoProjectCollection()
{
this.fModified = false;
}
#endregion Constructors
#region Properties
#region Indexers
public new TdoProjectClass this[int index]
{
get
{
return base[index];
}
set
{
base[index] = value;
}
}
public TdoProjectClass this[string name]
{
get
{
for (int i = 0; i < this.Count; i++)
{
if (this[i].Name == name)
return this[i];
}
return null;
}
set
{
for (int i = 0; i < this.Count; i++)
{
if (this[i].Name == name)
{
this[i] = value;
}
}
}
}
#endregion Indexers
public bool Modified
{
get
{
return this.fModified;
}
}
#endregion Properties
#region Methods
public new void Add(TdoProjectClass item)
{
base.Add(item);
this.fModified = true;
}
public new bool Remove(TdoProjectClass item)
{
if (base.Contains(item))
{
this.fModified = true;
return base.Remove(item);
}
else
{
return false;
}
}
public bool Contains(string tdoProjectName)
{
for (int i=0;i<this.Count;i++)
{
if (base[i].Name == tdoProjectName)
return true;
}
return false;
}
#endregion Methods
}
}
|