5
// Michael Hutchinson <mhutchinson@novell.com>
7
// Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
28
using System.Collections.Generic;
29
using System.Xml.Linq;
32
namespace MonoDevelop.MacDev.InterfaceBuilder
35
public class IBDocument : IBObject
41
public static IBDocument Deserialize (XDocument doc)
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 () },
62
public static IBDocument Deserialize (XDocument doc, Dictionary<string, Func<IBObject>> constructors)
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 ());
78
Dictionary<string, object> properties = new Dictionary<string, object> ();
80
public Dictionary<string, object> Properties {
81
get { return properties; }
84
public NSMutableArray RootObjects { get; set; }
86
protected override void OnPropertyDeserialized (string name, object value, IReferenceResolver resolver)
88
if (name == "IBDocument.RootObjects")
89
RootObjects = (NSMutableArray) value;
90
else if (name != null)
91
Properties [name] = value;
93
throw new InvalidOperationException ("XIB data element contains child with no key");
97
class ReferencePool : IReferenceResolver
99
List<IBReference> unresolvedReferences = new List<IBReference> ();
100
Dictionary<int,object> identifiableObjects = new Dictionary<int,object> ();
102
public List<IBReference> UnresolvedReferences {
103
get { return unresolvedReferences; }
106
public Dictionary<int,object> IdentifiableObjects {
107
get { return identifiableObjects; }
110
public void ResolveAll ()
113
var stillUnresolved = new List<IBReference> ();
114
foreach (IBReference reference in unresolvedReferences) {
115
if (identifiableObjects.TryGetValue (reference.Id, out identifiable))
116
reference.Reference = identifiable;
118
stillUnresolved.Add (reference);
120
unresolvedReferences = stillUnresolved;
123
public object Resolve (int id)
126
if (IdentifiableObjects.TryGetValue (id, out value))
131
public void Add (IBObject resolvable)
133
identifiableObjects.Add (resolvable.Id.Value, resolvable);
136
public void Add (IBReference reference)
138
unresolvedReferences.Add (reference);
141
public void Add (int id, object primitive)
143
identifiableObjects.Add (id, primitive);