Merge pull request #93 from konrad-kruczynski/dispatcher_timer_fix
[mono.git] / mcs / class / System.XML / System.Xml / XmlXapResolver.cs
1 //
2 // XmlXapResolver.cs
3 //
4 // Author:
5 //   Atsushi Enomoto (atsushi@ximian.com)
6 //
7 // (C) 2009 Novell Inc.
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.IO;
32 using System.Reflection;
33
34 #if !MOBILE
35 namespace System.Xml
36 {
37         public class XmlXapResolver : XmlResolver
38         {
39                 static readonly MethodInfo method;
40                 static readonly PropertyInfo property;
41                 static XmlXapResolver ()
42                 {
43                         var at = Type.GetType ("System.Windows.Application, System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e", true);
44                         method = at.GetMethod ("GetResourceStream", new Type [] {typeof (Uri)});
45                         var rt = Type.GetType ("System.Windows.Resources.StreamResourceInfo, System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e", true);
46                         property = rt.GetProperty ("Stream");
47                 }
48
49                 public override object GetEntity (Uri absoluteUri, string role, Type ofObjectToReturn)
50                 {
51                         if (absoluteUri == null)
52                                 throw new ArgumentNullException ("absoluteUri");
53                         if (absoluteUri.IsAbsoluteUri)
54                                 throw new XmlException ("URI must be relative to the application XAP");
55                         if (!SupportsType (absoluteUri, ofObjectToReturn))
56                                 throw new XmlException (String.Format ("Unsupported entity type '{0}'", ofObjectToReturn.GetType ()));
57                         
58                         try {
59                                 var sri = method.Invoke (null, new object [] {absoluteUri});
60                                 if (sri == null)
61                                         throw new XmlException (String.Format ("Resource '{0}' not found", absoluteUri));
62                                 return property.GetValue (sri, new object [0]);
63                         } catch (TargetInvocationException tie) {
64                                 // throw the original exception
65                                 throw tie.InnerException;
66                         }
67                 }
68         }
69 }
70 #endif