2 // Author: Atsushi Enomoto <atsushi@ximian.com>
4 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 using System.Collections.Generic;
27 using System.Collections.ObjectModel;
28 using System.ServiceModel;
29 using System.ServiceModel.Channels;
30 using System.ServiceModel.Description;
31 using System.ServiceModel.Dispatcher;
33 using System.Xml.Schema;
34 using System.Xml.Linq;
36 namespace System.ServiceModel.Discovery
38 public class FindCriteria
40 const string SerializationNS = "http://schemas.microsoft.com/ws/2008/06/discovery";
41 const int default_max_results = int.MaxValue;
43 public static readonly Uri ScopeMatchByExact = new Uri ("http://schemas.microsoft.com/ws/2008/06/discovery/strcmp0");
44 public static readonly Uri ScopeMatchByLdap = new Uri ("http://schemas.microsoft.com/ws/2008/06/discovery/ldap");
45 public static readonly Uri ScopeMatchByNone = new Uri ("http://schemas.microsoft.com/ws/2008/06/discovery/none");
46 public static readonly Uri ScopeMatchByPrefix = new Uri ("http://schemas.microsoft.com/ws/2008/06/discovery/rfc3986");
47 public static readonly Uri ScopeMatchByUuid = new Uri ("http://schemas.microsoft.com/ws/2008/06/discovery/uuid");
49 public static FindCriteria CreateMetadataExchangeEndpointCriteria ()
51 return CreateMetadataExchangeEndpointCriteria (typeof (IMetadataExchange));
54 public static FindCriteria CreateMetadataExchangeEndpointCriteria (IEnumerable<XmlQualifiedName> contractTypeNames)
56 var fc = new FindCriteria ();
57 foreach (var type in contractTypeNames)
58 fc.ContractTypeNames.Add (type);
62 public static FindCriteria CreateMetadataExchangeEndpointCriteria (Type contractType)
64 return new FindCriteria (contractType);
67 public FindCriteria ()
69 ContractTypeNames = new Collection<XmlQualifiedName> ();
70 Extensions = new Collection<XElement> ();
71 Scopes = new Collection<Uri> ();
72 MaxResults = default_max_results;
75 public FindCriteria (Type contractType)
78 var cd = ContractDescription.GetContract (contractType);
79 ContractTypeNames.Add (new XmlQualifiedName (cd.Name, cd.Namespace));
82 public Collection<XmlQualifiedName> ContractTypeNames { get; private set; }
83 public TimeSpan Duration { get; set; }
84 public Collection<XElement> Extensions { get; private set; }
85 public int MaxResults { get; set; }
86 public Uri ScopeMatchBy { get; set; }
87 public Collection<Uri> Scopes { get; private set; }
90 public bool IsMatch (EndpointDiscoveryMetadata endpointDiscoveryMetadata)
92 throw new NotImplementedException ();
95 internal void WriteXml (XmlWriter writer, DiscoveryVersion version)
98 throw new ArgumentNullException ("writer");
100 writer.WriteStartElement ("ProbeType", version.Namespace);
103 writer.WriteStartElement ("d", "Types", version.Namespace);
105 foreach (var qname in ContractTypeNames)
106 if (writer.LookupPrefix (qname.Namespace) == null)
107 writer.WriteAttributeString ("xmlns", "p" + p++, "http://www.w3.org/2000/xmlns/", qname.Namespace);
108 writer.WriteValue (ContractTypeNames);
109 writer.WriteEndElement ();
111 writer.WriteStartElement ("Scopes", version.Namespace);
112 if (ScopeMatchBy != null) {
113 writer.WriteStartAttribute ("MatchBy");
114 writer.WriteValue (ScopeMatchBy);
115 writer.WriteEndAttribute ();
117 writer.WriteValue (Scopes);
118 writer.WriteEndElement ();
120 // non-standard members
121 if (MaxResults != default_max_results) {
122 writer.WriteStartElement ("MaxResults", SerializationNS);
123 writer.WriteValue (Duration);
124 writer.WriteEndElement ();
126 writer.WriteStartElement ("Duration", SerializationNS);
127 writer.WriteValue (Duration);
128 writer.WriteEndElement ();
130 foreach (var ext in Extensions)
131 ext.WriteTo (writer);
133 writer.WriteEndElement ();
136 internal static XmlSchema BuildSchema (DiscoveryVersion version)
138 var schema = new XmlSchema () { TargetNamespace = version.Namespace };
140 var anyAttr = new XmlSchemaAnyAttribute () { Namespace = "##other", ProcessContents = XmlSchemaContentProcessing.Lax };
142 var probePart = new XmlSchemaSequence ();
143 probePart.Items.Add (new XmlSchemaElement () { RefName = new XmlQualifiedName ("Types", version.Namespace), MinOccurs = 0 });
144 probePart.Items.Add (new XmlSchemaElement () { RefName = new XmlQualifiedName ("Scopes", version.Namespace), MinOccurs = 0 });
145 probePart.Items.Add (new XmlSchemaAny () { MinOccurs = 0, MaxOccursString = "unbounded", Namespace = "##other", ProcessContents = XmlSchemaContentProcessing.Lax });
146 var ct = new XmlSchemaComplexType () { Name = "ProbeType", Particle = probePart, AnyAttribute = anyAttr };
147 schema.Items.Add (ct);
149 schema.Items.Add (new XmlSchemaSimpleType () { Name = "QNameListType", Content = new XmlSchemaSimpleTypeList () { ItemTypeName = new XmlQualifiedName ("QName", XmlSchema.Namespace) } });
151 var scr = new XmlSchemaSimpleContentRestriction () { BaseTypeName = new XmlQualifiedName ("UriListType", version.Namespace), AnyAttribute = anyAttr };
152 scr.Attributes.Add (new XmlSchemaAttribute () { Name = "matchBy", SchemaTypeName = new XmlQualifiedName ("anyURI", XmlSchema.Namespace) });
153 schema.Items.Add (new XmlSchemaComplexType () { Name = "ScopesType", ContentModel = new XmlSchemaSimpleContent () { Content = scr } });
155 schema.Items.Add (new XmlSchemaSimpleType () { Name = "UriListType", Content = new XmlSchemaSimpleTypeList () { ItemTypeName = new XmlQualifiedName ("anyURI", XmlSchema.Namespace) } });
157 schema.Items.Add (new XmlSchemaElement () { Name = "Types", SchemaTypeName = new XmlQualifiedName ("QNameListType", version.Namespace) });
158 schema.Items.Add (new XmlSchemaElement () { Name = "Scopes", SchemaTypeName = new XmlQualifiedName ("ScopesType", version.Namespace) });