//
// System.Xml.XmlDocument
//
-// Author:
+// Authors:
// Daniel Weber (daniel-weber@austin.rr.com)
+// Kral Ferch <kral_ferch@hotmail.com>
+// Jason Diamond <jason@injektilo.org>
+// Miguel de Icaza (miguel@ximian.com)
+// Duncan Mak (duncan@ximian.com)
+// Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
//
// (C) 2001 Daniel Weber
+// (C) 2002 Kral Ferch, Jason Diamond, Miguel de Icaza, Duncan Mak,
+// Atsushi Enomoto
+//
using System;
using System.IO;
+using System.Text;
using System.Xml.XPath;
+using System.Diagnostics;
+using System.Collections;
+using Mono.Xml;
+using Mono.Xml.Native;
namespace System.Xml
{
- public delegate void XmlNodeChangedEventHandler (XmlNodeChangedEventArgs args);
-
public class XmlDocument : XmlNode
{
+ #region Fields
+
+ XmlLinkedNode lastLinkedChild;
+ XmlNameTable nameTable;
+ string baseURI = String.Empty;
+ XmlImplementation implementation;
+ bool preserveWhitespace = false;
+ XmlResolver resolver;
+ Hashtable idTable = new Hashtable ();
+
+ #endregion
+
#region Constructors
- public XmlDocument () : base (null)
+ public XmlDocument () : this (null, null)
{
- FOwnerDocument = this;
}
- [MonoTODO]
- protected internal XmlDocument (XmlImplementation imp) : base (null)
+ protected internal XmlDocument (XmlImplementation imp) : this (imp, null)
{
- throw new NotImplementedException ();
}
- [MonoTODO]
- public XmlDocument (NameTable nt) : base (null)
+ public XmlDocument (XmlNameTable nt) : this (null, nt)
{
- throw new NotImplementedException ();
}
+ XmlDocument (XmlImplementation impl, XmlNameTable nt) : base (null)
+ {
+ implementation = (impl != null) ? impl : new XmlImplementation ();
+ nameTable = (nt != null) ? nt : implementation.internalNameTable;
+ AddDefaultNameTableKeys ();
+ resolver = new XmlUrlResolver ();
+ }
#endregion
#region Events
#region Properties
- [MonoTODO]
public override string BaseURI {
get {
- throw new NotImplementedException();
+ return baseURI;
}
}
}
}
- [MonoTODO]
public virtual XmlDocumentType DocumentType {
get {
- throw new NotImplementedException();
+ foreach(XmlNode n in this.ChildNodes) {
+ if(n.NodeType == XmlNodeType.DocumentType)
+ return (XmlDocumentType)n;
+ }
+ return null;
}
}
- [MonoTODO]
public XmlImplementation Implementation {
- get {
- throw new NotImplementedException();
- }
+ get { return implementation; }
}
- [MonoTODO]
public override string InnerXml {
get {
- throw new NotImplementedException();
+ return base.InnerXml;
}
-
- set {
- throw new NotImplementedException();
+ set { // reason for overriding
+ this.LoadXml (value);
}
}
public override bool IsReadOnly {
- get {
- return false;
+ get { return false; }
+ }
+
+ internal bool IsStandalone {
+ get {
+ return FirstChild != null &&
+ FirstChild.NodeType == XmlNodeType.XmlDeclaration &&
+ ((XmlDeclaration) this.FirstChild).Standalone == "yes";
}
}
- public override string LocalName {
- get {
- return "#document";
+ internal override XmlLinkedNode LastLinkedChild {
+ get {
+ return lastLinkedChild;
}
+
+ set {
+ lastLinkedChild = value;
+ }
+ }
+
+ public override string LocalName {
+ get { return "#document"; }
}
public override string Name {
- get {
- return "#document";
- }
+ get { return "#document"; }
}
- [MonoTODO]
public XmlNameTable NameTable {
- get {
- throw new NotImplementedException();
- }
+ get { return nameTable; }
}
public override XmlNodeType NodeType {
- get {
- return XmlNodeType.Document;
+ get { return XmlNodeType.Document; }
+ }
+
+ internal override XPathNodeType XPathNodeType {
+ get {
+ return XPathNodeType.Root;
}
}
public override XmlDocument OwnerDocument {
- get {
- return null;
- }
+ get { return null; }
}
- [MonoTODO]
public bool PreserveWhitespace {
- get {
- throw new NotImplementedException();
- }
+ get { return preserveWhitespace; }
+ set { preserveWhitespace = value; }
+ }
- set {
- throw new NotImplementedException();
- }
+ internal XmlResolver Resolver {
+ get { return resolver; }
}
- [MonoTODO]
- public XmlResolver XmlResolver {
- set {
- throw new NotImplementedException();
+ internal override string XmlLang {
+ get { return String.Empty; }
+ }
+
+ public virtual XmlResolver XmlResolver {
+ set { resolver = value; }
+ }
+
+ internal override XmlSpace XmlSpace {
+ get {
+ return XmlSpace.None;
+ }
+ }
+
+ internal Encoding TextEncoding {
+ get {
+ XmlDeclaration dec = FirstChild as XmlDeclaration;
+
+ if (dec == null || dec.Encoding == "")
+ return null;
+
+ return Encoding.GetEncoding (dec.Encoding);
}
}
#endregion
#region Methods
+ internal void AddIdenticalAttribute (XmlAttribute attr)
+ {
+ idTable [attr.Value] = attr;
+ }
- [MonoTODO]
public override XmlNode CloneNode (bool deep)
{
- throw new NotImplementedException ();
+ XmlDocument doc = implementation.CreateDocument ();
+ doc.baseURI = baseURI;
+
+ if(deep)
+ {
+ foreach(XmlNode n in ChildNodes)
+ doc.AppendChild (doc.ImportNode (n, deep));
+ }
+ return doc;
}
- [MonoTODO]
public XmlAttribute CreateAttribute (string name)
{
- int indexOfColon = name.IndexOf (':');
-
- if (indexOfColon == -1)
- return CreateAttribute (String.Empty, name, String.Empty);
+ string prefix;
+ string localName;
+ string namespaceURI = String.Empty;
- string prefix = name.Substring (0, indexOfColon);
- string localName = name.Substring (indexOfColon + 1);
+ ParseName (name, out prefix, out localName);
- return CreateAttribute (prefix, localName, String.Empty);
+ if (prefix == "xmlns" || (prefix == "" && localName == "xmlns"))
+ namespaceURI = XmlNamespaceManager.XmlnsXmlns;
+ else if (prefix == "xml")
+ namespaceURI = XmlNamespaceManager.XmlnsXml;
+
+ return CreateAttribute (prefix, localName, namespaceURI );
}
- [MonoTODO]
public XmlAttribute CreateAttribute (string qualifiedName, string namespaceURI)
{
- int indexOfColon = qualifiedName.IndexOf (':');
-
- if (indexOfColon == -1)
- return CreateAttribute (String.Empty, qualifiedName, String.Empty);
+ string prefix;
+ string localName;
- string prefix = qualifiedName.Substring (0, indexOfColon);
- string localName = qualifiedName.Substring (indexOfColon + 1);
+ ParseName (qualifiedName, out prefix, out localName);
- return CreateAttribute (prefix, localName, String.Empty);
+ return CreateAttribute (prefix, localName, namespaceURI);
}
- [MonoTODO]
public virtual XmlAttribute CreateAttribute (string prefix, string localName, string namespaceURI)
{
+ if ((localName == null) || (localName == String.Empty))
+ throw new ArgumentException ("The attribute local name cannot be empty.");
+
return new XmlAttribute (prefix, localName, namespaceURI, this);
}
public virtual XmlComment CreateComment (string data)
{
- return new XmlComment(data, this);
+ return new XmlComment (data, this);
}
- [MonoTODO]
protected internal virtual XmlAttribute CreateDefaultAttribute (string prefix, string localName, string namespaceURI)
{
- throw new NotImplementedException ();
+ XmlAttribute attr = CreateAttribute (prefix, localName, namespaceURI);
+ attr.isDefault = true;
+ return attr;
}
- [MonoTODO]
public virtual XmlDocumentFragment CreateDocumentFragment ()
{
- throw new NotImplementedException ();
+ return new XmlDocumentFragment (this);
}
- [MonoTODO]
- public virtual XmlDocumentType CreateDocumentType (
- string name,
- string publicId,
- string systemId,
- string internalSubset)
+ public virtual XmlDocumentType CreateDocumentType (string name, string publicId,
+ string systemId, string internalSubset)
{
- throw new NotImplementedException ();
+ return new XmlDocumentType (name, publicId, systemId, internalSubset, this);
}
- public XmlElement CreateElement (string name)
+ private XmlDocumentType CreateDocumentType (DTDObjectModel dtd)
{
- int indexOfColon = name.IndexOf (':');
-
- if (indexOfColon == -1)
- return CreateElement (String.Empty, name, String.Empty);
-
- string prefix = name.Substring (0, indexOfColon);
- string localName = name.Substring (indexOfColon + 1);
+ return new XmlDocumentType (dtd, this);
+ }
- return CreateElement (prefix, localName, String.Empty);
+ public XmlElement CreateElement (string name)
+ {
+ return CreateElement (name, String.Empty);
}
- [MonoTODO]
public XmlElement CreateElement (
string qualifiedName,
string namespaceURI)
{
- int indexOfColon = qualifiedName.IndexOf (':');
-
- if (indexOfColon == -1)
- return CreateElement (String.Empty, qualifiedName, namespaceURI);
-
- string prefix = qualifiedName.Substring (0, indexOfColon);
- string localName = qualifiedName.Substring (indexOfColon + 1);
+ string prefix;
+ string localName;
+ ParseName (qualifiedName, out prefix, out localName);
+
return CreateElement (prefix, localName, namespaceURI);
}
string localName,
string namespaceURI)
{
- return new XmlElement (prefix, localName, namespaceURI, this);
+ if ((localName == null) || (localName == String.Empty))
+ throw new ArgumentException ("The local name for elements or attributes cannot be null or an empty string.");
+ // FIXME: MS.NET has a weird behavior that they can Load() from XmlTextReader
+ // whose Namespaces = false, but their CreateElement() never allows qualified name.
+ // I leave it as it is.
+ if (!XmlChar.IsName (localName))
+ throw new ArgumentException ("Invalid name.", "localName");
+ return new XmlElement (prefix != null ? prefix : String.Empty, localName, namespaceURI != null ? namespaceURI : String.Empty, this);
}
- [MonoTODO]
public virtual XmlEntityReference CreateEntityReference (string name)
{
- throw new NotImplementedException ();
+ return new XmlEntityReference (name, this);
}
- [MonoTODO]
protected internal virtual XPathNavigator CreateNavigator (XmlNode node)
{
- throw new NotImplementedException ();
+ return new XmlDocumentNavigator (node);
}
- [MonoTODO]
public virtual XmlNode CreateNode (
string nodeTypeString,
string name,
string namespaceURI)
{
- throw new NotImplementedException ();
+ return CreateNode (GetNodeTypeFromString (nodeTypeString), name, namespaceURI);
}
- [MonoTODO]
public virtual XmlNode CreateNode (
XmlNodeType type,
string name,
string namespaceURI)
{
- throw new NotImplementedException ();
+ string prefix = null;
+ string localName = name;
+
+ if ((type == XmlNodeType.Attribute) || (type == XmlNodeType.Element) || (type == XmlNodeType.EntityReference))
+ ParseName (name, out prefix, out localName);
+
+ return CreateNode (type, prefix, localName, namespaceURI);
}
- [MonoTODO]
public virtual XmlNode CreateNode (
XmlNodeType type,
string prefix,
string name,
string namespaceURI)
{
- throw new NotImplementedException ();
+ switch (type) {
+ case XmlNodeType.Attribute: return CreateAttribute (prefix, name, namespaceURI);
+ case XmlNodeType.CDATA: return CreateCDataSection (null);
+ case XmlNodeType.Comment: return CreateComment (null);
+ case XmlNodeType.Document: return new XmlDocument ();
+ case XmlNodeType.DocumentFragment: return CreateDocumentFragment ();
+ case XmlNodeType.DocumentType: return CreateDocumentType (null, null, null, null);
+ case XmlNodeType.Element: return CreateElement (prefix, name, namespaceURI);
+ case XmlNodeType.EntityReference: return CreateEntityReference (null);
+ case XmlNodeType.ProcessingInstruction: return CreateProcessingInstruction (null, null);
+ case XmlNodeType.SignificantWhitespace: return CreateSignificantWhitespace (String.Empty);
+ case XmlNodeType.Text: return CreateTextNode (null);
+ case XmlNodeType.Whitespace: return CreateWhitespace (String.Empty);
+ case XmlNodeType.XmlDeclaration: return CreateXmlDeclaration ("1.0", null, null);
+ default: throw new ArgumentOutOfRangeException(String.Format("{0}\nParameter name: {1}",
+ "Specified argument was out of the range of valid values", type.ToString ()));
+ }
}
public virtual XmlProcessingInstruction CreateProcessingInstruction (
return new XmlProcessingInstruction (target, data, this);
}
- [MonoTODO]
public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
{
- throw new NotImplementedException ();
+ foreach (char c in text)
+ if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
+ throw new ArgumentException ("Invalid whitespace characters.");
+
+ return new XmlSignificantWhitespace (text, this);
}
public virtual XmlText CreateTextNode (string text)
return new XmlText (text, this);
}
- [MonoTODO]
public virtual XmlWhitespace CreateWhitespace (string text)
{
- throw new NotImplementedException ();
+ foreach (char c in text)
+ if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
+ throw new ArgumentException ("Invalid whitespace characters.");
+
+ return new XmlWhitespace (text, this);
}
- [MonoTODO]
- public virtual XmlDeclaration CreateXmlDeclaration (
- string version,
- string encoding,
- string standalone)
+ public virtual XmlDeclaration CreateXmlDeclaration (string version, string encoding,
+ string standalone)
{
- throw new NotImplementedException();
+ if (version != "1.0")
+ throw new ArgumentException ("version string is not correct.");
+
+ if ((standalone != null && standalone != String.Empty) && !((standalone == "yes") || (standalone == "no")))
+ throw new ArgumentException ("standalone string is not correct.");
+
+ return new XmlDeclaration (version, encoding, standalone, this);
}
[MonoTODO]
+ // FIXME: Currently XmlAttributeCollection.SetNamedItem() does
+ // add to the identity table, but in fact I delayed identity
+ // check on GetIdenticalAttribute. To make such way complete,
+ // we have to use MultiMap, not Hashtable.
public virtual XmlElement GetElementById (string elementId)
{
- throw new NotImplementedException ();
+ XmlAttribute attr = GetIdenticalAttribute (elementId);
+ return attr != null ? attr.OwnerElement : null;
}
- [MonoTODO]
public virtual XmlNodeList GetElementsByTagName (string name)
{
- throw new NotImplementedException ();
+ ArrayList nodeArrayList = new ArrayList ();
+ this.searchNodesRecursively (this, name, nodeArrayList);
+ return new XmlNodeArrayList (nodeArrayList);
+ }
+
+ private void searchNodesRecursively (XmlNode argNode, string argName,
+ ArrayList argArrayList)
+ {
+ XmlNodeList xmlNodeList = argNode.ChildNodes;
+ foreach (XmlNode node in xmlNodeList){
+ if (node.Name.Equals (argName))
+ argArrayList.Add (node);
+ else
+ this.searchNodesRecursively (node, argName, argArrayList);
+ }
+ }
+
+ private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI,
+ ArrayList argArrayList)
+ {
+ XmlNodeList xmlNodeList = argNode.ChildNodes;
+ foreach (XmlNode node in xmlNodeList){
+ if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
+ argArrayList.Add (node);
+ else
+ this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
+ }
}
- [MonoTODO]
public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
{
- throw new NotImplementedException();
+ ArrayList nodeArrayList = new ArrayList ();
+ this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
+ return new XmlNodeArrayList (nodeArrayList);
+ }
+
+ private XmlNodeType GetNodeTypeFromString (string nodeTypeString)
+ {
+ switch (nodeTypeString) {
+ case "attribute": return XmlNodeType.Attribute;
+ case "cdatasection": return XmlNodeType.CDATA;
+ case "comment": return XmlNodeType.Comment;
+ case "document": return XmlNodeType.Document;
+ case "documentfragment": return XmlNodeType.DocumentFragment;
+ case "documenttype": return XmlNodeType.DocumentType;
+ case "element": return XmlNodeType.Element;
+ case "entityreference": return XmlNodeType.EntityReference;
+ case "processinginstruction": return XmlNodeType.ProcessingInstruction;
+ case "significantwhitespace": return XmlNodeType.SignificantWhitespace;
+ case "text": return XmlNodeType.Text;
+ case "whitespace": return XmlNodeType.Whitespace;
+ default:
+ throw new ArgumentException(String.Format("The string doesn't represent any node type : {0}.", nodeTypeString));
+ }
+ }
+
+ internal XmlAttribute GetIdenticalAttribute (string id)
+ {
+ XmlAttribute attr = this.idTable [id] as XmlAttribute;
+ if (attr == null)
+ return null;
+ if (attr.OwnerElement == null || !attr.OwnerElement.IsRooted) {
+// idTable.Remove (id);
+ return null;
+ }
+ return attr;
}
- [MonoTODO]
public virtual XmlNode ImportNode (XmlNode node, bool deep)
{
- throw new NotImplementedException ();
+ switch (node.NodeType) {
+ case XmlNodeType.Attribute:
+ XmlAttribute srcAtt = node as XmlAttribute;
+ XmlAttribute dstAtt = this.CreateAttribute (srcAtt.Prefix, srcAtt.LocalName, srcAtt.NamespaceURI);
+ foreach (XmlNode child in srcAtt.ChildNodes)
+ dstAtt.AppendChild (this.ImportNode (child, deep));
+ return dstAtt;
+
+ case XmlNodeType.CDATA:
+ return this.CreateCDataSection (node.Value);
+
+ case XmlNodeType.Comment:
+ return this.CreateComment (node.Value);
+
+ case XmlNodeType.Document:
+ throw new XmlException ("Document cannot be imported.");
+
+ case XmlNodeType.DocumentFragment:
+ XmlDocumentFragment df = this.CreateDocumentFragment ();
+ if(deep)
+ foreach(XmlNode n in node.ChildNodes)
+ df.AppendChild (this.ImportNode (n, deep));
+ return df;
+
+ case XmlNodeType.DocumentType:
+ throw new XmlException ("DocumentType cannot be imported.");
+
+ case XmlNodeType.Element:
+ XmlElement src = (XmlElement)node;
+ XmlElement dst = this.CreateElement (src.Prefix, src.LocalName, src.NamespaceURI);
+ foreach(XmlAttribute attr in src.Attributes)
+ if(attr.Specified) // copies only specified attributes
+ dst.SetAttributeNode ((XmlAttribute)this.ImportNode (attr, deep));
+ if(deep)
+ foreach(XmlNode n in src.ChildNodes)
+ dst.AppendChild (this.ImportNode (n, deep));
+ return dst;
+
+ case XmlNodeType.EndElement:
+ throw new XmlException ("Illegal ImportNode call for NodeType.EndElement");
+ case XmlNodeType.EndEntity:
+ throw new XmlException ("Illegal ImportNode call for NodeType.EndEntity");
+
+ case XmlNodeType.EntityReference:
+ return this.CreateEntityReference (node.Name);
+
+ case XmlNodeType.None:
+ throw new XmlException ("Illegal ImportNode call for NodeType.None");
+
+ case XmlNodeType.ProcessingInstruction:
+ XmlProcessingInstruction pi = node as XmlProcessingInstruction;
+ return this.CreateProcessingInstruction (pi.Target, pi.Data);
+
+ case XmlNodeType.SignificantWhitespace:
+ return this.CreateSignificantWhitespace (node.Value);
+
+ case XmlNodeType.Text:
+ return this.CreateTextNode (node.Value);
+
+ case XmlNodeType.Whitespace:
+ return this.CreateWhitespace (node.Value);
+
+ case XmlNodeType.XmlDeclaration:
+ XmlDeclaration srcDecl = node as XmlDeclaration;
+ return this.CreateXmlDeclaration (srcDecl.Version, srcDecl.Encoding, srcDecl.Standalone);
+
+ default:
+ throw new InvalidOperationException ("Cannot import specified node type: " + node.NodeType);
+ }
}
- [MonoTODO]
public virtual void Load (Stream inStream)
{
- throw new NotImplementedException ();
+ Load (new XmlTextReader (inStream));
}
public virtual void Load (string filename)
{
- XmlReader xmlReader = new XmlTextReader (new StreamReader (filename));
- Load (xmlReader);
+ XmlTextReader xr = new XmlTextReader (filename);
+ xr.XmlResolver = resolver;
+ Load (xr);
+ xr.Close ();
}
- [MonoTODO]
public virtual void Load (TextReader txtReader)
{
- throw new NotImplementedException ();
+ XmlTextReader xr = new XmlTextReader (txtReader);
+ xr.XmlResolver = resolver;
+ Load (xr);
}
public virtual void Load (XmlReader xmlReader)
// like properties we have, etc.
RemoveAll ();
- XmlNode currentNode = this;
+ this.baseURI = xmlReader.BaseURI;
+ // create all contents with use of ReadNode()
+ do {
+ XmlNode n = ReadNode (xmlReader);
+ if(n == null) break;
+ AppendChild (n);
+ } while (true);
+ }
- while (xmlReader.Read ())
- {
- switch (xmlReader.NodeType) {
+ public virtual void LoadXml (string xml)
+ {
+ XmlTextReader xmlReader = new XmlTextReader (
+ xml, XmlNodeType.Document, null);
+ xmlReader.XmlResolver = resolver;
+ Load (xmlReader);
+ }
+
+ internal void onNodeChanged (XmlNode node, XmlNode Parent)
+ {
+ if (NodeChanged != null)
+ NodeChanged (node, new XmlNodeChangedEventArgs
+ (XmlNodeChangedAction.Change,
+ node, Parent, Parent));
+ }
+
+ internal void onNodeChanging(XmlNode node, XmlNode Parent)
+ {
+ if (NodeChanging != null)
+ NodeChanging (node, new XmlNodeChangedEventArgs
+ (XmlNodeChangedAction.Change,
+ node, Parent, Parent));
+ }
+
+ internal void onNodeInserted (XmlNode node, XmlNode newParent)
+ {
+ if (NodeInserted != null)
+ NodeInserted (node, new XmlNodeChangedEventArgs
+ (XmlNodeChangedAction.Insert,
+ node, null, newParent));
+ }
+
+ internal void onNodeInserting (XmlNode node, XmlNode newParent)
+ {
+ if (NodeInserting != null)
+ NodeInserting (node, new XmlNodeChangedEventArgs
+ (XmlNodeChangedAction.Insert,
+ node, null, newParent));
+ }
+
+ internal void onNodeRemoved (XmlNode node, XmlNode oldParent)
+ {
+ if (NodeRemoved != null)
+ NodeRemoved (node, new XmlNodeChangedEventArgs
+ (XmlNodeChangedAction.Remove,
+ node, oldParent, null));
+ }
+
+ internal void onNodeRemoving (XmlNode node, XmlNode oldParent)
+ {
+ if (NodeRemoving != null)
+ NodeRemoving (node, new XmlNodeChangedEventArgs
+ (XmlNodeChangedAction.Remove,
+ node, oldParent, null));
+ }
+
+ private void ParseName (string name, out string prefix, out string localName)
+ {
+ int indexOfColon = name.IndexOf (':');
+
+ if (indexOfColon != -1) {
+ prefix = name.Substring (0, indexOfColon);
+ localName = name.Substring (indexOfColon + 1);
+ } else {
+ prefix = "";
+ localName = name;
+ }
+ }
+
+ // Reads XmlReader and creates Attribute Node.
+ private XmlAttribute ReadAttributeNode(XmlReader reader)
+ {
+ if(reader.NodeType == XmlNodeType.Element)
+ reader.MoveToFirstAttribute ();
+ else if(reader.NodeType != XmlNodeType.Attribute)
+ throw new InvalidOperationException (MakeReaderErrorMessage ("bad position to read attribute.", reader));
+ XmlAttribute attribute = CreateAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
+ ReadAttributeNodeValue (reader, attribute);
+
+ // Keep the current reader position
+ bool res;
+ if (attribute.NamespaceURI == string.Empty || attribute.NamespaceURI == null)
+ res = reader.MoveToAttribute (attribute.Name);
+ else
+ res = reader.MoveToAttribute (attribute.LocalName, attribute.NamespaceURI);
+ if (reader.IsDefault)
+ attribute.SetDefault ();
+ return attribute;
+ }
+
+ // Reads attribute from XmlReader and then creates attribute value children. XmlAttribute also uses this.
+ internal void ReadAttributeNodeValue(XmlReader reader, XmlAttribute attribute)
+ {
+ while(reader.ReadAttributeValue ()) {
+ if(reader.NodeType == XmlNodeType.EntityReference)
+ // FIXME: if DocumentType is available, then try to resolve it.
+ attribute.AppendChild (CreateEntityReference (reader.Name));
+ // FIXME: else if(NodeType == EndEntity) -- reset BaseURI and so on -- ;
+ else
+ // Children of Attribute is restricted to CharacterData and EntityReference (Comment is not allowed).
+ attribute.AppendChild (CreateTextNode (reader.Value));
+ }
+ }
+
+ public virtual XmlNode ReadNode (XmlReader reader)
+ {
+ XmlNode resultNode = null;
+ XmlNode newNode = null;
+ XmlNode currentNode = null;
+
+ switch (reader.ReadState) {
+ case ReadState.Interactive:
+ break;
+ case ReadState.Initial:
+ reader.Read ();
+ break;
+ default:
+ return null;
+ }
+
+ int startDepth = reader.Depth;
+ bool ignoredWhitespace;
+ bool reachedEOF = false;
+
+ do {
+ ignoredWhitespace = false;
+ if (reader.ReadState != ReadState.Interactive)
+ if (reachedEOF)
+ throw new Exception ("XML Reader reached to end while reading node.");
+ else
+ reachedEOF = true;
+ switch (reader.NodeType) {
+
+ case XmlNodeType.Attribute:
+ newNode = ReadAttributeNode (reader);
+ break;
case XmlNodeType.CDATA:
- XmlCDataSection cdataSection = CreateCDataSection(xmlReader.Value);
- currentNode.AppendChild (cdataSection);
+ newNode = CreateCDataSection (reader.Value);
+ if(currentNode != null)
+ currentNode.AppendChild (newNode);
break;
case XmlNodeType.Comment:
- XmlComment comment = CreateComment (xmlReader.Value);
- currentNode.AppendChild (comment);
+ newNode = CreateComment (reader.Value);
+ if(currentNode != null)
+ currentNode.AppendChild (newNode);
break;
case XmlNodeType.Element:
- XmlElement element = CreateElement (xmlReader.Name, xmlReader.LocalName, xmlReader.NamespaceURI);
- currentNode.AppendChild (element);
+ XmlElement element = CreateElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
+ element.IsEmpty = reader.IsEmptyElement;
+ if(currentNode != null)
+ currentNode.AppendChild (element);
+ else
+ resultNode = element;
// set the element's attributes.
- while (xmlReader.MoveToNextAttribute ())
- element.SetAttribute (xmlReader.Name, xmlReader.Value);
+ while (reader.MoveToNextAttribute ()) {
+ element.SetAttributeNode (ReadAttributeNode (reader));
+ }
+
+ reader.MoveToElement ();
- // if this element isn't empty, push it onto our "stack".
- if (!xmlReader.IsEmptyElement)
+ if (!reader.IsEmptyElement)
currentNode = element;
break;
case XmlNodeType.EndElement:
+ if (currentNode == null)
+ throw new XmlException ("Unexpected end element.");
+ else if (currentNode.Name != reader.Name)
+ throw new XmlException (reader as IXmlLineInfo, String.Format ("mismatch end tag. Expected {0} but found {1}", currentNode.Name, reader.Name));
currentNode = currentNode.ParentNode;
break;
+ case XmlNodeType.EndEntity:
+ break; // no operation
+
case XmlNodeType.ProcessingInstruction:
- XmlProcessingInstruction processingInstruction = CreateProcessingInstruction (xmlReader.Name, xmlReader.Value);
- // Where does a processing instruction go in the doc?
- // I think we need to just hold on to them in an internal array in doc.
+ newNode = CreateProcessingInstruction (reader.Name, reader.Value);
+ if(currentNode != null)
+ currentNode.AppendChild (newNode);
break;
case XmlNodeType.Text:
- XmlText text = CreateTextNode (xmlReader.Value);
- currentNode.AppendChild (text);
+ newNode = CreateTextNode (reader.Value);
+ if(currentNode != null)
+ currentNode.AppendChild (newNode);
break;
- }
- }
- }
- public virtual void LoadXml (string xml)
- {
- XmlReader xmlReader = new XmlTextReader (new StringReader (xml));
- Load (xmlReader);
- }
+ case XmlNodeType.XmlDeclaration:
+ // empty strings are dummy, then gives over setting value contents to setter.
+ newNode = CreateXmlDeclaration ("1.0" , String.Empty, String.Empty);
+ ((XmlDeclaration)newNode).Value = reader.Value;
+ if(currentNode != null)
+ throw new XmlException (reader as IXmlLineInfo, "XmlDeclaration at invalid position.");
+ break;
- [MonoTODO]
- public virtual XmlNode ReadNode(XmlReader reader)
- {
- throw new NotImplementedException ();
- }
+ case XmlNodeType.DocumentType:
+ if(currentNode != null)
+ throw new XmlException (reader as IXmlLineInfo, "XmlDocumentType at invalid position.");
+
+ DTDObjectModel dtd = null;
+ XmlTextReader xtReader = reader as XmlTextReader;
+ if (xtReader != null)
+ dtd = xtReader.DTD;
+ XmlNodeReader xnReader = reader as XmlNodeReader;
+ if (xnReader != null)
+ dtd = xnReader.GetInternalParserContext ().Dtd;
+ XmlValidatingReader xvReader = reader as XmlValidatingReader;
+ if (xvReader != null)
+ dtd = xvReader.GetInternalParserContext ().Dtd;
+ IHasXmlParserContext ctxReader = reader as IHasXmlParserContext;
+ if (ctxReader != null)
+ dtd = ctxReader.ParserContext.Dtd;
+
+ if (dtd != null)
+ newNode = CreateDocumentType (dtd);
+ else
+ newNode = CreateDocumentType (reader.Name, reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
- [MonoTODO]
- public virtual void Save(Stream outStream)
- {
- throw new NotImplementedException ();
- }
+ break;
- [MonoTODO]
- public virtual void Save (string filename)
- {
- throw new NotImplementedException ();
- }
+ case XmlNodeType.EntityReference:
+ newNode = CreateEntityReference (reader.Name);
+ if(currentNode != null)
+ currentNode.AppendChild (newNode);
+ break;
- [MonoTODO]
- public virtual void Save (TextWriter writer)
- {
- throw new NotImplementedException ();
+ case XmlNodeType.SignificantWhitespace:
+ newNode = CreateSignificantWhitespace (reader.Value);
+ if(currentNode != null)
+ currentNode.AppendChild (newNode);
+ break;
+
+ case XmlNodeType.Whitespace:
+ if(PreserveWhitespace) {
+ newNode = CreateWhitespace (reader.Value);
+ if(currentNode != null)
+ currentNode.AppendChild (newNode);
+ }
+ else
+ ignoredWhitespace = true;
+ break;
+ }
+ // Read next, except for reading attribute node.
+ if (!(newNode is XmlAttribute) && !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;
}
- [MonoTODO]
- public virtual void Save (XmlWriter writer)
+ private string MakeReaderErrorMessage (string message, XmlReader reader)
{
- throw new NotImplementedException ();
+ IXmlLineInfo li = reader as IXmlLineInfo;
+ if (li != null)
+ return String.Format ("{0} Line number = {1}, Inline position = {2}.", message, li.LineNumber, li.LinePosition);
+ else
+ return message;
}
- [MonoTODO]
- public override void WriteContentTo (XmlWriter xw)
+ internal void RemoveIdenticalAttribute (string id)
{
- throw new NotImplementedException ();
+ idTable.Remove (id);
}
- [MonoTODO]
- public override void WriteTo (XmlWriter w)
+ public virtual void Save(Stream outStream)
{
- throw new NotImplementedException ();
+ XmlTextWriter xmlWriter = new XmlTextWriter (outStream, TextEncoding);
+ xmlWriter.Formatting = Formatting.Indented;
+ WriteContentTo (xmlWriter);
+ xmlWriter.Flush ();
}
- internal void onNodeChanging(XmlNode node, XmlNode Parent)
+ public virtual void Save (string filename)
{
- if (NodeInserting != null)
- NodeChanging( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Change,
- node, Parent, Parent));
+ XmlTextWriter xmlWriter = new XmlTextWriter (filename, TextEncoding);
+ xmlWriter.Formatting = Formatting.Indented;
+ WriteContentTo (xmlWriter);
+ xmlWriter.Close ();
}
- internal void onNodeChanged(XmlNode node, XmlNode Parent)
+ public virtual void Save (TextWriter writer)
{
- if (NodeChanged != null)
- NodeInserted( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Change,
- node, Parent, Parent));
+ XmlTextWriter xmlWriter = new XmlTextWriter (writer);
+ xmlWriter.Formatting = Formatting.Indented;
+ WriteContentTo (xmlWriter);
+ xmlWriter.Flush ();
}
- internal void onNodeInserting(XmlNode node, XmlNode newParent)
+ public virtual void Save (XmlWriter xmlWriter)
{
- if (NodeInserting != null)
- NodeInserting( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Insert,
- node, null, newParent));
+ //
+ // This should preserve white space if PreserveWhiteSpace is true
+ //
+ bool autoXmlDecl = FirstChild != null && FirstChild.NodeType != XmlNodeType.XmlDeclaration;
+ if (autoXmlDecl)
+ xmlWriter.WriteStartDocument ();
+ WriteContentTo (xmlWriter);
+ if (autoXmlDecl)
+ xmlWriter.WriteEndDocument ();
+ xmlWriter.Flush ();
}
- internal void onNodeInserted(XmlNode node, XmlNode newParent)
+ public override void WriteContentTo (XmlWriter w)
{
- if (NodeInserted != null)
- NodeInserted( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Insert,
- node, null, newParent));
+ foreach(XmlNode childNode in ChildNodes) {
+ childNode.WriteTo (w);
+ }
}
- internal void onNodeRemoving(XmlNode node, XmlNode oldParent)
+ public override void WriteTo (XmlWriter w)
{
- if (NodeRemoving != null)
- NodeRemoving(new XmlNodeChangedEventArgs(XmlNodeChangedAction.Remove,
- node, oldParent, null));
+ WriteContentTo (w);
}
- internal void onNodeRemoved(XmlNode node, XmlNode oldParent)
+ private void AddDefaultNameTableKeys ()
{
- if (NodeRemoved != null)
- NodeRemoved(new XmlNodeChangedEventArgs(XmlNodeChangedAction.Remove,
- node, oldParent, null));
+ // The following keys are default of MS .NET Framework
+ nameTable.Add ("#text");
+ nameTable.Add ("xml");
+ nameTable.Add ("xmlns");
+ nameTable.Add ("#entity");
+ nameTable.Add ("#document-fragment");
+ nameTable.Add ("#comment");
+ nameTable.Add ("space");
+ nameTable.Add ("id");
+ nameTable.Add ("#whitespace");
+ nameTable.Add ("http://www.w3.org/2000/xmlns/");
+ nameTable.Add ("#cdata-section");
+ nameTable.Add ("lang");
+ nameTable.Add ("#document");
+ nameTable.Add ("#significant-whitespace");
}
-
#endregion
}
}