// Kral Ferch <kral_ferch@hotmail.com>
// Jason Diamond <jason@injektilo.org>
// Miguel de Icaza (miguel@ximian.com)
+// Duncan Mak (duncan@ximian.com)
//
// (C) 2001 Daniel Weber
-// (C) 2002 Kral Ferch, Jason Diamond, Miguel de Icaza
+// (C) 2002 Kral Ferch, Jason Diamond, Miguel de Icaza, Duncan Mak
//
using System;
using System.Text;
using System.Xml.XPath;
using System.Diagnostics;
+using System.Collections;
namespace System.Xml
{
XmlLinkedNode lastLinkedChild;
XmlNameTable nameTable;
+ string baseURI = String.Empty;
#endregion
#region Constructors
- public XmlDocument () : base (null) { }
+ public XmlDocument () : base (null)
+ {
+ System.Xml.NameTable nt = new NameTable();
+ // keys below are default of MS .NET Framework
+ nt.Add("#text");
+ nt.Add("xml");
+ nt.Add("xmlns");
+ nt.Add("#entity");
+ nt.Add("#document-fragment");
+ nt.Add("#comment");
+ nt.Add("space");
+ nt.Add("id");
+ nt.Add("#whitespace");
+ nt.Add("http://www.w3.org/2000/xmlns/");
+ nt.Add("#cdata-section");
+ nt.Add("lang");
+
+ nameTable = nt;
+ }
[MonoTODO]
protected internal XmlDocument (XmlImplementation imp) : base (null)
#region Properties
- [MonoTODO]
public override string BaseURI {
- get { throw new NotImplementedException(); }
+ get {
+ return baseURI;
+ }
}
public XmlElement DocumentElement {
if ((localName == null) || (localName == String.Empty))
throw new ArgumentException ("The local name for elements or attributes cannot be null or an empty string.");
- return new XmlElement (prefix, localName, namespaceURI, this);
+ return new XmlElement (prefix != null ? prefix : String.Empty, localName, namespaceURI != null ? namespaceURI : String.Empty, this);
}
[MonoTODO]
throw new NotImplementedException ();
}
- [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)
[MonoTODO]
public virtual XmlNode ImportNode (XmlNode node, bool deep)
{
- throw new NotImplementedException ();
+ // How to resolve default attribute values?
+ switch(node.NodeType)
+ {
+ case XmlNodeType.Attribute:
+ {
+ XmlAttribute src_att = node as XmlAttribute;
+ XmlAttribute dst_att = this.CreateAttribute(src_att.Prefix, src_att.LocalName, src_att.NamespaceURI);
+ // TODO: resolve default attribute values
+ dst_att.Value = src_att.Value;
+ return dst_att;
+ }
+
+ 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)
+ {
+ // TODO: create default attribute values
+ 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.Entity:
+ throw new NotImplementedException ();
+
+ // [2002.10.14] CreateEntityReference not implemented.
+ case XmlNodeType.EntityReference:
+ throw new NotImplementedException("ImportNode of EntityReference not implemented mainly because CreateEntityReference was implemented in the meantime.");
+// return this.CreateEntityReference(node.Name);
+
+ case XmlNodeType.None:
+ throw new XmlException ("Illegal ImportNode call for NodeType.None");
+ case XmlNodeType.Notation:
+ throw new NotImplementedException ();
+
+ 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);
+
+ // I don't know how to test it...
+ case XmlNodeType.XmlDeclaration:
+ // return this.CreateNode(XmlNodeType.XmlDeclaration, String.Empty, node.Value);
+ throw new NotImplementedException ();
+
+ default:
+ throw new NotImplementedException ();
+ }
}
- [MonoTODO]
public virtual void Load (Stream inStream)
{
- throw new NotImplementedException ();
+ XmlReader xmlReader = new XmlTextReader (inStream);
+ Load (xmlReader);
}
public virtual void Load (string filename)
{
+ baseURI = filename;
XmlReader xmlReader = new XmlTextReader (new StreamReader (filename));
Load (xmlReader);
}
- [MonoTODO]
public virtual void Load (TextReader txtReader)
{
- throw new NotImplementedException ();
+ Load (new XmlTextReader (txtReader));
}
public virtual void Load (XmlReader xmlReader)
XmlNode currentNode = this;
XmlNode newNode;
+#if true
+ this.ConstructDOM(xmlReader, currentNode);
+#else
+ // Below are copied to XmlNode.Construct(currentNode, xmlReader)
while (xmlReader.Read ())
{
switch (xmlReader.NodeType) {
currentNode.AppendChild (element);
// set the element's attributes.
- while (xmlReader.MoveToNextAttribute ())
- element.SetAttribute (xmlReader.Name, xmlReader.Value);
+ while (xmlReader.MoveToNextAttribute ()) {
+ XmlAttribute attribute = CreateAttribute (xmlReader.Prefix, xmlReader.LocalName, xmlReader.NamespaceURI);
+ attribute.Value = xmlReader.Value;
+ element.SetAttributeNode (attribute);
+ }
xmlReader.MoveToElement ();
break;
}
}
+#endif
}
public virtual void LoadXml (string xml)
internal void onNodeChanged (XmlNode node, XmlNode Parent)
{
if (NodeChanged != null)
- NodeInserted (node, new XmlNodeChangedEventArgs
+ NodeChanged (node, new XmlNodeChangedEventArgs
(XmlNodeChangedAction.Change,
node, Parent, Parent));
}
internal void onNodeChanging(XmlNode node, XmlNode Parent)
{
- if (NodeInserting != null)
+ if (NodeChanging != null)
NodeChanging (node, new XmlNodeChangedEventArgs
(XmlNodeChangedAction.Change,
node, Parent, Parent));