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

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.MacDev/MonoDevelop.MacDev.InterfaceBuilder/IBObject.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
 
// XibObject.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.Xml.Linq;
29
 
using System.Text;
30
 
using System.Collections.Generic;
31
 
using System.Globalization;
32
 
 
33
 
namespace MonoDevelop.MacDev.InterfaceBuilder
34
 
{
35
 
        public abstract class IBObject
36
 
        {
37
 
                public bool EncodedWithXMLCoder { get; set; }
38
 
                
39
 
                public int? Id { get; private set; }
40
 
                
41
 
                protected void DeserializeContents (IEnumerable<XElement> children,
42
 
                        Dictionary<string, Func<IBObject>> constructors, IReferenceResolver resolver)
43
 
                {
44
 
                        foreach (XElement child in children) {
45
 
                                XAttribute keyAtt = child.Attribute ("key");
46
 
                                string keyStr = keyAtt == null? null : keyAtt.Value;
47
 
                                if (child.Name == "bool" && keyStr == "EncodedWithXMLCoder") {
48
 
                                        EncodedWithXMLCoder = child.Value == "YES";
49
 
                                } else {
50
 
                                        object val = Deserialize (child, constructors, resolver);
51
 
                                        try {
52
 
                                                OnPropertyDeserialized (keyStr, val, resolver);
53
 
                                        } catch (Exception ex) {
54
 
                                                MonoDevelop.Core.LoggingService.LogWarning (
55
 
                                                        "IB Parser: Error assigning {0}={1} to {2} in id {3}:\n{4}",
56
 
                                                        keyStr, val, GetType (), Id, ex);
57
 
                                        }
58
 
                                }
59
 
                        }
60
 
                }
61
 
                
62
 
                protected virtual void OnPropertyDeserialized (string name, object value, IReferenceResolver resolver)
63
 
                {
64
 
                        throw new InvalidOperationException (String.Format ("Unexpected property '{0}' of type '{1}' in type '{2}'",
65
 
                                                                            name, value.GetType (), GetType ()));
66
 
                }
67
 
                
68
 
                public static object Deserialize (XElement element, Dictionary<string, Func<IBObject>> constructors, IReferenceResolver resolver)
69
 
                {
70
 
                        var idAtt = element.Attribute ("id");
71
 
                        object val = DeserializeInner (element, constructors, resolver);
72
 
                        var ib = val as IBObject;
73
 
                        
74
 
                        if (idAtt != null) {
75
 
                                int id = Int32.Parse (idAtt.Value, CultureInfo.InvariantCulture);
76
 
                                if (ib != null) {
77
 
                                        ib.Id = id;
78
 
                                        resolver.Add (ib);
79
 
                                } else {
80
 
                                        resolver.Add (id, val);
81
 
                                }
82
 
                        }
83
 
                        
84
 
                        if (ib != null)
85
 
                                ib.DeserializeContents (element.Elements (), constructors, resolver);
86
 
                        
87
 
                        return val;
88
 
                }
89
 
                
90
 
                static object DeserializeInner (XElement element, Dictionary<string, Func<IBObject>> constructors, IReferenceResolver resolver)
91
 
                {
92
 
                        switch (element.Name.ToString ()) {
93
 
                        case "int":
94
 
                                return Int32.Parse (element.Value, CultureInfo.InvariantCulture);
95
 
                        case "integer":
96
 
                                return Int32.Parse (element.Attribute ("value").Value, CultureInfo.InvariantCulture);
97
 
                        case "nil":
98
 
                                return null;
99
 
                        case "string":
100
 
                                XAttribute typeAtt = element.Attribute ("type");
101
 
                                if (typeAtt != null) {
102
 
                                        switch (typeAtt.Value) {
103
 
                                        case "base64-UTF8":
104
 
                                                //FIXME: figure out the encoding they're using. why do we have to remove the last char to make it decode?
105
 
                                                string s = element.Value.Replace ("\n", "").Replace ("\r", "");
106
 
                                                int last = (s.Length / 4 ) * 4;
107
 
                                                return Encoding.UTF8.GetString (Convert.FromBase64String (s.Substring (0, last)));
108
 
                                        default:
109
 
                                                throw new Exception (String.Format ("Unknown string encoding type {0}", typeAtt.Value));
110
 
                                        }
111
 
                                }
112
 
                                return element.Value;
113
 
                        case "characters":
114
 
                                return element.Value;
115
 
                        case "bool":
116
 
                                return element.Value == "YES";
117
 
                        case "boolean":
118
 
                                return element.Attribute ("value").Value == "YES";
119
 
                        case "double":
120
 
                                return Double.Parse (element.Value, CultureInfo.InvariantCulture);
121
 
                        case "float":
122
 
                                return float.Parse (element.Value, CultureInfo.InvariantCulture);
123
 
                        case "real":
124
 
                                return float.Parse (element.Attribute ("value").Value, CultureInfo.InvariantCulture);
125
 
                        case "bytes":
126
 
                                //FIXME: figure out the encoding they're using. it's not straight base 64
127
 
                                return new AppleEvilByteArrayEncoding (element.Value);
128
 
                        case "reference":
129
 
                                var refAtt = element.Attribute ("ref");
130
 
                                IBReference xibRef;
131
 
                                if (refAtt != null) {
132
 
                                        xibRef = new IBReference (Int32.Parse (refAtt.Value, CultureInfo.InvariantCulture));
133
 
                                        resolver.Add (xibRef);
134
 
                                } else {
135
 
                                        //FIXME: handle null references more robustly
136
 
                                        xibRef = new IBReference (Int32.MinValue);
137
 
                                }
138
 
                                return xibRef;
139
 
                        case "object": {
140
 
                                var className = (string) element.Attribute ("class");
141
 
                                Func<IBObject> constructor;
142
 
                                IBObject obj;
143
 
                                if (constructors.TryGetValue (className, out constructor))
144
 
                                        obj = constructor ();
145
 
                                else
146
 
                                        obj = new UnknownIBObject (className);
147
 
                                return obj;
148
 
                        }
149
 
                        case "array": {
150
 
                                var className = (string) element.Attribute ("class");
151
 
                                if (className == null)
152
 
                                        return new NSArray ();
153
 
                                else if (className == "NSMutableArray")
154
 
                                        return new NSMutableArray ();
155
 
                                throw new InvalidOperationException ("Unknown array class '" + className + "'");
156
 
                        }
157
 
                        case "dictionary": {
158
 
                                var className = (string) element.Attribute ("class");
159
 
                                if (className == "NSMutableDictionary")
160
 
                                        return new NSMutableDictionaryDirect ();
161
 
                                throw new InvalidOperationException ("Unknown dictionary class '" + className + "'");
162
 
                        }
163
 
                        default:
164
 
                                throw new Exception (String.Format ("Cannot handle primitive type {0}", element.Name));
165
 
                        }
166
 
                }
167
 
        }
168
 
        
169
 
        struct AppleEvilByteArrayEncoding
170
 
        {
171
 
                string text;
172
 
                public AppleEvilByteArrayEncoding (string text)
173
 
                {
174
 
                        this.text = text;
175
 
                }
176
 
                
177
 
                public string Text {
178
 
                        get { return text; }
179
 
                }
180
 
        }
181
 
        
182
 
        public class IBProxyObject : IBObject
183
 
        {
184
 
                public string IBProxiedObjectIdentifier { get; set; }
185
 
                public string TargetRuntimeIdentifier { get; set; }
186
 
                
187
 
                protected override void OnPropertyDeserialized (string name, object value, IReferenceResolver resolver)
188
 
                {
189
 
                        if (name == "IBProxiedObjectIdentifier")
190
 
                                IBProxiedObjectIdentifier = (string) value;
191
 
                        else if (name == "targetRuntimeIdentifier")
192
 
                                TargetRuntimeIdentifier = (string) value;
193
 
                        else
194
 
                                base.OnPropertyDeserialized (name, value, resolver);
195
 
                }
196
 
        }
197
 
}