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 static FindCriteria ReadXml (XmlReader reader, DiscoveryVersion version)
98 throw new ArgumentNullException ("reader");
100 var ret = new FindCriteria ();
102 reader.MoveToContent ();
103 if (!reader.IsStartElement ("ProbeType", version.Namespace) || reader.IsEmptyElement)
104 throw new XmlException ("Non-empty ProbeType element is expected");
105 reader.ReadStartElement ("ProbeType", version.Namespace);
108 reader.MoveToContent ();
109 bool isEmpty = reader.IsEmptyElement;
110 ret.ContractTypeNames = new Collection<XmlQualifiedName> ((XmlQualifiedName []) reader.ReadElementContentAs (typeof (XmlQualifiedName []), null, "Types", version.Namespace));
112 reader.MoveToContent ();
113 if (!reader.IsStartElement ("Scopes", version.Namespace))
114 throw new XmlException ("Scopes element is expected");
115 if (reader.MoveToAttribute ("MatchBy")) {
116 ret.ScopeMatchBy = new Uri (reader.Value, UriKind.RelativeOrAbsolute);
117 reader.MoveToElement ();
119 ret.Scopes = new Collection<Uri> ((Uri []) reader.ReadElementContentAs (typeof (Uri []), null, "Scopes", version.Namespace));
121 // non-standard members
122 for (reader.MoveToContent (); !reader.EOF && reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
123 if (reader.NamespaceURI == SerializationNS) {
124 switch (reader.LocalName) {
126 ret.MaxResults = reader.ReadElementContentAsInt ();
129 ret.Duration = (TimeSpan) reader.ReadElementContentAs (typeof (TimeSpan), null);
134 ret.Extensions.Add (XElement.Load (reader));
137 reader.ReadEndElement ();
142 internal void WriteXml (XmlWriter writer, DiscoveryVersion version)
145 throw new ArgumentNullException ("writer");
147 writer.WriteStartElement ("ProbeType", version.Namespace);
150 writer.WriteStartElement ("d", "Types", version.Namespace);
152 foreach (var qname in ContractTypeNames)
153 if (writer.LookupPrefix (qname.Namespace) == null)
154 writer.WriteAttributeString ("xmlns", "p" + p++, "http://www.w3.org/2000/xmlns/", qname.Namespace);
155 writer.WriteValue (ContractTypeNames);
156 writer.WriteEndElement ();
158 writer.WriteStartElement ("Scopes", version.Namespace);
159 if (ScopeMatchBy != null) {
160 writer.WriteStartAttribute ("MatchBy");
161 writer.WriteValue (ScopeMatchBy);
162 writer.WriteEndAttribute ();
164 writer.WriteValue (Scopes);
165 writer.WriteEndElement ();
167 // non-standard members
168 if (MaxResults != default_max_results) {
169 writer.WriteStartElement ("MaxResults", SerializationNS);
170 writer.WriteValue (MaxResults);
171 writer.WriteEndElement ();
173 writer.WriteStartElement ("Duration", SerializationNS);
174 writer.WriteValue (Duration);
175 writer.WriteEndElement ();
177 foreach (var ext in Extensions)
178 ext.WriteTo (writer);
180 writer.WriteEndElement ();
183 internal static XmlSchema BuildSchema (DiscoveryVersion version)
185 var schema = new XmlSchema () { TargetNamespace = version.Namespace };
187 var anyAttr = new XmlSchemaAnyAttribute () { Namespace = "##other", ProcessContents = XmlSchemaContentProcessing.Lax };
189 var probePart = new XmlSchemaSequence ();
190 probePart.Items.Add (new XmlSchemaElement () { RefName = new XmlQualifiedName ("Types", version.Namespace), MinOccurs = 0 });
191 probePart.Items.Add (new XmlSchemaElement () { RefName = new XmlQualifiedName ("Scopes", version.Namespace), MinOccurs = 0 });
192 probePart.Items.Add (new XmlSchemaAny () { MinOccurs = 0, MaxOccursString = "unbounded", Namespace = "##other", ProcessContents = XmlSchemaContentProcessing.Lax });
193 var ct = new XmlSchemaComplexType () { Name = "ProbeType", Particle = probePart, AnyAttribute = anyAttr };
194 schema.Items.Add (ct);
196 schema.Items.Add (new XmlSchemaSimpleType () { Name = "QNameListType", Content = new XmlSchemaSimpleTypeList () { ItemTypeName = new XmlQualifiedName ("QName", XmlSchema.Namespace) } });
198 var scr = new XmlSchemaSimpleContentRestriction () { BaseTypeName = new XmlQualifiedName ("UriListType", version.Namespace), AnyAttribute = anyAttr };
199 scr.Attributes.Add (new XmlSchemaAttribute () { Name = "matchBy", SchemaTypeName = new XmlQualifiedName ("anyURI", XmlSchema.Namespace) });
200 schema.Items.Add (new XmlSchemaComplexType () { Name = "ScopesType", ContentModel = new XmlSchemaSimpleContent () { Content = scr } });
202 schema.Items.Add (new XmlSchemaSimpleType () { Name = "UriListType", Content = new XmlSchemaSimpleTypeList () { ItemTypeName = new XmlQualifiedName ("anyURI", XmlSchema.Namespace) } });
204 schema.Items.Add (new XmlSchemaElement () { Name = "Types", SchemaTypeName = new XmlQualifiedName ("QNameListType", version.Namespace) });
205 schema.Items.Add (new XmlSchemaElement () { Name = "Scopes", SchemaTypeName = new XmlQualifiedName ("ScopesType", version.Namespace) });