Browse Source

2003-12-16 Atsushi Enomoto <[email protected]>

	* DTDValidatingReader.cs : Validity check and entity expansion were
	  incorrectly mixed.
	* DTDObjectModel.cs, DTDReader.cs, XmlInputStream.cs :
	  Going to remove XmlInputStream. XmlStreamReader might be replacable.

svn path=/trunk/mcs/; revision=21242
Atsushi Eno 22 years ago
parent
commit
e22ed2eb99

+ 7 - 0
mcs/class/System.XML/System.Xml/ChangeLog

@@ -1,3 +1,10 @@
+2003-12-16  Atsushi Enomoto  <[email protected]>
+
+	* DTDValidatingReader.cs : Validity check and entity expansion were
+	  incorrectly mixed.
+	* DTDObjectModel.cs, DTDReader.cs, XmlInputStream.cs :
+	  Going to remove XmlInputStream. XmlStreamReader might be replacable.
+
 2003-12-15  Atsushi Enomoto  <[email protected]>
 
 	* XPathDocument2ChangedEventHandler.cs : fixed incorrect argument.

+ 7 - 8
mcs/class/System.XML/System.Xml/DTDObjectModel.cs

@@ -914,17 +914,16 @@ namespace Mono.Xml
 				baseUri = null;
 			Uri uri = resolver.ResolveUri (
 				baseUri != null ? new Uri (baseUri) : null, SystemId);
-			XmlStreamReader xsreader = null;
+			Stream stream = null;
 			try {
-				Stream stream = resolver.GetEntity (uri, null, typeof (Stream)) as Stream;
-				xsreader = new XmlStreamReader (stream, false);
-			} catch (Exception ex) { // FIXME: bad catch ;-(
+				stream = resolver.GetEntity (uri, null, typeof (Stream)) as Stream;
+			} catch (Exception ex) { // FIXME: (wishlist) bad catch ;-(
 				Root.AddError (new XmlSchemaException ("Cannot resolve external entity " + uri.ToString () + " .",
 					this.LineNumber, this.LinePosition, null, this.BaseURI, ex));
 			}
-			if (xsreader == null)
+			if (stream == null)
 				return String.Empty;
-			XmlTextReader extEntReader = new XmlTextReader (xsreader);
+			XmlTextReader extEntReader = new XmlTextReader (uri.AbsolutePath, stream, this.Root.NameTable);
 			extEntReader.SkipTextDeclaration ();
 			TextReader reader = extEntReader.GetRemainder ();
 			extEntReader.Close ();
@@ -1066,8 +1065,8 @@ namespace Mono.Xml
 			string absPath = absUri.ToString ();
 
 			try {
-				XmlStreamReader tw = new XmlStreamReader (absUri.ToString (), false, resolver, BaseURI);
-				XmlTextReader xtr = new XmlTextReader (tw);
+				Stream s = resolver.GetEntity (absUri, null, typeof (Stream)) as Stream;
+				XmlTextReader xtr = new XmlTextReader (s);
 				xtr.SkipTextDeclaration ();
 				resolvedValue = xtr.GetRemainder ().ReadToEnd ();
 			} catch (IOException ex) {

+ 7 - 3
mcs/class/System.XML/System.Xml/DTDReader.cs

@@ -162,7 +162,8 @@ namespace System.Xml
 				break;
 			case '<':
 				ReadChar ();
-				switch(ReadChar ())
+				int c = ReadChar ();
+				switch(c)
 				{
 				case '?':
 					// Only read, no store.
@@ -171,8 +172,10 @@ namespace System.Xml
 				case '!':
 					CompileDeclaration ();
 					break;
+				case -1:
+					throw new XmlException (this as IXmlLineInfo, "Unexpected end of stream.");
 				default:
-					throw new XmlException (this as IXmlLineInfo,"Syntax Error after '<' character.");
+					throw new XmlException (this as IXmlLineInfo, "Syntax Error after '<' character: " + (char) c);
 				}
 				break;
 			case ']':
@@ -1513,7 +1516,8 @@ namespace System.Xml
 			}
 			parserInputStack.Push (currentInput);
 			try {
-				currentInput = new XmlParserInput (new XmlStreamReader (url, false, DTD.Resolver, DTD.BaseURI), absPath);
+				Stream s = DTD.Resolver.GetEntity (absUri, null, typeof (Stream)) as Stream;
+				currentInput = new XmlParserInput (new XmlStreamReader (s), absPath);
 			} catch (Exception ex) { // FIXME: (wishlist) Bad exception catch ;-(
 				int line = currentInput == null ? 0 : currentInput.LineNumber;
 				int col = currentInput == null ? 0 : currentInput.LinePosition;

+ 55 - 34
mcs/class/System.XML/System.Xml/DTDValidatingReader.cs

@@ -398,7 +398,7 @@ namespace Mono.Xml
 				// startElementDeriv
 				// If no schema specification, then skip validation.
 				if (currentAutomata == null) {
-					SetupValidityIgnorantAttributes ();
+					ValidateAttributes (null, false);
 					if (reader.IsEmptyElement)
 						goto case XmlNodeType.EndElement;
 					break;
@@ -426,7 +426,7 @@ namespace Mono.Xml
 				DTDAttListDeclaration attList = dtd.AttListDecls [currentElement];
 				if (attList != null) {
 					// check attributes
-					ValidateAttributes (attList);
+					ValidateAttributes (attList, true);
 					currentAttribute = null;
 				} else {
 					if (reader.HasAttributes) {
@@ -434,7 +434,8 @@ namespace Mono.Xml
 							"Attributes are found on element {0} while it has no attribute definitions.", currentElement),
 							XmlSeverityType.Error);
 					}
-					SetupValidityIgnorantAttributes ();
+					// SetupValidityIgnorantAttributes ();
+					ValidateAttributes (null, false);
 				}
 
 				// If it is empty element then directly check end element.
@@ -531,6 +532,7 @@ namespace Mono.Xml
 			return true;
 		}
 
+		/*
 		private void SetupValidityIgnorantAttributes ()
 		{
 			if (reader.MoveToFirstAttribute ()) {
@@ -544,6 +546,7 @@ namespace Mono.Xml
 				reader.MoveToElement ();
 			}
 		}
+		*/
 
 		private void HandleError (string message, XmlSeverityType severity)
 		{
@@ -570,7 +573,7 @@ namespace Mono.Xml
 
 		Stack attributeValueEntityStack = new Stack ();
 
-		private void ValidateAttributes (DTDAttListDeclaration decl)
+		private void ValidateAttributes (DTDAttListDeclaration decl, bool validate)
 		{
 			while (reader.MoveToNextAttribute ()) {
 				string attrName = reader.Name;
@@ -580,41 +583,51 @@ namespace Mono.Xml
 				attributeNamespaces.Add (attrName, reader.NamespaceURI);
 				bool hasError = false;
 				XmlReader targetReader = reader;
-				while (attributeValueEntityStack.Count >= 0) {
-					if (!targetReader.ReadAttributeValue ()) {
-						if (attributeValueEntityStack.Count > 0) {
-							targetReader = attributeValueEntityStack.Pop () as XmlReader;
-							continue;
-						} else
+				string attrValue = null;
+				if (currentEntityHandling == EntityHandling.ExpandCharEntities)
+					attrValue = reader.Value;
+				else {
+					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);
+								hasError = true;
+							} else {
+								XmlTextReader etr = new XmlTextReader (edecl.EntityValue, XmlNodeType.Attribute, ParserContext);
+								attributeValueEntityStack.Push (targetReader);
+								targetReader = etr;
+								continue;
+							}
+							break;
+						case XmlNodeType.EndEntity:
+							break;
+						default:
+							valueBuilder.Append (targetReader.Value);
 							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);
-							hasError = true;
-						} else {
-							XmlTextReader etr = new XmlTextReader (edecl.EntityValue, XmlNodeType.Attribute, ParserContext);
-							attributeValueEntityStack.Push (targetReader);
-							targetReader = etr;
-							continue;
 						}
-						break;
-					case XmlNodeType.EndEntity:
-						break;
-					default:
-						valueBuilder.Append (targetReader.Value);
-						break;
 					}
+					attrValue = valueBuilder.ToString ();
+					valueBuilder.Length = 0;
 				}
 				reader.MoveToElement ();
 				reader.MoveToAttribute (attrName);
-				string attrValue = valueBuilder.ToString ();
-				valueBuilder.Length = 0;
 				attributeValues.Add (attrName, attrValue);
 
+				if (!validate)
+					continue;
+
+				// Validation
+
 				DTDAttributeDefinition def = decl [reader.Name];
 				if (def == null) {
 					HandleError (String.Format ("Attribute {0} is not declared.", reader.Name),
@@ -707,9 +720,18 @@ namespace Mono.Xml
 					}
 				}
 			}
+
+			if (validate)
+				VerifyDeclaredAttributes (decl);
+
+			MoveToElement ();
+		}
+
+		private void VerifyDeclaredAttributes (DTDAttListDeclaration decl)
+		{
 			// Check if all required attributes exist, and/or
 			// if there is default values, then add them.
-			foreach (DTDAttributeDefinition def in decl.Definitions)
+			foreach (DTDAttributeDefinition def in decl.Definitions) {
 				if (!attributes.Contains (def.Name)) {
 					if (def.OccurenceType == DTDAttributeOccurenceType.Required) {
 						HandleError (String.Format ("Required attribute {0} in element {1} not found .",
@@ -736,8 +758,7 @@ namespace Mono.Xml
 						}
 					}
 				}
-
-			reader.MoveToElement ();
+			}
 		}
 
 		public override bool ReadAttributeValue ()

+ 1 - 54
mcs/class/System.XML/System.Xml/XmlInputStream.cs

@@ -26,33 +26,7 @@ namespace Mono.Xml.Native
 		}
 
 		public XmlStreamReader (Stream input)
-			: this (new XmlInputStream (input, true))
-		{
-		}
-
-		public XmlStreamReader (Stream input, bool docent)
-			: this (new XmlInputStream (input, docent))
-		{
-		}
-
-//		public XmlStreamReader (string url)
-//			: this (url, true)
-//		{
-//		}
-//
-//		public XmlStreamReader (string url, bool docent)
-//			: this (new XmlInputStream (url, docent, null, null))
-//		{
-//		}
-
-		public XmlStreamReader (string url, XmlResolver resolver, string baseURI)
-			: this (url, true, resolver, baseURI)
-		{
-		}
-
-		public XmlStreamReader (string url, bool docent, XmlResolver resolver,
-			string baseURI)
-			: this (new XmlInputStream (url, docent, resolver, baseURI))
+			: this (new XmlInputStream (input))
 		{
 		}
 
@@ -79,38 +53,11 @@ namespace Mono.Xml.Native
 		byte[] buffer = new byte[256];
 		int bufLength;
 		int bufPos;
-		bool isDocumentEntity;	// allow omitting "version" or not.
 
 		static XmlException encodingException = new XmlException ("invalid encoding specification.");
-/*
-		public XmlInputStream (string url)
-			: this (url, true)
-		{
-		}
-*/
-		public XmlInputStream (string url, bool docent, XmlResolver resolver, string baseURI)
-		{
-			this.isDocumentEntity = docent;
-			// Use XmlResolver to resolve external entity.
-
-			if (resolver == null)
-				resolver = new XmlUrlResolver ();
-			Uri uri = resolver.ResolveUri (
-				baseURI == null || baseURI == String.Empty ?
-                                null : new Uri (baseURI), url);
-			Stream s = resolver.GetEntity (uri, null, typeof (Stream)) as Stream;
-
-			Initialize (s);
-		}
 
 		public XmlInputStream (Stream stream)
-			: this (stream, true)
-		{
-		}
-
-		public XmlInputStream (Stream stream, bool docent)
 		{
-			this.isDocumentEntity = docent;
 			Initialize (stream);
 		}