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);
        }
        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);
        }
Exemple #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();
                 }
             }
         }
     }
 }
Exemple #4
0
internal DTDAttributeDefinition GetAttributeDefinition ()
		{
			if (OwnerElement == null)
				return null;

			// If it is default, then directly create new attribute.
			DTDAttListDeclaration attList = OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD.AttListDecls [OwnerElement.Name] : null;
			return attList != null ? attList [Name] : null;
		}
        internal DTDAttributeDefinition GetAttributeDefinition()
        {
            if (this.OwnerElement == null)
            {
                return(null);
            }
            DTDAttListDeclaration dtdattListDeclaration = (this.OwnerDocument.DocumentType == null) ? null : this.OwnerDocument.DocumentType.DTD.AttListDecls[this.OwnerElement.Name];

            return((dtdattListDeclaration == null) ? null : dtdattListDeclaration[this.Name]);
        }
Exemple #6
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
        }
Exemple #7
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);
        }
		void VerifyDeclaredAttributes (DTDAttListDeclaration decl)
		{
			// Check if all required attributes exist, and/or
			// if there is default values, then add them.
			for (int i = 0; i < decl.Definitions.Count; i++) {
				DTDAttributeDefinition def = (DTDAttributeDefinition) decl.Definitions [i];
				bool exists = false;
				for (int a = 0; a < attributeCount; a++) {
					if (attributes [a].Name == def.Name) {
						exists = true;
						break;
					}
				}
				if (exists)
					continue;

				if (def.OccurenceType == DTDAttributeOccurenceType.Required) {
					HandleError (String.Format ("Required attribute {0} in element {1} not found .",
						def.Name, decl.Name),
						XmlSeverityType.Error);
					continue;
				}

				else if (def.DefaultValue == null)
					continue;

				if (this.isStandalone && !def.IsInternalSubset)
					HandleError ("In standalone document, external default value definition must not be applied.", XmlSeverityType.Error);

				switch (validatingReader.ValidationType) {
				case ValidationType.Auto:
					if (validatingReader.Schemas.Count == 0)
						goto case ValidationType.DTD;
					break;
				case ValidationType.DTD:
				case ValidationType.None:
					// Other than them, ignore DTD defaults.
					AttributeSlot slot = GetAttributeSlot ();
					slot.Name = def.Name;
					int colonAt = def.Name.IndexOf (':');
					slot.LocalName = colonAt < 0 ? def.Name :
						def.Name.Substring (colonAt + 1);
					string prefix = colonAt < 0 ?
						String.Empty :
						def.Name.Substring (0, colonAt);
					slot.Prefix = prefix;
					slot.Value = def.DefaultValue;
					slot.IsDefault = true;
					break;
				}
			}
		}
		private void DtdValidateAttributes (DTDAttListDeclaration decl, bool validate)
		{
			while (reader.MoveToNextAttribute ()) {
				string attrName = reader.Name;
				AttributeSlot slot = GetAttributeSlot ();
				slot.Name = reader.Name;
				slot.LocalName = reader.LocalName;
				slot.Prefix = reader.Prefix;
				XmlReader targetReader = reader;
				string attrValue = String.Empty;
				// For attribute node, it always resolves
				// entity references on attributes.
				while (attributeValueEntityStack.Count >= 0) {
					if (!targetReader.ReadAttributeValue ()) {
						if (attributeValueEntityStack.Count > 0) {
							targetReader = attributeValueEntityStack.Pop () as XmlReader;
							continue;
						} else
							break;
					}
					switch (targetReader.NodeType) {
					case XmlNodeType.EntityReference:
						DTDEntityDeclaration edecl = DTD.EntityDecls [targetReader.Name];
						if (edecl == null) {
							HandleError (String.Format ("Referenced entity {0} is not declared.", targetReader.Name),
								XmlSeverityType.Error);
						} else {
							XmlTextReader etr = new XmlTextReader (edecl.EntityValue, XmlNodeType.Attribute, ParserContext);
							attributeValueEntityStack.Push (targetReader);
							targetReader = etr;
							continue;
						}
						break;
					case XmlNodeType.EndEntity:
						break;
					default:
						attrValue += targetReader.Value;
						break;
					}
				}
				
				reader.MoveToElement ();
				reader.MoveToAttribute (attrName);
				slot.Value = FilterNormalization (attrName, attrValue);

				if (!validate)
					continue;

				// Validation

				DTDAttributeDefinition def = decl [reader.Name];
				if (def == null) {
					HandleError (String.Format ("Attribute {0} is not declared.", reader.Name),
						XmlSeverityType.Error);
					continue;
				}

				// check enumeration constraint
				if (def.EnumeratedAttributeDeclaration.Count > 0)
					if (!def.EnumeratedAttributeDeclaration.Contains (slot.Value))
						HandleError (String.Format ("Attribute enumeration constraint error in attribute {0}, value {1}.",
							reader.Name, attrValue), XmlSeverityType.Error);
				if (def.EnumeratedNotations.Count > 0)
					if (!def.EnumeratedNotations.Contains (
						slot.Value))
						HandleError (String.Format ("Attribute notation enumeration constraint error in attribute {0}, value {1}.",
							reader.Name, attrValue), XmlSeverityType.Error);

				// check type constraint
				string normalized = null;
				if (def.Datatype != null)
					normalized = FilterNormalization (def.Name, attrValue);
				else
					normalized = attrValue;
				DTDEntityDeclaration ent;

				// Common process to get list value
				string [] list = null;
				switch (def.Datatype.TokenizedType) {
				case XmlTokenizedType.IDREFS:
				case XmlTokenizedType.ENTITIES:
				case XmlTokenizedType.NMTOKENS:
					try {
						list = def.Datatype.ParseValue (normalized, NameTable, null) as string [];
					} catch (Exception) {
						HandleError ("Attribute value is invalid against its data type.", XmlSeverityType.Error);
						list = new string [0];
					}
					break;
				default:
					try {
						def.Datatype.ParseValue (normalized, NameTable, null);
					} catch (Exception ex) {
						HandleError (String.Format ("Attribute value is invalid against its data type '{0}'. {1}", def.Datatype, ex.Message), XmlSeverityType.Error);
					}
					break;
				}

				switch (def.Datatype.TokenizedType) {
				case XmlTokenizedType.ID:
					if (this.idList.Contains (normalized)) {
						HandleError (String.Format ("Node with ID {0} was already appeared.", attrValue),
							XmlSeverityType.Error);
					} else {
						if (missingIDReferences.Contains (normalized))
							missingIDReferences.Remove (normalized);
						idList.Add (normalized);
					}
					break;
				case XmlTokenizedType.IDREF:
					if (!idList.Contains (normalized))
						missingIDReferences.Add (normalized);
					break;
				case XmlTokenizedType.IDREFS:
					for (int i = 0; i < list.Length; i++) {
						string idref = list [i];
						if (!idList.Contains (idref))
							missingIDReferences.Add (idref);
					}
					break;
				case XmlTokenizedType.ENTITY:
					ent = dtd.EntityDecls [normalized];
					if (ent == null)
						HandleError ("Reference to undeclared entity was found in attribute: " + reader.Name + ".", XmlSeverityType.Error);
					else if (ent.NotationName == null)
						HandleError ("The entity specified by entity type value must be an unparsed entity. The entity definition has no NDATA in attribute: " + reader.Name + ".", XmlSeverityType.Error);
					break;
				case XmlTokenizedType.ENTITIES:
					for (int i = 0; i < list.Length; i++) {
						string entref = list [i];
						ent = dtd.EntityDecls [FilterNormalization (reader.Name, entref)];
						if (ent == null)
							HandleError ("Reference to undeclared entity was found in attribute: " + reader.Name + ".", XmlSeverityType.Error);
						else if (ent.NotationName == null)
							HandleError ("The entity specified by ENTITIES type value must be an unparsed entity. The entity definition has no NDATA in attribute: " + reader.Name + ".", XmlSeverityType.Error);
					}
					break;
//				case XmlTokenizedType.NMTOKEN: nothing to do
//				case XmlTokenizedType.NMTOKENS: nothing to do
				}

				if (isStandalone && !def.IsInternalSubset && 
					attrValue != normalized)
					HandleError ("In standalone document, attribute value characters must not be checked against external definition.", XmlSeverityType.Error);

				if (def.OccurenceType == 
					DTDAttributeOccurenceType.Fixed &&
					attrValue != def.DefaultValue)
					HandleError (String.Format ("Fixed attribute {0} in element {1} has invalid value {2}.",
						def.Name, decl.Name, attrValue),
						XmlSeverityType.Error);
			}

			if (validate)
				VerifyDeclaredAttributes (decl);

			MoveToElement ();
		}
		private void ValidateAttributes (DTDAttListDeclaration decl, bool validate)
		{
			DtdValidateAttributes (decl, validate);

			for (int i = 0; i < attributeCount; i++) {
				AttributeSlot slot = attributes [i];
				if (slot.Name == "xmlns" || slot.Prefix == "xmlns")
					nsmgr.AddNamespace (
						slot.Prefix == "xmlns" ? slot.LocalName : String.Empty,
						slot.Value);
			}

			for (int i = 0; i < attributeCount; i++) {
				AttributeSlot slot = attributes [i];
				if (slot.Name == "xmlns")
					slot.NS = XmlNamespaceManager.XmlnsXmlns;
				else if (slot.Prefix.Length > 0)
					slot.NS = LookupNamespace (slot.Prefix);
				else
					slot.NS = String.Empty;
			}
		}
Exemple #11
0
		private DTDAttListDeclaration ReadAttListDecl ()
		{
			TryExpandPERefSpaceKeep ();
			if (!SkipWhitespace ())
				throw NotWFError ("Whitespace is required between ATTLIST and name in DTD attlist declaration.");
			TryExpandPERef ();
			string name = ReadName ();	// target element name
			DTDAttListDeclaration decl =
				DTD.AttListDecls [name] as DTDAttListDeclaration;
			if (decl == null)
				decl = new DTDAttListDeclaration (DTD);
			decl.IsInternalSubset = this.processingInternalSubset;
			decl.Name = name;

			if (!SkipWhitespace ())
				if (PeekChar () != '>')
					throw NotWFError ("Whitespace is required between name and content in non-empty DTD attlist declaration.");

			TryExpandPERef ();

			while (XmlChar.IsNameChar (PeekChar ())) {
				DTDAttributeDefinition def = ReadAttributeDefinition ();
				// There must not be two or more ID attributes.
				if (def.Datatype.TokenizedType == XmlTokenizedType.ID) {
					for (int i = 0; i < decl.Definitions.Count; i++) {
						DTDAttributeDefinition d = decl [i];
						if (d.Datatype.TokenizedType == XmlTokenizedType.ID) {
							HandleError (new XmlSchemaException ("AttList declaration must not contain two or more ID attributes.",
								def.LineNumber, def.LinePosition, null, def.BaseURI, null));
							break;
						}
					}
				}
				if (decl [def.Name] == null)
					decl.Add (def);
				SkipWhitespace ();
				TryExpandPERef ();
			}
			SkipWhitespace ();
			// This expanding is only allowed as a non-validating parser.
			TryExpandPERef ();
			Expect ('>');
			return decl;
		}