//-----------------------------------------------------------------------
//
// Copyright (c) 2009 Deltares. All rights reserved.
//
// B. Faassen
// barry.faassen@deltares.nl
// 25-3-2009
//
// Conatins a base class for creatin input and output domain objects
// from xml documents
//
//-----------------------------------------------------------------------
using System;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using Deltares.Standard.IO.DtoAssembler;
namespace Deltares.Standard
{
///
/// Default implementation for the interface
///
///
/// This base class inherits from this class just fixes
/// the dto type to XElement
///
/// The domain object type
public abstract class DtoDocumentAssembler : IDtoAssembler
where TDomainInputObject : new()
where TDomainOutputObject : new()
{
///
/// Initializes a new instance of the DtoDocumentAssembler class
///
/// Sets the element name
/// The text reader stream for reading in ther xsd file which is embedded as an resource
public DtoDocumentAssembler(string elementName, string elementNamespace, string embeddedXsdPath)
: this(elementName, elementNamespace, embeddedXsdPath, null, null)
{
}
///
/// Initializes a new instance of the DtoDocumentAssembler class
///
/// Sets the element name
/// The text reader stream for reading in ther xsd file which is embedded as an resource
public DtoDocumentAssembler(string elementName, string elementNamespace, string embeddedXsdPath,
DtoAssembler inputAssembler, DtoAssembler outputAssembler)
{
if (elementName == null)
throw new ArgumentNullException("elementName");
if (elementNamespace == null)
throw new ArgumentNullException("elementNamespace");
this.ElementName = elementName;
this.ElementNamespace = elementNamespace;
this.OutputAssembler = outputAssembler;
this.InputAssembler = inputAssembler;
//Stream xsdStream = Common.GetEmbeddedFile(Assembly.GetExecutingAssembly(), embeddedXsdPath);
//this.Schema = XDocument.Load(new StreamReader(xsdStream));
}
///
/// Gets the xml element name
///
public string ElementName { get; private set; }
///
/// Gets or sets the namespace value for the element
///
public string ElementNamespace { get; private set; }
///
/// Gets the xml schema (if any)
///
public XDocument Schema { get; private set; }
///
///
///
public DtoAssembler InputAssembler { get; set; }
///
///
///
public DtoAssembler OutputAssembler { get; set; }
#region IDtoAssembler Members
public virtual TDomainOutputObject CreateOutputObject(XDocument dtoDocument)
{
if (dtoDocument == null)
throw new ArgumentNullException("dtoDocument");
if (this.OutputAssembler != null)
{
// the document should contain the element only once!
XElement el = (from x in dtoDocument.Descendants()
where x.Name.LocalName == this.OutputAssembler.ElementName
select x).Single();
return this.OutputAssembler.CreateDomainObject(el);
}
return default(TDomainOutputObject);
}
public virtual TDomainInputObject CreateInputObject(XDocument dtoDocument)
{
if (dtoDocument == null)
throw new ArgumentNullException("dtoDocument");
if (this.InputAssembler != null)
{
// the document should contain the element only once!
XElement el = (from x in dtoDocument.Descendants()
where x.Name.LocalName == this.InputAssembler.ElementName
select x).Single();
return this.InputAssembler.CreateDomainObject(el);
}
return default(TDomainInputObject);
}
public virtual XDocument CreateDataTransferObject(TDomainInputObject domainInputObj)
{
var doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
// add the file identification to the document
var fileId = new FileIdentification(Assembly.GetCallingAssembly());
var fileIdAssembler = new FileIdentificationAssembler();
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace tns = this.ElementNamespace;
XNamespace tns1 = fileIdAssembler.ElementNamespace;
var root = new XElement(tns + this.ElementName,
new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
new XAttribute(XNamespace.Xmlns + "tns", tns.NamespaceName),
new XAttribute(XNamespace.Xmlns + "tns1", tns1.NamespaceName));
doc.Add(root);
doc.Root.Add(fileId.ToDto(fileIdAssembler, tns));
if (this.InputAssembler != null)
{
XNamespace tns2 = this.InputAssembler.ElementNamespace;
root.Add(new XAttribute(XNamespace.Xmlns + "tns2", tns2.NamespaceName));
doc.Root.Add(domainInputObj.ToDto(this.InputAssembler, tns));
}
return doc;
}
#endregion
public TDomainOutputObject CreateOutputObject(string fileName)
{
return this.CreateOutputObject(this.CreateDocument(fileName));
}
public TDomainInputObject CreateInputObject(string fileName)
{
return this.CreateInputObject(this.CreateDocument(fileName));
}
private XDocument CreateDocument(string fileName)
{
if (!fileName.HasValidStringValue())
throw new ArgumentNullException("fileName");
XDocument doc = XDocument.Load(fileName);
return doc;
}
}
}