| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- //------------------------------------------------------------------------------
- // <copyright file="XPathNode.cs" company="Microsoft">
- // Copyright (c) Microsoft Corporation. All rights reserved.
- // </copyright>
- // <owner current="true" primary="true">Microsoft</owner>
- //------------------------------------------------------------------------------
- using System;
- using System.Diagnostics;
- using System.Text;
- using System.Xml;
- using System.Xml.XPath;
- using System.Xml.Schema;
- namespace MS.Internal.Xml.Cache {
- /// <summary>
- /// Implementation of a Node in the XPath/XQuery data model.
- /// 1. All nodes are stored in variable-size pages (max 65536 nodes/page) of XPathNode structures.
- /// 2. Pages are sequentially numbered. Nodes are allocated in strict document order.
- /// 3. Node references take the form of a (page, index) pair.
- /// 4. Each node explicitly stores a parent and a sibling reference.
- /// 5. If a node has one or more attributes and/or non-collapsed content children, then its first
- /// child is stored in the next slot. If the node is in the last slot of a page, then its first
- /// child is stored in the first slot of the next page.
- /// 6. Attributes are linked together at the start of the child list.
- /// 7. Namespaces are allocated in totally separate pages. Elements are associated with
- /// declared namespaces via a hashtable map in the document.
- /// 8. Name parts are always non-null (string.Empty for nodes without names)
- /// 9. XPathNodeInfoAtom contains all information that is common to many nodes in a
- /// document, and therefore is atomized to save space. This includes the document, the name,
- /// the child, sibling, parent, and value pages, and the schema type.
- /// 10. The node structure is 20 bytes in length. Out-of-line overhead is typically 2-4 bytes per node.
- /// </summary>
- internal struct XPathNode {
- private XPathNodeInfoAtom info; // Atomized node information
- private ushort idxSibling; // Page index of sibling node
- private ushort idxParent; // Page index of parent node
- private ushort idxSimilar; // Page index of next node in document order that has local name with same hashcode
- private ushort posOffset; // Line position offset of node (added to LinePositionBase)
- private uint props; // Node properties (broken down into bits below)
- private string value; // String value of node
- private const uint NodeTypeMask = 0xF;
- private const uint HasAttributeBit = 0x10;
- private const uint HasContentChildBit = 0x20;
- private const uint HasElementChildBit = 0x40;
- private const uint HasCollapsedTextBit = 0x80;
- private const uint AllowShortcutTagBit = 0x100; // True if this is an element that allows shortcut tag syntax
- private const uint HasNmspDeclsBit = 0x200; // True if this is an element with namespace declarations declared on it
- private const uint LineNumberMask = 0x00FFFC00; // 14 bits for line number offset (0 - 16K)
- private const int LineNumberShift = 10;
- private const int CollapsedPositionShift = 24; // 8 bits for collapsed text position offset (0 - 256)
- #if DEBUG
- public const int MaxLineNumberOffset = 0x20;
- public const int MaxLinePositionOffset = 0x20;
- public const int MaxCollapsedPositionOffset = 0x10;
- #else
- public const int MaxLineNumberOffset = 0x3FFF;
- public const int MaxLinePositionOffset = 0xFFFF;
- public const int MaxCollapsedPositionOffset = 0xFF;
- #endif
- /// <summary>
- /// Returns the type of this node
- /// </summary>
- public XPathNodeType NodeType {
- get { return (XPathNodeType) (this.props & NodeTypeMask); }
- }
- /// <summary>
- /// Returns the namespace prefix of this node. If this node has no prefix, then the empty string
- /// will be returned (never null).
- /// </summary>
- public string Prefix {
- get { return this.info.Prefix; }
- }
- /// <summary>
- /// Returns the local name of this node. If this node has no name, then the empty string
- /// will be returned (never null).
- /// </summary>
- public string LocalName {
- get { return this.info.LocalName; }
- }
- /// <summary>
- /// Returns the name of this node. If this node has no name, then the empty string
- /// will be returned (never null).
- /// </summary>
- public string Name {
- get {
- if (Prefix.Length == 0) {
- return LocalName;
- }
- else {
- return string.Concat(Prefix, ":", LocalName);
- }
- }
- }
- /// <summary>
- /// Returns the namespace part of this node's name. If this node has no name, then the empty string
- /// will be returned (never null).
- /// </summary>
- public string NamespaceUri {
- get { return this.info.NamespaceUri; }
- }
- /// <summary>
- /// Returns this node's document.
- /// </summary>
- public XPathDocument Document {
- get { return this.info.Document; }
- }
- /// <summary>
- /// Returns this node's base Uri. This is string.Empty for all node kinds except Element, Root, and PI.
- /// </summary>
- public string BaseUri {
- get { return this.info.BaseUri; }
- }
- /// <summary>
- /// Returns this node's source line number.
- /// </summary>
- public int LineNumber {
- get { return this.info.LineNumberBase + (int) ((this.props & LineNumberMask) >> LineNumberShift); }
- }
- /// <summary>
- /// Return this node's source line position.
- /// </summary>
- public int LinePosition {
- get { return this.info.LinePositionBase + (int) this.posOffset; }
- }
- /// <summary>
- /// If this node is an element with collapsed text, then return the source line position of the node (the
- /// source line number is the same as LineNumber).
- /// </summary>
- public int CollapsedLinePosition {
- get {
- Debug.Assert(HasCollapsedText, "Do not call CollapsedLinePosition unless HasCollapsedText is true.");
- return LinePosition + (int) (this.props >> CollapsedPositionShift);
- }
- }
- /// <summary>
- /// Returns information about the node page. Only the 0th node on each page has this property defined.
- /// </summary>
- public XPathNodePageInfo PageInfo {
- get { return this.info.PageInfo; }
- }
- /// <summary>
- /// Returns the root node of the current document. This always succeeds.
- /// </summary>
- public int GetRoot(out XPathNode[] pageNode) {
- return this.info.Document.GetRootNode(out pageNode);
- }
- /// <summary>
- /// Returns the parent of this node. If this node has no parent, then 0 is returned.
- /// </summary>
- public int GetParent(out XPathNode[] pageNode) {
- pageNode = this.info.ParentPage;
- return this.idxParent;
- }
- /// <summary>
- /// Returns the next sibling of this node. If this node has no next sibling, then 0 is returned.
- /// </summary>
- public int GetSibling(out XPathNode[] pageNode) {
- pageNode = this.info.SiblingPage;
- return this.idxSibling;
- }
- /// <summary>
- /// Returns the next element in document order that has the same local name hashcode as this element.
- /// If there are no similar elements, then 0 is returned.
- /// </summary>
- public int GetSimilarElement(out XPathNode[] pageNode) {
- pageNode = this.info.SimilarElementPage;
- return this.idxSimilar;
- }
- /// <summary>
- /// Returns true if this node's name matches the specified localName and namespaceName. Assume
- /// that localName has been atomized, but namespaceName has not.
- /// </summary>
- public bool NameMatch(string localName, string namespaceName) {
- Debug.Assert(localName == null || (object) Document.NameTable.Get(localName) == (object) localName, "localName must be atomized.");
- return (object) this.info.LocalName == (object) localName &&
- this.info.NamespaceUri == namespaceName;
- }
- /// <summary>
- /// Returns true if this is an Element node with a name that matches the specified localName and
- /// namespaceName. Assume that localName has been atomized, but namespaceName has not.
- /// </summary>
- public bool ElementMatch(string localName, string namespaceName) {
- Debug.Assert(localName == null || (object) Document.NameTable.Get(localName) == (object) localName, "localName must be atomized.");
- return NodeType == XPathNodeType.Element &&
- (object) this.info.LocalName == (object) localName &&
- this.info.NamespaceUri == namespaceName;
- }
- /// <summary>
- /// Return true if this node is an xmlns:xml node.
- /// </summary>
- public bool IsXmlNamespaceNode {
- get {
- string localName = this.info.LocalName;
- return NodeType == XPathNodeType.Namespace && localName.Length == 3 && localName == "xml";
- }
- }
- /// <summary>
- /// Returns true if this node has a sibling.
- /// </summary>
- public bool HasSibling {
- get { return this.idxSibling != 0; }
- }
- /// <summary>
- /// Returns true if this node has a collapsed text node as its only content-typed child.
- /// </summary>
- public bool HasCollapsedText {
- get { return (this.props & HasCollapsedTextBit) != 0; }
- }
- /// <summary>
- /// Returns true if this node has at least one attribute.
- /// </summary>
- public bool HasAttribute {
- get { return (this.props & HasAttributeBit) != 0; }
- }
- /// <summary>
- /// Returns true if this node has at least one content-typed child (attributes and namespaces
- /// don't count).
- /// </summary>
- public bool HasContentChild {
- get { return (this.props & HasContentChildBit) != 0; }
- }
- /// <summary>
- /// Returns true if this node has at least one element child.
- /// </summary>
- public bool HasElementChild {
- get { return (this.props & HasElementChildBit) != 0; }
- }
- /// <summary>
- /// Returns true if this is an attribute or namespace node.
- /// </summary>
- public bool IsAttrNmsp {
- get {
- XPathNodeType xptyp = NodeType;
- return xptyp == XPathNodeType.Attribute || xptyp == XPathNodeType.Namespace;
- }
- }
- /// <summary>
- /// Returns true if this is a text or whitespace node.
- /// </summary>
- public bool IsText {
- get { return XPathNavigator.IsText(NodeType); }
- }
- /// <summary>
- /// Returns true if this node has local namespace declarations associated with it. Since all
- /// namespace declarations are stored out-of-line in the owner Document, this property
- /// can be consulted in order to avoid a lookup in the common case where this node has no
- /// local namespace declarations.
- /// </summary>
- public bool HasNamespaceDecls {
- get { return (this.props & HasNmspDeclsBit) != 0; }
- set {
- if (value) this.props |= HasNmspDeclsBit;
- else unchecked { this.props &= (byte) ~((uint) HasNmspDeclsBit); }
- }
- }
- /// <summary>
- /// Returns true if this node is an empty element that allows shortcut tag syntax.
- /// </summary>
- public bool AllowShortcutTag {
- get { return (this.props & AllowShortcutTagBit) != 0; }
- }
- /// <summary>
- /// Cached hashcode computed over the local name of this element.
- /// </summary>
- public int LocalNameHashCode {
- get { return this.info.LocalNameHashCode; }
- }
- /// <summary>
- /// Return the precomputed String value of this node (null if no value exists, i.e. document node, element node with complex content, etc).
- /// </summary>
- public string Value {
- get { return this.value; }
- }
- //-----------------------------------------------
- // Node construction
- //-----------------------------------------------
- /// <summary>
- /// Constructs the 0th XPathNode in each page, which contains only page information.
- /// </summary>
- public void Create(XPathNodePageInfo pageInfo) {
- this.info = new XPathNodeInfoAtom(pageInfo);
- }
- /// <summary>
- /// Constructs a XPathNode. Later, the idxSibling and value fields may be fixed up.
- /// </summary>
- public void Create(XPathNodeInfoAtom info, XPathNodeType xptyp, int idxParent) {
- Debug.Assert(info != null && idxParent <= UInt16.MaxValue);
- this.info = info;
- this.props = (uint) xptyp;
- this.idxParent = (ushort) idxParent;
- }
- /// <summary>
- /// Set this node's line number information.
- /// </summary>
- public void SetLineInfoOffsets(int lineNumOffset, int linePosOffset) {
- Debug.Assert(lineNumOffset >= 0 && lineNumOffset <= MaxLineNumberOffset, "Line number offset too large or small: " + lineNumOffset);
- Debug.Assert(linePosOffset >= 0 && linePosOffset <= MaxLinePositionOffset, "Line position offset too large or small: " + linePosOffset);
- this.props |= ((uint) lineNumOffset << LineNumberShift);
- this.posOffset = (ushort) linePosOffset;
- }
- /// <summary>
- /// Set the position offset of this element's collapsed text.
- /// </summary>
- public void SetCollapsedLineInfoOffset(int posOffset) {
- Debug.Assert(posOffset >= 0 && posOffset <= MaxCollapsedPositionOffset, "Collapsed text line position offset too large or small: " + posOffset);
- this.props |= ((uint) posOffset << CollapsedPositionShift);
- }
- /// <summary>
- /// Set this node's value.
- /// </summary>
- public void SetValue(string value) {
- this.value = value;
- }
- /// <summary>
- /// Create an empty element value.
- /// </summary>
- public void SetEmptyValue(bool allowShortcutTag) {
- Debug.Assert(NodeType == XPathNodeType.Element);
- this.value = string.Empty;
- if (allowShortcutTag)
- this.props |= AllowShortcutTagBit;
- }
- /// <summary>
- /// Create a collapsed text node on this element having the specified value.
- /// </summary>
- public void SetCollapsedValue(string value) {
- Debug.Assert(NodeType == XPathNodeType.Element);
- this.value = value;
- this.props |= HasContentChildBit | HasCollapsedTextBit;
- }
- /// <summary>
- /// This method is called when a new child is appended to this node's list of attributes and children.
- /// The type of the new child is used to determine how various parent properties should be set.
- /// </summary>
- public void SetParentProperties(XPathNodeType xptyp) {
- if (xptyp == XPathNodeType.Attribute) {
- this.props |= HasAttributeBit;
- }
- else {
- this.props |= HasContentChildBit;
- if (xptyp == XPathNodeType.Element)
- this.props |= HasElementChildBit;
- }
- }
-
- /// <summary>
- /// Link this node to its next sibling. If "pageSibling" is different than the one stored in the InfoAtom, re-atomize.
- /// </summary>
- public void SetSibling(XPathNodeInfoTable infoTable, XPathNode[] pageSibling, int idxSibling) {
- Debug.Assert(pageSibling != null && idxSibling != 0 && idxSibling <= UInt16.MaxValue, "Bad argument");
- Debug.Assert(this.idxSibling == 0, "SetSibling should not be called more than once.");
- this.idxSibling = (ushort) idxSibling;
- if (pageSibling != this.info.SiblingPage) {
- // Re-atomize the InfoAtom
- this.info = infoTable.Create(this.info.LocalName, this.info.NamespaceUri, this.info.Prefix, this.info.BaseUri,
- this.info.ParentPage, pageSibling, this.info.SimilarElementPage,
- this.info.Document, this.info.LineNumberBase, this.info.LinePositionBase);
- }
- }
-
- /// <summary>
- /// Link this element to the next element in document order that shares a local name having the same hash code.
- /// If "pageSimilar" is different than the one stored in the InfoAtom, re-atomize.
- /// </summary>
- public void SetSimilarElement(XPathNodeInfoTable infoTable, XPathNode[] pageSimilar, int idxSimilar) {
- Debug.Assert(pageSimilar != null && idxSimilar != 0 && idxSimilar <= UInt16.MaxValue, "Bad argument");
- Debug.Assert(this.idxSimilar == 0, "SetSimilarElement should not be called more than once.");
- this.idxSimilar = (ushort) idxSimilar;
- if (pageSimilar != this.info.SimilarElementPage) {
- // Re-atomize the InfoAtom
- this.info = infoTable.Create(this.info.LocalName, this.info.NamespaceUri, this.info.Prefix, this.info.BaseUri,
- this.info.ParentPage, this.info.SiblingPage, pageSimilar,
- this.info.Document, this.info.LineNumberBase, this.info.LinePositionBase);
- }
- }
- }
- /// <summary>
- /// A reference to a XPathNode is composed of two values: the page on which the node is located, and the node's
- /// index in the page.
- /// </summary>
- internal struct XPathNodeRef {
- private XPathNode[] page;
- private int idx;
- public static XPathNodeRef Null {
- get { return new XPathNodeRef(); }
- }
- public XPathNodeRef(XPathNode[] page, int idx) {
- this.page = page;
- this.idx = idx;
- }
- public bool IsNull {
- get { return this.page == null; }
- }
- public XPathNode[] Page {
- get { return this.page; }
- }
- public int Index {
- get { return this.idx; }
- }
- public override int GetHashCode() {
- return XPathNodeHelper.GetLocation(this.page, this.idx);
- }
- }
- }
|