The Labs \ Source Viewer \ SSCLI \ System.Xml.Xsl \ XmlQualifiedNameTest

  1. //------------------------------------------------------------------------------
  2. // <copyright file="XmlQualifiedNameTest.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System;
  16. using System.Xml;
  17. using System.Xml.Schema;
  18. using System.Collections.Generic;
  19. using System.Diagnostics;
  20. using System.Text;
  21. namespace System.Xml.Xsl
  22. {
  23.    
  24.     /// <summary>
  25.     /// XmlQualifiedName extends XmlQualifiedName to support wildcards and adds nametest functionality
  26.     /// Following are the examples:
  27.     /// {A}:B XmlQualifiedNameTest.New("B", "A") Match QName with namespace A and local name B
  28.     /// * XmlQualifiedNameTest.New(null, null) Match any QName
  29.     /// {A}:* XmlQualifiedNameTest.New(null, "A") Match QName with namespace A and any local name
  30.     /// XmlQualifiedNameTest.New("A", false)
  31.     /// *:B XmlQualifiedNameTest.New("B", null) Match QName with any namespace and local name B
  32.     /// ~{A}:* XmlQualifiedNameTest.New("B", "A") Match QName with namespace not A and any local name
  33.     /// {~A}:B only as a result of the intersection Match QName with namespace not A and local name B
  34.     /// </summary>
  35.     internal class XmlQualifiedNameTest : XmlQualifiedName
  36.     {
  37.         bool exclude;
  38.         const string wildcard = "*";
  39.         static XmlQualifiedNameTest wc = XmlQualifiedNameTest.New(wildcard, wildcard);
  40.        
  41.         /// <summary>
  42.         /// Full wildcard
  43.         /// </summary>
  44.         public static XmlQualifiedNameTest Wildcard {
  45.             get { return wc; }
  46.         }
  47.        
  48.         /// <summary>
  49.         /// Constructor
  50.         /// </summary>
  51.         private XmlQualifiedNameTest(string name, string ns, bool exclude) : base(name, ns)
  52.         {
  53.             this.exclude = exclude;
  54.         }
  55.        
  56.         /// <summary>
  57.         /// Construct new from name and namespace. Returns singleton Wildcard in case full wildcard
  58.         /// </summary>
  59.         public static XmlQualifiedNameTest New(string name, string ns)
  60.         {
  61.             if (ns == null && name == null) {
  62.                 return Wildcard;
  63.             }
  64.             else {
  65.                 return new XmlQualifiedNameTest(name == null ? wildcard : name, ns == null ? wildcard : ns, false);
  66.             }
  67.         }
  68.        
  69.         /// <summary>
  70.         /// True if matches any name and any namespace
  71.         /// </summary>
  72.         public bool IsWildcard {
  73.             get { return (object)this == (object)Wildcard; }
  74.         }
  75.        
  76.         /// <summary>
  77.         /// True if matches any name
  78.         /// </summary>
  79.         public bool IsNameWildcard {
  80.             get { return (object)this.Name == (object)wildcard; }
  81.         }
  82.        
  83.         /// <summary>
  84.         /// True if matches any namespace
  85.         /// </summary>
  86.         public bool IsNamespaceWildcard {
  87.             get { return (object)this.Namespace == (object)wildcard; }
  88.         }
  89.        
  90.         private bool IsNameSubsetOf(XmlQualifiedNameTest other)
  91.         {
  92.             return other.IsNameWildcard || this.Name == other.Name;
  93.         }
  94.        
  95.         private bool IsNamespaceSubsetOf(XmlQualifiedNameTest other)
  96.         {
  97.             return other.IsNamespaceWildcard || (this.exclude == other.exclude && this.Namespace == other.Namespace) || (other.exclude && !this.exclude && this.Namespace != other.Namespace);
  98.         }
  99.        
  100.         /// <summary>
  101.         /// True if this matches every QName other does
  102.         /// </summary>
  103.         public bool IsSubsetOf(XmlQualifiedNameTest other)
  104.         {
  105.             return IsNameSubsetOf(other) && IsNamespaceSubsetOf(other);
  106.         }
  107.        
  108.         /// <summary>
  109.         /// Return true if the result of intersection with other is not empty
  110.         /// </summary>
  111.         public bool HasIntersection(XmlQualifiedNameTest other)
  112.         {
  113.             return (IsNamespaceSubsetOf(other) || other.IsNamespaceSubsetOf(this)) && (IsNameSubsetOf(other) || other.IsNameSubsetOf(this));
  114.         }
  115.        
  116.         /// <summary>
  117.         /// String representation
  118.         /// </summary>
  119.         public override string ToString()
  120.         {
  121.             if ((object)this == (object)Wildcard) {
  122.                 return "*";
  123.             }
  124.             else {
  125.                 if (this.Namespace.Length == 0) {
  126.                     return this.Name;
  127.                 }
  128.                 else if ((object)this.Namespace == (object)wildcard) {
  129.                     return "*:" + this.Name;
  130.                 }
  131.                 else if (this.exclude) {
  132.                     return "{~" + this.Namespace + "}:" + this.Name;
  133.                 }
  134.                 else {
  135.                     return "{" + this.Namespace + "}:" + this.Name;
  136.                 }
  137.             }
  138.         }
  139.        
  140.     }
  141. }

Developer Fusion