Пример #1
0
        public virtual bool PassesValidation(System.Xml.Linq.XObject node, out string details)
        {
            if (null == node)
            {
                throw new ArgumentNullException("node");
            }
            string value;

            if (node is XAttribute)
            {
                value = (node as XAttribute).Value;
            }
            else if (node is XElement)
            {
                value = (node as XElement).Value;
            }
            else
            {
                throw new NotImplementedException("The node type" + node.GetType() + " was not expected");
            }
            return(this.PassesValidation(value, out details));
        }
Пример #2
0
        public override bool PassesValidation(System.Xml.Linq.XObject input, out string details)
        {
            details = null;
            if (null == input)
            {
                throw new ArgumentNullException("input");
            }
            string value;

            if (input is XElement)
            {
                value = (input as XElement).Value;
            }
            else if (input is XAttribute)
            {
                value = (input as XAttribute).Value;
            }
            else
            {
                throw new ArgumentException("The input parameter type " + input.GetType().FullName + " was not expected", "input");
            }
            int count;
            IEnumerable <XElement> same;

            switch (this.UniqueAcross)
            {
            case UniqueAcrossTypes.Document:
                if (null == input.Document)
                {
                    throw new ArgumentException("The XElement must be associated with an XDocument", "input");
                }
                same = input.Document.XPathSelectElements(this.XPathSelector, this.NamespaceManager)
                       .Where(xe => xe.Value == value);
                count = same.Count();
                if (count == 1)
                {
                    return(true);
                }
                else
                {
                    details = String.Format
                              (
                        "There are {0} element{2} with the identifier {1} (on line{2} ",
                        count,
                        value,
                        count == 1 ? String.Empty : "s"
                              );
                    bool firstItem = true;
                    foreach (System.Xml.Linq.XElement xe in same)
                    {
                        if (false == (xe is IXmlLineInfo))
                        {
                            continue;
                        }
                        if (firstItem == false)
                        {
                            details += ", ";
                        }
                        else
                        {
                            firstItem = false;
                        };
                        details += (xe as IXmlLineInfo).LineNumber.ToString();
                    }
                    details += ")";
                    return(false);
                }

            case UniqueAcrossTypes.Context:
                if (null == input.Document)
                {
                    throw new ArgumentException("The XElement must be associated with an XDocument", "input");
                }
                XNode root = input.Document;
                // Is this an identifier inside a provider inside a venue? Because we need to handle it differently if it is...
                var isIdentifierInsideProvider = (
                    (input.Parent.Name.LocalName == "provider")
                    &&
                    (input.Parent.Name.NamespaceName == "http://xcri.org/profiles/1.2/catalog")
                    );
                if (
                    isIdentifierInsideProvider
                    &&
                    (input.Parent.Parent.Name.LocalName == "venue")
                    &&
                    (input.Parent.Parent.Name.NamespaceName == "http://xcri.org/profiles/1.2/catalog")
                    )
                {
                    // For providers inside venue, only worry about the id being unique within this presentation
                    root = XDocument.Parse(input.Parent.Parent.Parent.OuterXml());     // To the presentation!
                }
                // If this is a qualification then the identifier must be unique within the course
                if (
                    (input.Parent.Name.LocalName == "qualification")
                    &&
                    (input.Parent.Name.NamespaceName == "http://purl.org/net/mlo")
                    )
                {
                    root = XDocument.Parse(input.Parent.Parent.OuterXml());
                }
                same = root.XPathSelectElements(this.XPathSelector, this.NamespaceManager)
                       .Where(xe => xe.Value == value)
                       .Where(xe => xe.Parent.Name.LocalName == input.Parent.Name.LocalName)
                       .Where(xe => xe.Parent.Name.NamespaceName == input.Parent.Name.NamespaceName);
                if (
                    isIdentifierInsideProvider
                    &&
                    !((input.Parent.Parent.Name.LocalName == "venue") && (input.Parent.Parent.Name.NamespaceName == "http://xcri.org/profiles/1.2/catalog"))
                    )
                {
                    // For providers outside venue, ignore matches that exist within venues!
                    same = same
                           .Where(xe => xe.Parent.Parent.Name.LocalName != "venue")
                           .Where(xe => xe.Parent.Parent.Name.NamespaceName == "http://xcri.org/profiles/1.2/catalog");
                }
                count = same.Count();
                if (count == 1)
                {
                    return(true);
                }
                else
                {
                    details = String.Format
                              (
                        "There are {0} element{2} with the identifier {1} (on line{2} ",
                        count,
                        value,
                        count == 1 ? String.Empty : "s"
                              );
                    bool firstItem = true;
                    foreach (System.Xml.Linq.XElement xe in same)
                    {
                        if (false == (xe is IXmlLineInfo))
                        {
                            continue;
                        }
                        if (firstItem == false)
                        {
                            details += ", ";
                        }
                        else
                        {
                            firstItem = false;
                        };
                        details += (xe as IXmlLineInfo).LineNumber.ToString();
                    }
                    details += ")";
                    return(false);
                }

            default:
                throw new NotImplementedException();
            }
        }
        public override bool PassesValidation(System.Xml.Linq.XObject input, out string details)
        {
            details = null;
            if (null == input)
            {
                throw new ArgumentNullException("input");
            }
            string value;

            if (input is XElement)
            {
                value = (input as XElement).Value;
            }
            else
            {
                throw new ArgumentException("The input parameter type " + input.GetType().FullName + " was not expected", "input");
            }
            switch (this.EnforcementType)
            {
            case EnforcementTypes.ForceEmpty:
                if (false == String.IsNullOrWhiteSpace(value))
                {
                    details = String.Format
                              (
                        "Element has a value of '{0}'",
                        value
                              );
                    return(false);
                }
                if ((input as XElement).DescendantNodes().Count() > 0)
                {
                    details = String.Format
                              (
                        "Element has {0} children (none allowed)",
                        (input as XElement).DescendantNodes().Count()
                              );
                    return(false);
                }
                return(true);

            case EnforcementTypes.ForceNotEmpty:
                if (String.IsNullOrWhiteSpace(value))
                {
                    details = "Element has no value when one was expected";
                    return(false);
                }
                if ((input as XElement).DescendantNodes().Count() == 0)
                {
                    details = String.Format
                              (
                        "Element has 0 child nodes when they were expected",
                        (input as XElement).DescendantNodes().Count()
                              );
                    return(false);
                }
                return(true);

            default:
                throw new NotImplementedException();
            }
        }
Пример #4
0
        public override bool PassesValidation(System.Xml.Linq.XObject node, out string details)
        {
            if (
                null == this.ValidCaptions
                ||
                null == this.ValidIdentifiers
                )
            {
                this.Setup();
            }
            if (false == (node is XElement))
            {
                throw new InvalidOperationException("The xobject must be an xelement");
            }
            details = null;
            var element = node as XElement;

            if (false == this.PassesXsiTypeFilter(element))
            {
                return(true);
            }
            if (null != element.Attribute("identifier"))
            {
                var identifier = element.Attribute("identifier").Value;
                if (false == String.IsNullOrWhiteSpace(identifier))
                {
                    if (false == this.IsValidIdentifier(identifier))
                    {
                        details = String.Format
                                  (
                            "The value {0} was not a valid identifier value",
                            identifier
                                  );
                        return(false);
                    }
                }
                else
                {
                    if (false == this.AllowBlankIdentifier)
                    {
                        details = "Blank identifiers are not allowed";
                        return(false);
                    }
                }
            }
            var value = element.Value;

            if (false == String.IsNullOrWhiteSpace(value))
            {
                if (false == this.IsValidElementValue(value))
                {
                    details = String.Format
                              (
                        "The value {0} was not a valid element value",
                        value
                              );
                    return(false);
                }
            }
            else
            {
                if (false == this.AllowBlankCaption)
                {
                    details = "Blank values are not allowed";
                    return(false);
                }
            }
            return(true);
        }
Пример #5
0
 public static object GetDetachedObject(XObject child)
 {
     return(child.Owner != null?Clone(child) : child);
 }
        public override bool PassesValidation(System.Xml.Linq.XObject node, out string details)
        {
            if (null == node)
            {
                throw new ArgumentNullException("node");
            }
            if (false == (node is XElement))
            {
                throw new NotImplementedException("The node type" + node.GetType() + " was not expected");
            }
            details = null;
            node    = node as XElement;
            var languageCounts = this.GetLanguageCounts
                                 (
                ((node as XElement).XPathEvaluate(this.ChildElementSelector, this.NamespaceManager) as System.Collections.IEnumerable).Cast <XObject>()
                                 );
            var languageCountsBelowMinimum = languageCounts.Where((k) =>
            {
                if (this.Minimum.HasValue)
                {
                    return(k.Value < this.Minimum.Value);
                }
                return(false);
            });
            var languageCountsAboveMaximum = languageCounts.Where((k) =>
            {
                if (this.Maximum.HasValue)
                {
                    return(k.Value > this.Maximum.Value);
                }
                return(false);
            });

            // None below min or above max?
            if (
                0 == languageCountsBelowMinimum.Count()
                &&
                0 == languageCountsAboveMaximum.Count()
                )
            {
                return(true);
            }
            details = String.Format
                      (
                "The '{0}' element contained some invalid children:{1}",
                (node as XElement).Name,
                System.Environment.NewLine
                      );
            foreach (var kvp in languageCountsBelowMinimum)
            {
                details += String.Format
                           (
                    "There were {0} elements with the language {1}, which was below the minimum expected of {2}.{3}",
                    kvp.Value,
                    kvp.Key,
                    this.Minimum.Value,
                    System.Environment.NewLine
                           );
            }
            foreach (var kvp in languageCountsAboveMaximum)
            {
                details += String.Format
                           (
                    "There were {0} elements with the language {1}, which was above the maximum expected of {2}.{3}",
                    kvp.Value,
                    kvp.Key,
                    this.Maximum.Value,
                    System.Environment.NewLine
                           );
            }
            return(false);
        }