Ejemplo n.º 1
0
        private bool IsValidElementContent(ElementType et, ICollection list)
        {
            // list is assumed to contain all children (including indirect children via entity reference)
            ICollection col = et.ContentModel.GetValidFirstElements();

            foreach (XmlNode n in list)
            {
                if (XmlUtil.IsTextContent(n))
                {
                    return(false);
                }

                XmlElement e = n as XmlElement;
                if (e == null)
                {
                    // not significant, eg. comment
                    continue;
                }

                ElementTypeRef sr = FindInCollection(col, new XmlName(e));
                if (sr == null)
                {
                    return(false);
                }

                col = sr.OriginalReference.GetValidNextElements();
            }
            // TODO: think about required elements (remaining col)
            return(true);
        }
Ejemplo n.º 2
0
        private ElementListItem[] FindValidElements(ElementType et, XmlElement parent, XmlNode n, bool replace)
        {
            int index = -1;

            ArrayList children = new ArrayList();

            foreach (XmlNode c in parent.ChildNodes)
            {
                if (c.Equals(n))
                {
                    index = children.Count;
                }

                if (c.NodeType != XmlNodeType.Element)
                {
                    continue;
                }

                XmlName xn = new XmlName(c);
                children.Add(xn);
            }
            if (index < 0)
            {
                Debug.Assert(!replace, "This can't happen if in replace mode");
                children.Add(null);
                index = children.Count - 1;
            }
            else
            {
                if (!replace)
                {
                    children.Insert(index, null);
                }
            }

            ArrayList ret = new ArrayList();

            foreach (ElementType t in et.ChildElements)
            {
                children[index] = t.Name;

                ElementTypeRef sr = IsValidSequence(et, children, index);
                if (sr != null)
                {
                    ret.Add(new ElementListItem(sr.Name, sr.IsRequired, sr.IsChoice));
                }
            }
            return(ret.ToArray(typeof(ElementListItem)) as ElementListItem[]);
        }
Ejemplo n.º 3
0
        internal ElementTypeRef IsValidSequence(ElementType et, ICollection s, int index)
        {
            ICollection col = et.ContentModel.GetValidFirstElements();

            int            count = 0;
            ElementTypeRef ret   = null;

            foreach (XmlName name in s)
            {
                count++;

                if (!et.HasChildElement(name))
                {
                    // ignore elements that simply aren't in the model
                    // TODO: H: count can get out
                    continue;
                }

                ElementTypeRef sr = FindInCollection(col, name);
                if (count == index + 1)
                {
                    ret = sr;
                }

                if (sr == null)
                {
                    // element name is not allowed here
                    if (ret != null && count < index + 1)
                    {
                        return(ret);
                    }

                    return(null);
                }

                // TODO: M: optimise - this is called at end of s even though not going to loop again
                col = sr.OriginalReference.GetValidNextElements();
            }
            // TODO: M: think about required elements at end - but probably
            //			not needed here because don't want to stop someone
            //			adding an element even if more are allowed
            return(ret);
        }
Ejemplo n.º 4
0
        public ElementListItem[] GetMissingElements(XmlElement e)
        {
            ElementType et = documentType.GetElementType(e);

            ICollection col = et.ContentModel.GetValidFirstElements();

            foreach (XmlNode n in e.ChildNodes)
            {
                if (XmlUtil.IsTextContent(n))
                {
                    // not interested for this method
                    continue;
                }

                XmlElement e2 = n as XmlElement;
                if (e2 == null)
                {
                    // not significant, eg. comment
                    // TODO: E: entities!
                    continue;
                }

                ElementTypeRef sr = FindInCollection(col, new XmlName(e2));
                if (sr == null)
                {
                    // just keep on going until we get to the end
                    continue;
                }

                col = sr.OriginalReference.GetValidNextElements();
            }
            ArrayList ret = new ArrayList();

            foreach (ElementTypeRef etr in col)
            {
                if (etr.IsRequired)
                {
                    ret.Add(new ElementListItem(etr.Name, etr.IsRequired, etr.IsChoice));
                }
            }
            return(ret.ToArray(typeof(ElementListItem)) as ElementListItem[]);
        }
Ejemplo n.º 5
0
        private void ValidateElementModel(XmlElement e, ElementType et, ICollection children)
        {
            ICollection col = et.ContentModel.GetValidFirstElements();

            bool elementMissing = false;

            foreach (XmlNode n in children)
            {
                client.StartChildValidation(n);

                if (n.NodeType == XmlNodeType.Text)
                {
                    client.ProcessError(n, ValidationErrorType.NodeNotAllowed);
                }
                else if (n.NodeType == XmlNodeType.Element)
                {
                    ElementType et2 = documentType.GetElementType((XmlElement)n);
                    if (et2 == null)
                    {
                        // not in DTD, will be picked up later
                        continue;
                    }

                    // TODO: E: entities!
                    if (!et.HasChildElement(new XmlName(n)))
                    {
                        // this element can never be allowed here
                        client.ProcessError(n, ValidationErrorType.ElementNotInContentModel);
                        continue;
                    }

                    if (elementMissing)
                    {
                        continue;
                    }

                    ElementTypeRef sr = FindInCollection(col, (XmlElement)n);
                    if (sr == null)
                    {
                        foreach (ElementTypeRef etr in col)
                        {
                            if (etr.IsRequired)
                            {
                                elementMissing = true;
                                client.ProcessError(n, ValidationErrorType.ElementNotAllowedHere);
                                break;
                            }
                        }
                        if (elementMissing)
                        {
                            continue;
                        }

                        client.ProcessError(n, ValidationErrorType.NodeNotAllowed);
                    }
                    else
                    {
                        col = sr.OriginalReference.GetValidNextElements();
                    }
                }
            }
            if (!elementMissing)
            {
                foreach (ElementTypeRef sr in col)
                {
                    if (sr.IsRequired)
                    {
                        // TODO: M: can provide some more info to the error here
                        client.ProcessError(e, ValidationErrorType.RequiredElementMissing);
                        break;
                    }
                }
            }
        }