using System; using System.Xml.Linq; namespace dk.nsi.seal.Model.DomBuilders { public abstract class AbstractDomBuilder { private XDocument localDoc; /// /// Instructs the build to construct the object. /// /// The constructed instance. public abstract T Build(); /// /// Add a NameSpace attribute to the supplied. /// /// The < code > Element to modify. /// The name space short name.When added, that value will be prefixed with xmlns:. /// The schema location of the NameSpace. protected void AddNs(XElement element, string ns, string schema) => element.Add(new XAttribute(XNamespace.Xmlns + ns, schema)); protected XDocument CreateDocument() { ValidateBeforeBuild(); // Store in class to temporary use. localDoc = new XDocument(); var root = CreateRoot(); AddRootAttributes(root); localDoc.Add(root); AppendToRoot(root); var result = localDoc; localDoc = null; return result; } protected abstract XElement CreateRoot(); /// /// Method called by . /// Use this method to add additional NameSpace declarations to the or attributes to the root element. /// /// The envelope instance. protected abstract void AddRootAttributes(XElement root); protected abstract void AppendToRoot(XElement root); /// /// Validate the value of the attribute. /// This method is a simple validator, that only checks for null values. /// Throws if the value fails validation. /// /// The name of the attribute being validated. /// This value is used for providing an informative exception cause. /// The value to validate. protected void Validate(string attribute, Object value) { if (value == null) { throw new ModelException(attribute + " is mandatory - but was null."); } } /// /// Validate an attribute. /// This method validates that a string attribute is neither null, an empty string or a string of spaces. /// Throws if the value fails validation. /// /// The name of the attribute being validated. /// This value is used for providing an informative exception cause. /// The value to validate. protected void Validate(string attribute, string value) { if (value == null) { throw new ModelException(attribute + " is mandatory - but was null."); } ValidateValue(attribute, value); } /// /// Method called by before the process of creating the is initiated. /// Implementers can use the method to verify that all mandatory attriutes are set before constructing the . /// Throws if one or more requirements are not fulfilled. /// protected abstract void ValidateBeforeBuild(); /// /// Validate the value of an attribute. /// This method validates that a string attribute is neither an empty string or a string of spaces. /// Throws if the value fails validation. /// /// The name of the attribute being validated. /// This value is used for providing an informative exception cause. /// The value to validate - if null the validation will be skipped. protected void ValidateValue(string attribute, string value) { if (value != null) { if (value.Length == 0) { throw new ModelException(attribute + " is mandatory - but was an empty String."); } else if (string.IsNullOrWhiteSpace(value)) { throw new ModelException(attribute + " is mandatory - but was an empty String."); } } } } }