~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlTypeResolverProvider.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.ComponentModel;
 
7
using System.Diagnostics;
 
8
using System.Globalization;
 
9
using System.Text;
 
10
using System.Windows;
 
11
using System.Windows.Markup;
 
12
using System.Xml;
 
13
 
 
14
namespace ICSharpCode.WpfDesign.XamlDom
 
15
{
 
16
        sealed class XamlTypeResolverProvider : IXamlTypeResolver, IServiceProvider
 
17
        {
 
18
                XamlDocument document;
 
19
                XamlObject containingObject;
 
20
                
 
21
                public XamlTypeResolverProvider(XamlObject containingObject)
 
22
                {
 
23
                        if (containingObject == null)
 
24
                                throw new ArgumentNullException("containingObject");
 
25
                        this.document = containingObject.OwnerDocument;
 
26
                        this.containingObject = containingObject;
 
27
                }
 
28
 
 
29
                XmlElement ContainingElement{
 
30
                        get { return containingObject.XmlElement; }
 
31
                }
 
32
                
 
33
                public Type Resolve(string typeName)
 
34
                {
 
35
                        string typeNamespaceUri;
 
36
                        string typeLocalName;
 
37
                        if (typeName.Contains(":")) {
 
38
                                typeNamespaceUri = ContainingElement.GetNamespaceOfPrefix(typeName.Substring(0, typeName.IndexOf(':')));
 
39
                                typeLocalName = typeName.Substring(typeName.IndexOf(':') + 1);
 
40
                        } else {
 
41
                                typeNamespaceUri = ContainingElement.GetNamespaceOfPrefix("");
 
42
                                typeLocalName = typeName;
 
43
                        }
 
44
                        if (string.IsNullOrEmpty(typeNamespaceUri))
 
45
                                throw new XamlMarkupExtensionParseException("Unrecognized namespace prefix in type " + typeName);
 
46
                        return document.TypeFinder.GetType(typeNamespaceUri, typeLocalName);
 
47
                }
 
48
                
 
49
                public object GetService(Type serviceType)
 
50
                {
 
51
                        if (serviceType == typeof(IXamlTypeResolver) || serviceType == typeof(XamlTypeResolverProvider))
 
52
                                return this;
 
53
                        else
 
54
                                return document.ServiceProvider.GetService(serviceType);
 
55
                }
 
56
                
 
57
                public XamlPropertyInfo ResolveProperty(string propertyName)
 
58
                {
 
59
                        string propertyNamespace;
 
60
                        if (propertyName.Contains(":")) {
 
61
                                propertyNamespace = ContainingElement.GetNamespaceOfPrefix(propertyName.Substring(0, propertyName.IndexOf(':')));
 
62
                                propertyName = propertyName.Substring(propertyName.IndexOf(':') + 1);
 
63
                        } else {
 
64
                                propertyNamespace = ContainingElement.GetNamespaceOfPrefix("");
 
65
                        }
 
66
                        Type elementType = null;
 
67
                        XamlObject obj = containingObject;
 
68
                        while (obj != null) {
 
69
                                Style style = obj.Instance as Style;
 
70
                                if (style != null && style.TargetType != null) {
 
71
                                        elementType = style.TargetType;
 
72
                                        break;
 
73
                                }
 
74
                                obj = obj.ParentObject;
 
75
                        }
 
76
                        if (propertyName.Contains(".")) {
 
77
                                return XamlParser.GetPropertyInfo(document.TypeFinder, null, elementType, propertyNamespace, propertyName);
 
78
                        } else if (elementType != null) {
 
79
                                return XamlParser.FindProperty(null, elementType, propertyName);
 
80
                        } else {
 
81
                                return null;
 
82
                        }
 
83
                }
 
84
                
 
85
                public object FindResource(object key)
 
86
                {
 
87
                        XamlObject obj = containingObject;
 
88
                        while (obj != null) {
 
89
                                FrameworkElement el = obj.Instance as FrameworkElement;
 
90
                                if (el != null) {
 
91
                                        object val = el.Resources[key];
 
92
                                        if (val != null)
 
93
                                                return val;
 
94
                                }
 
95
                                obj = obj.ParentObject;
 
96
                        }
 
97
                        return null;
 
98
                }
 
99
        }
 
100
}