Fix TypeValueSerializer and ValueSerializerContext to work fine with XamlObjectWriter.
[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 (IXamlTypeResolver))
67                                 return type_resolver;
68                         if (serviceType == typeof (IXamlSchemaContextProvider))
69                                 return this;
70                         return null;
71                 }
72                 
73                 XamlSchemaContext IXamlSchemaContextProvider.SchemaContext {
74                         get { return sctx; }
75                 }
76                 
77                 public IContainer Container {
78                         get { throw new NotImplementedException (); }
79                 }
80                 public object Instance {
81                         get { throw new NotImplementedException (); }
82                 }
83                 public PropertyDescriptor PropertyDescriptor {
84                         get { throw new NotImplementedException (); }
85                 }
86                 public void OnComponentChanged ()
87                 {
88                         throw new NotImplementedException ();
89                 }
90                 public bool OnComponentChanging ()
91                 {
92                         throw new NotImplementedException ();
93                 }
94                 public ValueSerializer GetValueSerializerFor (PropertyDescriptor descriptor)
95                 {
96                         throw new NotImplementedException ();
97                 }
98                 public ValueSerializer GetValueSerializerFor (Type type)
99                 {
100                         throw new NotImplementedException ();
101                 }
102         }
103
104         internal class XamlTypeResolver : IXamlTypeResolver
105         {
106                 NamespaceResolver ns_resolver;
107                 XamlSchemaContext schema_context;
108
109                 public XamlTypeResolver (NamespaceResolver namespaceResolver, XamlSchemaContext schemaContext)
110                 {
111                         ns_resolver = namespaceResolver;
112                         schema_context = schemaContext;
113                 }
114
115                 public Type Resolve (string typeName)
116                 {
117                         var tn = XamlTypeName.Parse (typeName, ns_resolver);
118                         var xt = schema_context.GetXamlType (tn);
119                         return xt != null ? xt.UnderlyingType : null;
120                 }
121         }
122
123         internal class NamespaceResolver : IXamlNamespaceResolver
124         {
125                 public NamespaceResolver (IList<NamespaceDeclaration> source)
126                 {
127                         this.source = source;
128                 }
129         
130                 IList<NamespaceDeclaration> source;
131         
132                 public string GetNamespace (string prefix)
133                 {
134                         foreach (var nsd in source)
135                                 if (nsd.Prefix == prefix)
136                                         return nsd.Namespace;
137                         return null;
138                 }
139         
140                 public IEnumerable<NamespaceDeclaration> GetNamespacePrefixes ()
141                 {
142                         return source;
143                 }
144         }
145 }