dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/System.Xml XmlTextWriter.cs,1.1,1.2


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/System.Xml XmlTextWriter.cs,1.1,1.2
Date: Mon, 02 Dec 2002 01:36:40 -0500

Update of /cvsroot/dotgnu-pnet/pnetlib/System.Xml
In directory subversions:/tmp/cvs-serv18798/System.Xml

Modified Files:
        XmlTextWriter.cs 
Log Message:


Implement some of the missing functionality in "XmlTextWriter".


Index: XmlTextWriter.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/System.Xml/XmlTextWriter.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** XmlTextWriter.cs    27 Jul 2002 04:23:28 -0000      1.1
--- XmlTextWriter.cs    2 Dec 2002 06:36:36 -0000       1.2
***************
*** 63,67 ****
                        {
                                writer = new StreamWriter
!                                       (new FileStream(filename, 
FileMode.Open),
                                         ((encoding != null) ? encoding : 
Encoding.UTF8));
                                Initialize();
--- 63,67 ----
                        {
                                writer = new StreamWriter
!                                       (new FileStream(filename, 
FileMode.Open, FileAccess.Read),
                                         ((encoding != null) ? encoding : 
Encoding.UTF8));
                                Initialize();
***************
*** 137,140 ****
--- 137,141 ----
                                        {
                                                writer.Write(indentChar);
+                                               --indent;
                                        }
                                }
***************
*** 155,158 ****
--- 156,245 ----
                        }
  
+       // State flags for "Sync".
+       [Flags]
+       private enum WriteStateFlag
+       {
+               StartFlag     = (1<<0),
+               PrologFlag    = (1<<1),
+               ElementFlag   = (1<<2),
+               AttributeFlag = (1<<3),
+               ContentFlag   = (1<<4),
+               ClosedFlag    = (1<<5)
+ 
+       }; // enum WriteStateFlag
+ 
+       // Synchronize the output with a particular document area.
+       private void Sync(WriteStateFlag flags)
+                       {
+                               // Determine if the current write state is 
compatible
+                               // with the synchronisation flags, and shift to 
the
+                               // requested state if necessary.
+                               switch(writeState)
+                               {
+                                       case WriteState.Element:
+                                       {
+                                               if((flags & 
WriteStateFlag.ContentFlag) != 0)
+                                               {
+                                                       writer.Write(">");
+                                                       writeState = 
System.Xml.WriteState.Content;
+                                               }
+                                               else
+                                               {
+                                                       throw new 
InvalidOperationException
+                                                               
(S._("Xml_InvalidWriteState"));
+                                               }
+                                       }
+                                       break;
+ 
+                                       case WriteState.Attribute:
+                                       {
+                                               if((flags & 
WriteStateFlag.AttributeFlag) != 0)
+                                               {
+                                                       // We can write 
directly to the attribute.
+                                               }
+                                               else if((flags & 
WriteStateFlag.ContentFlag) != 0)
+                                               {
+                                                       // Terminate the 
attribute and switch to contents.
+                                                       writer.Write(quoteChar);
+                                                       writer.Write(">");
+                                                       writeState = 
System.Xml.WriteState.Content;
+                                               }
+                                               else
+                                               {
+                                                       throw new 
InvalidOperationException
+                                                               
(S._("Xml_InvalidWriteState"));
+                                               }
+                                       }
+                                       break;
+ 
+                                       case WriteState.Content:
+                                       {
+                                               if((flags & 
WriteStateFlag.ContentFlag) == 0)
+                                               {
+                                                       throw new 
InvalidOperationException
+                                                               
(S._("Xml_InvalidWriteState"));
+                                               }
+                                       }
+                                       break;
+ 
+                                       case WriteState.Start:
+                                       case WriteState.Prolog:
+                                       case WriteState.Closed:
+                                       {
+                                               if(((1 << (int)writeState) & 
(int)flags) == 0)
+                                               {
+                                                       throw new 
InvalidOperationException
+                                                               
(S._("Xml_InvalidWriteState"));
+                                               }
+                                       }
+                                       break;
+                               }
+                               if(writeState == System.Xml.WriteState.Content)
+                               {
+                                       // Record that we wrote some text to a 
content field.
+                                       prevWasText = true;
+                               }
+                       }
+ 
        // Close this writer and free all resources.
        public override void Close()
***************
*** 229,233 ****
        public override void WriteCData(String text)
                        {
!                               // TODO
                        }
  
--- 316,329 ----
        public override void WriteCData(String text)
                        {
!                               if(text != null && text.IndexOf("]]>") != -1)
!                               {
!                                       throw new ArgumentException
!                                               (S._("Xml_InvalidXmlWritten"), 
"text");
!                               }
!                               Sync(WriteStateFlag.ContentFlag);
!                               if(text != null)
!                               {
!                                       writer.Write(text);
!                               }
                        }
  
***************
*** 235,239 ****
        public override void WriteCharEntity(char ch)
                        {
!                               // TODO
                        }
  
--- 331,337 ----
        public override void WriteCharEntity(char ch)
                        {
!                               Sync(WriteStateFlag.ContentFlag |
!                                        WriteStateFlag.AttributeFlag);
!                               writer.Write("&#x{0:X2};", (int)ch);
                        }
  
***************
*** 257,279 ****
                                }
  
!                               // We must not be in the closed state.
!                               if(writeState == System.Xml.WriteState.Closed)
!                               {
!                                       throw new InvalidOperationException
!                                               (S._("Xml_InvalidWriteState"));
!                               }
! 
!                               // If we are in the element state, then shift 
to content.
!                               if(writeState == System.Xml.WriteState.Element)
!                               {
!                                       writer.Write('>');
!                                       writeState = 
System.Xml.WriteState.Content;
!                               }
! 
!                               // Record that we wrote some text to a content 
field.
!                               if(writeState == System.Xml.WriteState.Content)
!                               {
!                                       prevWasText = true;
!                               }
  
                                // The buffer must not end in a low surrogate.
--- 355,361 ----
                                }
  
!                               // Synchronize to the content or attribute area.
!                               Sync(WriteStateFlag.ContentFlag |
!                                        WriteStateFlag.AttributeFlag);
  
                                // The buffer must not end in a low surrogate.
***************
*** 395,406 ****
        public override void WriteComment(String text)
                        {
-                               // We must not be closed or in attribute.
-                               if(writeState == System.Xml.WriteState.Closed ||
-                                  writeState == 
System.Xml.WriteState.Attribute)
-                               {
-                                       throw new InvalidOperationException
-                                               (S._("Xml_InvalidWriteState"));
-                               }
- 
                                // Bail out if the comment text contains "-->".
                                if(text != null && text.IndexOf("-->") != -1)
--- 477,480 ----
***************
*** 410,425 ****
                                }
  
!                               // If we are in the element state, then shift 
to content.
!                               if(writeState == System.Xml.WriteState.Element)
!                               {
!                                       writer.Write('>');
!                                       writeState = 
System.Xml.WriteState.Content;
!                               }
! 
!                               // Record that we wrote some text to a content 
field.
!                               if(writeState == System.Xml.WriteState.Content)
!                               {
!                                       prevWasText = true;
!                               }
  
                                // Write out the comment.
--- 484,490 ----
                                }
  
!                               // Synchronize to an area that allows comments.
!                               Sync(WriteStateFlag.ContentFlag |
!                                        WriteStateFlag.PrologFlag);
  
                                // Write out the comment.
***************
*** 494,498 ****
        public override void WriteEndDocument()
                        {
!                               // TODO
                        }
  
--- 559,600 ----
        public override void WriteEndDocument()
                        {
!                               if(writeState == System.Xml.WriteState.Start ||
!                                  writeState == System.Xml.WriteState.Prolog ||
!                                  writeState == System.Xml.WriteState.Closed)
!                               {
!                                       throw new ArgumentException
!                                               (S._("Xml_InvalidWriteState"), 
"WriteState");
!                               }
!                               if(writeState == 
System.Xml.WriteState.Attribute)
!                               {
!                                       // Terminate the attribute and the 
element start.
!                                       writer.Write(quoteChar);
!                                       writer.Write(" />");
!                                       PopScope();
!                                       if(xmlSpace != 
System.Xml.XmlSpace.Preserve)
!                                       {
!                                               writer.WriteLine();
!                                       }
!                               }
!                               else if(writeState == 
System.Xml.WriteState.Element)
!                               {
!                                       // Terminate the element start.
!                                       writer.Write(" />");
!                                       PopScope();
!                               }
!                               while(scope != null)
!                               {
!                                       DoIndent();
!                                       writer.Write("</");
!                                       writer.Write(scope.localName);
!                                       writer.Write('>');
!                                       PopScope();
!                                       if(xmlSpace != 
System.Xml.XmlSpace.Preserve)
!                                       {
!                                               writer.WriteLine();
!                                       }
!                               }
!                               writeState = System.Xml.WriteState.Start;
!                               prevWasText = false;
                        }
  
***************
*** 541,545 ****
        public override void WriteEntityRef(String name)
                        {
!                               // TODO
                        }
  
--- 643,654 ----
        public override void WriteEntityRef(String name)
                        {
!                               if(!XmlReader.IsNameToken(name))
!                               {
!                                       throw new ArgumentException
!                                               (S._("Xml_InvalidEntityRef"), 
"name");
!                               }
!                               Sync(WriteStateFlag.ContentFlag |
!                                        WriteStateFlag.AttributeFlag);
!                               writer.Write("&{0};", name);
                        }
  
***************
*** 589,593 ****
        public override void WriteName(String name)
                        {
!                               // TODO
                        }
  
--- 698,709 ----
        public override void WriteName(String name)
                        {
!                               if(!XmlReader.IsName(name))
!                               {
!                                       throw new ArgumentException
!                                               (S._("Xml_InvalidName"), 
"name");
!                               }
!                               Sync(WriteStateFlag.ContentFlag |
!                                        WriteStateFlag.AttributeFlag);
!                               writer.Write(name);
                        }
  
***************
*** 595,599 ****
        public override void WriteNmToken(String name)
                        {
!                               // TODO
                        }
  
--- 711,722 ----
        public override void WriteNmToken(String name)
                        {
!                               if(!XmlReader.IsNameToken(name))
!                               {
!                                       throw new ArgumentException
!                                               (S._("Xml_InvalidName"), 
"name");
!                               }
!                               Sync(WriteStateFlag.ContentFlag |
!                                        WriteStateFlag.AttributeFlag);
!                               writer.Write(name);
                        }
  
***************
*** 736,740 ****
        public override void WriteStartDocument(bool standalone)
                        {
!                               // TODO
                        }
  
--- 859,879 ----
        public override void WriteStartDocument(bool standalone)
                        {
!                               if(writeState != System.Xml.WriteState.Start)
!                               {
!                                       throw new InvalidOperationException
!                                               (S._("Xml_InvalidWriteState"));
!                               }
!                       #if !ECMA_COMPAT
!                               writer.WriteLine
!                                       ("<?xml version=\"1.0\" 
encoding=\"{0}\" " +
!                                        "standalone=\"{1}\"?>",
!                                        writer.Encoding.WebName,
!                                        (standalone ? "yes" : "no"));
!                       #else
!                               writer.WriteLine
!                                       ("<?xml version=\"1.0\" 
"standalone=\"{0}\"?>",
!                                        (standalone ? "yes" : "no"));
!                       #endif
!                               writeState = System.Xml.WriteState.Prolog;
                        }
  
***************
*** 742,746 ****
        public override void WriteStartDocument()
                        {
!                               // TODO
                        }
  
--- 881,896 ----
        public override void WriteStartDocument()
                        {
!                               if(writeState != System.Xml.WriteState.Start)
!                               {
!                                       throw new InvalidOperationException
!                                               (S._("Xml_InvalidWriteState"));
!                               }
!                       #if !ECMA_COMPAT
!                               writer.WriteLine("<?xml version=\"1.0\" 
encoding=\"{0}\"?>",
!                                                                
writer.Encoding.WebName);
!                       #else
!                               writer.WriteLine("<?xml version=\"1.0\"?>");
!                       #endif
!                               writeState = System.Xml.WriteState.Prolog;
                        }
  
***************
*** 922,936 ****
  
                                // If we are in the element state, then shift 
to content.
!                               if(writeState == System.Xml.WriteState.Element)
!                               {
!                                       writer.Write('>');
!                                       writeState = 
System.Xml.WriteState.Content;
!                               }
! 
!                               // Record that we wrote some text to a content 
field.
!                               if(writeState == System.Xml.WriteState.Content)
!                               {
!                                       prevWasText = true;
!                               }
  
                                // Bail out if the text is empty.
--- 1072,1077 ----
  
                                // If we are in the element state, then shift 
to content.
!                               Sync(WriteStateFlag.ContentFlag |
!                                        WriteStateFlag.AttributeFlag);
  
                                // Bail out if the text is empty.
***************
*** 1045,1049 ****
        public override void WriteSurrogateCharEntity(char lowChar, char 
highChar)
                        {
!                               // TODO
                        }
  
--- 1186,1204 ----
        public override void WriteSurrogateCharEntity(char lowChar, char 
highChar)
                        {
!                               if(lowChar < 0xDC00 || lowChar > 0xDFFF)
!                               {
!                                       throw new ArgumentException
!                                               (S._("Xml_InvalidSurrogate"), 
"lowChar");
!                               }
!                               if(highChar < 0xD800 || highChar > 0xDBFF)
!                               {
!                                       throw new ArgumentException
!                                               (S._("Xml_InvalidSurrogate"), 
"highChar");
!                               }
!                               Sync(WriteStateFlag.ContentFlag |
!                                        WriteStateFlag.AttributeFlag);
!                               int ch = 0x10000 + ((highChar - 0xD800) << 10) +
!                                                       (lowChar - 0xDC00);
!                               writer.Write("&#x{0:X5};", ch);
                        }
  
***************
*** 1051,1055 ****
        public override void WriteWhitespace(String ws)
                        {
!                               // TODO
                        }
  
--- 1206,1226 ----
        public override void WriteWhitespace(String ws)
                        {
!                               if(ws == null || ws == String.Empty)
!                               {
!                                       throw new ArgumentException
!                                               (S._("Xml_InvalidWhitespace"), 
"ws");
!                               }
!                               foreach(char ch in ws)
!                               {
!                                       if(!Char.IsWhiteSpace(ch))
!                                       {
!                                               throw new ArgumentException
!                                                       
(S._("Xml_InvalidWhitespace"), "ws");
!                                       }
!                               }
!                               Sync(WriteStateFlag.ContentFlag |
!                                        WriteStateFlag.AttributeFlag |
!                                        WriteStateFlag.PrologFlag);
!                               writer.Write(ws);
                        }
  





reply via email to

[Prev in Thread] Current Thread [Next in Thread]