1 //------------------------------------------------------------------------------
2 // <copyright file="XmlQualifiedNameTest.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
10 using System.Xml.Schema;
11 using System.Collections.Generic;
12 using System.Diagnostics;
15 namespace System.Xml.Xsl {
18 /// XmlQualifiedName extends XmlQualifiedName to support wildcards and adds nametest functionality
19 /// Following are the examples:
20 /// {A}:B XmlQualifiedNameTest.New("B", "A") Match QName with namespace A and local name B
21 /// * XmlQualifiedNameTest.New(null, null) Match any QName
22 /// {A}:* XmlQualifiedNameTest.New(null, "A") Match QName with namespace A and any local name
23 /// XmlQualifiedNameTest.New("A", false)
24 /// *:B XmlQualifiedNameTest.New("B", null) Match QName with any namespace and local name B
25 /// ~{A}:* XmlQualifiedNameTest.New("B", "A") Match QName with namespace not A and any local name
26 /// {~A}:B only as a result of the intersection Match QName with namespace not A and local name B
28 internal class XmlQualifiedNameTest : XmlQualifiedName {
30 const string wildcard = "*";
31 static XmlQualifiedNameTest wc = XmlQualifiedNameTest.New(wildcard, wildcard);
36 public static XmlQualifiedNameTest Wildcard {
43 private XmlQualifiedNameTest(string name, string ns, bool exclude) : base (name, ns) {
44 this.exclude = exclude;
48 /// Construct new from name and namespace. Returns singleton Wildcard in case full wildcard
50 public static XmlQualifiedNameTest New(string name, string ns) {
51 if (ns == null && name == null) {
55 return new XmlQualifiedNameTest(name == null ? wildcard : name, ns == null ? wildcard : ns, false);
60 /// True if matches any name and any namespace
62 public bool IsWildcard {
63 get { return (object)this == (object)Wildcard; }
67 /// True if matches any name
69 public bool IsNameWildcard {
70 get { return (object)this.Name == (object)wildcard; }
74 /// True if matches any namespace
76 public bool IsNamespaceWildcard {
77 get { return (object)this.Namespace == (object)wildcard; }
80 private bool IsNameSubsetOf(XmlQualifiedNameTest other) {
81 return other.IsNameWildcard || this.Name == other.Name;
85 private bool IsNamespaceSubsetOf(XmlQualifiedNameTest other) {
86 return other.IsNamespaceWildcard
87 || (this.exclude == other.exclude && this.Namespace == other.Namespace)
88 || (other.exclude && !this.exclude && this.Namespace != other.Namespace);
92 /// True if this matches every QName other does
94 public bool IsSubsetOf(XmlQualifiedNameTest other) {
95 return IsNameSubsetOf(other) && IsNamespaceSubsetOf(other);
99 /// Return true if the result of intersection with other is not empty
101 public bool HasIntersection(XmlQualifiedNameTest other) {
102 return (IsNamespaceSubsetOf(other) || other.IsNamespaceSubsetOf(this)) && (IsNameSubsetOf(other) || other.IsNameSubsetOf(this));
106 /// String representation
108 public override string ToString() {
109 if ((object)this == (object)Wildcard) {
113 if (this.Namespace.Length == 0) {
116 else if ((object)this.Namespace == (object)wildcard) {
117 return "*:" + this.Name;
119 else if (this.exclude) {
120 return "{~" + this.Namespace + "}:" + this.Name;
123 return "{" + this.Namespace + "}:" + this.Name;
130 /// Construct new from XmlQualifiedName. Returns singleton Wildcard in case full wildcard
132 public static XmlQualifiedNameTest New(XmlQualifiedName name) {
137 return new XmlQualifiedNameTest(name.Name, name.Namespace, false);
142 /// Construct new "exclusion" name test
144 public static XmlQualifiedNameTest New(string ns, bool exclude) {
145 Debug.Assert(ns != null);
146 return new XmlQualifiedNameTest(wildcard, ns, exclude);
150 /// Return the result of intersection with other
152 public XmlQualifiedNameTest Intersect(XmlQualifiedNameTest other) {
155 // ~x x=y ? this|other : null this x!=y ? other : null
156 // * other this|other other
157 // x x!=y ? this : null this x=y ? this|other : null
158 XmlQualifiedNameTest namespaceFrom = IsNamespaceSubsetOf(other) ? this : other.IsNamespaceSubsetOf(this) ? other : null;
159 XmlQualifiedNameTest nameFrom = IsNameSubsetOf(other) ? this : other.IsNameSubsetOf(this) ? other : null;
161 if ((object)namespaceFrom == (object)nameFrom) {
162 return namespaceFrom;
164 else if (namespaceFrom == null || nameFrom == null) {
168 return new XmlQualifiedNameTest(nameFrom.Name, namespaceFrom.Namespace, namespaceFrom.ExcludeNamespace);
173 /// True if neither name nor namespace is a wildcard
175 public bool IsSingleName {
176 get { return (object)this.Name != (object)wildcard && (object)this.Namespace != (object)wildcard && this.exclude == false; }
180 /// True if matches any namespace other then this.Namespace
182 public bool ExcludeNamespace {
183 get { return this.exclude; }