예제 #1
0
        private void ProcessElement(ApplyElementSubNode applyElementSubNode, bool result)
        {
            if (result)
            {
                foreach (var child in new Queue <XNode>(applyElementSubNode.Element.Nodes()))
                {
                    child.Remove();
                    applyElementSubNode.ParentApplyElement.AddBeforeSelf(child);
                }
            }
            else
            {
                switch (applyElementSubNode.OnNotAppliedAction)
                {
                case OnNotAppliedAction.Remove:
                    applyElementSubNode.Element.Remove();
                    break;

                case OnNotAppliedAction.CommentOut:
                    using (var ms = new MemoryStream())
                        using (var writer = new XmlTextWriter(ms, Encoding.UTF8))
                            using (var reader = new StreamReader(ms))
                            {
                                foreach (var child in new Queue <XNode>(applyElementSubNode.Element.Nodes()))
                                {
                                    child.WriteTo(writer);
                                    child.Remove();
                                }
                                writer.Flush();
                                ms.Position = 0;

                                var commentedOutNode = new XComment(reader.ReadToEnd().Trim());
                                applyElementSubNode.ParentApplyElement.AddBeforeSelf(commentedOutNode);

                                if (!applyElementSubNode.OnCommentedOutComment.IsNullOrEmpty())
                                {
                                    var commentForCommentedOutNode = new XComment(" " + applyElementSubNode.OnCommentedOutComment + " ");
                                    commentedOutNode.AddBeforeSelf(commentForCommentedOutNode);
                                    commentForCommentedOutNode.AddAfterSelf(new XText(Environment.NewLine));
                                    commentedOutNode.AddAfterSelf(new XText(Environment.NewLine));
                                    commentedOutNode.AddAfterSelf(new XText(Environment.NewLine));
                                }
                            }
                    break;

                default:
                    throw new NotSupportedException("OnNotAppliedAction not supported: " +
                                                    applyElementSubNode.OnNotAppliedAction);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Returns a <see cref="Result{ApplyElementSubNode}"/> containing either a successfully created <see cref="ApplyElementSubNode"/> from the
        /// supplied element, or an error message.
        /// </summary>
        /// <param name="element">XElement from which to create the <see cref="ApplyElementSubNode"/> instance.</param>
        /// <param name="defaultOnNotAppliedAction">The default action for sub-nodes which do not specify their own "on no applied" action.</param>
        /// <returns>An <see cref="ApplyElementSubNode"/> instance created from the supplied element</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="element"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if the supplied <paramref name="element"/> is not in the configgen namespace.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the supplied <paramref name="element"/> does not have a name of "When", "ElseWhen" or "Else".</exception>
        public virtual Result <ApplyElementSubNode, string> Create([NotNull] XElement element, OnNotAppliedAction defaultOnNotAppliedAction)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }
            if (element.Name.Namespace != XmlTemplate.ConfigGenXmlNamespace)
            {
                throw new ArgumentException("The supplied element is not in the ConfigGen namespace", nameof(element));
            }

            var instance = new ApplyElementSubNode {
                ParentApplyElement = element.Parent, Element = element
            };

            switch (element.Name.LocalName)
            {
            case "When":
                instance.SubNodeType = ApplyElementSubNodeType.When;
                instance.Predicate   = element.GetConditionAttribute();
                if (instance.Predicate == null)
                {
                    return(Result <ApplyElementSubNode, string> .CreateFailureResult("A 'When' element must contain a 'condition' attribute"));
                }
                break;

            case "ElseWhen":
                instance.SubNodeType = ApplyElementSubNodeType.ElseWhen;
                instance.Predicate   = element.GetConditionAttribute();
                if (instance.Predicate == null)
                {
                    return(Result <ApplyElementSubNode, string> .CreateFailureResult("An 'ElseWhen' element must contain a 'condition' attribute"));
                }
                break;

            case "Else":
                instance.SubNodeType = ApplyElementSubNodeType.Else;
                var predicate = element.GetConditionAttribute();
                if (predicate != null)
                {
                    return(Result <ApplyElementSubNode, string> .CreateFailureResult("An 'Else' element must not contain a 'condition' attribute"));
                }
                break;

            default:
                throw new InvalidOperationException("Cannot create a ApplyElementSubNode from an element with the name: " + element.Name.LocalName);
            }

            instance.OnNotAppliedAction = element.GetOnNotAppliedAttribute(defaultOnNotAppliedAction);

            var onCommentedOutCommentAttribute = element.Attribute(XName.Get("onCommentedOutComment"));

            if (onCommentedOutCommentAttribute != null)
            {
                if (instance.OnNotAppliedAction == OnNotAppliedAction.CommentOut)
                {
                    instance.OnCommentedOutComment = onCommentedOutCommentAttribute.Value;
                }
                else
                {
                    throw new InvalidOperationException("Cannot supply onCommentedOutComment attribute unless onNotApplied='CommentOut'");
                }
            }

            return(Result <ApplyElementSubNode, string> .CreateSuccessResult(instance));
        }