Imported Upstream version 4.0.0~alpha1

Former-commit-id: 806294f5ded97629b74c85c09952f2a74fe182d9
This commit is contained in:
Jo Shields
2015-04-07 09:35:12 +01:00
parent 283343f570
commit 3c1f479b9d
22469 changed files with 2931443 additions and 869343 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,113 @@
//------------------------------------------------------------------------------
// <copyright file="DomNameTable.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Xml.Schema;
namespace System.Xml {
internal class DomNameTable {
XmlName[] entries;
int count;
int mask;
XmlDocument ownerDocument;
XmlNameTable nameTable;
const int InitialSize = 64; // must be a power of two
public DomNameTable( XmlDocument document ) {
ownerDocument = document;
nameTable = document.NameTable;
entries = new XmlName[InitialSize];
mask = InitialSize - 1;
Debug.Assert( ( entries.Length & mask ) == 0 ); // entries.Length must be a power of two
}
public XmlName GetName(string prefix, string localName, string ns, IXmlSchemaInfo schemaInfo) {
if (prefix == null) {
prefix = string.Empty;
}
if (ns == null) {
ns = string.Empty;
}
int hashCode = XmlName.GetHashCode(localName);
for (XmlName e = entries[hashCode & mask]; e != null; e = e.next) {
if (e.HashCode == hashCode
&& ((object)e.LocalName == (object)localName
|| e.LocalName.Equals(localName))
&& ((object)e.Prefix == (object)prefix
|| e.Prefix.Equals(prefix))
&& ((object)e.NamespaceURI == (object)ns
|| e.NamespaceURI.Equals(ns))
&& e.Equals(schemaInfo)) {
return e;
}
}
return null;
}
public XmlName AddName(string prefix, string localName, string ns, IXmlSchemaInfo schemaInfo) {
if (prefix == null) {
prefix = string.Empty;
}
if (ns == null) {
ns = string.Empty;
}
int hashCode = XmlName.GetHashCode(localName);
for (XmlName e = entries[hashCode & mask]; e != null; e = e.next) {
if (e.HashCode == hashCode
&& ((object)e.LocalName == (object)localName
|| e.LocalName.Equals(localName))
&& ((object)e.Prefix == (object)prefix
|| e.Prefix.Equals(prefix))
&& ((object)e.NamespaceURI == (object)ns
|| e.NamespaceURI.Equals(ns))
&& e.Equals(schemaInfo)) {
return e;
}
}
prefix = nameTable.Add(prefix);
localName = nameTable.Add(localName);
ns = nameTable.Add(ns);
int index = hashCode & mask;
XmlName name = XmlName.Create(prefix, localName, ns, hashCode, ownerDocument, entries[index], schemaInfo);
entries[index] = name;
if (count++ == mask) {
Grow();
}
return name;
}
private void Grow() {
int newMask = mask * 2 + 1;
XmlName[] oldEntries = entries;
XmlName[] newEntries = new XmlName[newMask+1];
// use oldEntries.Length to eliminate the rangecheck
for ( int i = 0; i < oldEntries.Length; i++ ) {
XmlName name = oldEntries[i];
while ( name != null ) {
int newIndex = name.HashCode & newMask;
XmlName tmp = name.next;
name.next = newEntries[newIndex];
newEntries[newIndex] = name;
name = tmp;
}
}
entries = newEntries;
mask = newMask;
}
}
}

View File

@@ -0,0 +1,107 @@
//------------------------------------------------------------------------------
// <copyright file="XPathNodeList.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System.Xml.XPath;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
internal class XPathNodeList: XmlNodeList {
List<XmlNode> list;
XPathNodeIterator nodeIterator;
bool done;
public XPathNodeList(XPathNodeIterator nodeIterator) {
this.nodeIterator = nodeIterator;
this.list = new List<XmlNode>();
this.done = false;
}
public override int Count {
get {
if (! done) {
ReadUntil(Int32.MaxValue);
}
return list.Count;
}
}
private static readonly object[] nullparams = {};
private XmlNode GetNode(XPathNavigator n) {
IHasXmlNode iHasNode = (IHasXmlNode) n;
return iHasNode.GetNode();
}
internal int ReadUntil(int index) {
int count = list.Count;
while (! done && count <= index) {
if (nodeIterator.MoveNext()) {
XmlNode n = GetNode(nodeIterator.Current);
if (n != null) {
list.Add(n);
count++;
}
} else {
done = true;
break;
}
}
return count;
}
public override XmlNode Item(int index) {
if (list.Count <= index) {
ReadUntil(index);
}
if (index < 0 || list.Count <= index) {
return null;
}
return list[index];
}
public override IEnumerator GetEnumerator() {
return new XmlNodeListEnumerator(this);
}
}
internal class XmlNodeListEnumerator : IEnumerator {
XPathNodeList list;
int index;
bool valid;
public XmlNodeListEnumerator(XPathNodeList list) {
this.list = list;
this.index = -1;
this.valid = false;
}
public void Reset() {
index = -1;
}
public bool MoveNext() {
index++;
int count = list.ReadUntil(index + 1); // read past for delete-node case
if (count - 1 < index) {
return false;
}
valid = (list[index] != null);
return valid;
}
public object Current {
get {
if (valid) {
return list[index];
}
return null;
}
}
}
}

View File

@@ -0,0 +1,350 @@
//------------------------------------------------------------------------------
// <copyright file="XmlAttribute.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Diagnostics;
// Represents an attribute of the XMLElement object. Valid and default
// values for the attribute are defined in a DTD or schema.
public class XmlAttribute : XmlNode {
XmlName name;
XmlLinkedNode lastChild;
internal XmlAttribute( XmlName name, XmlDocument doc ): base( doc ) {
Debug.Assert(name!=null);
Debug.Assert(doc!=null);
this.parentNode = null;
if ( !doc.IsLoading ) {
XmlDocument.CheckName( name.Prefix );
XmlDocument.CheckName( name.LocalName );
}
if (name.LocalName.Length == 0)
throw new ArgumentException(Res.GetString(Res.Xdom_Attr_Name));
this.name = name;
}
internal int LocalNameHash {
get { return name.HashCode; }
}
protected internal XmlAttribute( string prefix, string localName, string namespaceURI, XmlDocument doc )
: this(doc.AddAttrXmlName(prefix, localName, namespaceURI, null), doc) {
}
internal XmlName XmlName {
get { return name; }
set { name = value; }
}
// Creates a duplicate of this node.
public override XmlNode CloneNode(bool deep) {
// CloneNode for attributes is deep irrespective of parameter 'deep' value
Debug.Assert( OwnerDocument != null );
XmlDocument doc = OwnerDocument;
XmlAttribute attr = doc.CreateAttribute( Prefix, LocalName, NamespaceURI );
attr.CopyChildren( doc, this, true );
return attr;
}
// Gets the parent of this node (for nodes that can have parents).
public override XmlNode ParentNode {
get { return null;}
}
// Gets the name of the node.
public override String Name {
get { return name.Name;}
}
// Gets the name of the node without the namespace prefix.
public override String LocalName {
get { return name.LocalName;}
}
// Gets the namespace URI of this node.
public override String NamespaceURI {
get { return name.NamespaceURI;}
}
// Gets or sets the namespace prefix of this node.
public override String Prefix {
get { return name.Prefix;}
set { name = name.OwnerDocument.AddAttrXmlName( value, LocalName, NamespaceURI, SchemaInfo ); }
}
// Gets the type of the current node.
public override XmlNodeType NodeType {
get { return XmlNodeType.Attribute;}
}
// Gets the XmlDocument that contains this node.
public override XmlDocument OwnerDocument {
get {
return name.OwnerDocument;
}
}
// Gets or sets the value of the node.
public override String Value {
get { return InnerText; }
set { InnerText = value; } //use InnerText which has perf optimization
}
public override IXmlSchemaInfo SchemaInfo {
get {
return name;
}
}
public override String InnerText {
set {
if (PrepareOwnerElementInElementIdAttrMap()) {
string innerText = base.InnerText;
base.InnerText = value;
ResetOwnerElementInElementIdAttrMap(innerText);
}
else {
base.InnerText = value;
}
}
}
internal bool PrepareOwnerElementInElementIdAttrMap() {
XmlDocument ownerDocument = OwnerDocument;
if (ownerDocument.DtdSchemaInfo != null) { // DTD exists
XmlElement ownerElement = OwnerElement;
if (ownerElement != null) {
return ownerElement.Attributes.PrepareParentInElementIdAttrMap(Prefix, LocalName);
}
}
return false;
}
internal void ResetOwnerElementInElementIdAttrMap(string oldInnerText) {
XmlElement ownerElement = OwnerElement;
if (ownerElement != null) {
ownerElement.Attributes.ResetParentInElementIdAttrMap(oldInnerText, InnerText);
}
}
internal override bool IsContainer {
get { return true;}
}
//the function is provided only at Load time to speed up Load process
internal override XmlNode AppendChildForLoad(XmlNode newChild, XmlDocument doc) {
XmlNodeChangedEventArgs args = doc.GetInsertEventArgsForLoad( newChild, this );
if (args != null)
doc.BeforeEvent( args );
XmlLinkedNode newNode = (XmlLinkedNode)newChild;
if (lastChild == null) { // if LastNode == null
newNode.next = newNode;
lastChild = newNode;
newNode.SetParentForLoad(this);
}
else {
XmlLinkedNode refNode = lastChild; // refNode = LastNode;
newNode.next = refNode.next;
refNode.next = newNode;
lastChild = newNode; // LastNode = newNode;
if (refNode.IsText
&& newNode.IsText) {
NestTextNodes(refNode, newNode);
}
else {
newNode.SetParentForLoad(this);
}
}
if (args != null)
doc.AfterEvent( args );
return newNode;
}
internal override XmlLinkedNode LastNode {
get { return lastChild;}
set { lastChild = value;}
}
[System.Runtime.TargetedPatchingOptOutAttribute("Performance critical to inline across NGen image boundaries")]
internal override bool IsValidChildType( XmlNodeType type ) {
return(type == XmlNodeType.Text) || (type == XmlNodeType.EntityReference);
}
// Gets a value indicating whether the value was explicitly set.
public virtual bool Specified {
get { return true;}
}
public override XmlNode InsertBefore(XmlNode newChild, XmlNode refChild) {
XmlNode node;
if (PrepareOwnerElementInElementIdAttrMap()) {
string innerText = InnerText;
node = base.InsertBefore(newChild, refChild);
ResetOwnerElementInElementIdAttrMap(innerText);
}
else {
node = base.InsertBefore(newChild, refChild);
}
return node;
}
public override XmlNode InsertAfter(XmlNode newChild, XmlNode refChild) {
XmlNode node;
if (PrepareOwnerElementInElementIdAttrMap()) {
string innerText = InnerText;
node = base.InsertAfter(newChild, refChild);
ResetOwnerElementInElementIdAttrMap(innerText);
}
else {
node = base.InsertAfter(newChild, refChild);
}
return node;
}
public override XmlNode ReplaceChild(XmlNode newChild, XmlNode oldChild) {
XmlNode node;
if (PrepareOwnerElementInElementIdAttrMap()) {
string innerText = InnerText;
node = base.ReplaceChild(newChild, oldChild);
ResetOwnerElementInElementIdAttrMap(innerText);
}
else {
node = base.ReplaceChild(newChild, oldChild);
}
return node;
}
public override XmlNode RemoveChild(XmlNode oldChild) {
XmlNode node;
if (PrepareOwnerElementInElementIdAttrMap()) {
string innerText = InnerText;
node = base.RemoveChild(oldChild);
ResetOwnerElementInElementIdAttrMap(innerText);
}
else {
node = base.RemoveChild(oldChild);
}
return node;
}
public override XmlNode PrependChild(XmlNode newChild) {
XmlNode node;
if (PrepareOwnerElementInElementIdAttrMap()) {
string innerText = InnerText;
node = base.PrependChild(newChild);
ResetOwnerElementInElementIdAttrMap(innerText);
}
else {
node = base.PrependChild(newChild);
}
return node;
}
public override XmlNode AppendChild(XmlNode newChild) {
XmlNode node;
if (PrepareOwnerElementInElementIdAttrMap()) {
string innerText = InnerText;
node = base.AppendChild(newChild);
ResetOwnerElementInElementIdAttrMap(innerText);
}
else {
node = base.AppendChild(newChild);
}
return node;
}
// DOM Level 2
// Gets the XmlElement node that contains this attribute.
public virtual XmlElement OwnerElement {
get {
return parentNode as XmlElement;
}
}
// Gets or sets the markup representing just the children of this node.
public override string InnerXml {
set {
RemoveAll();
XmlLoader loader = new XmlLoader();
loader.LoadInnerXmlAttribute( this, value );
}
}
// Saves the node to the specified XmlWriter.
public override void WriteTo(XmlWriter w) {
w.WriteStartAttribute( Prefix, LocalName, NamespaceURI );
WriteContentTo(w);
w.WriteEndAttribute();
}
// Saves all the children of the node to the specified XmlWriter.
[System.Runtime.TargetedPatchingOptOutAttribute("Performance critical to inline across NGen image boundaries")]
public override void WriteContentTo(XmlWriter w) {
for (XmlNode node = FirstChild; node != null; node = node.NextSibling) {
node.WriteTo(w);
}
}
public override String BaseURI {
get {
if ( OwnerElement != null )
return OwnerElement.BaseURI;
return String.Empty;
}
}
internal override void SetParent(XmlNode node) {
this.parentNode = node;
}
internal override XmlSpace XmlSpace {
get {
if ( OwnerElement != null )
return OwnerElement.XmlSpace;
return XmlSpace.None;
}
}
internal override String XmlLang {
get {
if ( OwnerElement != null )
return OwnerElement.XmlLang;
return String.Empty;
}
}
internal override XPathNodeType XPNodeType {
get {
if (IsNamespace) {
return XPathNodeType.Namespace;
}
return XPathNodeType.Attribute;
}
}
internal override string XPLocalName {
get {
if (name.Prefix.Length == 0 && name.LocalName == "xmlns") return string.Empty;
return name.LocalName;
}
}
internal bool IsNamespace {
get {
return Ref.Equal(name.NamespaceURI, name.OwnerDocument.strReservedXmlns);
}
}
}
}

View File

@@ -0,0 +1,369 @@
//------------------------------------------------------------------------------
// <copyright file="XmlAttributeCollection.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System;
using System.Collections;
using System.Diagnostics;
// Represents a collection of attributes that can be accessed by name or index.
public sealed class XmlAttributeCollection: XmlNamedNodeMap, ICollection {
internal XmlAttributeCollection( XmlNode parent ): base( parent ) {
}
// Gets the attribute with the specified index.
[System.Runtime.CompilerServices.IndexerName ("ItemOf")]
public XmlAttribute this[ int i ] {
get {
try {
return (XmlAttribute)nodes[i];
} catch ( ArgumentOutOfRangeException ) {
throw new IndexOutOfRangeException(Res.GetString(Res.Xdom_IndexOutOfRange));
}
}
}
// Gets the attribute with the specified name.
[System.Runtime.CompilerServices.IndexerName ("ItemOf")]
public XmlAttribute this[ string name ]
{
get {
int hash = XmlName.GetHashCode(name);
for (int i = 0; i < nodes.Count; i++) {
XmlAttribute node = (XmlAttribute) nodes[i];
if (hash == node.LocalNameHash
&& name == node.Name )
{
return node;
}
}
return null;
}
}
// Gets the attribute with the specified LocalName and NamespaceUri.
[System.Runtime.CompilerServices.IndexerName ("ItemOf")]
public XmlAttribute this[ string localName, string namespaceURI ]
{
get {
int hash = XmlName.GetHashCode(localName);
for (int i = 0; i < nodes.Count; i++) {
XmlAttribute node = (XmlAttribute) nodes[i];
if (hash == node.LocalNameHash
&& localName == node.LocalName
&& namespaceURI == node.NamespaceURI)
{
return node;
}
}
return null;
}
}
internal int FindNodeOffset( XmlAttribute node ) {
for (int i = 0; i < nodes.Count; i++) {
XmlAttribute tmp = (XmlAttribute) nodes[i];
if (tmp.LocalNameHash == node.LocalNameHash
&& tmp.Name == node.Name
&& tmp.NamespaceURI == node.NamespaceURI )
{
return i;
}
}
return -1;
}
internal int FindNodeOffsetNS(XmlAttribute node) {
for (int i = 0; i < nodes.Count; i++) {
XmlAttribute tmp = (XmlAttribute) nodes[i];
if (tmp.LocalNameHash == node.LocalNameHash
&& tmp.LocalName == node.LocalName
&& tmp.NamespaceURI == node.NamespaceURI) {
return i;
}
}
return -1;
}
// Adds a XmlNode using its Name property
public override XmlNode SetNamedItem(XmlNode node) {
if (node != null && !(node is XmlAttribute))
throw new ArgumentException(Res.GetString(Res.Xdom_AttrCol_Object));
int offset = FindNodeOffset( node.LocalName, node.NamespaceURI );
if (offset == -1) {
return InternalAppendAttribute( (XmlAttribute) node );
}
else {
XmlNode oldNode = base.RemoveNodeAt( offset );
InsertNodeAt( offset, node );
return oldNode;
}
}
// Inserts the specified node as the first node in the collection.
public XmlAttribute Prepend( XmlAttribute node ) {
if (node.OwnerDocument != null && node.OwnerDocument != parent.OwnerDocument)
throw new ArgumentException(Res.GetString(Res.Xdom_NamedNode_Context));
if (node.OwnerElement != null)
Detach( node );
RemoveDuplicateAttribute( node );
InsertNodeAt( 0, node );
return node;
}
// Inserts the specified node as the last node in the collection.
public XmlAttribute Append(XmlAttribute node) {
XmlDocument doc = node.OwnerDocument;
if (doc == null || doc.IsLoading == false) {
if (doc != null && doc != parent.OwnerDocument) {
throw new ArgumentException(Res.GetString(Res.Xdom_NamedNode_Context));
}
if (node.OwnerElement != null) {
Detach(node);
}
AddNode(node);
}
else {
base.AddNodeForLoad(node, doc);
InsertParentIntoElementIdAttrMap(node);
}
return node;
}
// Inserts the specified attribute immediately before the specified reference attribute.
public XmlAttribute InsertBefore( XmlAttribute newNode, XmlAttribute refNode ) {
if ( newNode == refNode )
return newNode;
if (refNode == null)
return Append(newNode);
if (refNode.OwnerElement != parent)
throw new ArgumentException(Res.GetString(Res.Xdom_AttrCol_Insert));
if (newNode.OwnerDocument != null && newNode.OwnerDocument != parent.OwnerDocument)
throw new ArgumentException(Res.GetString(Res.Xdom_NamedNode_Context));
if (newNode.OwnerElement != null)
Detach( newNode );
int offset = FindNodeOffset( refNode.LocalName, refNode.NamespaceURI );
Debug.Assert( offset != -1 ); // the if statement above guarantees that the ref node is in the collection
int dupoff = RemoveDuplicateAttribute( newNode );
if ( dupoff >= 0 && dupoff < offset )
offset--;
InsertNodeAt( offset, newNode );
return newNode;
}
// Inserts the specified attribute immediately after the specified reference attribute.
public XmlAttribute InsertAfter( XmlAttribute newNode, XmlAttribute refNode ) {
if ( newNode == refNode )
return newNode;
if (refNode == null)
return Prepend(newNode);
if (refNode.OwnerElement != parent)
throw new ArgumentException(Res.GetString(Res.Xdom_AttrCol_Insert));
if (newNode.OwnerDocument != null && newNode.OwnerDocument != parent.OwnerDocument)
throw new ArgumentException(Res.GetString(Res.Xdom_NamedNode_Context));
if (newNode.OwnerElement != null)
Detach( newNode );
int offset = FindNodeOffset( refNode.LocalName, refNode.NamespaceURI );
Debug.Assert( offset != -1 ); // the if statement above guarantees that the ref node is in the collection
int dupoff = RemoveDuplicateAttribute( newNode );
if ( dupoff >= 0 && dupoff < offset )
offset--;
InsertNodeAt( offset+1, newNode );
return newNode;
}
// Removes the specified attribute node from the map.
public XmlAttribute Remove( XmlAttribute node ) {
int cNodes = nodes.Count;
for (int offset = 0; offset < cNodes; offset++) {
if (nodes[offset] == node) {
RemoveNodeAt( offset );
return node;
}
}
return null;
}
// Removes the attribute node with the specified index from the map.
public XmlAttribute RemoveAt( int i ) {
if (i < 0 || i >= Count)
return null;
return(XmlAttribute) RemoveNodeAt( i );
}
// Removes all attributes from the map.
public void RemoveAll() {
int n = Count;
while (n > 0) {
n--;
RemoveAt( n );
}
}
void ICollection.CopyTo(Array array, int index) {
for (int i=0, max=Count; i<max; i++, index++)
array.SetValue(nodes[i], index);
}
bool ICollection.IsSynchronized {
get { return false; }
}
object ICollection.SyncRoot {
get { return this; }
}
int ICollection.Count {
get { return base.Count; }
}
public void CopyTo(XmlAttribute[] array, int index) {
for (int i=0, max=Count; i<max; i++, index++)
array[index] = (XmlAttribute)(((XmlNode)nodes[i]).CloneNode(true));
}
internal override XmlNode AddNode( XmlNode node ) {
//should be sure by now that the node doesn't have the same name with an existing node in the collection
RemoveDuplicateAttribute( (XmlAttribute)node );
XmlNode retNode = base.AddNode( node );
Debug.Assert( retNode is XmlAttribute );
InsertParentIntoElementIdAttrMap( (XmlAttribute) node );
return retNode;
}
internal override XmlNode InsertNodeAt( int i, XmlNode node ) {
XmlNode retNode = base.InsertNodeAt(i, node);
InsertParentIntoElementIdAttrMap( (XmlAttribute)node );
return retNode;
}
internal override XmlNode RemoveNodeAt( int i ) {
//remove the node without checking replacement
XmlNode retNode = base.RemoveNodeAt( i );
Debug.Assert(retNode is XmlAttribute);
RemoveParentFromElementIdAttrMap( (XmlAttribute) retNode );
// after remove the attribute, we need to check if a default attribute node should be created and inserted into the tree
XmlAttribute defattr = parent.OwnerDocument.GetDefaultAttribute( (XmlElement)parent, retNode.Prefix, retNode.LocalName, retNode.NamespaceURI );
if ( defattr != null )
InsertNodeAt( i, defattr );
return retNode;
}
internal void Detach( XmlAttribute attr ) {
attr.OwnerElement.Attributes.Remove( attr );
}
//insert the parent element node into the map
internal void InsertParentIntoElementIdAttrMap(XmlAttribute attr)
{
XmlElement parentElem = parent as XmlElement;
if (parentElem != null)
{
if (parent.OwnerDocument == null)
return;
XmlName attrname = parent.OwnerDocument.GetIDInfoByElement(parentElem.XmlName);
if (attrname != null && attrname.Prefix == attr.XmlName.Prefix && attrname.LocalName == attr.XmlName.LocalName) {
parent.OwnerDocument.AddElementWithId(attr.Value, parentElem); //add the element into the hashtable
}
}
}
//remove the parent element node from the map when the ID attribute is removed
internal void RemoveParentFromElementIdAttrMap(XmlAttribute attr)
{
XmlElement parentElem = parent as XmlElement;
if (parentElem != null)
{
if (parent.OwnerDocument == null)
return;
XmlName attrname = parent.OwnerDocument.GetIDInfoByElement(parentElem.XmlName);
if (attrname != null && attrname.Prefix == attr.XmlName.Prefix && attrname.LocalName == attr.XmlName.LocalName) {
parent.OwnerDocument.RemoveElementWithId(attr.Value, parentElem); //remove the element from the hashtable
}
}
}
//the function checks if there is already node with the same name existing in the collection
// if so, remove it because the new one will be inserted to replace this one (could be in different position though )
// by the calling function later
internal int RemoveDuplicateAttribute( XmlAttribute attr ) {
int ind = FindNodeOffset( attr.LocalName, attr.NamespaceURI );
if ( ind != -1 ) {
XmlAttribute at = (XmlAttribute)nodes[ind];
base.RemoveNodeAt( ind );
RemoveParentFromElementIdAttrMap( at );
}
return ind;
}
internal bool PrepareParentInElementIdAttrMap(string attrPrefix, string attrLocalName) {
XmlElement parentElem = parent as XmlElement;
Debug.Assert( parentElem != null );
XmlDocument doc = parent.OwnerDocument;
Debug.Assert( doc != null );
//The returned attrname if not null is the name with namespaceURI being set to string.Empty
//Because DTD doesn't support namespaceURI so all comparisons are based on no namespaceURI (string.Empty);
XmlName attrname = doc.GetIDInfoByElement(parentElem.XmlName);
if (attrname != null && attrname.Prefix == attrPrefix && attrname.LocalName == attrLocalName) {
return true;
}
return false;
}
internal void ResetParentInElementIdAttrMap(string oldVal, string newVal) {
XmlElement parentElem = parent as XmlElement;
Debug.Assert( parentElem != null );
XmlDocument doc = parent.OwnerDocument;
Debug.Assert( doc != null );
doc.RemoveElementWithId(oldVal, parentElem); //add the element into the hashtable
doc.AddElementWithId(newVal, parentElem);
}
// WARNING:
// For performance reasons, this function does not check
// for xml attributes within the collection with the same full name.
// This means that any caller of this function must be sure that
// a duplicate attribute does not exist.
[System.Runtime.TargetedPatchingOptOutAttribute("Performance critical to inline across NGen image boundaries")]
internal XmlAttribute InternalAppendAttribute( XmlAttribute node ) {
// a duplicate node better not exist
Debug.Assert( -1 == FindNodeOffset( node ));
XmlNode retNode = base.AddNode( node );
Debug.Assert( retNode is XmlAttribute );
InsertParentIntoElementIdAttrMap( (XmlAttribute) node );
return (XmlAttribute)retNode;
}
}
}

View File

@@ -0,0 +1,98 @@
//------------------------------------------------------------------------------
// <copyright file="XmlCDATASection.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System;
using System.Text;
using System.Diagnostics;
using System.Xml.XPath;
// Used to quote or escape blocks of text to keep that text from being
// interpreted as markup language.
public class XmlCDataSection : XmlCharacterData {
protected internal XmlCDataSection( string data, XmlDocument doc ): base( data, doc ) {
}
// Gets the name of the node.
public override String Name {
get {
return OwnerDocument.strCDataSectionName;
}
}
// Gets the name of the node without the namespace prefix.
public override String LocalName {
get {
return OwnerDocument.strCDataSectionName;
}
}
// Gets the type of the current node.
public override XmlNodeType NodeType {
get {
return XmlNodeType.CDATA;
}
}
public override XmlNode ParentNode {
get {
switch (parentNode.NodeType) {
case XmlNodeType.Document:
return null;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
XmlNode parent = parentNode.parentNode;
while (parent.IsText) {
parent = parent.parentNode;
}
return parent;
default:
return parentNode;
}
}
}
// Creates a duplicate of this node.
public override XmlNode CloneNode(bool deep) {
Debug.Assert( OwnerDocument != null );
return OwnerDocument.CreateCDataSection( Data );
}
// Saves the node to the specified XmlWriter.
public override void WriteTo(XmlWriter w) {
w.WriteCData( Data );
}
// Saves the node to the specified XmlWriter.
public override void WriteContentTo(XmlWriter w) {
// Intentionally do nothing
}
internal override XPathNodeType XPNodeType {
get {
return XPathNodeType.Text;
}
}
internal override bool IsText {
get {
return true;
}
}
internal override XmlNode PreviousText {
get {
if (parentNode.IsText) {
return parentNode;
}
return null;
}
}
}
}

View File

@@ -0,0 +1,203 @@
//------------------------------------------------------------------------------
// <copyright file="XmlCharacterData.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System.Diagnostics;
using System.Text;
using System.Xml.XPath;
// Provides text-manipulation methods that are used by several classes.
public abstract class XmlCharacterData : XmlLinkedNode {
string data;
//base(doc) will throw exception if doc is null.
protected internal XmlCharacterData( string data, XmlDocument doc ): base( doc ) {
this.data = data;
}
// Gets or sets the value of the node.
public override String Value {
get { return Data;}
set { Data = value;}
}
// Gets or sets the concatenated values of the node and
// all its children.
public override string InnerText {
get { return Value;}
set { Value = value;}
}
// Contains this node's data.
public virtual string Data {
[System.Runtime.TargetedPatchingOptOutAttribute("Performance critical to inline across NGen image boundaries")]
get {
if (data != null) {
return data;
}
else {
return String.Empty;
}
}
set {
XmlNode parent = ParentNode;
XmlNodeChangedEventArgs args = GetEventArgs( this, parent, parent, this.data, value, XmlNodeChangedAction.Change );
if (args != null)
BeforeEvent( args );
data = value;
if (args != null)
AfterEvent( args );
}
}
// Gets the length of the data, in characters.
public virtual int Length {
get {
if (data != null) {
return data.Length;
}
return 0;
}
}
// Retrieves a substring of the full string from the specified range.
public virtual String Substring(int offset, int count) {
int len = data != null ? data.Length : 0;
if (len > 0) {
if (len < (offset + count)) {
count = len - offset;
}
return data.Substring( offset, count );
}
return String.Empty;
}
// Appends the specified string to the end of the character
// data of the node.
public virtual void AppendData(String strData) {
XmlNode parent = ParentNode;
int capacity = data != null ? data.Length : 0;
if (strData != null) capacity += strData.Length;
string newValue = new StringBuilder( capacity ).Append( data ).Append( strData ).ToString();
XmlNodeChangedEventArgs args = GetEventArgs( this, parent, parent, data, newValue, XmlNodeChangedAction.Change );
if (args != null)
BeforeEvent( args );
this.data = newValue;
if (args != null)
AfterEvent( args );
}
// Insert the specified string at the specified character offset.
public virtual void InsertData(int offset, string strData) {
XmlNode parent = ParentNode;
int capacity = data != null ? data.Length : 0;
if (strData != null) capacity += strData.Length;
string newValue = new StringBuilder( capacity ).Append( data ).Insert(offset, strData).ToString();
XmlNodeChangedEventArgs args = GetEventArgs( this, parent, parent, data, newValue, XmlNodeChangedAction.Change );
if (args != null)
BeforeEvent( args );
this.data = newValue;
if (args != null)
AfterEvent( args );
}
// Remove a range of characters from the node.
public virtual void DeleteData(int offset, int count) {
//Debug.Assert(offset >= 0 && offset <= Length);
int len = data != null ? data.Length : 0;
if (len > 0) {
if (len < (offset + count)) {
count = Math.Max ( len - offset, 0);
}
}
string newValue = new StringBuilder( data ).Remove(offset, count).ToString();
XmlNode parent = ParentNode;
XmlNodeChangedEventArgs args = GetEventArgs( this, parent, parent, data, newValue, XmlNodeChangedAction.Change );
if (args != null)
BeforeEvent( args );
this.data = newValue;
if (args != null)
AfterEvent( args );
}
// Replace the specified number of characters starting at the specified offset with the
// specified string.
public virtual void ReplaceData(int offset, int count, String strData) {
//Debug.Assert(offset >= 0 && offset <= Length);
int len = data != null ? data.Length : 0;
if (len > 0) {
if (len < (offset + count)) {
count = Math.Max ( len - offset, 0);
}
}
StringBuilder temp = new StringBuilder( data ).Remove( offset, count );
string newValue = temp.Insert( offset, strData ).ToString();
XmlNode parent = ParentNode;
XmlNodeChangedEventArgs args = GetEventArgs( this, parent, parent, data, newValue, XmlNodeChangedAction.Change );
if (args != null)
BeforeEvent( args );
this.data = newValue;
if (args != null)
AfterEvent( args );
}
internal bool CheckOnData( string data ) {
return XmlCharType.Instance.IsOnlyWhitespace( data );
}
internal bool DecideXPNodeTypeForTextNodes(XmlNode node, ref XPathNodeType xnt) {
//returns true - if all siblings of the node are processed else returns false.
//The reference XPathNodeType argument being passed in is the watermark that
//changes according to the siblings nodetype and will contain the correct
//nodetype when it returns.
Debug.Assert(XmlDocument.IsTextNode(node.NodeType) || (node.ParentNode != null && node.ParentNode.NodeType == XmlNodeType.EntityReference));
while (node != null) {
switch (node.NodeType) {
case XmlNodeType.Whitespace :
break;
case XmlNodeType.SignificantWhitespace :
xnt = XPathNodeType.SignificantWhitespace;
break;
case XmlNodeType.Text :
case XmlNodeType.CDATA:
xnt = XPathNodeType.Text;
return false;
case XmlNodeType.EntityReference :
if (!DecideXPNodeTypeForTextNodes(node.FirstChild, ref xnt)) {
return false;
}
break;
default :
return false;
}
node = node.NextSibling;
}
return true;
}
}
}

View File

@@ -0,0 +1,58 @@
//------------------------------------------------------------------------------
// <copyright file="XmlChildEnumerator.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System.Collections;
internal sealed class XmlChildEnumerator: IEnumerator {
internal XmlNode container;
internal XmlNode child;
internal bool isFirst;
internal XmlChildEnumerator( XmlNode container ) {
this.container = container;
this.child = container.FirstChild;
this.isFirst = true;
}
bool IEnumerator.MoveNext() {
return this.MoveNext();
}
internal bool MoveNext() {
if (isFirst) {
child = container.FirstChild;
isFirst = false;
}
else if (child != null) {
child = child.NextSibling;
}
return child != null;
}
void IEnumerator.Reset() {
isFirst = true;
child = container.FirstChild;
}
object IEnumerator.Current {
get {
return this.Current;
}
}
internal XmlNode Current {
get {
if (isFirst || child == null)
throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
return child;
}
}
}
}

View File

@@ -0,0 +1,47 @@
//------------------------------------------------------------------------------
// <copyright file="XmlChildNodes.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System.Collections;
internal class XmlChildNodes: XmlNodeList {
private XmlNode container;
public XmlChildNodes( XmlNode container ) {
this.container = container;
}
public override XmlNode Item( int i ) {
// Out of range indexes return a null XmlNode
if (i < 0)
return null;
for (XmlNode n = container.FirstChild; n != null; n = n.NextSibling, i--) {
if (i == 0)
return n;
}
return null;
}
public override int Count {
get {
int c = 0;
for (XmlNode n = container.FirstChild; n != null; n = n.NextSibling) {
c++;
}
return c;
}
}
public override IEnumerator GetEnumerator() {
if ( container.FirstChild == null ) {
return XmlDocument.EmptyEnumerator;
}
else {
return new XmlChildEnumerator( container );
}
}
}
}

View File

@@ -0,0 +1,51 @@
//------------------------------------------------------------------------------
// <copyright file="XmlComment.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System.Xml.XPath;
using System.Diagnostics;
// Represents the content of an XML comment.
public class XmlComment: XmlCharacterData {
protected internal XmlComment( string comment, XmlDocument doc ): base( comment, doc ) {
}
// Gets the name of the node.
public override String Name {
get { return OwnerDocument.strCommentName;}
}
// Gets the name of the current node without the namespace prefix.
public override String LocalName {
get { return OwnerDocument.strCommentName;}
}
// Gets the type of the current node.
public override XmlNodeType NodeType {
get { return XmlNodeType.Comment;}
}
// Creates a duplicate of this node.
public override XmlNode CloneNode(bool deep) {
Debug.Assert( OwnerDocument != null );
return OwnerDocument.CreateComment( Data );
}
// Saves the node to the specified XmlWriter.
public override void WriteTo(XmlWriter w) {
w.WriteComment( Data );
}
// Saves all the children of the node to the specified XmlWriter.
public override void WriteContentTo(XmlWriter w) {
// Intentionally do nothing
}
internal override XPathNodeType XPNodeType { get { return XPathNodeType.Comment; } }
}
}

View File

@@ -0,0 +1,153 @@
//------------------------------------------------------------------------------
// <copyright file="XmlDeclaration.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System.Text;
using System.Diagnostics;
// Represents the xml declaration nodes: <?xml version='1.0' ...?>
public class XmlDeclaration : XmlLinkedNode {
const string YES = "yes";
const string NO = "no";
private string version;
private string encoding;
private string standalone;
protected internal XmlDeclaration( string version, string encoding, string standalone, XmlDocument doc ) : base( doc ) {
if ( !IsValidXmlVersion( version ) )
throw new ArgumentException( Res.GetString( Res.Xdom_Version ) );
if( ( standalone != null ) && ( standalone.Length > 0 ) )
if ( ( standalone != YES ) && ( standalone != NO ) )
throw new ArgumentException( Res.GetString(Res.Xdom_standalone, standalone) );
this.Encoding = encoding;
this.Standalone = standalone;
this.Version = version;
}
// The version attribute for <?xml version= '1.0' ... ?>
public string Version {
get { return this.version; }
internal set { this.version = value; }
}
// Specifies the value of the encoding attribute, as for
// <?xml version= '1.0' encoding= 'UTF-8' ?>
public string Encoding {
get { return this.encoding; }
set { this.encoding = ( (value == null) ? String.Empty : value ); }
}
// Specifies the value of the standalone attribute.
public string Standalone {
get { return this.standalone; }
set {
if ( value == null )
this.standalone = String.Empty;
else if ( value.Length == 0 || value == YES || value == NO )
this.standalone = value;
else
throw new ArgumentException( Res.GetString(Res.Xdom_standalone, value) );
}
}
public override String Value {
get { return InnerText; }
set { InnerText = value; }
}
// Gets or sets the concatenated values of the node and
// all its children.
public override string InnerText {
get {
StringBuilder strb = new StringBuilder("version=\"" + Version + "\"");
if ( Encoding.Length > 0 ) {
strb.Append(" encoding=\"");
strb.Append(Encoding);
strb.Append("\"");
}
if ( Standalone.Length > 0 ) {
strb.Append(" standalone=\"");
strb.Append(Standalone);
strb.Append("\"");
}
return strb.ToString();
}
set {
string tempVersion = null;
string tempEncoding = null;
string tempStandalone = null;
string orgEncoding = this.Encoding;
string orgStandalone = this.Standalone;
string orgVersion = this.Version;
XmlLoader.ParseXmlDeclarationValue( value, out tempVersion, out tempEncoding, out tempStandalone );
try {
if ( tempVersion != null && !IsValidXmlVersion(tempVersion) )
throw new ArgumentException(Res.GetString(Res.Xdom_Version));
Version = tempVersion;
if ( tempEncoding != null )
Encoding = tempEncoding;
if ( tempStandalone != null )
Standalone = tempStandalone;
}
catch {
Encoding = orgEncoding;
Standalone = orgStandalone;
Version = orgVersion;
throw;
}
}
}
//override methods and properties from XmlNode
// Gets the name of the node.
public override String Name {
get {
return "xml";
}
}
// Gets the name of the current node without the namespace prefix.
public override string LocalName {
get { return Name;}
}
// Gets the type of the current node.
public override XmlNodeType NodeType {
get { return XmlNodeType.XmlDeclaration;}
}
// Creates a duplicate of this node.
public override XmlNode CloneNode(bool deep) {
Debug.Assert( OwnerDocument != null );
return OwnerDocument.CreateXmlDeclaration( Version, Encoding, Standalone );
}
// Saves the node to the specified XmlWriter.
public override void WriteTo(XmlWriter w) {
w.WriteProcessingInstruction(Name, InnerText);
}
// Saves all the children of the node to the specified XmlWriter.
public override void WriteContentTo(XmlWriter w) {
// Intentionally do nothing since the node doesn't have children.
}
private bool IsValidXmlVersion(string ver) {
return ver.Length >= 3 && ver[0] == '1' && ver[1] == '.' && XmlCharType.IsOnlyDigits(ver, 2, ver.Length - 2);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,176 @@
//------------------------------------------------------------------------------
// <copyright file="XmlDocumentFragment.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
// <code>DocumentFragment</code> is a "lightweight" or "minimal"
// <code>Document</code> object. It is very common to want to be able to
// extract a portion of a document's tree or to create a new fragment of a
// document. Imagine implementing a user command like cut or rearranging a
// document by moving fragments around. It is desirable to have an object
// which can hold such fragments and it is quite natural to use a Node for
// this purpose. While it is true that a <code>Document</code> object could
// fulfil this role, a <code>Document</code> object can potentially be a
// heavyweight object, depending on the underlying implementation. What is
// really needed for this is a very lightweight object.
// <code>DocumentFragment</code> is such an object.
// <p>Furthermore, various operations -- such as inserting nodes as children
// of another <code>Node</code> -- may take <code>DocumentFragment</code>
// objects as arguments; this results in all the child nodes of the
// <code>DocumentFragment</code> being moved to the child list of this node.
// <p>The children of a <code>DocumentFragment</code> node are zero or more
// nodes representing the tops of any sub-trees defining the structure of the
// document. <code>DocumentFragment</code> nodes do not need to be
// well-formed XML documents (although they do need to follow the rules
// imposed upon well-formed XML parsed entities, which can have multiple top
// nodes). For example, a <code>DocumentFragment</code> might have only one
// child and that child node could be a <code>Text</code> node. Such a
// structure model represents neither an HTML document nor a well-formed XML
// document.
// <p>When a <code>DocumentFragment</code> is inserted into a
// <code>Document</code> (or indeed any other <code>Node</code> that may take
// children) the children of the <code>DocumentFragment</code> and not the
// <code>DocumentFragment</code> itself are inserted into the
// <code>Node</code>. This makes the <code>DocumentFragment</code> very
// useful when the user wishes to create nodes that are siblings; the
// <code>DocumentFragment</code> acts as the parent of these nodes so that the
// user can use the standard methods from the <code>Node</code> interface,
// such as <code>insertBefore()</code> and <code>appendChild()</code>.
namespace System.Xml {
using System.Diagnostics;
using System.Xml.XPath;
// Represents a lightweight object that is useful for tree insert
// operations.
public class XmlDocumentFragment : XmlNode {
XmlLinkedNode lastChild;
protected internal XmlDocumentFragment( XmlDocument ownerDocument ): base( ) {
if ( ownerDocument == null )
throw new ArgumentException(Res.GetString(Res.Xdom_Node_Null_Doc));
parentNode= ownerDocument;
}
// Gets the name of the node.
public override String Name {
get { return OwnerDocument.strDocumentFragmentName;}
}
// Gets the name of the current node without the namespace prefix.
public override String LocalName {
get { return OwnerDocument.strDocumentFragmentName;}
}
// Gets the type of the current node.
public override XmlNodeType NodeType {
get { return XmlNodeType.DocumentFragment;}
}
// Gets the parent of this node (for nodes that can have parents).
public override XmlNode ParentNode {
get { return null;}
}
// Gets the XmlDocument that contains this node.
public override XmlDocument OwnerDocument {
get {
return (XmlDocument)parentNode;
}
}
// Gets or sets the markup representing just
// the children of this node.
public override string InnerXml {
get {
return base.InnerXml;
}
set {
RemoveAll();
XmlLoader loader = new XmlLoader();
//Hack that the content is the same element
loader.ParsePartialContent( this, value, XmlNodeType.Element );
}
}
// Creates a duplicate of this node.
public override XmlNode CloneNode(bool deep) {
Debug.Assert( OwnerDocument != null );
XmlDocument doc = OwnerDocument;
XmlDocumentFragment clone = doc.CreateDocumentFragment();
if (deep)
clone.CopyChildren( doc, this, deep );
return clone;
}
internal override bool IsContainer {
get { return true;}
}
internal override XmlLinkedNode LastNode {
get { return lastChild;}
set { lastChild = value;}
}
internal override bool IsValidChildType( XmlNodeType type ) {
switch (type) {
case XmlNodeType.Element:
case XmlNodeType.Text:
case XmlNodeType.EntityReference:
case XmlNodeType.Comment:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.CDATA:
return true;
case XmlNodeType.XmlDeclaration:
//if there is an XmlDeclaration node, it has to be the first node;
XmlNode firstNode = FirstChild;
if (firstNode == null || firstNode.NodeType != XmlNodeType.XmlDeclaration)
return true;
else
return false; //not allowed to insert a second XmlDeclaration node
default:
return false;
}
}
internal override bool CanInsertAfter( XmlNode newChild, XmlNode refChild ) {
Debug.Assert(newChild != null); //should be checked that newChild is not null before this function call
if (newChild.NodeType == XmlNodeType.XmlDeclaration) {
if (refChild == null) {
//append at the end
return (LastNode == null);
} else
return false;
}
return true;
}
internal override bool CanInsertBefore( XmlNode newChild, XmlNode refChild ) {
Debug.Assert(newChild != null); //should be checked that newChild is not null before this function call
if (newChild.NodeType == XmlNodeType.XmlDeclaration) {
return (refChild==null || refChild==FirstChild);
}
return true;
}
// Saves the node to the specified XmlWriter.
public override void WriteTo(XmlWriter w) {
WriteContentTo( w );
}
// Saves all the children of the node to the specified XmlWriter.
public override void WriteContentTo(XmlWriter w) {
foreach( XmlNode n in this ) {
n.WriteTo( w );
}
}
internal override XPathNodeType XPNodeType { get { return XPathNodeType.Root; } }
}
}

View File

@@ -0,0 +1,138 @@
//------------------------------------------------------------------------------
// <copyright file="XmlDocumentType.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System.Xml.Schema;
using System.Diagnostics;
// Contains information associated with the document type declaration.
public class XmlDocumentType : XmlLinkedNode {
string name;
string publicId;
string systemId;
string internalSubset;
bool namespaces;
XmlNamedNodeMap entities;
XmlNamedNodeMap notations;
// parsed DTD
SchemaInfo schemaInfo;
protected internal XmlDocumentType( string name, string publicId, string systemId, string internalSubset, XmlDocument doc ) : base( doc ) {
this.name = name;
this.publicId = publicId;
this.systemId = systemId;
this.namespaces = true;
this.internalSubset = internalSubset;
Debug.Assert( doc != null );
if ( !doc.IsLoading ) {
doc.IsLoading = true;
XmlLoader loader = new XmlLoader();
loader.ParseDocumentType( this ); //will edit notation nodes, etc.
doc.IsLoading = false;
}
}
// Gets the name of the node.
public override string Name {
get { return name;}
}
// Gets the name of the current node without the namespace prefix.
public override string LocalName {
get { return name;}
}
// Gets the type of the current node.
public override XmlNodeType NodeType {
get { return XmlNodeType.DocumentType;}
}
// Creates a duplicate of this node.
public override XmlNode CloneNode(bool deep) {
Debug.Assert( OwnerDocument != null );
return OwnerDocument.CreateDocumentType( name, publicId, systemId, internalSubset );
}
//
// Microsoft extensions
//
// Gets a value indicating whether the node is read-only.
public override bool IsReadOnly {
get {
return true; // Make entities and notations readonly
}
}
// Gets the collection of XmlEntity nodes declared in the document type declaration.
public XmlNamedNodeMap Entities {
get {
if (entities == null)
entities = new XmlNamedNodeMap( this );
return entities;
}
}
// Gets the collection of XmlNotation nodes present in the document type declaration.
public XmlNamedNodeMap Notations {
get {
if (notations == null)
notations = new XmlNamedNodeMap( this );
return notations;
}
}
//
// DOM Level 2
//
// Gets the value of the public identifier on the DOCTYPE declaration.
public string PublicId {
get { return publicId;}
}
// Gets the value of
// the system identifier on the DOCTYPE declaration.
public string SystemId {
get { return systemId;}
}
// Gets the entire value of the DTD internal subset
// on the DOCTYPE declaration.
public string InternalSubset {
get { return internalSubset;}
}
internal bool ParseWithNamespaces {
get { return namespaces; }
set { namespaces = value; }
}
// Saves the node to the specified XmlWriter.
public override void WriteTo(XmlWriter w) {
w.WriteDocType( name, publicId, systemId, internalSubset );
}
// Saves all the children of the node to the specified XmlWriter.
public override void WriteContentTo(XmlWriter w) {
// Intentionally do nothing
}
internal SchemaInfo DtdSchemaInfo {
get {
return schemaInfo;
}
set {
schemaInfo = value;
}
}
}
}

View File

@@ -0,0 +1,52 @@
//------------------------------------------------------------------------------
// <copyright file="XmlDomTextWriter.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System;
using System.IO;
using System.Text;
using System.Runtime.Versioning;
// Represents a writer that will make it possible to work with prefixes even
// if the namespace is not specified.
// This is not possible with XmlTextWriter. But this class inherits XmlTextWriter.
internal class XmlDOMTextWriter : XmlTextWriter {
public XmlDOMTextWriter( Stream w, Encoding encoding ) : base( w,encoding ) {
}
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
public XmlDOMTextWriter( String filename, Encoding encoding ) : base( filename,encoding ){
}
public XmlDOMTextWriter( TextWriter w ) : base( w ){
}
// Overrides the baseclass implementation so that emptystring prefixes do
// do not fail if namespace is not specified.
public override void WriteStartElement( string prefix, string localName, string ns ){
if( ( ns.Length == 0 ) && ( prefix.Length != 0 ) )
prefix = "" ;
base.WriteStartElement( prefix, localName, ns );
}
// Overrides the baseclass implementation so that emptystring prefixes do
// do not fail if namespace is not specified.
public override void WriteStartAttribute( string prefix, string localName, string ns ){
if( ( ns.Length == 0 ) && ( prefix.Length != 0 ) )
prefix = "" ;
base.WriteStartAttribute( prefix, localName, ns );
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,341 @@
//------------------------------------------------------------------------------
// <copyright file="XmlElementList.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System;
using System.Collections;
using System.Diagnostics;
internal class XmlElementList: XmlNodeList {
string asterisk;
int changeCount; //recording the total number that the dom tree has been changed ( insertion and deletetion )
//the member vars below are saved for further reconstruction
string name; //only one of 2 string groups will be initialized depends on which constructor is called.
string localName;
string namespaceURI;
XmlNode rootNode;
// the memeber vars belwo serves the optimization of accessing of the elements in the list
int curInd; // -1 means the starting point for a new search round
XmlNode curElem; // if sets to rootNode, means the starting point for a new search round
bool empty; // whether the list is empty
bool atomized; //whether the localname and namespaceuri are aomized
int matchCount; // cached list count. -1 means it needs reconstruction
WeakReference listener; // XmlElementListListener
private XmlElementList( XmlNode parent) {
Debug.Assert ( parent != null );
Debug.Assert( parent.NodeType == XmlNodeType.Element || parent.NodeType == XmlNodeType.Document );
this.rootNode = parent;
Debug.Assert( parent.Document != null );
this.curInd = -1;
this.curElem = rootNode;
this.changeCount = 0;
this.empty = false;
this.atomized = true;
this.matchCount = -1;
// This can be a regular reference, but it would cause some kind of loop inside the GC
this.listener = new WeakReference(new XmlElementListListener(parent.Document, this));
}
~XmlElementList() {
Dispose(false);
}
internal void ConcurrencyCheck(XmlNodeChangedEventArgs args){
if( atomized == false ) {
XmlNameTable nameTable = this.rootNode.Document.NameTable;
this.localName = nameTable.Add( this.localName );
this.namespaceURI = nameTable.Add( this.namespaceURI );
this.atomized = true;
}
if ( IsMatch( args.Node ) ) {
this.changeCount++ ;
this.curInd = -1;
this.curElem = rootNode;
if( args.Action == XmlNodeChangedAction.Insert )
this.empty = false;
}
this.matchCount = -1;
}
internal XmlElementList( XmlNode parent, string name ): this( parent ) {
Debug.Assert( parent.Document != null );
XmlNameTable nt = parent.Document.NameTable;
Debug.Assert( nt != null );
asterisk = nt.Add("*");
this.name = nt.Add( name );
this.localName = null;
this.namespaceURI = null;
}
internal XmlElementList( XmlNode parent, string localName, string namespaceURI ): this( parent ) {
Debug.Assert( parent.Document != null );
XmlNameTable nt = parent.Document.NameTable;
Debug.Assert( nt != null );
asterisk = nt.Add("*");
this.localName = nt.Get( localName );
this.namespaceURI = nt.Get( namespaceURI );
if( (this.localName == null) || (this.namespaceURI== null) ) {
this.empty = true;
this.atomized = false;
this.localName = localName;
this.namespaceURI = namespaceURI;
}
this.name = null;
}
internal int ChangeCount {
get { return changeCount; }
}
// return the next element node that is in PreOrder
private XmlNode NextElemInPreOrder( XmlNode curNode ) {
Debug.Assert( curNode != null );
//For preorder walking, first try its child
XmlNode retNode = curNode.FirstChild;
if ( retNode == null ) {
//if no child, the next node forward will the be the NextSibling of the first ancestor which has NextSibling
//so, first while-loop find out such an ancestor (until no more ancestor or the ancestor is the rootNode
retNode = curNode;
while ( retNode != null
&& retNode != rootNode
&& retNode.NextSibling == null ) {
retNode = retNode.ParentNode;
}
//then if such ancestor exists, set the retNode to its NextSibling
if ( retNode != null && retNode != rootNode )
retNode = retNode.NextSibling;
}
if ( retNode == this.rootNode )
//if reach the rootNode, consider having walked through the whole tree and no more element after the curNode
retNode = null;
return retNode;
}
// return the previous element node that is in PreOrder
private XmlNode PrevElemInPreOrder( XmlNode curNode ) {
Debug.Assert( curNode != null );
//For preorder walking, the previous node will be the right-most node in the tree of PreviousSibling of the curNode
XmlNode retNode = curNode.PreviousSibling;
// so if the PreviousSibling is not null, going through the tree down to find the right-most node
while ( retNode != null ) {
if ( retNode.LastChild == null )
break;
retNode = retNode.LastChild;
}
// if no PreviousSibling, the previous node will be the curNode's parentNode
if ( retNode == null )
retNode = curNode.ParentNode;
// if the final retNode is rootNode, consider having walked through the tree and no more previous node
if ( retNode == this.rootNode )
retNode = null;
return retNode;
}
// if the current node a matching element node
private bool IsMatch ( XmlNode curNode ) {
if (curNode.NodeType == XmlNodeType.Element) {
if ( this.name != null ) {
if ( Ref.Equal(this.name, asterisk) || Ref.Equal(curNode.Name, this.name) )
return true;
}
else {
if (
(Ref.Equal(this.localName, asterisk) || Ref.Equal(curNode.LocalName, this.localName) ) &&
(Ref.Equal(this.namespaceURI, asterisk) || curNode.NamespaceURI == this.namespaceURI )
) {
return true;
}
}
}
return false;
}
private XmlNode GetMatchingNode( XmlNode n, bool bNext ) {
Debug.Assert( n!= null );
XmlNode node = n;
do {
if ( bNext )
node = NextElemInPreOrder( node );
else
node = PrevElemInPreOrder( node );
} while ( node != null && !IsMatch( node ) );
return node;
}
private XmlNode GetNthMatchingNode( XmlNode n, bool bNext, int nCount ) {
Debug.Assert( n!= null );
XmlNode node = n;
for ( int ind = 0 ; ind < nCount; ind++ ) {
node = GetMatchingNode( node, bNext );
if ( node == null )
return null;
}
return node;
}
//the function is for the enumerator to find out the next available matching element node
public XmlNode GetNextNode( XmlNode n ) {
if( this.empty == true )
return null;
XmlNode node = ( n == null ) ? rootNode : n;
return GetMatchingNode( node, true );
}
public override XmlNode Item(int index) {
if ( rootNode == null || index < 0 )
return null;
if( this.empty == true )
return null;
if ( curInd == index )
return curElem;
int nDiff = index - curInd;
bool bForward = ( nDiff > 0 );
if ( nDiff < 0 )
nDiff = -nDiff;
XmlNode node;
if ( ( node = GetNthMatchingNode( curElem, bForward, nDiff ) ) != null ) {
curInd = index;
curElem = node;
return curElem;
}
return null;
}
public override int Count {
get {
if( this.empty == true )
return 0;
if (this.matchCount < 0) {
int currMatchCount = 0;
int currChangeCount = this.changeCount;
XmlNode node = rootNode;
while ((node = GetMatchingNode(node, true)) != null) {
currMatchCount++;
}
if (currChangeCount != this.changeCount) {
return currMatchCount;
}
this.matchCount = currMatchCount;
}
return this.matchCount;
}
}
public override IEnumerator GetEnumerator() {
if( this.empty == true )
return new XmlEmptyElementListEnumerator(this);;
return new XmlElementListEnumerator(this);
}
protected override void PrivateDisposeNodeList() {
GC.SuppressFinalize(this);
Dispose(true);
}
protected virtual void Dispose(bool disposing) {
if (this.listener != null) {
XmlElementListListener listener = (XmlElementListListener)this.listener.Target;
if (listener != null) {
listener.Unregister();
}
this.listener = null;
}
}
}
internal class XmlElementListEnumerator : IEnumerator {
XmlElementList list;
XmlNode curElem;
int changeCount; //save the total number that the dom tree has been changed ( insertion and deletetion ) when this enumerator is created
public XmlElementListEnumerator( XmlElementList list ) {
this.list = list;
this.curElem = null;
this.changeCount = list.ChangeCount;
}
public bool MoveNext() {
if ( list.ChangeCount != this.changeCount ) {
//the number mismatch, there is new change(s) happened since last MoveNext() is called.
throw new InvalidOperationException( Res.GetString(Res.Xdom_Enum_ElementList) );
}
else {
curElem = list.GetNextNode( curElem );
}
return curElem != null;
}
public void Reset() {
curElem = null;
//reset the number of changes to be synced with current dom tree as well
this.changeCount = list.ChangeCount;
}
public object Current {
get { return curElem; }
}
}
internal class XmlEmptyElementListEnumerator : IEnumerator {
public XmlEmptyElementListEnumerator( XmlElementList list ) {
}
public bool MoveNext() {
return false;
}
public void Reset() {
}
public object Current {
get { return null; }
}
}
internal class XmlElementListListener {
WeakReference elemList;
XmlDocument doc;
XmlNodeChangedEventHandler nodeChangeHandler = null;
internal XmlElementListListener(XmlDocument doc, XmlElementList elemList) {
this.doc = doc;
this.elemList = new WeakReference(elemList);
this.nodeChangeHandler = new XmlNodeChangedEventHandler( this.OnListChanged );
doc.NodeInserted += this.nodeChangeHandler;
doc.NodeRemoved += this.nodeChangeHandler;
}
private void OnListChanged( object sender, XmlNodeChangedEventArgs args ) {
lock (this) {
if (this.elemList != null) {
XmlElementList el = (XmlElementList)this.elemList.Target;
if (null != el) {
el.ConcurrencyCheck(args);
} else {
this.doc.NodeInserted -= this.nodeChangeHandler;
this.doc.NodeRemoved -= this.nodeChangeHandler;
this.elemList = null;
}
}
}
}
// This method is called from the finalizer of XmlElementList
internal void Unregister() {
lock (this) {
if (elemList != null) {
this.doc.NodeInserted -= this.nodeChangeHandler;
this.doc.NodeRemoved -= this.nodeChangeHandler;
this.elemList = null;
}
}
}
}
}

View File

@@ -0,0 +1,144 @@
//------------------------------------------------------------------------------
// <copyright file="XmlEntity.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System.Diagnostics;
// Represents a parsed or unparsed entity in the XML document.
public class XmlEntity : XmlNode {
string publicId;
string systemId;
String notationName;
String name;
String unparsedReplacementStr;
String baseURI;
XmlLinkedNode lastChild;
private bool childrenFoliating;
internal XmlEntity( String name, String strdata, string publicId, string systemId, String notationName, XmlDocument doc ) : base( doc ) {
this.name = doc.NameTable.Add(name);
this.publicId = publicId;
this.systemId = systemId;
this.notationName = notationName;
this.unparsedReplacementStr = strdata;
this.childrenFoliating = false;
}
// Throws an excption since an entity can not be cloned.
public override XmlNode CloneNode(bool deep) {
throw new InvalidOperationException(Res.GetString(Res.Xdom_Node_Cloning));
}
//
// Microsoft extensions
//
// Gets a value indicating whether the node is read-only.
public override bool IsReadOnly {
get {
return true; // Make entities readonly
}
}
// Gets the name of the node.
public override string Name {
get { return name;}
}
// Gets the name of the node without the namespace prefix.
public override string LocalName {
get { return name;}
}
// Gets the concatenated values of the entity node and all its children.
// The property is read-only and when tried to be set, exception will be thrown.
public override string InnerText {
get { return base.InnerText; }
set {
throw new InvalidOperationException(Res.GetString(Res.Xdom_Ent_Innertext));
}
}
internal override bool IsContainer {
get { return true;}
}
internal override XmlLinkedNode LastNode {
get {
if (lastChild == null && !childrenFoliating)
{ //expand the unparsedreplacementstring
childrenFoliating = true;
//wrap the replacement string with an element
XmlLoader loader = new XmlLoader();
loader.ExpandEntity(this);
}
return lastChild;
}
set { lastChild = value;}
}
internal override bool IsValidChildType( XmlNodeType type ) {
return(type == XmlNodeType.Text ||
type == XmlNodeType.Element ||
type == XmlNodeType.ProcessingInstruction ||
type == XmlNodeType.Comment ||
type == XmlNodeType.CDATA ||
type == XmlNodeType.Whitespace ||
type == XmlNodeType.SignificantWhitespace ||
type == XmlNodeType.EntityReference);
}
// Gets the type of the node.
public override XmlNodeType NodeType {
get { return XmlNodeType.Entity;}
}
// Gets the value of the public identifier on the entity declaration.
public String PublicId {
get { return publicId;}
}
// Gets the value of the system identifier on the entity declaration.
public String SystemId {
get { return systemId;}
}
// Gets the name of the optional NDATA attribute on the
// entity declaration.
public String NotationName {
get { return notationName;}
}
//Without override these two functions, we can't guarantee that WriteTo()/WriteContent() functions will never be called
public override String OuterXml {
get { return String.Empty; }
}
public override String InnerXml {
get { return String.Empty; }
set { throw new InvalidOperationException( Res.GetString(Res.Xdom_Set_InnerXml ) ); }
}
// Saves the node to the specified XmlWriter.
public override void WriteTo(XmlWriter w) {
}
// Saves all the children of the node to the specified XmlWriter.
public override void WriteContentTo(XmlWriter w) {
}
public override String BaseURI {
get { return baseURI; }
}
internal void SetBaseURI( String inBaseURI ) {
baseURI = inBaseURI;
}
}
}

Some files were not shown because too many files have changed in this diff Show More