/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Globalization;
using System.Collections.Specialized;
using Everest.Library;
using Everest.Library.Resource;
using Everest.Library.ExtensionMethod;
namespace Everest.Forms.SchemaProvider{
public class XmlSchemaProvider : ISchemaProvider
{
protected string schemaPath;
private string fullSchemaPath;
IControlTypeFactory controlTypeFactory;
public XmlSchemaProvider(string schemaPath, IControlTypeFactory controlTypeFactory)
{
this.schemaPath = schemaPath;
InitSchemaPath(schemaPath);
this.controlTypeFactory = controlTypeFactory;
}
private void InitSchemaPath(string schemaPath)
{
this.fullSchemaPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, schemaPath);
string pathWithCulture = Path.Combine(fullSchemaPath, System.Globalization.CultureInfo.CurrentUICulture.Name);
if (Directory.Exists(pathWithCulture))
{
this.fullSchemaPath = pathWithCulture;
}
}
#region ISchemaProvider Members
public ISchema GetSchema(string name, NameValueCollection nameValues)
{
string schemaFile = GetSchameFile(name, nameValues);
if (!File.Exists(schemaFile))
{
throw new FileNotFoundException(new TextResource("SchemaFileNotFound", "Everest.Forms", schemaFile).ToString());
}
XmlDocument xDom = new XmlDocument();
xDom.Load(schemaFile);
XmlNode schemaNode = xDom.SelectSingleNode("/schema");
if (schemaNode == null)
{
throw new Exception(new TextResource("SchemaNodeNotFound", "Everest.Forms").ToString());
}
ISchema schema = new Schema();
schema.Name = name;
schema.Title = schemaNode.Attributes["Title"].Value;
if (schemaNode.Attributes["QueryUrl"] != null)
{
OnDeserializeUrl(schemaNode.Attributes["QueryUrl"]);
schema.QueryUrl = schemaNode.Attributes["QueryUrl"].Value;
}
if (schemaNode.Attributes["DetailsUrl"] != null)
{
OnDeserializeUrl(schemaNode.Attributes["DetailsUrl"]);
schema.DetailsUrl = schemaNode.Attributes["DetailsUrl"].Value;
}
if (schemaNode.Attributes["SubmitUrl"] != null)
{
OnDeserializeUrl(schemaNode.Attributes["SubmitUrl"]);
schema.SubmitUrl = schemaNode.Attributes["SubmitUrl"].Value;
}
if (schemaNode.Attributes["DeleteUrl"] != null)
{
OnDeserializeUrl(schemaNode.Attributes["DeleteUrl"]);
schema.DeleteUrl = schemaNode.Attributes["DeleteUrl"].Value;
}
if (schemaNode.Attributes["GridType"] != null)
{
schema.GridType = schemaNode.Attributes["GridType"].Value;
}
if (schemaNode.Attributes["FormType"] != null)
{
schema.FormType = schemaNode.Attributes["FormType"].Value;
}
DeserializeAttributes(schemaNode, schema);
DeserializeColumns(schemaNode.SelectSingleNode("columns"), schema);
DeserializeReferenceSchema(schemaNode, schema);
return schema;
}
protected virtual string GetSchameFile(string name, NameValueCollection nameValues)
{
string schemaFile = Path.Combine(fullSchemaPath, name + ".xml");
return schemaFile;
}
private void DeserializeColumns(XmlNode columnsNode, IColumnContainer container)
{
if (columnsNode != null)
{
foreach (XmlNode node in columnsNode.ChildNodes)
{
if (node.NodeType == XmlNodeType.Element)
{
IColumn column = null;
if (node.Name == "column")
{
column = ParseColumn(node);
}
if (node.Name == "group")
{
column = ParseGroup(node);
}
if (node != null)
{
container.AddColumn(column);
}
}
}
}
}
private IColumn ParseGroup(XmlNode groupNode)
{
IColumn group = new Column();
group.DataIndexInDetail = "group";
group.Control = controlTypeFactory.ResolveControl("fieldset");
foreach (XmlAttribute attribute in groupNode.Attributes)
{
group.DetailAttributes.Add(attribute.Name, attribute.Value);
}
DeserializeColumns(groupNode, group);
return group;
}
private IColumn ParseColumn(XmlNode node)
{
IColumn column = new Column();
column.Name = node.Attributes["Name"].Value;
column.Label = node.Attributes["Label"].Value;
if (node.Attributes["Control"] != null)
{
IControlType control = controlTypeFactory.ResolveControl(node.Attributes["Control"].Value);
if (control == null)
{
throw new Exception(new TextResource("ControlTypeNotFound", "Everest.Forms", node.Attributes["Control"].Value).ToString());
}
column.Control = control;
}
if (node.Attributes["Queryable"] != null)
{
column.Queryable = bool.Parse(node.Attributes["Queryable"].Value);
}
if (node.Attributes["Length"] != null)
{
column.Length = int.Parse(node.Attributes["Length"].Value);
}
if (node.Attributes["AllowNull"] != null)
{
column.AllowNull = Boolean.Parse(node.Attributes["AllowNull"].Value);
}
if (node.Attributes["Description"] != null)
{
column.Description = node.Attributes["Description"].Value;
}
if (node.Attributes["DefaultValue"] != null)
{
column.DefaultValue = node.Attributes["DefaultValue"].Value;
}
if (node.Attributes["Items"] != null)
{
column.Items = node.Attributes["Items"].Value;
}
if (node.Attributes["EntireRow"] != null)
{
column.EntireRow = bool.Parse(node.Attributes["EntireRow"].Value);
}
if (node.Attributes["DataIndexInList"] != null)
{
column.DataIndexInList = node.Attributes["DataIndexInList"].Value;
}
if (node.Attributes["DataIndexInDetail"] != null)
{
column.DataIndexInDetail = node.Attributes["DataIndexInDetail"].Value;
}
if (node.Attributes["Modifiable"] != null)
{
column.Modifiable = bool.Parse(node.Attributes["Modifiable"].Value);
}
XmlNode list = node.SelectSingleNode("list");
if (list != null)
{
foreach (XmlAttribute att in list.Attributes)
{
OnDeserializeColumnListAttributes(att);
column.ListAttributes.Add(att.Name, att.Value);
}
}
XmlNode detail = node.SelectSingleNode("detail");
if (detail != null)
{
foreach (XmlAttribute att in detail.Attributes)
{
OnDeserializeColumnDetailAttributes(att);
column.DetailAttributes.Add(att.Name, att.Value);
}
}
XmlNode events = node.SelectSingleNode("events");
if (events != null)
{
foreach (XmlAttribute att in events.Attributes)
{
OnDeserializeColumnEventAttributes(att);
column.Events.Add(att.Name, att.Value);
}
}
return column;
}
private void DeserializeReferenceSchema(XmlNode schemaNode, ISchema schema)
{
XmlNode refrenceSchemasNode = schemaNode.SelectSingleNode("references");
if (refrenceSchemasNode != null)
{
List<IReferenceForm> schemaReferences = new List<IReferenceForm>();
foreach (XmlNode node in refrenceSchemasNode.ChildNodes)
{
if (node.NodeType != XmlNodeType.Comment)
{
if (node.Name == "schema")
{
DesignByContract.Require(node.Attributes["Name"] != null && !string.IsNullOrEmpty(node.Attributes["Name"].Value)
, "Name");
DesignByContract.Require(node.Attributes["FormType"] != null && !string.IsNullOrEmpty(node.Attributes["FormType"].Value)
, "FormType");
SchemaReference refrence = new SchemaReference();
refrence.SchemaName = node.Attributes["Name"].Value;
refrence.FormType = node.Attributes["FormType"].Value;
refrence.DataIndex = node.Attributes["DataIndex"].Value;
schemaReferences.Add(refrence);
}
else if (node.Name == "generator")
{
DesignByContract.Require(node.Attributes["Type"] != null && !string.IsNullOrEmpty(node.Attributes["Type"].Value)
, "Type");
GeneratorReference generator = new GeneratorReference();
generator.Type = node.Attributes["Type"].Value;
generator.DataIndex = node.Attributes["DataIndex"].Value;
schemaReferences.Add(generator);
}
}
}
schema.ReferenceForms = schemaReferences.ToArray();
}
}
private void DeserializeAttributes(XmlNode schemaNode, ISchema schema)
{
XmlNode attributesNode = schemaNode.SelectSingleNode("attributes");
if (attributesNode != null)
{
XmlNode formAttributesNode = attributesNode.SelectSingleNode("form");
if (formAttributesNode != null)
{
foreach (XmlAttribute att in formAttributesNode.Attributes)
{
OnDeserializeFormAttributes(att);
schema.FormAttributes.Add(att.Name, att.Value);
}
}
XmlNode gridAttributesNode = attributesNode.SelectSingleNode("grid");
if (gridAttributesNode != null)
{
foreach (XmlAttribute att in gridAttributesNode.Attributes)
{
OnDeserializeGridAttributes(att);
schema.GridAttributes.Add(att.Name, att.Value);
}
}
}
}
#endregion
#region override point
protected virtual void OnDeserializeUrl(XmlAttribute attr)
{
}
protected virtual void OnDeserializeFormAttributes(XmlAttribute attr)
{
}
protected virtual void OnDeserializeGridAttributes(XmlAttribute attr)
{
}
protected virtual void OnDeserializeColumnListAttributes(XmlAttribute attr)
{
}
protected virtual void OnDeserializeColumnDetailAttributes(XmlAttribute attr)
{
}
protected virtual void OnDeserializeColumnEventAttributes(XmlAttribute attr)
{
}
#endregion
}
}
|