|
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
namespace ObjectMapper.Providers
{
class XmlClassMapperProvider : IClassMapperProvider
{
XmlDocument document;
public List<ClassMapper> GetClasses(object dataSource)
{
this.LoadXmlDocument((Stream)dataSource);
return ParseDocument();
}
private void LoadXmlDocument(Stream source)
{
document = new XmlDocument();
document.Load(source);
}
private List<ClassMapper> ParseDocument()
{
XmlNode node = document.SelectSingleNode("types");
XmlNodeList classNodes = node.SelectNodes("class");
List<ClassMapper> classes = new List<ClassMapper>();
foreach (XmlNode n in classNodes)
classes.Add(ParseClass(n));
return classes;
}
private static ClassMapper ParseClass(XmlNode classNode)
{
ClassMapper c = new ClassMapper();
c.ClassName = classNode.Attributes["name"].Value;
c.TableName = classNode.Attributes["table"].Value;
c.Namespace = classNode.Attributes["namespace"].Value;
if (classNode.Attributes["SqlStoredProceduresPrefix"] != null)
c.SqlStoredProceduresPrefix = classNode.Attributes["SqlStoredProceduresPrefix"].Value;
if (classNode.Attributes["ExportLocation"] != null)
c.ExportLocation = classNode.Attributes["ExportLocation"].Value;
if (classNode.Attributes["ExecuteStoredProcedures"] != null)
if (classNode.Attributes["ExecuteStoredProcedures"].Value == "true")
c.ExecuteStoredProcedures = true;
if (classNode.Attributes["DropExistingStoredProcedures"] != null)
if (classNode.Attributes["DropExistingStoredProcedures"].Value == "false")
c.DropExistingStoredProcedures = false;
if (classNode.Attributes["GenerateStoredProcedures"] != null)
if (classNode.Attributes["GenerateStoredProcedures"].Value == "true")
c.GenerateStoredProcedures = true;
XmlNodeList propertyNodes = classNode.SelectNodes("property");
foreach (XmlNode propertyNode in propertyNodes)
{
Property p = new Property();
p.Name = propertyNode.Attributes["name"].Value;
p.Column = propertyNode.Attributes["column"].Value;
p.Class = c;
XmlAttribute attr = propertyNode.Attributes["IsPrimaryKey"];
if (attr != null && attr.Value.ToLower() == "true")
p.IsPrimaryKey = true;
attr = propertyNode.Attributes["IsIdentity"];
if (attr != null && attr.Value.ToLower() == "true")
p.IsIdentity = true;
p.Type = propertyNode.Attributes["type"].Value;
p.SqlType = propertyNode.Attributes["SqlType"].Value;
attr = propertyNode.Attributes["SqlLength"];
if (attr != null)
p.SqlLength = int.Parse(propertyNode.Attributes["SqlLength"].Value);
c.Properties.Add(p);
}
return c;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.