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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
//------------------------------------------------------------------------------
// <copyright file="Shape.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">derekdb</owner>
//------------------------------------------------------------------------------
#if ENABLEDATABINDING
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Collections;
using System.Diagnostics;
using System.ComponentModel;
using System.Text;
namespace System.Xml.XPath.DataBinding
{
internal enum BindingType {
Text,
Element,
Attribute,
ElementNested,
Repeat,
Sequence,
Choice,
All
}
internal sealed class Shape
{
string name;
BindingType bindingType;
ArrayList particles; // XmlSchemaElement or XmlSchemaAttribute
ArrayList subShapes;
Shape nestedShape;
PropertyDescriptor[] propertyDescriptors;
XmlSchemaElement containerDecl;
static object[] emptyIList = new object[0];
public Shape(string name, BindingType bindingType)
{
this.name = name;
this.bindingType = bindingType;
}
public string Name {
get { return this.name; }
set { this.name = value; }
}
public BindingType BindingType {
get { return this.bindingType; }
set { this.bindingType = value; }
}
public XmlSchemaElement ContainerDecl {
get { return this.containerDecl; }
set { this.containerDecl = value; }
}
public bool IsNestedTable {
get {
switch (this.BindingType) {
case BindingType.ElementNested:
case BindingType.Repeat:
case BindingType.Sequence:
case BindingType.Choice:
case BindingType.All:
return true;
default:
return false;
}
}
}
public bool IsGroup {
get {
switch (this.BindingType) {
case BindingType.Sequence:
case BindingType.Choice:
case BindingType.All:
return true;
default:
return false;
}
}
}
public XmlSchemaType SchemaType {
get {
switch (this.bindingType) {
case BindingType.Text:
case BindingType.Element:
case BindingType.ElementNested: {
Debug.Assert(this.particles.Count == 1);
XmlSchemaElement xse = (XmlSchemaElement)this.particles[0];
return xse.ElementSchemaType;
}
case BindingType.Attribute: {
Debug.Assert(this.particles.Count == 1);
XmlSchemaAttribute xsa = (XmlSchemaAttribute)this.particles[0];
return xsa.AttributeSchemaType;
}
default:
return null;
}
}
}
public XmlSchemaElement XmlSchemaElement {
get {
switch (this.bindingType) {
case BindingType.Text:
case BindingType.Element:
case BindingType.ElementNested: {
Debug.Assert(this.particles.Count == 1);
return (XmlSchemaElement)this.particles[0];
}
default:
return this.containerDecl;
}
}
}
public IList Particles {
get {
if (null == this.particles)
return emptyIList;
return this.particles;
}
}
public IList SubShapes {
get {
if (null == this.subShapes)
return emptyIList;
return this.subShapes;
}
}
public Shape SubShape(int i) {
return (Shape)SubShapes[i];
}
public Shape NestedShape {
get {
//Debug.Assert(this.bindingType == BindingType.ElementNested);
return this.nestedShape;
}
set {
this.nestedShape = value;
}
}
public XmlQualifiedName AttributeName {
get {
Debug.Assert(this.bindingType == BindingType.Attribute);
XmlSchemaAttribute xsa = (XmlSchemaAttribute)this.particles[0];
return xsa.QualifiedName;
}
}
public void Clear() {
if (this.subShapes != null) {
this.subShapes.Clear();
this.subShapes = null;
}
if (this.particles != null) {
this.particles.Clear();
this.particles = null;
}
}
public void AddParticle(XmlSchemaElement elem) {
if (null == this.particles)
this.particles = new ArrayList();
Debug.Assert(this.bindingType != BindingType.Attribute);
this.particles.Add(elem);
}
public void AddParticle(XmlSchemaAttribute elem) {
Debug.Assert(this.bindingType == BindingType.Attribute);
Debug.Assert(this.particles == null);
this.particles = new ArrayList();
this.particles.Add(elem);
}
public void AddSubShape(Shape shape) {
if (null == this.subShapes)
this.subShapes = new ArrayList();
this.subShapes.Add(shape);
foreach (object p in shape.Particles) {
XmlSchemaElement xse = p as XmlSchemaElement;
if (null != xse)
AddParticle(xse);
}
}
public void AddAttrShapeAt(Shape shape, int pos) {
if (null == this.subShapes)
this.subShapes = new ArrayList();
this.subShapes.Insert(pos, shape);
}
public string[] SubShapeNames() {
string[] names = new string[SubShapes.Count];
for (int i=0; i<SubShapes.Count; i++)
names[i] = this.SubShape(i).Name;
return names;
}
public PropertyDescriptor[] PropertyDescriptors {
get {
if (null == this.propertyDescriptors) {
PropertyDescriptor[] descs;
switch (this.BindingType) {
case BindingType.Element:
case BindingType.Text:
case BindingType.Attribute:
case BindingType.Repeat:
descs = new PropertyDescriptor[1];
descs[0] = new XPathNodeViewPropertyDescriptor(this);
break;
case BindingType.ElementNested:
descs = this.nestedShape.PropertyDescriptors;
break;
case BindingType.Sequence:
case BindingType.Choice:
case BindingType.All:
descs = new PropertyDescriptor[SubShapes.Count];
for (int i=0; i < descs.Length; i++) {
descs[i] = new XPathNodeViewPropertyDescriptor(this, this.SubShape(i), i);
}
break;
default:
throw new NotSupportedException();
}
this.propertyDescriptors = descs;
}
return this.propertyDescriptors;
}
}
public int FindNamedSubShape(string name) {
for (int i=0; i<SubShapes.Count; i++) {
Shape shape = SubShape(i);
if (shape.Name == name)
return i;
}
return -1;
}
public int FindMatchingSubShape(object particle) {
for (int i=0; i<SubShapes.Count; i++) {
Shape shape = SubShape(i);
if (shape.IsParticleMatch(particle))
return i;
}
return -1;
}
public bool IsParticleMatch(object particle) {
for (int i=0; i<this.particles.Count; i++) {
if (particle == this.particles[i])
return true;
}
return false;
}
#if DEBUG
public string DebugDump() {
StringBuilder sb = new StringBuilder();
DebugDump(sb,"");
return sb.ToString();
}
void DebugDump(StringBuilder sb, String indent) {
sb.AppendFormat("{0}{1} '{2}'", indent, this.BindingType.ToString(), this.Name);
if (this.subShapes != null) {
sb.AppendLine(" {");
string subindent = String.Concat(indent, " ");
foreach (Shape s in this.SubShapes) {
s.DebugDump(sb, subindent);
}
sb.Append(indent);
sb.Append('}');
}
sb.AppendLine();
}
#endif
}
}
#endif
|