+2002-04-05 Kral Ferch <kral_ferch@hotmail.com>
+
+ * XmlAttributes.cs: Added reminder MonoTODO to set NamespaceURI
+ if prefix in constructor is one of the default ones.
+
+ * XmlCharacterData.cs: Returns String.Empty for Value and Data
+ even when constructed with null.
+
+ * XmlDeclaration.cs: Value doesn't put encoding or standalone
+ in if they are empty.
+
+ * XmlDocument.cs: Implemented CreateNode methods and this caused
+ the changes in the other files in this checkin.
+
+ * XmlProcessingInstruction.cs: Returns String.Empty for Value and Data
+ even when constructed with null.
+
+ * XmlWhitespace.cs: Changed Value 'get' to return Data.
+
2002-04-01 Kral Ferch <kral_ferch@hotmail.com>
* XmlTextWriter.cs: Impls for WriteEndDocument and WriteFullEndElement.
#region Constructor
+ [MonoTODO("need to set namespaceURI if prefix is recognized built-in ones like xmlns")]
protected internal XmlAttribute (
string prefix,
string localName,
protected internal XmlCharacterData (string data, XmlDocument doc)
: base (doc)
{
+ if (data == null)
+ data = String.Empty;
+
this.data = data;
}
-//\r
-// System.Xml.XmlDeclaration\r
-//\r
-// Author:\r
-// Duncan Mak (duncan@ximian.com)\r
-//\r
-// (C) Ximian, Inc.\r
-\r
-using System;\r
-using System.Xml;\r
-\r
-namespace System.Xml\r
-{\r
- public class XmlDeclaration : XmlLinkedNode\r
- {\r
- string encoding = "UTF-8"; // defaults to UTF-8\r
- string standAlone;\r
- string version;\r
-\r
- protected internal XmlDeclaration (string version, string encoding,\r
- string standAlone, XmlDocument doc)\r
- : base (doc)\r
- {\r
- this.version = version;\r
- this.encoding = encoding;\r
- this.standAlone = standAlone;\r
- }\r
-\r
- public string Encoding {\r
- get {\r
- if (encoding == null)\r
- return String.Empty;\r
- else\r
- return encoding;\r
- } \r
-\r
- set { encoding = value ; } // Note: MS' doesn't check this string, should we?\r
- }\r
-\r
- public override string InnerText {\r
- get { return Value; }\r
- set { ParseInput (value); }\r
- }\r
- \r
- public override string LocalName {\r
- get { return "xml"; }\r
- }\r
-\r
- public override string Name {\r
- get { return "xml"; }\r
- }\r
-\r
- public override XmlNodeType NodeType {\r
- get { return XmlNodeType.XmlDeclaration; }\r
- }\r
-\r
- public string Standalone {\r
- get {\r
- if (standAlone == null)\r
- return String.Empty;\r
- else\r
- return standAlone;\r
- }\r
-\r
- set {\r
- if (value.ToUpper() == "YES")\r
- standAlone = "yes";\r
- if (value.ToUpper() == "NO")\r
- standAlone = "no";\r
- }\r
- }\r
-\r
- public override string Value {\r
- get { return String.Format ("version=\"{0}\" encoding=\"{1}\" standalone=\"{2}\"",\r
- Version, Encoding, Standalone); }\r
- set { ParseInput (value); }\r
- }\r
-\r
- public string Version {\r
- get { return version; }\r
- }\r
-\r
- public override XmlNode CloneNode (bool deep)\r
- {\r
- return new XmlDeclaration (Version, Encoding, standAlone, OwnerDocument);\r
- }\r
-\r
- public override void WriteContentTo (XmlWriter w)\r
- {\r
- // Nothing to do - no children.\r
- }\r
-\r
- [MonoTODO]\r
- public override void WriteTo (XmlWriter w)\r
- {\r
- if ((Standalone == String.Empty) || (Encoding == String.Empty))\r
- return;\r
- }\r
-\r
- void ParseInput (string input)\r
- { \r
- Encoding = input.Split (new char [] { ' ' }) [1].Split (new char [] { '=' }) [1];\r
- Standalone = input.Split (new char [] { ' ' }) [2].Split (new char [] { '=' }) [1];\r
- }\r
- }\r
-}\r
+//
+// System.Xml.XmlDeclaration
+//
+// Author:
+// Duncan Mak (duncan@ximian.com)
+//
+// (C) Ximian, Inc.
+
+using System;
+using System.Xml;
+
+namespace System.Xml
+{
+ public class XmlDeclaration : XmlLinkedNode
+ {
+ string encoding = "UTF-8"; // defaults to UTF-8
+ string standalone;
+ string version;
+
+ protected internal XmlDeclaration (string version, string encoding,
+ string standalone, XmlDocument doc)
+ : base (doc)
+ {
+ if (encoding == null)
+ encoding = "";
+
+ if (standalone == null)
+ standalone = "";
+
+ this.version = version;
+ this.encoding = encoding;
+ this.standalone = standalone;
+ }
+
+ public string Encoding {
+ get { return encoding; }
+ set { encoding = (value == null) ? String.Empty : value; }
+ }
+
+ public override string InnerText {
+ get { return Value; }
+ set { ParseInput (value); }
+ }
+
+ public override string LocalName {
+ get { return "xml"; }
+ }
+
+ public override string Name {
+ get { return "xml"; }
+ }
+
+ public override XmlNodeType NodeType {
+ get { return XmlNodeType.XmlDeclaration; }
+ }
+
+ public string Standalone {
+ get { return standalone; }
+ set {
+ if (value.ToUpper() == "YES")
+ standalone = "yes";
+ if (value.ToUpper() == "NO")
+ standalone = "no";
+ }
+ }
+
+ public override string Value {
+ get {
+ string formatEncoding = "";
+ string formatStandalone = "";
+
+ if (encoding != String.Empty)
+ formatEncoding = String.Format (" encoding=\"{0}\"", encoding);
+
+ if (standalone != String.Empty)
+ formatStandalone = String.Format (" standalone=\"{0}\"", standalone);
+
+ return String.Format ("version=\"{0}\"{1}{2}", Version, formatEncoding, formatStandalone);
+ }
+ set { ParseInput (value); }
+ }
+
+ public string Version {
+ get { return version; }
+ }
+
+ public override XmlNode CloneNode (bool deep)
+ {
+ return new XmlDeclaration (Version, Encoding, standalone, OwnerDocument);
+ }
+
+ public override void WriteContentTo (XmlWriter w)
+ {
+ // Nothing to do - no children.
+ }
+
+ [MonoTODO]
+ public override void WriteTo (XmlWriter w)
+ {
+ if ((Standalone == String.Empty) || (Encoding == String.Empty))
+ return;
+ }
+
+ void ParseInput (string input)
+ {
+ Encoding = input.Split (new char [] { ' ' }) [1].Split (new char [] { '=' }) [1];
+ Standalone = input.Split (new char [] { ' ' }) [2].Split (new char [] { '=' }) [1];
+ }
+ }
+}
[MonoTODO]
public XmlAttribute CreateAttribute (string name)
{
- int indexOfColon = name.IndexOf (':');
-
- if (indexOfColon == -1)
- return CreateAttribute (String.Empty, name, String.Empty);
-
- string prefix = name.Substring (0, indexOfColon);
- string localName = name.Substring (indexOfColon + 1);
-
- return CreateAttribute (prefix, localName, String.Empty);
+ return CreateAttribute (name, String.Empty);
}
[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);
}
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 XmlElement CreateElement (string name)
{
- 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 CreateElement (prefix, localName, String.Empty);
+ 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;
+ string localName;
- string prefix = qualifiedName.Substring (0, indexOfColon);
- string localName = qualifiedName.Substring (indexOfColon + 1);
+ ParseName (qualifiedName, out prefix, out localName);
return CreateElement (prefix, localName, namespaceURI);
}
string localName,
string namespaceURI)
{
+ 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);
}
throw new NotImplementedException ();
}
- [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 (); // TODO - test to see which constructor to use, i.e. use existing NameTable or not.
+ 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 (
throw new NotImplementedException();
}
+ 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));
+ }
+ }
+
[MonoTODO]
public virtual XmlNode ImportNode (XmlNode node, bool deep)
{
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;
+ }
+ }
+
[MonoTODO]
public virtual XmlNode ReadNode(XmlReader reader)
{
protected internal XmlProcessingInstruction (string target, string data, XmlDocument doc) : base(doc)
{
+ if (data == null)
+ data = String.Empty;
+
this.target = target;
this.data = data;
}
-//\r
-// System.Xml.XmlWhitespace.cs\r
-//\r
-// Author:\r
-// Duncan Mak (duncan@ximian.com)\r
-//\r
-// (C) Ximian, Inc. http://www.ximian.com\r
-//\r
-\r
-using System;\r
-\r
-namespace System.Xml\r
-{\r
- public class XmlWhitespace : XmlCharacterData\r
- {\r
- // Constructor\r
- protected internal XmlWhitespace (string strData, XmlDocument doc)\r
- : base (strData, doc)\r
- {\r
- }\r
- \r
- // Properties\r
- public override string LocalName {\r
- get { return "#whitespace"; }\r
- }\r
-\r
- public override string Name {\r
- get { return "#whitespace"; }\r
- }\r
-\r
- public override XmlNodeType NodeType {\r
- get { return XmlNodeType.Whitespace; }\r
- }\r
-\r
- [MonoTODO]\r
- public override string Value {\r
- get { return null; }\r
- set {}\r
- }\r
-\r
- // Methods\r
- public override XmlNode CloneNode (bool deep)\r
- {\r
- // always return the data value\r
- return new XmlWhitespace (Data, OwnerDocument); \r
- }\r
-\r
- [MonoTODO]\r
- public override void WriteContentTo (XmlWriter w)\r
- { \r
- }\r
-\r
- [MonoTODO]\r
- public override void WriteTo (XmlWriter w)\r
- { \r
- }\r
- }\r
-}\r
+//
+// System.Xml.XmlWhitespace.cs
+//
+// Author:
+// Duncan Mak (duncan@ximian.com)
+//
+// (C) Ximian, Inc. http://www.ximian.com
+//
+
+using System;
+
+namespace System.Xml
+{
+ public class XmlWhitespace : XmlCharacterData
+ {
+ // Constructor
+ protected internal XmlWhitespace (string strData, XmlDocument doc)
+ : base (strData, doc)
+ {
+ }
+
+ // Properties
+ public override string LocalName {
+ get { return "#whitespace"; }
+ }
+
+ public override string Name {
+ get { return "#whitespace"; }
+ }
+
+ public override XmlNodeType NodeType {
+ get { return XmlNodeType.Whitespace; }
+ }
+
+ [MonoTODO]
+ public override string Value {
+ get { return Data; }
+ set {}
+ }
+
+ // Methods
+ public override XmlNode CloneNode (bool deep)
+ {
+ // always return the data value
+ return new XmlWhitespace (Data, OwnerDocument);
+ }
+
+ [MonoTODO]
+ public override void WriteContentTo (XmlWriter w)
+ {
+ }
+
+ [MonoTODO]
+ public override void WriteTo (XmlWriter w)
+ {
+ }
+ }
+}
+2002-04-05 Kral Ferch <kral_ferch@hotmail.com>
+
+ * XmlDocumentTests.cs: CreateNode tests.
+
2002-04-01 Kral Ferch <kral_ferch@hotmail.com>
* XmlTextWriterTests.cs: Tests for WriteEndDocument and WriteFullEndElement.
<Reference
Name = "System.XML"
AssemblyName = "System.XML"
- HintPath = "..\obj\Debug\System.XML.dll"
+ HintPath = "..\..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.0.3705\System.XML.dll"
/>
</References>
</Build>
document = new XmlDocument ();
}
+ public void TestCreateNodeNodeTypeNameEmptyParams ()
+ {
+ XmlNode node;
+
+ try {
+ node = document.CreateNode (null, null, null);
+ Fail ("Expected an ArgumentException to be thrown.");
+ } catch (ArgumentException) {}
+
+ try {
+ node = document.CreateNode ("attribute", null, null);
+ Fail ("Expected a NullReferenceException to be thrown.");
+ } catch (NullReferenceException) {}
+
+ try {
+ node = document.CreateNode ("attribute", "", null);
+ Fail ("Expected an ArgumentException to be thrown.");
+ } catch (ArgumentException) {}
+
+ try {
+ node = document.CreateNode ("element", null, null);
+ Fail ("Expected a NullReferenceException to be thrown.");
+ } catch (NullReferenceException) {}
+
+ try {
+ node = document.CreateNode ("element", "", null);
+ Fail ("Expected an ArgumentException to be thrown.");
+ } catch (ArgumentException) {}
+
+ try {
+ node = document.CreateNode ("entityreference", null, null);
+ Fail ("Expected a NullReferenceException to be thrown.");
+ } catch (NullReferenceException) {}
+ }
+
+ public void TestCreateNodeInvalidXmlNodeType ()
+ {
+ XmlNode node;
+
+ try {
+ node = document.CreateNode (XmlNodeType.EndElement, null, null);
+ Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
+ } catch (ArgumentOutOfRangeException) {}
+
+ try {
+ node = document.CreateNode (XmlNodeType.EndEntity, null, null);
+ Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
+ } catch (ArgumentOutOfRangeException) {}
+
+ try {
+ node = document.CreateNode (XmlNodeType.Entity, null, null);
+ Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
+ } catch (ArgumentOutOfRangeException) {}
+
+ try {
+ node = document.CreateNode (XmlNodeType.None, null, null);
+ Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
+ } catch (ArgumentOutOfRangeException) {}
+
+ try {
+ node = document.CreateNode (XmlNodeType.Notation, null, null);
+ Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
+ } catch (ArgumentOutOfRangeException) {}
+
+ // TODO: undocumented allowable type.
+ node = document.CreateNode (XmlNodeType.XmlDeclaration, null, null);
+ AssertEquals (XmlNodeType.XmlDeclaration, node.NodeType);
+ }
+
+ public void TestCreateNodeWhichParamIsUsed ()
+ {
+ XmlNode node;
+
+ // No constructor params for Document, DocumentFragment.
+
+ node = document.CreateNode (XmlNodeType.CDATA, "a", "b", "c");
+ AssertEquals (String.Empty, ((XmlCDataSection)node).Value);
+
+ node = document.CreateNode (XmlNodeType.Comment, "a", "b", "c");
+ AssertEquals (String.Empty, ((XmlComment)node).Value);
+
+ node = document.CreateNode (XmlNodeType.DocumentType, "a", "b", "c");
+ AssertNull (((XmlDocumentType)node).Value);
+
+// TODO: add this back in to test when it's implemented.
+// node = document.CreateNode (XmlNodeType.EntityReference, "a", "b", "c");
+// AssertNull (((XmlEntityReference)node).Value);
+
+ node = document.CreateNode (XmlNodeType.ProcessingInstruction, "a", "b", "c");
+ AssertEquals (String.Empty, ((XmlProcessingInstruction)node).Value);
+
+ node = document.CreateNode (XmlNodeType.SignificantWhitespace, "a", "b", "c");
+ AssertEquals (String.Empty, ((XmlSignificantWhitespace)node).Value);
+
+ node = document.CreateNode (XmlNodeType.Text, "a", "b", "c");
+ AssertEquals (String.Empty, ((XmlText)node).Value);
+
+ node = document.CreateNode (XmlNodeType.Whitespace, "a", "b", "c");
+ AssertEquals (String.Empty, ((XmlWhitespace)node).Value);
+
+ node = document.CreateNode (XmlNodeType.XmlDeclaration, "a", "b", "c");
+ AssertEquals ("version=\"1.0\"", ((XmlDeclaration)node).Value);
+ }
+
+ public void TestCreateNodeNodeTypeName ()
+ {
+ XmlNode node;
+
+ try {
+ node = document.CreateNode ("foo", null, null);
+ Fail ("Expected an ArgumentException to be thrown.");
+ } catch (ArgumentException) {}
+
+ node = document.CreateNode("attribute", "foo", null);
+ AssertEquals (XmlNodeType.Attribute, node.NodeType);
+
+ node = document.CreateNode("cdatasection", null, null);
+ AssertEquals (XmlNodeType.CDATA, node.NodeType);
+
+ node = document.CreateNode("comment", null, null);
+ AssertEquals (XmlNodeType.Comment, node.NodeType);
+
+ node = document.CreateNode("document", null, null);
+ AssertEquals (XmlNodeType.Document, node.NodeType);
+ // TODO: test which constructor this ended up calling,
+ // i.e. reuse underlying NameTable or not?
+
+// TODO: add this back in to test when it's implemented.
+// node = document.CreateNode("documentfragment", null, null);
+// AssertEquals (XmlNodeType.DocumentFragment, node.NodeType);
+
+ node = document.CreateNode("documenttype", null, null);
+ AssertEquals (XmlNodeType.DocumentType, node.NodeType);
+
+ node = document.CreateNode("element", "foo", null);
+ AssertEquals (XmlNodeType.Element, node.NodeType);
+
+// TODO: add this back in to test when it's implemented.
+// node = document.CreateNode("entityreference", "foo", null);
+// AssertEquals (XmlNodeType.EntityReference, node.NodeType);
+
+ node = document.CreateNode("processinginstruction", null, null);
+ AssertEquals (XmlNodeType.ProcessingInstruction, node.NodeType);
+
+ node = document.CreateNode("significantwhitespace", null, null);
+ AssertEquals (XmlNodeType.SignificantWhitespace, node.NodeType);
+
+ node = document.CreateNode("text", null, null);
+ AssertEquals (XmlNodeType.Text, node.NodeType);
+
+ node = document.CreateNode("whitespace", null, null);
+ AssertEquals (XmlNodeType.Whitespace, node.NodeType);
+ }
+
public void TestDocumentElement ()
{
AssertNull (document.DocumentElement);