2 // Copyright (C) 2010 Novell Inc. http://novell.com
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 using System.Collections.Generic;
26 using System.ComponentModel;
29 using System.Reflection;
31 using System.Windows.Markup;
33 using System.Xaml.Schema;
38 internal class ValueSerializerContext : IValueSerializerContext, IXamlSchemaContextProvider
40 XamlNameResolver name_resolver = new XamlNameResolver ();
41 XamlTypeResolver type_resolver;
42 NamespaceResolver namespace_resolver;
43 PrefixLookup prefix_lookup;
44 XamlSchemaContext sctx;
46 public ValueSerializerContext (PrefixLookup prefixLookup, XamlSchemaContext schemaContext)
48 if (prefixLookup == null)
49 throw new ArgumentNullException ("prefixLookup");
50 if (schemaContext == null)
51 throw new ArgumentNullException ("schemaContext");
52 prefix_lookup = prefixLookup;
53 namespace_resolver = new NamespaceResolver (prefix_lookup.Namespaces);
54 type_resolver = new XamlTypeResolver (namespace_resolver, schemaContext);
58 public object GetService (Type serviceType)
60 if (serviceType == typeof (INamespacePrefixLookup))
62 if (serviceType == typeof (IXamlNamespaceResolver))
63 return namespace_resolver;
64 if (serviceType == typeof (IXamlNameResolver))
66 if (serviceType == typeof (IXamlTypeResolver))
68 if (serviceType == typeof (IXamlSchemaContextProvider))
73 XamlSchemaContext IXamlSchemaContextProvider.SchemaContext {
77 public IContainer Container {
78 get { throw new NotImplementedException (); }
80 public object Instance {
81 get { throw new NotImplementedException (); }
83 public PropertyDescriptor PropertyDescriptor {
84 get { throw new NotImplementedException (); }
86 public void OnComponentChanged ()
88 throw new NotImplementedException ();
90 public bool OnComponentChanging ()
92 throw new NotImplementedException ();
94 public ValueSerializer GetValueSerializerFor (PropertyDescriptor descriptor)
96 throw new NotImplementedException ();
98 public ValueSerializer GetValueSerializerFor (Type type)
100 throw new NotImplementedException ();
104 internal class XamlTypeResolver : IXamlTypeResolver
106 NamespaceResolver ns_resolver;
107 XamlSchemaContext schema_context;
109 public XamlTypeResolver (NamespaceResolver namespaceResolver, XamlSchemaContext schemaContext)
111 ns_resolver = namespaceResolver;
112 schema_context = schemaContext;
115 public Type Resolve (string typeName)
117 var tn = XamlTypeName.Parse (typeName, ns_resolver);
118 var xt = schema_context.GetXamlType (tn);
119 return xt != null ? xt.UnderlyingType : null;
123 internal class NamespaceResolver : IXamlNamespaceResolver
125 public NamespaceResolver (IList<NamespaceDeclaration> source)
127 this.source = source;
130 IList<NamespaceDeclaration> source;
132 public string GetNamespace (string prefix)
134 foreach (var nsd in source)
135 if (nsd.Prefix == prefix)
136 return nsd.Namespace;
140 public IEnumerable<NamespaceDeclaration> GetNamespacePrefixes ()