using System;
using System.Xml;
using Chernobyl.Collections.Generic.Event;
namespace Chernobyl.Reflection.Template.Xml
{
///
/// An in the form of a constructor that is generated
/// from or can be written to an XML stream.
///
public class XmlConstructor : Member
{
///
/// Initializes a new instance of the class.
///
/// The instance that
/// takes and sets service.
/// The this constructor
/// belongs to.
/// The to read the members
/// from.
/// Thrown if the
/// is not positioned on an
/// (see ) of
/// .
public XmlConstructor(IEventCollection services, IInstance instance, XmlReader xmlr)
: base(services, string.Empty, instance)
{
if (xmlr.NodeType != XmlNodeType.Element && xmlr.NodeType != XmlNodeType.Text)
throw new ArgumentException("Unable to extract the XML member data because " +
"The XmlReader passed into this constructor is not positioned on " +
"XML text or an element (i.e., xmlr.NodeType != XmlNodeType.Element || " +
"XmlReader.NodeType != XmlNodeType.Text). Please ensure the " +
"XmlReader is positioned correctly.");
// if this is the Fallout Arguments element then we need to process
// this element before we can move onto the instances defined within
// it.
if (xmlr.LocalName == ArgumentsName && xmlr.NamespaceURI == Fallout.NamepaceUri)
{
// grab all of the attributes off of the member.
XmlAttribute.Create(services, xmlr);
// Make sure we go past the attributes, if they exist, and drop back
// down on the element. After that, we'll want to move to the elements
// content or the next node. If the element is not empty we'll process
// the elements content
xmlr.MoveToElement();
if (xmlr.IsEmptyElement == true)
{
throw new Exception("Explicitly calling a parameterless " +
"constructor is not allowed. This is done for you if " +
"you do not specify a constructor.");
}
xmlr.Read();
}
Name = DefaultName;
// read in the arguments
foreach (IInstance component in XmlInstance.Create(services, xmlr))
ComponentChildren.Add(component);
}
///
/// The name of this component which can be used to generate code for
/// this component.
///
public override string CodeName
{
get
{
if (string.IsNullOrEmpty(_CodeName) == true)
_CodeName = "ctor";
return _CodeName;
}
}
///
/// The local name of the constructor arguments element.
///
public const string ArgumentsName = "Arguments";
///
/// The default value of the property for
/// s.
///
public const string DefaultName = ".ctor";
}
}