2003-04-04 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
authorAtsushi Eno <atsushieno@gmail.com>
Thu, 3 Apr 2003 19:03:17 +0000 (19:03 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Thu, 3 Apr 2003 19:03:17 +0000 (19:03 -0000)
* XmlDocument.cs : Load() now closes the given XmlReader.
  Don't allow creation of XmlTextReader for Doctype. (workaround.)
* XmlInputStream.cs : It now closes its internal stream explicitly.
* XmlNode.cs : RemoveChild() bugfix for removing LastLinkedChild.
* XmlNodeReader.cs : GetAttribute() bugfix for not-present attribute.
* XmlParserInput.cs : added Close() method.
* XmlReader.cs : MoveToContent() should (1) not Read in case of empty
  element, and (2) MoveToElement() in case of attribute.
* XmlTextReader.cs : Close() now actually closes source TextReaders.

svn path=/trunk/mcs/; revision=13070

mcs/class/System.XML/System.Xml/XmlDocument.cs
mcs/class/System.XML/System.Xml/XmlInputStream.cs
mcs/class/System.XML/System.Xml/XmlNode.cs
mcs/class/System.XML/System.Xml/XmlNodeReader.cs
mcs/class/System.XML/System.Xml/XmlParserInput.cs
mcs/class/System.XML/System.Xml/XmlReader.cs
mcs/class/System.XML/System.Xml/XmlTextReader.cs

index 999695a6c16a3a8c0fb6ac1c6beb473cd5f24d19..e34116b1aefc7fd1bc0b375cd9d556c3511504e1 100644 (file)
@@ -581,6 +581,7 @@ namespace System.Xml
                                if(n == null) break;
                                AppendChild (n);
                        } while (true);
+                       xmlReader.Close ();
                }
 
                public virtual void LoadXml (string xml)
@@ -776,12 +777,17 @@ namespace System.Xml
                                        // hack ;-)
                                        XmlTextReader xtReader = reader as XmlTextReader;
                                        if(xtReader == null) {
+                                               // XmlTextReader doesn't allow such creation that starts reading from Doctype.
+                                       /*
                                                xtReader = new XmlTextReader (reader.ReadOuterXml (),
                                                        XmlNodeType.DocumentType,
                                                        new XmlParserContext (NameTable, ConstructNamespaceManager(), XmlLang, XmlSpace));
                                                xtReader.Read ();
+                                       */
+                                               newNode = CreateDocumentType (reader.Name, reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
+                                       } else {
+                                               newNode = ReadDoctypeNode (xtReader);
                                        }
-                                       newNode = ReadDoctypeNode (xtReader);
                                        if(currentNode != null)
                                                throw new XmlException ("XmlDocumentType at invalid position.");
                                        break;
@@ -808,9 +814,12 @@ namespace System.Xml
                                                ignoredWhitespace = true;
                                        break;
                                }
-                               reader.Read ();
+                               if (!reader.Read ())
+                                       break;
                        } while (ignoredWhitespace || reader.Depth > startDepth ||
                                (reader.Depth == startDepth && reader.NodeType == XmlNodeType.EndElement));
+                       if (startDepth != reader.Depth && reader.EOF)
+                               throw new XmlException ("Unexpected end of xml reader.");
                        return resultNode != null ? resultNode : newNode;
                }
 
index 9e3fb407e44e18d5eec5e5d8565565370282fc08..b5c5e5abc405551fb932a47b4fb0de1c92410b86 100644 (file)
@@ -17,9 +17,12 @@ namespace Mono.Xml.Native
        #region XmlStreamReader
        public class XmlStreamReader : StreamReader
        {
+               XmlInputStream input;
+
                XmlStreamReader (XmlInputStream input)
                        : base (input, input.ActualEncoding != null ? input.ActualEncoding : Encoding.UTF8)
                {
+                       this.input = input;
                }
 
                public XmlStreamReader (Stream input)
@@ -41,6 +44,19 @@ namespace Mono.Xml.Native
                        : this (new XmlInputStream (url, docent))
                {
                }
+
+               public override void Close ()
+               {
+                       this.input.Close ();
+               }
+
+               protected override void Dispose (bool disposing)
+               {
+                       base.Dispose (disposing);
+                       if (disposing) {
+                               Close ();
+                       }
+               }
        }
        #endregion
 \r
@@ -260,7 +276,12 @@ namespace Mono.Xml.Native
                        }\r
                }\r
 \r
-               public override void Flush()\r
+               public override void Close ()\r
+               {\r
+                       stream.Close ();\r
+               }\r
+\r
+               public override void Flush ()\r
                {\r
                        stream.Flush ();\r
                }\r
index 6eb0999f618f14bc38de21aa2e39ee32e777b259..64abf6fd1c8118b0198ca98f7f3a6326428c9fea 100644 (file)
@@ -411,33 +411,43 @@ namespace System.Xml
 
                        ownerDoc.onNodeRemoving (oldChild, oldChild.ParentNode);
 
-                       if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute || NodeType == XmlNodeType.DocumentFragment) {
-                               if (IsReadOnly)
-                                       throw new ArgumentException ("This node is read only.");
+                       if (NodeType != XmlNodeType.Attribute && 
+                               NodeType != XmlNodeType.Element && 
+                               NodeType != XmlNodeType.Document && 
+                               NodeType != XmlNodeType.DocumentFragment)
+                               throw new ArgumentException (String.Format ("This {0} node cannot remove child.", NodeType));
 
-                               if (Object.ReferenceEquals (LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals (LastLinkedChild, oldChild))
-                                       LastLinkedChild = null;
-                               else {
-                                       XmlLinkedNode oldLinkedChild = (XmlLinkedNode)oldChild;
-                                       XmlLinkedNode beforeLinkedChild = LastLinkedChild;
-                                       
-                                       while (!Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, LastLinkedChild) && !Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
-                                               beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
+                       if (IsReadOnly)
+                               throw new ArgumentException (String.Format ("This {0} node is read only.", NodeType));
 
-                                       if (!Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
-                                               throw new ArgumentException ();
+                       if (Object.ReferenceEquals (LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals (LastLinkedChild, oldChild))
+                               // If there is only one children, simply clear.
+                               LastLinkedChild = null;
+                       else {
+                               XmlLinkedNode oldLinkedChild = (XmlLinkedNode) oldChild;
+                               XmlLinkedNode beforeLinkedChild = LastLinkedChild;
+                               XmlLinkedNode firstChild = (XmlLinkedNode) FirstChild;
+                               
+                               while (Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, LastLinkedChild) == false && 
+                                       Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, oldLinkedChild) == false)
+                                       beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
 
-                                       beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
-                                       oldLinkedChild.NextLinkedSibling = null;
-                                }
+                               if (Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, oldLinkedChild) == false)
+                                       throw new ArgumentException ();
 
-                               ownerDoc.onNodeRemoved (oldChild, oldChild.ParentNode);
-                               oldChild.parentNode = null;     // clear parent 'after' above logic.
+                               beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
 
-                               return oldChild;
-                       } 
-                       else
-                               throw new ArgumentException (String.Format ("This {0} node cannot remove child.", NodeType));
+                               // Each derived class may have its own LastLinkedChild, so we must set it explicitly.
+                               if (oldLinkedChild.NextLinkedSibling == firstChild)
+                                       this.LastLinkedChild = beforeLinkedChild;
+
+                               oldLinkedChild.NextLinkedSibling = null;
+                               }
+
+                       ownerDoc.onNodeRemoved (oldChild, oldChild.ParentNode);
+                       oldChild.parentNode = null;     // clear parent 'after' above logic.
+
+                       return oldChild;
                }
 
                public virtual XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
index 99aaeff648e500b08aff484e961179d84b1ee156..681aabb0c40b3680515125a750888b9c876f5c49 100755 (executable)
@@ -153,12 +153,12 @@ namespace System.Xml
                                if (current == null)
                                        return null;
 
-                               string ret =  current.Attributes [name].Value;
+                               XmlAttribute attr = current.Attributes [name];
                                
-                               if (ret == null)
+                               if (attr == null)
                                        return String.Empty;
                                else
-                                       return ret;
+                                       return attr.Value;
                        }
                }
 
@@ -167,12 +167,12 @@ namespace System.Xml
                                if (current == null)
                                        return null;
 
-                               string ret =  current.Attributes [name, namespaceURI].Value;
+                               XmlAttribute attr = current.Attributes [name, namespaceURI];
                                
-                               if (ret == null)
+                               if (attr == null)
                                        return String.Empty;
                                else
-                                       return ret;
+                                       return attr.Value;
                        }
                }
 
index 390c4920150c38904be6cb0ef3fa5a407ac70148..fcd5d516e0cae222d2a67d47439892d4658bfbe2 100644 (file)
@@ -39,6 +39,11 @@ namespace Mono.Xml.Native
                #region Public Methods\r
                // Read the next character and compare it against the
                // specified character.
+               public void Close ()
+               {
+                       this.reader.Close ();
+               }
+
                public void Expect (int expected)
                {
                        int ch = ReadChar ();
index abd0be49991915adc4b09c3d8ca6edf4674f4940..826b48d0959fd2d3e67fa38988eafc165536cd3a 100644 (file)
@@ -188,11 +188,21 @@ namespace System.Xml
 
                public virtual XmlNodeType MoveToContent ()
                {
+                       XmlNodeType nodeType = NodeType;
+                       if (IsEmptyElement)
+                               return nodeType;
+
+                       if (nodeType == XmlNodeType.Attribute) {
+                               MoveToElement ();
+                               return nodeType;
+                       }
+
                        do {
-                               XmlNodeType nodeType = NodeType;
+                               Read ();
+                               nodeType = NodeType;
                                if (IsContent (nodeType))
                                        return nodeType;
-                       } while (Read ());
+                       } while (ReadState != ReadState.EndOfFile);
                        
                        return XmlNodeType.None;
                }
index f76f1bba3336a7d2845c27ffb1ad3760edfbb08c..ff2eee8ca8966de401cef91d0df69dde91350c19 100644 (file)
@@ -304,6 +304,9 @@ namespace System.Xml
                public override void Close ()
                {
                        readState = ReadState.Closed;
+                       foreach (XmlParserInput input in parserInputStack.ToArray ())
+                               input.Close ();
+                       this.currentInput.Close ();
                }
 
                public override string GetAttribute (int i)