~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.MacDev/MonoDevelop.MacDev.InterfaceBuilder/IBDocument.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// 
2
 
// XibDocument.cs
3
 
//  
4
 
// Author:
5
 
//       Michael Hutchinson <mhutchinson@novell.com>
6
 
// 
7
 
// Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
8
 
// 
9
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
 
// of this software and associated documentation files (the "Software"), to deal
11
 
// in the Software without restriction, including without limitation the rights
12
 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 
// copies of the Software, and to permit persons to whom the Software is
14
 
// furnished to do so, subject to the following conditions:
15
 
// 
16
 
// The above copyright notice and this permission notice shall be included in
17
 
// all copies or substantial portions of the Software.
18
 
// 
19
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 
// THE SOFTWARE.
26
 
 
27
 
using System;
28
 
using System.Collections.Generic;
29
 
using System.Xml.Linq;
30
 
using System.Text;
31
 
 
32
 
namespace MonoDevelop.MacDev.InterfaceBuilder
33
 
{
34
 
        
35
 
        public class IBDocument : IBObject
36
 
        {
37
 
                public IBDocument ()
38
 
                {
39
 
                }
40
 
                
41
 
                public static IBDocument Deserialize (XDocument doc)
42
 
                {
43
 
                        return Deserialize (doc,
44
 
                                new Dictionary<string, Func<IBObject>> () {
45
 
                                        { "NSMutableArray", () => new NSMutableArray () },
46
 
                                        { "NSArray", () => new NSArray () },
47
 
                                        { "NSMutableDictionary", () => new NSMutableDictionary () },
48
 
                                        { "IBMutableOrderedSet", () => new IBMutableOrderedSet () },
49
 
                                        { "IBCocoaTouchOutletConnection", () => new IBCocoaTouchOutletConnection () },
50
 
                                        { "IBCocoaTouchEventConnection", () => new IBCocoaTouchEventConnection () },
51
 
                                        { "IBActionConnection", () => new IBActionConnection () },
52
 
                                        { "IBOutletConnection", () => new IBOutletConnection () },
53
 
                                        { "IBConnectionRecord", () => new IBConnectionRecord () },
54
 
                                        { "IBProxyObject", () => new IBProxyObject () },
55
 
                                        { "IBObjectRecord", () => new IBObjectRecord () },
56
 
                                        { "IBClassDescriptionSource", () => new IBClassDescriptionSource () },
57
 
                                        { "IBPartialClassDescription", () => new IBPartialClassDescription () },
58
 
                                }
59
 
                        );
60
 
                }
61
 
                
62
 
                public static IBDocument Deserialize (XDocument doc, Dictionary<string, Func<IBObject>> constructors)
63
 
                {
64
 
                        var resolver = new ReferencePool ();
65
 
                        var ibDoc = new IBDocument ();
66
 
                        var dataEl = doc.Root.Element ("data");
67
 
                        ibDoc.DeserializeContents (dataEl.Elements (), constructors, resolver);
68
 
                        resolver.ResolveAll ();
69
 
                        if (resolver.UnresolvedReferences.Count > 0) {
70
 
                                var sb = new StringBuilder ("Unresolved references in XIB document: ");
71
 
                                foreach (var r in resolver.UnresolvedReferences)
72
 
                                        sb.Append (r.Id + " ");
73
 
                                throw new InvalidOperationException (sb.ToString ());
74
 
                        }
75
 
                        return ibDoc;
76
 
                }
77
 
                
78
 
                Dictionary<string, object> properties = new Dictionary<string, object> ();
79
 
                
80
 
                public Dictionary<string, object> Properties {
81
 
                        get { return properties; }
82
 
                }
83
 
                
84
 
                public NSMutableArray RootObjects { get; set; }
85
 
                
86
 
                protected override void OnPropertyDeserialized (string name, object value, IReferenceResolver resolver)
87
 
                {
88
 
                        if (name == "IBDocument.RootObjects")
89
 
                                RootObjects = (NSMutableArray) value;
90
 
                        else if (name != null)
91
 
                                Properties [name] = value;
92
 
                        else
93
 
                                throw new InvalidOperationException ("XIB data element contains child with no key");
94
 
                }
95
 
        }
96
 
        
97
 
        class ReferencePool : IReferenceResolver
98
 
        {
99
 
                List<IBReference> unresolvedReferences = new List<IBReference> ();
100
 
                Dictionary<int,object> identifiableObjects = new Dictionary<int,object> ();
101
 
                
102
 
                public List<IBReference> UnresolvedReferences {
103
 
                        get { return unresolvedReferences; }
104
 
                }
105
 
        
106
 
                public Dictionary<int,object> IdentifiableObjects {
107
 
                        get { return identifiableObjects; }
108
 
                }
109
 
                
110
 
                public void ResolveAll ()
111
 
                {
112
 
                        object identifiable;
113
 
                        var stillUnresolved = new List<IBReference> ();
114
 
                        foreach (IBReference reference in unresolvedReferences) {
115
 
                                if (identifiableObjects.TryGetValue (reference.Id, out identifiable))
116
 
                                        reference.Reference = identifiable;
117
 
                                else
118
 
                                        stillUnresolved.Add (reference);
119
 
                        }
120
 
                        unresolvedReferences = stillUnresolved;
121
 
                }
122
 
                
123
 
                public object Resolve (int id)
124
 
                {
125
 
                        object value;
126
 
                        if (IdentifiableObjects.TryGetValue (id, out value))
127
 
                                return value;
128
 
                        return null;
129
 
                }
130
 
                
131
 
                public void Add (IBObject resolvable)
132
 
                {
133
 
                        identifiableObjects.Add (resolvable.Id.Value, resolvable);
134
 
                }
135
 
                
136
 
                public void Add (IBReference reference)
137
 
                {
138
 
                        unresolvedReferences.Add (reference);
139
 
                }
140
 
                
141
 
                public void Add (int id, object primitive)
142
 
                {
143
 
                        identifiableObjects.Add (id, primitive);
144
 
                }
145
 
        }
146
 
}