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 XmlTextReader.cs,1.11,1.12


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/System.Xml XmlTextReader.cs,1.11,1.12
Date: Mon, 23 Dec 2002 22:31:14 -0500

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

Modified Files:
        XmlTextReader.cs 
Log Message:


Rearrange the XML reading code so that it won't be necessary to create
explicit XmlNode objects during parsing, reducing memory requirements.


Index: XmlTextReader.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/System.Xml/XmlTextReader.cs,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -r1.11 -r1.12
*** XmlTextReader.cs    7 Dec 2002 07:34:14 -0000       1.11
--- XmlTextReader.cs    24 Dec 2002 03:31:11 -0000      1.12
***************
*** 48,54 ****
        private String baseURI;
        private Encoding encoding;
-       private XmlNode currentNode;
-       private XmlNode contextNode;
        private int ungetch;
  
        // Constructors.
--- 48,61 ----
        private String baseURI;
        private Encoding encoding;
        private int ungetch;
+       private XmlNodeType nodeType;
+       private String prefix;
+       private String localName;
+       private String namespaceURI;
+       private String value;
+       private XmlAttributeCollection attributes;
+       private int attributeIndex;
+       private int depth;
+       private bool isEmpty;
  
        // Constructors.
***************
*** 76,85 ****
                                xmlResolver = new XmlUrlResolver();
                                lineNumber = 1;
!                               linePosition = 1;
                                baseURI = String.Empty;
                                encoding = null;
-                               currentNode = null;
-                               contextNode = document;
                                ungetch = -1;
                        }
        public XmlTextReader(Stream input)
--- 83,99 ----
                                xmlResolver = new XmlUrlResolver();
                                lineNumber = 1;
!                               linePosition = 0;
                                baseURI = String.Empty;
                                encoding = null;
                                ungetch = -1;
+                               nodeType = XmlNodeType.None;
+                               prefix = String.Empty;
+                               localName = String.Empty;
+                               namespaceURI = String.Empty;
+                               value = String.Empty;
+                               attributes = null;
+                               attributeIndex = -1;
+                               depth = 1;
+                               isEmpty = false;
                        }
        public XmlTextReader(Stream input)
***************
*** 220,232 ****
        public override String GetAttribute(int i)
                        {
-                               XmlAttributeCollection attributes;
-                               if(currentNode == null)
-                               {
-                                       attributes = null;
-                               }
-                               else
-                               {
-                                       attributes = 
currentNode.AttributesInternal;
-                               }
                                if(attributes != null)
                                {
--- 234,237 ----
***************
*** 243,253 ****
        public override String GetAttribute(String name, String namespaceURI)
                        {
-                               XmlAttributeCollection attributes;
                                XmlAttribute attribute;
-                               if(currentNode == null)
-                               {
-                                       return null;
-                               }
-                               attributes = currentNode.AttributesInternal;
                                if(attributes != null)
                                {
--- 248,252 ----
***************
*** 264,274 ****
        public override String GetAttribute(String name)
                        {
-                               XmlAttributeCollection attributes;
                                XmlAttribute attribute;
-                               if(currentNode == null)
-                               {
-                                       return null;
-                               }
-                               attributes = currentNode.AttributesInternal;
                                if(attributes != null)
                                {
--- 263,267 ----
***************
*** 296,327 ****
                        }
  
-       // Synchronize on an element if the current is an attribute.
-       private void SyncOnElement()
-                       {
-                               XmlAttribute attr = (currentNode as 
XmlAttribute);
-                               if(attr != null)
-                               {
-                                       currentNode = attr.parent;
-                               }
-                       }
- 
        // Move the current position to a particular attribute.
        public override void MoveToAttribute(int i)
                        {
-                               XmlAttributeCollection attributes;
-                               SyncOnElement();
-                               if(currentNode == null)
-                               {
-                                       attributes = null;
-                               }
-                               else
-                               {
-                                       attributes = 
currentNode.AttributesInternal;
-                               }
                                if(attributes != null)
                                {
                                        if(i >= 0 && i < attributes.Count)
                                        {
!                                               currentNode = attributes[i];
                                                return;
                                        }
--- 289,300 ----
                        }
  
        // Move the current position to a particular attribute.
        public override void MoveToAttribute(int i)
                        {
                                if(attributes != null)
                                {
                                        if(i >= 0 && i < attributes.Count)
                                        {
!                                               attributeIndex = i;
                                                return;
                                        }
***************
*** 334,345 ****
        public override bool MoveToAttribute(String name, String ns)
                        {
-                               XmlAttributeCollection attributes;
                                XmlAttribute attribute;
-                               if(currentNode == null)
-                               {
-                                       return false;
-                               }
-                               SyncOnElement();
-                               attributes = currentNode.AttributesInternal;
                                if(attributes != null)
                                {
--- 307,311 ----
***************
*** 347,351 ****
                                        if(attribute != null)
                                        {
!                                               currentNode = attribute;
                                                return true;
                                        }
--- 313,317 ----
                                        if(attribute != null)
                                        {
!                                               attributeIndex = 
attributes.IndexOf(attribute);
                                                return true;
                                        }
***************
*** 357,368 ****
        public override bool MoveToAttribute(String name)
                        {
-                               XmlAttributeCollection attributes;
                                XmlAttribute attribute;
-                               if(currentNode == null)
-                               {
-                                       return false;
-                               }
-                               SyncOnElement();
-                               attributes = currentNode.AttributesInternal;
                                if(attributes != null)
                                {
--- 323,327 ----
***************
*** 370,374 ****
                                        if(attribute != null)
                                        {
!                                               currentNode = attribute;
                                                return true;
                                        }
--- 329,333 ----
                                        if(attribute != null)
                                        {
!                                               attributeIndex = 
attributes.IndexOf(attribute);
                                                return true;
                                        }
***************
*** 380,387 ****
        public override bool MoveToElement()
                        {
!                               XmlAttribute attr = (currentNode as 
XmlAttribute);
!                               if(attr != null)
                                {
!                                       currentNode = attr.parent;
                                        return true;
                                }
--- 339,345 ----
        public override bool MoveToElement()
                        {
!                               if(attributeIndex != -1)
                                {
!                                       attributeIndex = -1;
                                        return true;
                                }
***************
*** 395,408 ****
        public override bool MoveToFirstAttribute()
                        {
!                               XmlAttributeCollection attributes;
!                               if(currentNode != null)
                                {
!                                       SyncOnElement();
!                                       attributes = 
currentNode.AttributesInternal;
!                                       if(attributes != null && 
attributes.Count > 0)
!                                       {
!                                               currentNode = attributes[0];
!                                               return true;
!                                       }
                                }
                                return false;
--- 353,360 ----
        public override bool MoveToFirstAttribute()
                        {
!                               if(attributes != null && attributes.Count > 0)
                                {
!                                       attributeIndex = 0;
!                                       return true;
                                }
                                return false;
***************
*** 412,424 ****
        public override bool MoveToNextAttribute()
                        {
-                               XmlAttribute attr = (currentNode as 
XmlAttribute);
-                               XmlAttributeCollection attributes;
                                int index;
!                               if(attr != null)
                                {
-                                       attributes = 
currentNode.ParentNode.AttributesInternal;
                                        if(attributes != null)
                                        {
!                                               index = 
attributes.IndexOf(attr) + 1;
                                                if(index <= 0 || index >= 
attributes.Count)
                                                {
--- 364,373 ----
        public override bool MoveToNextAttribute()
                        {
                                int index;
!                               if(attributeIndex != -1)
                                {
                                        if(attributes != null)
                                        {
!                                               index = attributeIndex + 1;
                                                if(index <= 0 || index >= 
attributes.Count)
                                                {
***************
*** 427,431 ****
                                                else
                                                {
!                                                       currentNode = 
attributes[index];
                                                        return true;
                                                }
--- 376,380 ----
                                                else
                                                {
!                                                       attributeIndex = index;
                                                        return true;
                                                }
***************
*** 445,448 ****
--- 394,398 ----
        private int ReadChar()
                        {
+                               ++linePosition;
                                if(ungetch != -1)
                                {
***************
*** 457,464 ****
                        }
  
!       // Unget the last character to be read again next time.
!       private void UngetChar(int ch)
                        {
!                               ungetch = ch;
                        }
  
--- 407,482 ----
                        }
  
!       // Clear the node information.
!       private void ClearNodeInfo()
!                       {
!                               nodeType = XmlNodeType.None;
!                               prefix = String.Empty;
!                               localName = String.Empty;
!                               namespaceURI = String.Empty;
!                               value = String.Empty;
!                               attributes = null;
!                               attributeIndex = -1;
!                               isEmpty = false;
!                       }
! 
!       // Unget the last character and throw a read error.
!       private void UngetAndThrow(int ch)
!                       {
!                               --linePosition;
!                               if(ch != -1)
!                               {
!                                       ungetch = ch;
!                               }
!                               readState = ReadState.Error;
!                               ClearNodeInfo();
!                               throw new XmlException(S._("Xml_ReaderError"));
!                       }
! 
!       // Read an identifier from the input stream.
!       private String ReadIdentifier()
!                       {
!                               // TODO
!                               return null;
!                       }
! 
!       // Set the name information from an identifier.
!       private void SetName(String identifier)
!                       {
!                               int index = identifier.LastIndexOf(':');
!                               if(index >= 0)
!                               {
!                                       prefix = 
nameTable.Add(identifier.Substring(0, index));
!                                       localName = 
nameTable.Add(identifier.Substring(index + 1));
!                               }
!                               else
!                               {
!                                       prefix = String.Empty;
!                                       localName = nameTable.Add(identifier);
!                               }
!                               namespaceURI = String.Empty;
!                       }
! 
!       // Skip white space characters.
!       private void SkipWhite()
!                       {
!                               int ch;
!                               while((ch = ReadChar()) != -1)
!                               {
!                                       if(!Char.IsWhiteSpace((char)ch))
!                                       {
!                                               ungetch = ch;
!                                               break;
!                                       }
!                               }
!                       }
! 
!       // Expect the next character to be a certain value.
!       private void Expect(char expected)
                        {
!                               int ch = ReadChar();
!                               if(ch != expected)
!                               {
!                                       UngetAndThrow(ch);
!                               }
                        }
  
***************
*** 469,472 ****
--- 487,491 ----
                                int ch;
                                StringBuilder builder;
+                               String name;
                                int count;
  
***************
*** 487,491 ****
                                // Skip white space in the input stream.  TODO: 
collect
                                // up significant white space.
-                               ++linePosition;
                                while((ch = ReadChar()) != -1)
                                {
--- 506,509 ----
***************
*** 497,505 ****
                                        {
                                                ++lineNumber;
!                                               linePosition = 1;
!                                       }
!                                       else
!                                       {
!                                               ++linePosition;
                                        }
                                }
--- 515,519 ----
                                        {
                                                ++lineNumber;
!                                               linePosition = 0;
                                        }
                                }
***************
*** 508,515 ****
                                        // We've reached the end of the stream. 
 Throw
                                        // an error if we haven't closed all 
elements.
!                                       // TODO: error handling
!                                       --linePosition;
                                        readState = ReadState.EndOfFile;
!                                       currentNode = null;
                                        return false;
                                }
--- 522,531 ----
                                        // We've reached the end of the stream. 
 Throw
                                        // an error if we haven't closed all 
elements.
!                                       if(linePosition > 1)
!                                       {
!                                               --linePosition;
!                                       }
                                        readState = ReadState.EndOfFile;
!                                       ClearNodeInfo();
                                        return false;
                                }
***************
*** 519,547 ****
                                {
                                        // Some kind of tag.
-                                       ++linePosition;
                                        ch = ReadChar();
                                        if(ch == '/')
                                        {
                                                // End element tag.
!                                               // TODO
                                        }
                                        else if(ch == '!')
                                        {
                                                // Comment, CDATA, or document 
type information.
-                                               ++linePosition;
                                                ch = ReadChar();
                                                if(ch == '-')
                                                {
                                                        // Parse the "<!--" 
comment start sequence.
-                                                       ++linePosition;
                                                        ch = ReadChar();
                                                        if(ch != '-')
                                                        {
!                                                               goto error;
                                                        }
  
                                                        // Search for the "-->" 
comment end sequence.
                                                        builder = new 
StringBuilder();
-                                                       ++linePosition;
                                                        count = 0;
                                                        while((ch = ReadChar()) 
!= -1)
--- 535,568 ----
                                {
                                        // Some kind of tag.
                                        ch = ReadChar();
                                        if(ch == '/')
                                        {
                                                // End element tag.
!                                               name = ReadIdentifier();
!                                               SkipWhite();
!                                               Expect('>');
!                                               nodeType = XmlNodeType.Element;
!                                               SetName(name);
!                                               value = String.Empty;
!                                               attributes = null;
!                                               attributeIndex = -1;
!                                               isEmpty = false;
!                                               --depth;
                                        }
                                        else if(ch == '!')
                                        {
                                                // Comment, CDATA, or document 
type information.
                                                ch = ReadChar();
                                                if(ch == '-')
                                                {
                                                        // Parse the "<!--" 
comment start sequence.
                                                        ch = ReadChar();
                                                        if(ch != '-')
                                                        {
!                                                               
UngetAndThrow(ch);
                                                        }
  
                                                        // Search for the "-->" 
comment end sequence.
                                                        builder = new 
StringBuilder();
                                                        count = 0;
                                                        while((ch = ReadChar()) 
!= -1)
***************
*** 568,596 ****
                                                                {
                                                                        
++lineNumber;
!                                                                       
linePosition = 1;
!                                                               }
!                                                               else
!                                                               {
!                                                                       
++linePosition;
                                                                }
                                                        }
                                                        if(ch != '>')
                                                        {
!                                                               goto error;
                                                        }
  
                                                        // Create a comment 
node and return.
!                                                       currentNode = 
document.CreateComment
!                                                               
(builder.ToString());
!                                                       try
!                                                       {
!                                                               
contextNode.AppendChild(currentNode);
!                                                       }
!                                                       
catch(InvalidOperationException)
!                                                       {
!                                                               readState = 
ReadState.Error;
!                                                               currentNode = 
null;
!                                                               throw new 
XmlException(S._("Xml_ReaderError"));
!                                                       }
                                                        return true;
                                                }
--- 589,604 ----
                                                                {
                                                                        
++lineNumber;
!                                                                       
linePosition = 0;
                                                                }
                                                        }
                                                        if(ch != '>')
                                                        {
!                                                               
UngetAndThrow(ch);
                                                        }
  
                                                        // Create a comment 
node and return.
!                                                       ClearNodeInfo();
!                                                       nodeType = 
XmlNodeType.Comment;
!                                                       value = 
builder.ToString();
                                                        return true;
                                                }
***************
*** 605,609 ****
                                                else
                                                {
!                                                       goto error;
                                                }
                                        }
--- 613,617 ----
                                                else
                                                {
!                                                       UngetAndThrow(ch);
                                                }
                                        }
***************
*** 620,624 ****
                                        else
                                        {
!                                               goto error;
                                        }
                                }
--- 628,632 ----
                                        else
                                        {
!                                               UngetAndThrow(ch);
                                        }
                                }
***************
*** 630,645 ****
  
                                return false;
- 
-                               // We jump to here if some kind of parse error 
occurred
-                               // on the last character that we read.
-                       error:
-                               --linePosition;
-                               if(ch != -1)
-                               {
-                                       UngetChar(ch);
-                               }
-                               readState = ReadState.Error;
-                               currentNode = null;
-                               throw new XmlException(S._("Xml_ReaderError"));
                        }
  
--- 638,641 ----
***************
*** 719,736 ****
                                get
                                {
-                                       XmlAttributeCollection attributes;
-                                       XmlAttribute attr = (currentNode as 
XmlAttribute);
-                                       if(attr != null)
-                                       {
-                                               attributes = 
attr.ParentNode.AttributesInternal;
-                                       }
-                                       else if(currentNode != null)
-                                       {
-                                               attributes = 
currentNode.AttributesInternal;
-                                       }
-                                       else
-                                       {
-                                               return 0;
-                                       }
                                        if(attributes != null)
                                        {
--- 715,718 ----
***************
*** 758,770 ****
                                get
                                {
!                                       XmlNode node = currentNode;
!                                       int depth = 0;
!                                       while(node != null && !(node is 
XmlDocument) &&
!                                             !(node is XmlDocumentFragment))
                                        {
!                                               ++depth;
!                                               node = node.parent;
                                        }
-                                       return depth;
                                }
                        }
--- 740,751 ----
                                get
                                {
!                                       if(attributeIndex == -1)
!                                       {
!                                               return depth;
!                                       }
!                                       else
                                        {
!                                               return depth + 1;
                                        }
                                }
                        }
***************
*** 818,821 ****
--- 799,815 ----
                        }
  
+       // Get the current node as an attribute, if appropriate.
+       private XmlAttribute GetAsAttribute()
+                       {
+                               if(attributes != null && attributeIndex != -1)
+                               {
+                                       return attributes[attributeIndex];
+                               }
+                               else
+                               {
+                                       return null;
+                               }
+                       }
+ 
        // Determine if the current node's value was generated from a DTD 
default.
        public override bool IsDefault
***************
*** 823,835 ****
                                get
                                {
!                                       XmlAttribute attr = (currentNode as 
XmlAttribute);
                                        if(attr != null)
                                        {
                                                return !(attr.Specified);
                                        }
!                                       else
!                                       {
!                                               return false;
!                                       }
                                }
                        }
--- 817,826 ----
                                get
                                {
!                                       XmlAttribute attr = GetAsAttribute();
                                        if(attr != null)
                                        {
                                                return !(attr.Specified);
                                        }
!                                       return false;
                                }
                        }
***************
*** 840,847 ****
                                get
                                {
!                                       XmlElement element = (currentNode as 
XmlElement);
!                                       if(element != null)
                                        {
!                                               return element.IsEmpty;
                                        }
                                        else
--- 831,837 ----
                                get
                                {
!                                       if(nodeType == XmlNodeType.Element)
                                        {
!                                               return isEmpty;
                                        }
                                        else
***************
*** 901,905 ****
                                get
                                {
!                                       return linePosition;
                                }
                        }
--- 891,902 ----
                                get
                                {
!                                       if(linePosition > 0)
!                                       {
!                                               return linePosition;
!                                       }
!                                       else
!                                       {
!                                               return 0;
!                                       }
                                }
                        }
***************
*** 910,920 ****
                                get
                                {
!                                       if(currentNode != null)
                                        {
!                                               return currentNode.LocalName;
                                        }
                                        else
                                        {
!                                               return String.Empty;
                                        }
                                }
--- 907,918 ----
                                get
                                {
!                                       XmlAttribute attr = GetAsAttribute();
!                                       if(attr != null)
                                        {
!                                               return attr.LocalName;
                                        }
                                        else
                                        {
!                                               return localName;
                                        }
                                }
***************
*** 926,936 ****
                                get
                                {
!                                       if(currentNode != null)
                                        {
!                                               return currentNode.Name;
                                        }
                                        else
                                        {
!                                               return String.Empty;
                                        }
                                }
--- 924,939 ----
                                get
                                {
!                                       XmlAttribute attr = GetAsAttribute();
!                                       if(attr != null)
!                                       {
!                                               return attr.Name;
!                                       }
!                                       else if(prefix != String.Empty)
                                        {
!                                               return nameTable.Add(prefix + 
":" + localName);
                                        }
                                        else
                                        {
!                                               return localName;
                                        }
                                }
***************
*** 951,961 ****
                                get
                                {
!                                       if(currentNode != null)
                                        {
!                                               return currentNode.NamespaceURI;
                                        }
                                        else
                                        {
!                                               return String.Empty;
                                        }
                                }
--- 954,965 ----
                                get
                                {
!                                       XmlAttribute attr = GetAsAttribute();
!                                       if(attr != null)
                                        {
!                                               return attr.NamespaceURI;
                                        }
                                        else
                                        {
!                                               return namespaceURI;
                                        }
                                }
***************
*** 1003,1013 ****
                                get
                                {
!                                       if(currentNode != null)
                                        {
!                                               return currentNode.NodeType;
                                        }
                                        else
                                        {
!                                               return XmlNodeType.None;
                                        }
                                }
--- 1007,1017 ----
                                get
                                {
!                                       if(attributeIndex != -1)
                                        {
!                                               return XmlNodeType.Attribute;
                                        }
                                        else
                                        {
!                                               return nodeType;
                                        }
                                }
***************
*** 1019,1029 ****
                                get
                                {
!                                       if(currentNode != null)
                                        {
!                                               return currentNode.Prefix;
                                        }
                                        else
                                        {
!                                               return String.Empty;
                                        }
                                }
--- 1023,1034 ----
                                get
                                {
!                                       XmlAttribute attr = GetAsAttribute();
!                                       if(attr != null)
                                        {
!                                               return attr.Prefix;
                                        }
                                        else
                                        {
!                                               return prefix;
                                        }
                                }
***************
*** 1053,1063 ****
                                get
                                {
!                                       if(currentNode != null)
                                        {
!                                               return currentNode.Value;
                                        }
                                        else
                                        {
!                                               return String.Empty;
                                        }
                                }
--- 1058,1069 ----
                                get
                                {
!                                       XmlAttribute attr = GetAsAttribute();
!                                       if(attr != null)
                                        {
!                                               return attr.Value;
                                        }
                                        else
                                        {
!                                               return value;
                                        }
                                }




reply via email to

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