Merge branch 'master' of github.com:mono/mono into masterwork
[mono.git] / mcs / class / System.Xaml / System.Xaml / ValueSerializerContext.cs
1 //
2 // Copyright (C) 2010 Novell Inc. http://novell.com
3 //
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:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
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.
22 //
23
24 using System;
25 using System.Collections.Generic;
26 using System.ComponentModel;
27 using System.IO;
28 using System.Linq;
29 using System.Reflection;
30 using System.Text;
31 using System.Windows.Markup;
32 using System.Xaml;
33 using System.Xaml.Schema;
34 using System.Xml;
35
36 namespace System.Xaml
37 {
38         internal class ValueSerializerContext : IValueSerializerContext, IXamlSchemaContextProvider
39         {
40                 XamlNameResolver name_resolver = new XamlNameResolver ();
41                 XamlTypeResolver type_resolver;
42                 NamespaceResolver namespace_resolver;
43                 PrefixLookup prefix_lookup;
44                 XamlSchemaContext sctx;
45
46                 public ValueSerializerContext (PrefixLookup prefixLookup, XamlSchemaContext schemaContext)
47                 {
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);
55                         sctx = schemaContext;
56                 }
57
58                 public object GetService (Type serviceType)
59                 {
60                         if (serviceType == typeof (INamespacePrefixLookup))
61                                 return prefix_lookup;
62                         if (serviceType == typeof (IXamlNamespaceResolver))
63                                 return namespace_resolver;
64                         if (serviceType == typeof (IXamlNameResolver))
65                                 return name_resolver;
66                         if (serviceType == typeof (IXamlNameProvider))
67                                 return name_resolver;
68                         if (serviceType == typeof (IXamlTypeResolver))
69                                 return type_resolver;
70                         if (serviceType == typeof (IXamlSchemaContextProvider))
71                                 return this;
72                         return null;
73                 }
74                 
75                 XamlSchemaContext IXamlSchemaContextProvider.SchemaContext {
76                         get { return sctx; }
77                 }
78                 
79                 public IContainer Container {
80                         get { throw new NotImplementedException (); }
81                 }
82                 public object Instance {
83                         get { throw new NotImplementedException (); }
84                 }
85                 public PropertyDescriptor PropertyDescriptor {
86                         get { throw new NotImplementedException (); }
87                 }
88                 public void OnComponentChanged ()
89                 {
90                         throw new NotImplementedException ();
91                 }
92                 public bool OnComponentChanging ()
93                 {
94                         throw new NotImplementedException ();
95                 }
96                 public ValueSerializer GetValueSerializerFor (PropertyDescriptor descriptor)
97                 {
98                         throw new NotImplementedException ();
99                 }
100                 public ValueSerializer GetValueSerializerFor (Type type)
101                 {
102                         throw new NotImplementedException ();
103                 }
104         }
105
106         internal class XamlTypeResolver : IXamlTypeResolver
107         {
108                 NamespaceResolver ns_resolver;
109                 XamlSchemaContext schema_context;
110
111                 public XamlTypeResolver (NamespaceResolver namespaceResolver, XamlSchemaContext schemaContext)
112                 {
113                         ns_resolver = namespaceResolver;
114                         schema_context = schemaContext;
115                 }
116
117                 public Type Resolve (string typeName)
118                 {
119                         var tn = XamlTypeName.Parse (typeName, ns_resolver);
120                         var xt = schema_context.GetXamlType (tn);
121                         return xt != null ? xt.UnderlyingType : null;
122                 }
123         }
124
125         internal class NamespaceResolver : IXamlNamespaceResolver
126         {
127                 public NamespaceResolver (IList<NamespaceDeclaration> source)
128                 {
129                         this.source = source;
130                 }
131         
132                 IList<NamespaceDeclaration> source;
133         
134                 public string GetNamespace (string prefix)
135                 {
136                         foreach (var nsd in source)
137                                 if (nsd.Prefix == prefix)
138                                         return nsd.Namespace;
139                         return null;
140                 }
141         
142                 public IEnumerable<NamespaceDeclaration> GetNamespacePrefixes ()
143                 {
144                         return source;
145                 }
146         }
147 }