Exemple #1
0
        private QuickFix[] GetAttributeValueFixes(XmlElement element, string name)
        {
            ElementType et = documentType.GetElementType(element);

            if (et == null)
            {
                return(QuickFix.EmptyList);
            }

            ArrayList ret = new ArrayList();

            Attribute attr = et.GetAttribute(name);

            // TODO: M: think about fixing up others, eg. NMTOKEN
            switch (attr.Type)
            {
            case AttributeType.ID:
                // TODO: M: think about re-parsing rather than gen new
                Guid   guid = Guid.NewGuid();
                string id   = "ID-" + guid.ToString();
                ret.Add(new QuickFixChangeAttribute(element, name, id));
                break;
            }

            return(ret.ToArray(typeof(QuickFix)) as QuickFix[]);
        }
Exemple #2
0
        private void ValidateRemovedAttribute(XmlElement e, XmlAttribute a)
        {
            ElementType et = documentType.GetElementType(e);

            if (et == null)
            {
                return;
            }

            Attribute attr = et.GetAttribute(a);

            if (attr == null)
            {
                // attribute was invalid, so remove that error
                AttributeErrorFilter aef = new AttributeErrorFilter(e, a, ValidationErrorType.AttributeNotDefined);
                RemoveValidationErrors(aef);
                return;
            }
            else
            {
                // remove any validation errors for this attribute
                AttributeErrorFilter aef = new AttributeErrorFilter(e, a);
                RemoveValidationErrors(aef);
            }

            if (attr.State == AttributeState.Required)
            {
                // removing a required attribute
                AddValidationError(e, new ValidationErrorAttribute(e, a.Name, ValidationErrorType.RequiredAttributeMissing));
            }
        }
Exemple #3
0
        public bool IsAttributeValid(XmlElement element, string name, XmlAttribute actual)
        {
            if (!HasElements)
            {
                // there is no DTD so return valid
                return(true);
            }

            Attribute attr = LookupAttribute(element, name);

            if (attr == null && actual != null)
            {
                return(false);
            }

            if (actual == null && attr != null)
            {
                return(attr.State != AttributeState.Required);
            }

            // TODO: L: don't think this can happen
            if (actual == null && attr == null)
            {
                return(false);
            }

            return(validationEngine.ValidateAttribute(attr, element, actual));
        }
Exemple #4
0
        private void RemoveIdOrIdRef(XmlElement e, XmlAttribute a)
        {
            ElementType et   = documentType.GetElementType(e);
            XmlName     xn   = new XmlName(a);
            Attribute   attr = et.GetAttribute(xn);

            if (attr == null)
            {
                return;
            }

            AttributeBinding ab;
            string           val = a.Value;

            switch (attr.Type)
            {
            case AttributeType.ID:
                // record the previous id because we'll need that later (NodeChanged event)
                ab = new AttributeBinding(e, attr.Name, val, a.Specified);
                idTracker.RemoveId(ab);
                ProcessRemovedId(val);
                break;

            case AttributeType.IDREF:
            case AttributeType.IDREFS:
                ab = new AttributeBinding(e, attr.Name, val, a.Specified);
                idTracker.RemoveIdRefs(ab);
                break;
            }
        }
Exemple #5
0
 private bool ValidateEnumeratedAttribute(Attribute a, XmlAttribute val)
 {
     foreach (string s in a.Enums)
     {
         if (s.Equals(val.Value))
         {
             return(true);
         }
     }
     return(false);
 }
Exemple #6
0
        public AttributeType GetAttributeType(XmlElement element, string name)
        {
            Attribute attr = LookupAttribute(element, name);

            if (attr == null)
            {
                throw new ArgumentException("Attribute is not defined");
            }

            return(attr.Type);
        }
Exemple #7
0
        private void AddIdOrIdRef(XmlElement e, XmlAttribute a, Attribute attr)
        {
            // any previous value will have been removed in NodeChanging event

            AttributeBinding ab;
            string           val = a.Value;

            switch (attr.Type)
            {
            case AttributeType.ID:
                ab = new AttributeBinding(e, attr.Name, val, a.Specified);
                idTracker.AddId(ab);
                if (idTracker.IdCount(val) > 1)
                {
                    foreach (AttributeBinding ab2 in idTracker.GetIdBindings(val))
                    {
                        // TODO: L: inefficient, lots of removing then adding
                        XmlAttribute         a2  = ab2.Element.GetAttributeNode(ab2.Name);
                        AttributeErrorFilter aef = new AttributeErrorFilter(ab2.Element, a2, ValidationErrorType.IdAttributeInUse);
                        RemoveValidationErrors(aef);
                        ValidationErrorAttribute vea = new ValidationErrorAttribute(ab2.Element, a2.Name, ValidationErrorType.IdAttributeInUse);
                        AddValidationError(ab2.Element, vea);
                    }
                }

                foreach (AttributeBinding ab2 in idTracker.GetIdRefBindings(val))
                {
                    XmlAttribute         ar  = ab.Element.GetAttributeNode(ab2.Name);
                    AttributeErrorFilter aef = new AttributeErrorFilter(ab2.Element, ar, ValidationErrorType.IdAttributeNotDefined);
                    RemoveValidationErrors(aef);
                }

                break;

            case AttributeType.IDREF:
            case AttributeType.IDREFS:
                ab = new AttributeBinding(e, attr.Name, val, a.Specified);
                idTracker.AddIdRefs(ab);
                // TODO: L: inefficient - we split twice, here and in AddIdRefs
                foreach (string id in ab.Value.Split(' '))
                {
                    if (idTracker.IdCount(id) == 0)
                    {
                        ValidationErrorAttribute vea = new ValidationErrorAttribute(e, a.Name, ValidationErrorType.IdAttributeNotDefined);
                        AddValidationError(e, vea);
                    }
                }
                break;
            }
        }
Exemple #8
0
        public string[] GetEnumValues(XmlElement element, string name)
        {
            Attribute attr = LookupAttribute(element, name);

            if (attr == null)
            {
                throw new ArgumentException("Attribute is not defined");
            }

            if (attr.Type != AttributeType.Enumerated)
            {
                throw new ArgumentException("Attribute is not enumeration");
            }

            return(attr.Enums);
        }
Exemple #9
0
//		private IList FilterValidNext(IList col, XmlElement n)
//		{
//			ArrayList ret=new ArrayList();
//			foreach ( SimpleReference sr in col )
//			{
//				if ( ret.Contains(sr) )
//					continue;
//
//				if ( n != null &&
//						FindInCollection(sr.OriginalReference.GetValidNextElements(), n) != null )
//					ret.Add(sr);
//			}
//			return ret;
//		}

//		private IList GetValidFirstCaseElements(ElementType et, XmlElement e)
//		{
//			ArrayList ret=new ArrayList();
//			IList col=et.ContentModel.GetValidFirstElements();
//			foreach ( SimpleReference sr in col )
//			{
//				if ( ret.Contains(sr) )
//					continue;
//
//				if ( FindInCollection(sr.OriginalReference.GetValidNextElements(), e) != null )
//					ret.Add(sr);
//			}
//			return ret;
//		}

//		private SimpleReference FindReference(ElementType et, XmlElement parent, XmlElement n)
//		{
//			IList col=et.ContentModel.GetValidFirstElements();
//			SimpleReference r=null;
//			foreach ( XmlNode c in parent.ChildNodes )
//			{
//				if ( c.NodeType != XmlNodeType.Element )
//					continue;
//
//				XmlElement child=(XmlElement) c;
//
//				if ( child.Equals(n) )
//					break;
//
//				r=FindInCollection(col, child);
//				if ( r == null )
//					return null;
//
//				col=r.OriginalReference.GetValidNextElements();
//				if ( col.Count == 0 )
//					return null;
//
//				r=(SimpleReference) col[0];
//			}
//			return r;
//		}

        private void ValidateAttributeAddOrChange(XmlAttribute a)
        {
            XmlElement e = a.OwnerElement;

            if (documentType == null || e == null)
            {
                return;
            }

            if (e.ParentNode == null)
            {
                // node is not in document yet
                return;
            }

            ElementType et = documentType.GetElementType(e);

            if (et == null)
            {
                return;
            }

            AttributeErrorFilter aef = new AttributeErrorFilter(e, a);

            RemoveValidationErrors(aef);

            Attribute attr = et.GetAttribute(a);

            if (attr == null)
            {
                ValidationError ve = new ValidationErrorAttribute(e, a.Name, ValidationErrorType.AttributeNotDefined);
                AddValidationError(e, ve);
                return;
            }

            // it's only at this point we can even tell if it's ID/IDREF
            AddIdOrIdRef(e, a, attr);

            if (!validationEngine.ValidateAttribute(attr, e, a))
            {
                ValidationError ve = new ValidationErrorAttribute(e, a.Name, ValidationErrorType.InvalidAttributeValue);
                AddValidationError(e, ve);
            }
        }
Exemple #10
0
        internal bool ValidateAttribute(Attribute a, XmlElement e, XmlAttribute val)
        {
            if (!val.Specified)
            {
                // if not specified then either required | (defaulted | implied)
                return(a.State != AttributeState.Required);
            }

            switch (a.Type)
            {
            case AttributeType.CDATA:
                // any value is ok
                return(true);

            case AttributeType.Enumerated:
                return(ValidateEnumeratedAttribute(a, val));

            case AttributeType.ID:
                return(ValidateIdAttribute(val));

            case AttributeType.IDREF:
                return(ValidateIdRefAttribute(val, false));

            case AttributeType.IDREFS:
                return(ValidateIdRefAttribute(val, true));

            case AttributeType.NMTOKEN:
                return(ValidateNmtokenAttribute(val, false));

            case AttributeType.NMTOKENS:
                return(ValidateNmtokenAttribute(val, true));

            default:
                // TODO: M: support other types (entity and notation)
                return(true);
            }
        }
 private bool ValidateEnumeratedAttribute(Attribute a, XmlAttribute val)
 {
     foreach ( string s in a.Enums )
     {
         if ( s.Equals(val.Value) )
             return true;
     }
     return false;
 }
        internal bool ValidateAttribute(Attribute a, XmlElement e, XmlAttribute val)
        {
            if ( !val.Specified )
                // if not specified then either required | (defaulted | implied)
                return a.State != AttributeState.Required;

            switch ( a.Type )
            {
                case AttributeType.CDATA:
                    // any value is ok
                    return true;

                case AttributeType.Enumerated:
                    return ValidateEnumeratedAttribute(a, val);

                case AttributeType.ID:
                    return ValidateIdAttribute(val);

                case AttributeType.IDREF:
                    return ValidateIdRefAttribute(val, false);

                case AttributeType.IDREFS:
                    return ValidateIdRefAttribute(val, true);

                case AttributeType.NMTOKEN:
                    return ValidateNmtokenAttribute(val, false);

                case AttributeType.NMTOKENS:
                    return ValidateNmtokenAttribute(val, true);

                default:
                    // TODO: M: support other types (entity and notation)
                    return true;
            }
        }
Exemple #13
0
        private void AddIdOrIdRef(XmlElement e, XmlAttribute a, Attribute attr)
        {
            // any previous value will have been removed in NodeChanging event

            AttributeBinding ab;
            string val=a.Value;
            switch ( attr.Type )
            {
                case AttributeType.ID:
                    ab=new AttributeBinding(e, attr.Name, val, a.Specified);
                    idTracker.AddId(ab);
                    if ( idTracker.IdCount(val) > 1 )
                    {
                        foreach ( AttributeBinding ab2 in idTracker.GetIdBindings(val) )
                        {
                            // TODO: L: inefficient, lots of removing then adding
                            XmlAttribute a2=ab2.Element.GetAttributeNode(ab2.Name);
                            AttributeErrorFilter aef=new AttributeErrorFilter(ab2.Element, a2, ValidationErrorType.IdAttributeInUse);
                            RemoveValidationErrors(aef);
                            ValidationErrorAttribute vea=new ValidationErrorAttribute(ab2.Element, a2.Name, ValidationErrorType.IdAttributeInUse);
                            AddValidationError(ab2.Element, vea);
                        }
                    }

                    foreach ( AttributeBinding ab2 in idTracker.GetIdRefBindings(val) )
                    {
                        XmlAttribute ar=ab.Element.GetAttributeNode(ab2.Name);
                        AttributeErrorFilter aef=new AttributeErrorFilter(ab2.Element, ar, ValidationErrorType.IdAttributeNotDefined);
                        RemoveValidationErrors(aef);
                    }

                    break;

                case AttributeType.IDREF:
                case AttributeType.IDREFS:
                    ab=new AttributeBinding(e, attr.Name, val, a.Specified);
                    idTracker.AddIdRefs(ab);
                    // TODO: L: inefficient - we split twice, here and in AddIdRefs
                    foreach ( string id in ab.Value.Split(' ') )
                    if ( idTracker.IdCount(id) == 0 )
                    {
                        ValidationErrorAttribute vea=new ValidationErrorAttribute(e, a.Name, ValidationErrorType.IdAttributeNotDefined);
                        AddValidationError(e, vea);
                    }
                    break;
            }
        }
Exemple #14
0
        public bool IsAttributeDefined(XmlElement element, string name)
        {
            Attribute attr = LookupAttribute(element, name);

            return(attr != null);
        }
Exemple #15
0
        public bool IsAttributeFixed(XmlElement element, string name)
        {
            Attribute attr = LookupAttribute(element, name);

            return(attr != null && attr.State == AttributeState.Fixed);
        }