[Mono-list] [PATCH] Test case and fix for XML Declaration parsing

David Sheldon dave@earth.li
Tue, 4 Jun 2002 16:23:32 +0100


--wzJLGUyc3ArbnUjN
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Mon, Jun 03, 2002 at 04:09:58PM -0700, Jason Diamond wrote:
> > Right, my first useful submission. Please let me have feedback so that I
> > can be more useful with future attempts.
> 
> Did you forget to attach your patch?

That sounds like the first bug in it :)

David

-- 
"Anyone attempting to generate random numbers by deterministic means is, of
course, living in a state of sin."
-- John Von Neumann

--wzJLGUyc3ArbnUjN
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="XmlDeclaration.patch"

Index: XmlDeclaration.cs
===================================================================
RCS file: /mono/mcs/class/System.XML/System.Xml/XmlDeclaration.cs,v
retrieving revision 1.9
diff -u -r1.9 XmlDeclaration.cs
--- XmlDeclaration.cs	9 Apr 2002 01:38:59 -0000	1.9
+++ XmlDeclaration.cs	2 Jun 2002 22:31:26 -0000
@@ -98,10 +98,41 @@
 			w.WriteRaw (String.Format ("<?xml {0}?>", Value));
 		}
 
-		void ParseInput (string input)
-		{			
-			Encoding = input.Split (new char [] { ' ' }) [1].Split (new char [] { '=' }) [1];
-			Standalone = input.Split (new char [] { ' ' }) [2].Split (new char [] { '=' }) [1];
+
+		void ParseInput (string input) 
+		{
+			string [] tokens = input.Split (new char [] { ' ', '\u0009', '\u000d','\u000a' });
+			for (int i=0;i<tokens.Length; i++) 
+			{
+				string token = tokens [i];
+				if (token.StartsWith ("encoding")) {
+					Encoding=ParseDeclAttribute (tokens, ref i);
+				} else if (token.StartsWith ("standalone")) {
+					Standalone=ParseDeclAttribute (tokens,ref i);
+				} else if (token.StartsWith ("version")) {
+					version = ParseDeclAttribute (tokens, ref i);
+					/* TODO: Should we check this is "1.0"? */
+				}
+			}
+		}
+	  
+		private string ParseDeclAttribute (string [] tokens, ref int i) {
+			int quote;
+			string token = tokens [i];
+			while ((quote = Math.Max (token.IndexOf ('"'), token.IndexOf ('\''))) == -1) 
+				token = tokens [++i]; 
+				/* The attribute value in quotes can contain no whitespace */
+			
+			int endQuote = token.IndexOf (token [quote], quote + 1) ;
+			if (endQuote == -1) {
+			/* TODO
+			 * Malformed Declaration, I assume we should throw an 
+			 * exception here */
+				return null;
+			} else {
+				return token.Substring (quote + 1, endQuote - (quote + 1));
+			}
+			/* I assume all these +1s will be optimised somewhere */
 		}
 	}
 }

--wzJLGUyc3ArbnUjN
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="XmlDeclarationTests.patch"

Index: XmlDeclarationTests.cs
===================================================================
RCS file: /mono/mcs/class/System.XML/Test/XmlDeclarationTests.cs,v
retrieving revision 1.4
diff -u -r1.4 XmlDeclarationTests.cs
--- XmlDeclarationTests.cs	5 May 2002 10:31:13 -0000	1.4
+++ XmlDeclarationTests.cs	2 Jun 2002 22:01:43 -0000
@@ -2,7 +2,7 @@
 // System.Xml.XmlDeclarationTests.cs
 //
 // Author:
-// 	Duncan Mak  (duncan@ximian.com)
+//	Duncan Mak  (duncan@ximian.com)
 //
 // (C) Ximian, Inc.
 //
@@ -59,7 +59,7 @@
 			AssertEquals ("Value incorrectly cloned",
 				      original.Value, cloned.Value);
 			
-                        Assert ("Copies, not pointers", !Object.ReferenceEquals (original,cloned));
+			Assert ("Copies, not pointers", !Object.ReferenceEquals (original,cloned));
 		}
 
 		public void TestConstructor ()
@@ -108,9 +108,32 @@
 
 		public void TestValueProperty ()
 		{
+			string expected ="version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"" ;
+      
 			XmlDeclaration d = document.CreateXmlDeclaration ("1.0", "UTF-8", "yes");
-			AssertEquals ("Value property", "version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"",
-				      d.Value);
+			AssertEquals ("Value property", expected, d.Value);
+
+			d.Value = expected;
+			AssertEquals("Value round-trip", expected, d.Value);
+
+			d.Value="   "+ expected;
+			AssertEquals("Value round-trip (padded)", expected, d.Value);
+      
+			d.Value ="version=\"1.0\"     encoding=\"UTF-8\" standalone=\"yes\"" ;
+			AssertEquals("Value round-trip (padded 2)", expected, d.Value);
+      
+			d.Value ="version=\"1.0\"\tencoding=\"UTF-8\" standalone=\"yes\"" ;
+			AssertEquals("Value round-trip (\\t)", expected, d.Value);
+      
+			d.Value ="version=\"1.0\"\n    encoding=\"UTF-8\" standalone=\"yes\"" ;
+			AssertEquals("Value round-trip (\\n)", expected, d.Value);
+      
+			d.Value ="version=\"1.0\"    encoding	=   \"UTF-8\" standalone = \"yes\"" ;
+			AssertEquals("Value round-trip (spaces)", expected, d.Value);
+
+			d.Value ="version='1.0' encoding='UTF-8' standalone='yes'" ;
+			AssertEquals("Value round-trip ('s)", expected, d.Value);
+      
 		}
 
 		public void TestXmlCommentCloneNode ()
@@ -123,7 +146,7 @@
 			XmlNode deep = declaration.CloneNode (true); // deep
 			TestXmlNodeBaseProperties (original, deep);
 
-                        AssertEquals ("deep cloning differs from shallow cloning",
+			AssertEquals ("deep cloning differs from shallow cloning",
 				      deep.OuterXml, shallow.OuterXml);
 		}
 	}

--wzJLGUyc3ArbnUjN--