Пример #1
0
        private void AdjustIdenticalAttributes(XmlAttribute node, XmlNode existing)
        {
            // If owner element is not appended to the document,
            // ID table should not be filled.
            if (ownerElement == null)
            {
                return;
            }

            if (existing != null)
            {
                RemoveIdenticalAttribute(existing);
            }

            // Check if new attribute's datatype is ID.
            XmlDocumentType doctype = node.OwnerDocument.DocumentType;

            if (doctype == null || doctype.DTD == null)
            {
                return;
            }
            DTDAttListDeclaration  attList = doctype.DTD.AttListDecls [ownerElement.Name];
            DTDAttributeDefinition attdef  = attList == null ? null : attList.Get(node.Name);

            if (attdef == null || attdef.Datatype.TokenizedType != XmlTokenizedType.ID)
            {
                return;
            }

            ownerDocument.AddIdenticalAttribute(node);
        }
        private void AdjustIdenticalAttributes(XmlAttribute node, XmlNode existing)
        {
            if (this.ownerElement == null)
            {
                return;
            }
            if (existing != null)
            {
                this.RemoveIdenticalAttribute(existing);
            }
            XmlDocumentType documentType = node.OwnerDocument.DocumentType;

            if (documentType == null || documentType.DTD == null)
            {
                return;
            }
            DTDAttListDeclaration  dtdattListDeclaration  = documentType.DTD.AttListDecls[this.ownerElement.Name];
            DTDAttributeDefinition dtdattributeDefinition = (dtdattListDeclaration != null) ? dtdattListDeclaration.Get(node.Name) : null;

            if (dtdattributeDefinition == null || dtdattributeDefinition.Datatype.TokenizedType != XmlTokenizedType.ID)
            {
                return;
            }
            this.ownerDocument.AddIdenticalAttribute(node);
        }
Пример #3
0
 internal XmlElement(string prefix, string localName, string namespaceURI, XmlDocument doc, bool atomizedNames) : base(doc)
 {
     if (!atomizedNames)
     {
         if (prefix == null)
         {
             prefix = string.Empty;
         }
         if (namespaceURI == null)
         {
             namespaceURI = string.Empty;
         }
         XmlConvert.VerifyName(localName);
         prefix       = doc.NameTable.Add(prefix);
         localName    = doc.NameTable.Add(localName);
         namespaceURI = doc.NameTable.Add(namespaceURI);
     }
     this.name = doc.NameCache.Add(prefix, localName, namespaceURI, true);
     if (doc.DocumentType != null)
     {
         DTDAttListDeclaration dtdattListDeclaration = doc.DocumentType.DTD.AttListDecls[localName];
         if (dtdattListDeclaration != null)
         {
             for (int i = 0; i < dtdattListDeclaration.Definitions.Count; i++)
             {
                 DTDAttributeDefinition dtdattributeDefinition = dtdattListDeclaration[i];
                 if (dtdattributeDefinition.DefaultValue != null)
                 {
                     this.SetAttribute(dtdattributeDefinition.Name, dtdattributeDefinition.DefaultValue);
                     this.Attributes[dtdattributeDefinition.Name].SetDefault();
                 }
             }
         }
     }
 }
Пример #4
0
        public virtual XmlAttribute Remove(XmlAttribute node)
#endif
        {
            if (IsReadOnly)
            {
                throw new ArgumentException("This attribute collection is read-only.");
            }
            if (node == null)
            {
                throw new ArgumentException("Specified node is null.");
            }
            if (node.OwnerDocument != ownerDocument)
            {
                throw new ArgumentException("Specified node is in a different document.");
            }
            if (node.OwnerElement != this.ownerElement)
            {
                throw new ArgumentException("The specified attribute is not contained in the element.");
            }

            XmlAttribute retAttr = null;

            for (int i = 0; i < Count; i++)
            {
                XmlAttribute attr = (XmlAttribute)Nodes [i];
                if (attr == node)
                {
                    retAttr = attr;
                    break;
                }
            }

            if (retAttr != null)
            {
                ownerDocument.onNodeRemoving(node, ownerElement);
                base.RemoveNamedItem(retAttr.LocalName, retAttr.NamespaceURI);
#if NOT_PFX
                RemoveIdenticalAttribute(retAttr);
#endif
                ownerDocument.onNodeRemoved(node, ownerElement);
            }
            // If it is default, then directly create new attribute.
#if NOT_PFX
            DTDAttributeDefinition def = retAttr.GetAttributeDefinition();
            if (def != null && def.DefaultValue != null)
            {
                XmlAttribute attr = ownerDocument.CreateAttribute(
                    retAttr.Prefix, retAttr.LocalName, retAttr.NamespaceURI);
                attr.Value = def.DefaultValue;
                attr.SetDefault();
                this.SetNamedItem(attr);
            }
#endif

            retAttr.AttributeOwnerElement = null;
            return(retAttr);
        }
        /// <summary>Removes the specified attribute from the collection.</summary>
        /// <returns>The node removed or null if it is not found in the collection.</returns>
        /// <param name="node">The <see cref="T:System.Xml.XmlAttribute" /> to remove. </param>
        public XmlAttribute Remove(XmlAttribute node)
        {
            if (this.IsReadOnly)
            {
                throw new ArgumentException("This attribute collection is read-only.");
            }
            if (node == null)
            {
                throw new ArgumentException("Specified node is null.");
            }
            if (node.OwnerDocument != this.ownerDocument)
            {
                throw new ArgumentException("Specified node is in a different document.");
            }
            if (node.OwnerElement != this.ownerElement)
            {
                throw new ArgumentException("The specified attribute is not contained in the element.");
            }
            XmlAttribute xmlAttribute = null;

            for (int i = 0; i < this.Count; i++)
            {
                XmlAttribute xmlAttribute2 = (XmlAttribute)base.Nodes[i];
                if (xmlAttribute2 == node)
                {
                    xmlAttribute = xmlAttribute2;
                    break;
                }
            }
            if (xmlAttribute != null)
            {
                this.ownerDocument.onNodeRemoving(node, this.ownerElement);
                base.RemoveNamedItem(xmlAttribute.LocalName, xmlAttribute.NamespaceURI);
                this.RemoveIdenticalAttribute(xmlAttribute);
                this.ownerDocument.onNodeRemoved(node, this.ownerElement);
            }
            DTDAttributeDefinition attributeDefinition = xmlAttribute.GetAttributeDefinition();

            if (attributeDefinition != null && attributeDefinition.DefaultValue != null)
            {
                XmlAttribute xmlAttribute3 = this.ownerDocument.CreateAttribute(xmlAttribute.Prefix, xmlAttribute.LocalName, xmlAttribute.NamespaceURI, true, false);
                xmlAttribute3.Value = attributeDefinition.DefaultValue;
                xmlAttribute3.SetDefault();
                this.SetNamedItem(xmlAttribute3);
            }
            xmlAttribute.AttributeOwnerElement = null;
            return(xmlAttribute);
        }
Пример #6
0
        private void SetIdenticalAttribute(bool remove)
        {
            if (ownerElement == null)
            {
                return;
            }

            // Check if new attribute's datatype is ID.
            XmlDocumentType doctype = ownerDocument.DocumentType;

            if (doctype == null || doctype.DTD == null)
            {
                return;
            }
            DTDElementDeclaration elem = doctype.DTD.ElementDecls [ownerElement.Name];

            for (int i = 0; i < Count; i++)
            {
                XmlAttribute           node   = (XmlAttribute)Nodes [i];
                DTDAttributeDefinition attdef = elem == null ? null : elem.Attributes [node.Name];
                if (attdef == null || attdef.Datatype.TokenizedType != XmlTokenizedType.ID)
                {
                    continue;
                }


                if (remove)
                {
                    if (ownerDocument.GetIdenticalAttribute(node.Value) != null)
                    {
                        ownerDocument.RemoveIdenticalAttribute(node.Value);
                        return;
                    }
                }
                else
                {
                    // adding new identical attribute, but
                    // MS.NET is pity for ID support, so I'm wondering how to correct it...
                    if (ownerDocument.GetIdenticalAttribute(node.Value) != null)
                    {
                        throw new XmlException(String.Format(
                                                   "ID value {0} already exists in this document.", node.Value));
                    }
                    ownerDocument.AddIdenticalAttribute(node);
                    return;
                }
            }
        }
Пример #7
0
        internal XmlElement(
            string prefix,
            string localName,
            string namespaceURI,
            XmlDocument doc,
            bool atomizedNames) : base(doc)
        {
            if (!atomizedNames)
            {
                if (prefix == null)
                {
                    prefix = String.Empty;
                }
                if (namespaceURI == null)
                {
                    namespaceURI = String.Empty;
                }

                XmlConvert.VerifyName(localName);

                prefix       = doc.NameTable.Add(prefix);
                localName    = doc.NameTable.Add(localName);
                namespaceURI = doc.NameTable.Add(namespaceURI);
            }
            name = doc.NameCache.Add(prefix, localName, namespaceURI, true);

#if NOT_PFX
            if (doc.DocumentType != null)
            {
                DTDAttListDeclaration attlist = doc.DocumentType.DTD.AttListDecls [localName];
                if (attlist != null)
                {
                    for (int i = 0; i < attlist.Definitions.Count; i++)
                    {
                        DTDAttributeDefinition def = attlist [i];
                        if (def.DefaultValue != null)
                        {
                            SetAttribute(def.Name, def.DefaultValue);
                            Attributes [def.Name].SetDefault();
                        }
                    }
                }
            }
#endif
        }
        private void SetIdenticalAttribute(bool remove)
        {
            if (this.ownerElement == null)
            {
                return;
            }
            XmlDocumentType documentType = this.ownerDocument.DocumentType;

            if (documentType == null || documentType.DTD == null)
            {
                return;
            }
            DTDElementDeclaration dtdelementDeclaration = documentType.DTD.ElementDecls[this.ownerElement.Name];

            for (int i = 0; i < this.Count; i++)
            {
                XmlAttribute           xmlAttribute           = (XmlAttribute)base.Nodes[i];
                DTDAttributeDefinition dtdattributeDefinition = (dtdelementDeclaration != null) ? dtdelementDeclaration.Attributes[xmlAttribute.Name] : null;
                if (dtdattributeDefinition != null && dtdattributeDefinition.Datatype.TokenizedType == XmlTokenizedType.ID)
                {
                    if (remove)
                    {
                        if (this.ownerDocument.GetIdenticalAttribute(xmlAttribute.Value) != null)
                        {
                            this.ownerDocument.RemoveIdenticalAttribute(xmlAttribute.Value);
                            return;
                        }
                    }
                    else
                    {
                        if (this.ownerDocument.GetIdenticalAttribute(xmlAttribute.Value) != null)
                        {
                            throw new XmlException(string.Format("ID value {0} already exists in this document.", xmlAttribute.Value));
                        }
                        this.ownerDocument.AddIdenticalAttribute(xmlAttribute);
                        return;
                    }
                }
            }
        }
        /// <summary>Removes the node from the XmlNamedNodeMap.</summary>
        /// <returns>The XmlNode removed from this XmlNamedNodeMap or null if a matching node was not found.</returns>
        /// <param name="name">The qualified name of the node to remove. The name is matched against the <see cref="P:System.Xml.XmlNode.Name" /> property of the matching node. </param>
        public virtual XmlNode RemoveNamedItem(string name)
        {
            if (this.nodeList == null)
            {
                return(null);
            }
            int i = 0;

            while (i < this.nodeList.Count)
            {
                XmlNode xmlNode = (XmlNode)this.nodeList[i];
                if (xmlNode.Name == name)
                {
                    if (xmlNode.IsReadOnly)
                    {
                        throw new InvalidOperationException("Cannot remove. This node is read only: " + name);
                    }
                    this.nodeList.Remove(xmlNode);
                    XmlAttribute xmlAttribute = xmlNode as XmlAttribute;
                    if (xmlAttribute != null)
                    {
                        DTDAttributeDefinition attributeDefinition = xmlAttribute.GetAttributeDefinition();
                        if (attributeDefinition != null && attributeDefinition.DefaultValue != null)
                        {
                            XmlAttribute xmlAttribute2 = xmlAttribute.OwnerDocument.CreateAttribute(xmlAttribute.Prefix, xmlAttribute.LocalName, xmlAttribute.NamespaceURI, true, false);
                            xmlAttribute2.Value = attributeDefinition.DefaultValue;
                            xmlAttribute2.SetDefault();
                            xmlAttribute.OwnerElement.SetAttributeNode(xmlAttribute2);
                        }
                    }
                    return(xmlNode);
                }
                else
                {
                    i++;
                }
            }
            return(null);
        }
Пример #10
0
        public virtual XmlNode RemoveNamedItem(string name)
        {
            if (nodeList == null)
            {
                return(null);
            }

            for (int i = 0; i < nodeList.Count; i++)
            {
                XmlNode node = (XmlNode)nodeList [i];
                if (node.Name == name)
                {
                    if (node.IsReadOnly)
                    {
                        throw new InvalidOperationException("Cannot remove. This node is read only: " + name);
                    }
                    nodeList.Remove(node);
                    // Since XmlAttributeCollection does not override
                    // it while attribute have to keep it in the
                    // collection, it adds to the collection immediately.
                    XmlAttribute attr = node as XmlAttribute;
#if NOT_PFX
                    if (attr != null)
                    {
                        DTDAttributeDefinition def = attr.GetAttributeDefinition();
                        if (def != null && def.DefaultValue != null)
                        {
                            XmlAttribute newAttr = attr.OwnerDocument.CreateAttribute(attr.Prefix, attr.LocalName, attr.NamespaceURI, true, false);
                            newAttr.Value = def.DefaultValue;
                            newAttr.SetDefault();
                            attr.OwnerElement.SetAttributeNode(newAttr);
                        }
                    }
#endif
                    return(node);
                }
            }
            return(null);
        }
Пример #11
0
        private void AdjustIdenticalAttributes(XmlAttribute node, XmlNode existing)
        {
            // If owner element is not appended to the document,
            // ID table should not be filled.
            if (ownerElement == null)
            {
                return;
            }

            if (existing != null)
            {
                RemoveIdenticalAttribute(existing);
            }

            // Check if new attribute's datatype is ID.
            XmlDocumentType doctype = node.OwnerDocument.DocumentType;

            if (doctype == null || doctype.DTD == null)
            {
                return;
            }
            DTDAttListDeclaration  attList = doctype.DTD.AttListDecls [ownerElement.Name];
            DTDAttributeDefinition attdef  = attList == null ? null : attList.Get(node.Name);

            if (attdef == null || attdef.Datatype.TokenizedType != XmlTokenizedType.ID)
            {
                return;
            }

            // adding new identical attribute, but
            // MS.NET is pity for ID support, so I'm wondering how to correct it...
            if (ownerDocument.GetIdenticalAttribute(node.Value) != null)
            {
                throw new XmlException(String.Format(
                                           "ID value {0} already exists in this document.", node.Value));
            }
            ownerDocument.AddIdenticalAttribute(node);
        }
Пример #12
0
		private DTDAttributeDefinition ReadAttributeDefinition ()
		{
#if NET_2_1_HACK
			throw new NotImplementedException ();
#else
			DTDAttributeDefinition def = new DTDAttributeDefinition (DTD);
			def.IsInternalSubset = this.processingInternalSubset;

			// attr_name
			TryExpandPERef ();
			def.Name = ReadName ();
			if (!SkipWhitespace ())
				throw NotWFError ("Whitespace is required between name and content in DTD attribute definition.");

			// attr_value
			TryExpandPERef ();
			switch(PeekChar ()) {
			case 'C':	// CDATA
				Expect ("CDATA");
				def.Datatype = XmlSchemaDatatype.FromName ("normalizedString", XmlSchema.Namespace);
				break;
			case 'I':	// ID, IDREF, IDREFS
				Expect ("ID");
				if(PeekChar () == 'R') {
					Expect ("REF");
					if(PeekChar () == 'S') {
						// IDREFS
						ReadChar ();
						def.Datatype = XmlSchemaDatatype.FromName ("IDREFS", XmlSchema.Namespace);
					}
					else	// IDREF
						def.Datatype = XmlSchemaDatatype.FromName ("IDREF", XmlSchema.Namespace);
				}
				else	// ID
					def.Datatype = XmlSchemaDatatype.FromName ("ID", XmlSchema.Namespace);
				break;
			case 'E':	// ENTITY, ENTITIES
				Expect ("ENTIT");
				switch(ReadChar ()) {
					case 'Y':	// ENTITY
						def.Datatype = XmlSchemaDatatype.FromName ("ENTITY", XmlSchema.Namespace);
						break;
					case 'I':	// ENTITIES
						Expect ("ES");
						def.Datatype = XmlSchemaDatatype.FromName ("ENTITIES", XmlSchema.Namespace);
						break;
				}
				break;
			case 'N':	// NMTOKEN, NMTOKENS, NOTATION
				ReadChar ();
				switch(PeekChar ()) {
				case 'M':
					Expect ("MTOKEN");
					if(PeekChar ()=='S') {	// NMTOKENS
						ReadChar ();
						def.Datatype = XmlSchemaDatatype.FromName ("NMTOKENS", XmlSchema.Namespace);
					}
					else	// NMTOKEN
						def.Datatype = XmlSchemaDatatype.FromName ("NMTOKEN", XmlSchema.Namespace);
					break;
				case 'O':
					Expect ("OTATION");
					def.Datatype = XmlSchemaDatatype.FromName ("NOTATION", XmlSchema.Namespace);
					TryExpandPERefSpaceKeep ();
					if (!SkipWhitespace ())
						throw NotWFError ("Whitespace is required after notation name in DTD attribute definition.");
					Expect ('(');
					SkipWhitespace ();
					TryExpandPERef ();
					def.EnumeratedNotations.Add (ReadName ());		// notation name
					SkipWhitespace ();
					TryExpandPERef ();
					while(PeekChar () == '|') {
						ReadChar ();
						SkipWhitespace ();
						TryExpandPERef ();
						def.EnumeratedNotations.Add (ReadName ());	// notation name
						SkipWhitespace ();
						TryExpandPERef ();
					}
					Expect (')');
					break;
				default:
					throw NotWFError ("attribute declaration syntax error.");
				}
				break;
			default:	// Enumerated Values
				def.Datatype = XmlSchemaDatatype.FromName ("NMTOKEN", XmlSchema.Namespace);
				TryExpandPERef ();
				Expect ('(');
				SkipWhitespace ();
				TryExpandPERef ();
				def.EnumeratedAttributeDeclaration.Add (
					def.Datatype.Normalize (ReadNmToken ()));	// enum value
				SkipWhitespace ();
				while(PeekChar () == '|') {
					ReadChar ();
					SkipWhitespace ();
					TryExpandPERef ();
					def.EnumeratedAttributeDeclaration.Add (
						def.Datatype.Normalize (ReadNmToken ()));	// enum value
					SkipWhitespace ();
					TryExpandPERef ();
				}
				Expect (')');
				break;
			}
			TryExpandPERefSpaceKeep ();
			if (!SkipWhitespace ())
				throw NotWFError ("Whitespace is required between type and occurence in DTD attribute definition.");

			// def_value
			ReadAttributeDefaultValue (def);

			return def;
#endif
		}
Пример #13
0
		private void ReadAttributeDefaultValue (DTDAttributeDefinition def)
		{
			if(PeekChar () == '#')
			{
				ReadChar ();
				switch(PeekChar ())
				{
				case 'R':
					Expect ("REQUIRED");
					def.OccurenceType = DTDAttributeOccurenceType.Required;
					break;
				case 'I':
					Expect ("IMPLIED");
					def.OccurenceType = DTDAttributeOccurenceType.Optional;
					break;
				case 'F':
					Expect ("FIXED");
					def.OccurenceType = DTDAttributeOccurenceType.Fixed;
					if (!SkipWhitespace ())
						throw NotWFError ("Whitespace is required between FIXED and actual value in DTD attribute definition.");
					def.UnresolvedDefaultValue = ReadDefaultAttribute ();
					break;
				}
			} else {
				// one of the enumerated value
				SkipWhitespace ();
				TryExpandPERef ();
				def.UnresolvedDefaultValue = ReadDefaultAttribute ();
			}

			// VC: If default value exists, it should be valid.
			if (def.DefaultValue != null) {
				string normalized = def.Datatype.Normalize (def.DefaultValue);
				bool breakup = false;
				object parsed = null;

				// enumeration validity
				if (def.EnumeratedAttributeDeclaration.Count > 0) {
					if (!def.EnumeratedAttributeDeclaration.Contains (normalized)) {
						HandleError (new XmlSchemaException ("Default value is not one of the enumerated values.",
							def.LineNumber, def.LinePosition, null, def.BaseURI, null));
						breakup = true;
					}
				}
				if (def.EnumeratedNotations.Count > 0) {
					if (!def.EnumeratedNotations.Contains (normalized)) {
						HandleError (new XmlSchemaException ("Default value is not one of the enumerated notation values.",
							def.LineNumber, def.LinePosition, null, def.BaseURI, null));
						breakup = true;
					}
				}

				// type based validity
				if (!breakup) {
					try {
						parsed = def.Datatype.ParseValue (normalized, DTD.NameTable, null);
					} catch (Exception ex) { // FIXME: (wishlist) bad catch ;-(
						HandleError (new XmlSchemaException ("Invalid default value for ENTITY type.",
							def.LineNumber, def.LinePosition, null, def.BaseURI, ex));
						breakup = true;
					}
				}
				if (!breakup) {
					switch (def.Datatype.TokenizedType) {
					case XmlTokenizedType.ENTITY:
						if (DTD.EntityDecls [normalized] == null)
							HandleError (new XmlSchemaException ("Specified entity declaration used by default attribute value was not found.",
								def.LineNumber, def.LinePosition, null, def.BaseURI, null));
						break;
					case XmlTokenizedType.ENTITIES:
						string [] entities = parsed as string [];
						for (int i = 0; i < entities.Length; i++) {
							string entity = entities [i];
							if (DTD.EntityDecls [entity] == null)
								HandleError (new XmlSchemaException ("Specified entity declaration used by default attribute value was not found.",
									def.LineNumber, def.LinePosition, null, def.BaseURI, null));
						}
						break;
					}
				}
			}
			// Extra ID attribute validity check.
			if (def.Datatype != null && def.Datatype.TokenizedType == XmlTokenizedType.ID)
				if (def.UnresolvedDefaultValue != null)
					HandleError (new XmlSchemaException ("ID attribute must not have fixed value constraint.",
						def.LineNumber, def.LinePosition, null, def.BaseURI, null));

		}