1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
//------------------------------------------------------------------------------
// <copyright file="XmlHierarchyData.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI.WebControls {
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing.Design;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Xml;
using AttributeCollection = System.ComponentModel.AttributeCollection;
/// <devdoc>
/// Represents the data associated with a single hierarchy item - in this case an XmlNode.
/// </devdoc>
internal sealed class XmlHierarchyData : IHierarchyData, ICustomTypeDescriptor {
private XmlNode _item;
private XmlHierarchicalEnumerable _parent;
private string _path;
/// <devdoc>
/// Create a new instance of XmlHierarchyData.
/// </devdoc>
internal XmlHierarchyData(XmlHierarchicalEnumerable parent, XmlNode item) {
_parent = parent;
_item = item;
}
/// <devdoc>
/// Creates a path to a given XmlNode from the root using XPath.
/// </devdoc>
private string CreateRecursivePath(XmlNode node) {
if (node.ParentNode == null)
return String.Empty;
return CreateRecursivePath(node.ParentNode) + FindNodePosition(node);
}
/// <devdoc>
/// Finds a node's position relative to its parent.
/// </devdoc>
private string FindNodePosition(XmlNode node) {
XmlNodeList nodeList = node.ParentNode.ChildNodes;
int index = 0;
for (int i = 0; i < nodeList.Count; i++) {
// Position only considers elements, not other node types
if (nodeList[i].NodeType == XmlNodeType.Element)
index++;
if (nodeList[i] == node)
return "/*[position()=" + Convert.ToString(index, CultureInfo.InvariantCulture) + "]";
}
throw new ArgumentException(SR.GetString(SR.XmlHierarchyData_CouldNotFindNode));
}
public override string ToString() {
return _item.Name;
}
bool IHierarchyData.HasChildren {
get {
return _item.HasChildNodes;
}
}
object IHierarchyData.Item {
get {
return _item;
}
}
string IHierarchyData.Path {
get {
if (_path == null) {
// If we don't yet have a path, create one and cache it
if (_parent != null) {
// If we have a parent enumerable, then our path is the parent's
// path plus our position within that parent.
if (_parent.Path == null) {
_parent.Path = CreateRecursivePath(_item.ParentNode);
}
_path = _parent.Path + FindNodePosition(_item);
}
else {
// If we are not associated with a parent enumerable, we
// have to build up our entire path from scratch.
_path = CreateRecursivePath(_item);
}
}
return _path;
}
}
string IHierarchyData.Type {
get {
return _item.Name;
}
}
IHierarchicalEnumerable IHierarchyData.GetChildren() {
return new XmlHierarchicalEnumerable(_item.ChildNodes);
}
IHierarchyData IHierarchyData.GetParent() {
XmlNode parentNode = _item.ParentNode;
if (parentNode == null)
return null;
return new XmlHierarchyData(null, parentNode);
}
AttributeCollection ICustomTypeDescriptor.GetAttributes() {
return AttributeCollection.Empty;
}
string ICustomTypeDescriptor.GetClassName() {
return GetType().Name;
}
string ICustomTypeDescriptor.GetComponentName() {
return null;
}
TypeConverter ICustomTypeDescriptor.GetConverter() {
return null;
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() {
return null;
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() {
return new XmlHierarchyDataPropertyDescriptor("#Name");
}
object ICustomTypeDescriptor.GetEditor(Type editorBaseType) {
return null;
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents() {
return null;
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attrs) {
return null;
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() {
return ((ICustomTypeDescriptor)this).GetProperties(null);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attrFilter) {
System.Collections.Generic.List<PropertyDescriptor> list = new System.Collections.Generic.List<PropertyDescriptor>();
list.Add(new XmlHierarchyDataPropertyDescriptor("#Name"));
list.Add(new XmlHierarchyDataPropertyDescriptor("#Value"));
list.Add(new XmlHierarchyDataPropertyDescriptor("#InnerText"));
XmlAttributeCollection attrs = _item.Attributes;
if (attrs != null) {
for (int i = 0; i < attrs.Count; i++) {
list.Add(new XmlHierarchyDataPropertyDescriptor(attrs[i].Name));
}
}
return new PropertyDescriptorCollection(list.ToArray());
}
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd) {
if (pd is XmlHierarchyDataPropertyDescriptor) {
return this;
}
return null;
}
private class XmlHierarchyDataPropertyDescriptor : PropertyDescriptor {
private string _name;
public XmlHierarchyDataPropertyDescriptor(string name) : base(name, null) {
_name = name;
}
public override Type ComponentType {
get {
return typeof(XmlHierarchyData);
}
}
public override bool IsReadOnly {
get {
return true;
}
}
public override Type PropertyType {
get {
return typeof(string);
}
}
public override bool CanResetValue(object o) {
return false;
}
public override object GetValue(object o) {
XmlHierarchyData data = o as XmlHierarchyData;
if (data != null) {
switch (_name) {
case "#Name":
return data._item.Name;
case "#Value":
return data._item.Value;
case "#InnerText":
return data._item.InnerText;
default:
XmlAttributeCollection attrs = data._item.Attributes;
if (attrs != null) {
XmlAttribute attr = attrs[_name];
if (attr != null) {
return attr.Value;
}
}
break;
}
}
return String.Empty;
}
public override void ResetValue(object o) {
return;
}
public override void SetValue(object o, object value) {
}
public override bool ShouldSerializeValue(object o) {
return true;
}
}
}
}
|