Переглянути джерело

XmlLang and XmlSpace for XmlTextWriter.

svn path=/trunk/mcs/; revision=3508
Kral Ferch 24 роки тому
батько
коміт
97a1576096

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

@@ -1,3 +1,11 @@
+2002-03-29  Kral Ferch  <[email protected]>
+
+	* XmlTextWriter.cs: XmlLang and XmlSpace properties
+	and WriteWhitespace.
+	
+	* XmlTextWriterOpenElement.cs: scope support for XmlLang
+	and XmlSpace.
+
 2002-03-29  Kral Ferch  <[email protected]>
 
 	* XmlTextWriter.cs: Working on Attribute methods.

+ 76 - 24
mcs/class/System.XML/System.Xml/XmlTextWriter.cs

@@ -35,6 +35,10 @@ namespace System.Xml
 		protected int indentLevel = 0;
 		protected string indentFormatting;
 		protected Stream baseStream = null;
+		protected string xmlLang = null;
+		protected XmlSpace xmlSpace = XmlSpace.None;
+		protected bool openXmlLang = false;
+		protected bool openXmlSpace = false;
 
 		#endregion
 
@@ -136,14 +140,36 @@ namespace System.Xml
 			get { return ws; }
 		}
 		
-		[MonoTODO]
 		public override string XmlLang {
-			get { throw new NotImplementedException(); }
+			get {
+				string xmlLang = null;
+				int i;
+
+				for (i = 0; i < openElements.Count; i++) 
+				{
+					xmlLang = ((XmlTextWriterOpenElement)openElements.ToArray().GetValue(i)).XmlLang;
+					if (xmlLang != null)
+						break;
+				}
+
+				return xmlLang;
+			}
 		}
 
-		[MonoTODO]
 		public override XmlSpace XmlSpace {
-			get { throw new NotImplementedException(); }
+			get {
+				XmlSpace xmlSpace = XmlSpace.None;
+				int i;
+
+				for (i = 0; i < openElements.Count; i++) 
+				{
+					xmlSpace = ((XmlTextWriterOpenElement)openElements.ToArray().GetValue(i)).XmlSpace;
+					if (xmlSpace != XmlSpace.None)
+						break;
+				}
+
+				return xmlSpace;
+			}
 		}
 
 		#endregion
@@ -180,15 +206,6 @@ namespace System.Xml
 			openWriter = false;
 		}
 
-		private void CloseStartAttribute ()
-		{
-			if (openStartAttribute) 
-			{
-				w.Write("={0}", quoteChar);
-				openStartAttribute = false;
-			}
-		}
-
 		private void CloseStartElement ()
 		{
 			if (openStartElement) 
@@ -279,8 +296,18 @@ namespace System.Xml
 
 			CheckState ();
 
-			if (openStartAttribute)
-				CloseStartAttribute ();
+			if (openXmlLang) {
+				w.Write (xmlLang);
+				openXmlLang = false;
+				((XmlTextWriterOpenElement)openElements.Peek()).XmlLang = xmlLang;
+			}
+
+			if (openXmlSpace) 
+			{
+				w.Write (xmlSpace.ToString ().ToLower ());
+				openXmlSpace = false;
+				((XmlTextWriterOpenElement)openElements.Peek()).XmlSpace = xmlSpace;
+			}
 
 			w.Write ("{0}", quoteChar);
 
@@ -367,8 +394,15 @@ namespace System.Xml
 			throw new NotImplementedException ();
 		}
 
+		[MonoTODO("haven't tested namespaces on attributes code yet.")]
 		public override void WriteStartAttribute (string prefix, string localName, string ns)
 		{
+			if ((prefix == "xml") && (localName == "lang"))
+				openXmlLang = true;
+
+			if ((prefix == "xml") && (localName == "space"))
+				openXmlSpace = true;
+
 			if ((prefix == "xmlns") && (localName == "xmlns"))
 				throw new ArgumentException ("Prefixes beginning with \"xml\" (regardless of whether the characters are uppercase, lowercase, or some combination thereof) are reserved for use by XML.");
 
@@ -395,11 +429,10 @@ namespace System.Xml
 				formatPrefix = prefix + ":";
 			}
 
-			w.Write (" {0}{1}", formatPrefix, localName);
+			w.Write (" {0}{1}={2}", formatPrefix, localName, quoteChar);
 
 			openAttribute = true;
 			ws = WriteState.Attribute;
-			openStartAttribute = true;
 		}
 
 		public override void WriteStartDocument ()
@@ -508,19 +541,34 @@ namespace System.Xml
 				text = text.Replace ("<", "&lt;");
 				text = text.Replace (">", "&gt;");
 				
-				if (openStartAttribute) {
+				if (openAttribute) {
 					if (quoteChar == '"')
 						text = text.Replace ("\"", "&quot;");
 					else
 						text = text.Replace ("'", "&apos;");
 				}
 
-				if (openStartAttribute)
-					CloseStartAttribute ();
-				else
+				if (!openAttribute)
 					CloseStartElement ();
 
-				w.Write (text);
+				if (!openXmlLang && !openXmlSpace)
+					w.Write (text);
+				else {
+					if (openXmlLang)
+						xmlLang = text;
+					else {
+						switch (text) {
+							case "default":
+								xmlSpace = XmlSpace.Default;
+								break;
+							case "preserve":
+								xmlSpace = XmlSpace.Preserve;
+								break;
+							default:
+								throw new ArgumentException ("'{0}' is an invalid xml:space value.");
+						}
+					}
+				}
 			}
 
 			IndentingOverriden = true;
@@ -532,10 +580,14 @@ namespace System.Xml
 			throw new NotImplementedException ();
 		}
 
-		[MonoTODO]
 		public override void WriteWhitespace (string ws)
 		{
-			throw new NotImplementedException ();
+			foreach (char c in ws) {
+				if ((c != ' ') && (c != '\t') && (c != '\r') && (c != '\n'))
+					throw new ArgumentException ();
+			}
+
+			w.Write (ws);
 		}
 
 		#endregion

+ 14 - 0
mcs/class/System.XML/System.Xml/XmlTextWriterOpenElement.cs

@@ -7,6 +7,8 @@ namespace System.Xml
 		#region Fields
 
 		string name;
+		string xmlLang;
+		XmlSpace xmlSpace;
 		bool indentingOverriden = false;
 
 		#endregion
@@ -33,6 +35,18 @@ namespace System.Xml
 			set { indentingOverriden = value; }
 		}
 
+		public string XmlLang
+		{
+			get { return xmlLang; }
+			set { xmlLang = value; }
+		}
+
+		public XmlSpace XmlSpace
+		{
+			get { return xmlSpace; }
+			set { xmlSpace = value; }
+		}
+
 		#endregion
 
 		#region Methods

+ 4 - 0
mcs/class/System.XML/Test/ChangeLog

@@ -1,3 +1,7 @@
+2002-03-29  Kral Ferch <[email protected]>
+
+	* XmlTextWriterTests.cs: XmlLang and XmlSpace tests.
+	
 2002-03-29  Kral Ferch <[email protected]>
 
 	* XmlTextWriterTests.cs: Working on Attributes.

+ 2 - 2
mcs/class/System.XML/Test/MonoMicro.Test.csproj

@@ -69,8 +69,8 @@
                 />
                 <Reference
                     Name = "System.XML"
-                    AssemblyName = "System.Xml"
-                    HintPath = "..\..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.0.3705\System.XML.dll"
+                    AssemblyName = "System.XML"
+                    HintPath = "..\obj\Debug\System.XML.dll"
                 />
             </References>
         </Build>

+ 57 - 9
mcs/class/System.XML/Test/XmlTextWriterTests.cs

@@ -591,49 +591,71 @@ namespace Ximian.Mono.Tests
 				sw.GetStringBuilder ().ToString ());
 		}
 
-		// TODO: need attribute methods to be implemented before this test will pass.
-		public void saveTestXmlLang ()
+		public void TestXmlLang ()
 		{
+			xtw.QuoteChar = '\'';
+
 			AssertNull (xtw.XmlLang);
+			
 			xtw.WriteStartElement ("foo");
 			xtw.WriteAttributeString ("xml", "lang", null, "langfoo");
 			AssertEquals ("langfoo", xtw.XmlLang);
+			AssertEquals ("<foo xml:lang='langfoo'", sw.GetStringBuilder ().ToString ());
 
 			xtw.WriteAttributeString ("boo", "yah");
 			AssertEquals ("langfoo", xtw.XmlLang);
+			AssertEquals ("<foo xml:lang='langfoo' boo='yah'", sw.GetStringBuilder ().ToString ());
 			
 			xtw.WriteElementString("bar", "baz");
 			AssertEquals ("langfoo", xtw.XmlLang);
+			AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>",
+				sw.GetStringBuilder ().ToString ());
 			
 			xtw.WriteString("baz");
 			AssertEquals ("langfoo", xtw.XmlLang);
+			AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz",
+				sw.GetStringBuilder ().ToString ());
 			
 			xtw.WriteStartElement ("quux");
 			xtw.WriteStartAttribute ("xml", "lang", null);
 			AssertEquals ("langfoo", xtw.XmlLang);
+			AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='",
+				sw.GetStringBuilder ().ToString ());
 			
 			xtw.WriteString("langbar");
 			AssertEquals ("langfoo", xtw.XmlLang);
+			AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='",
+				sw.GetStringBuilder ().ToString ());
 			
 			xtw.WriteEndAttribute ();
 			AssertEquals ("langbar", xtw.XmlLang);
+			AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'",
+				sw.GetStringBuilder ().ToString ());
 			
 			xtw.WriteElementString ("quuux", "squonk");
 			AssertEquals ("langbar", xtw.XmlLang);
+			AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><quuux>squonk</quuux>",
+				sw.GetStringBuilder ().ToString ());
 			
 			xtw.WriteEndElement ();
 			AssertEquals ("langfoo", xtw.XmlLang);
+			AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><quuux>squonk</quuux></quux>",
+				sw.GetStringBuilder ().ToString ());
 			
 			xtw.WriteEndElement ();
 			AssertNull (xtw.XmlLang);
+			AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><quuux>squonk</quuux></quux></foo>",
+				sw.GetStringBuilder ().ToString ());
 			
 			xtw.Close ();
 			AssertNull (xtw.XmlLang);
 		}
 
 		// TODO: test operational aspects
-		public void saveTestXmlSpace ()
+		public void TestXmlSpace ()
 		{
+			xtw.QuoteChar = '\'';
+
 			xtw.WriteStartElement ("foo");
 			AssertEquals (XmlSpace.None, xtw.XmlSpace);
 
@@ -652,26 +674,52 @@ namespace Ximian.Mono.Tests
 			xtw.WriteWhitespace (" ");
 			xtw.WriteString ("bar");
 			xtw.WriteString (" baz quux");
-			AssertEquals ("<foo>foo bar baz quux<bar xml:space=\"preserve\">foo bar baz quux",
+			AssertEquals ("<foo>foo bar baz quux<bar xml:space='preserve'>foo bar baz quux",
 				sw.GetStringBuilder ().ToString ());
 
 			xtw.WriteStartElement ("baz");
-			xtw.WriteAttributeString ("xml", "space", null, "default");
+			xtw.WriteStartAttribute ("xml", "space", null);
+			AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
+			AssertEquals ("<foo>foo bar baz quux<bar xml:space='preserve'>foo bar baz quux<baz xml:space='",
+				sw.GetStringBuilder ().ToString ());
+
+			xtw.WriteString ("default");
+			AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
+			AssertEquals ("<foo>foo bar baz quux<bar xml:space='preserve'>foo bar baz quux<baz xml:space='",
+				sw.GetStringBuilder ().ToString ());
+			
+			xtw.WriteEndAttribute ();
 			AssertEquals (XmlSpace.Default, xtw.XmlSpace);
+			AssertEquals ("<foo>foo bar baz quux<bar xml:space='preserve'>foo bar baz quux<baz xml:space='default'",
+				sw.GetStringBuilder ().ToString ());
 
 			xtw.WriteString ("foo");
 			xtw.WriteWhitespace (" ");
 			xtw.WriteString ("bar");
 			xtw.WriteString (" baz quux");
-			AssertEquals ("<foo>foo bar baz quux<bar xml:space=\"preserve\">foo bar baz quux<baz xml:space=\"default\">foo bar baz quux",
+			AssertEquals ("<foo>foo bar baz quux<bar xml:space='preserve'>foo bar baz quux<baz xml:space='default'>foo bar baz quux",
 				sw.GetStringBuilder ().ToString ());
 
 			xtw.WriteStartElement ("quux");
 			try {
 				xtw.WriteAttributeString ("xml", "space", null, "bubba");
-			} catch (ArgumentException e) {
-				AssertEquals ("'{0}' is an invalid xml:space value.", e.Message);
-			}
+			} catch (ArgumentException) {}
+
+			try {
+				xtw.WriteAttributeString ("xml", "space", null, "PRESERVE");
+			} catch (ArgumentException) {}
+
+			try {
+				xtw.WriteAttributeString ("xml", "space", null, "Preserve");
+			} catch (ArgumentException) {}
+
+			try {
+				xtw.WriteAttributeString ("xml", "space", null, "Default");
+			} catch (ArgumentException) {}
+
+			try {
+				xtw.WriteWhitespace ("x");
+			} catch (ArgumentException) { }
 		}
 	}
 }