Merge pull request #799 from kebby/master
[mono.git] / mcs / class / Managed.Windows.Forms / System.Resources / ResXDataNodeHandler.cs
1 //
2 // ResXDataNodeHandler.cs : ResXDataNodeHandler is the base class for the 
3 // handler classes which are used to abstract away the different ways in
4 // which the ResXDataNode members behave based on the resource type / how
5 // it is stored in the resx file / whether it has been stored in a resx 
6 // file as determined by tests against the .net framework.
7 //
8 // The IWritableHandler interface signifies a handler can return a copy
9 // of the resource in the string form it appeared in the resx file thus
10 // avoiding the need for instantiation.
11 // 
12 // Author:
13 //      Gary Barnett (gary.barnett.mono@gmail.com)
14 // 
15 // Copyright (C) Gary Barnett (2012)
16 //
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
24 // 
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
27 // 
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
36 using System;
37 using System.ComponentModel.Design;
38 using System.Reflection;
39
40 namespace System.Resources {
41         internal interface IWritableHandler {
42                 string DataString { get;}
43         }
44
45         internal abstract class ResXDataNodeHandler {
46                 protected ResXDataNodeHandler ()
47                 {
48                 }
49
50                 public abstract object GetValue (ITypeResolutionService typeResolver);
51                 
52                 public abstract object GetValue (AssemblyName [] assemblyNames);
53
54                 public abstract string GetValueTypeName (ITypeResolutionService typeResolver);
55
56                 public abstract string GetValueTypeName (AssemblyName [] assemblyNames);
57
58                 //override by any inheritor that doesnt want to send the default output of GetValue to be written to ResXFile
59                 public virtual object GetValueForResX ()
60                 {
61                         return GetValue ((AssemblyName []) null);
62                 }
63
64                 protected Type ResolveType (string typeString) 
65                 {
66                         // FIXME: check the test that shows you cant load a type with just a fullname from current assembly is valid
67                         return Type.GetType (typeString);
68                 }
69
70                 protected Type ResolveType (string typeString, AssemblyName [] assemblyNames) 
71                 {
72                         Type result = null;
73
74                         if (assemblyNames != null) {
75                                 foreach (AssemblyName assem in assemblyNames) {
76                                                 Assembly myAssembly = Assembly.Load (assem);
77                                                 result = myAssembly.GetType (typeString, false);
78                                                 if (result != null)
79                                                         return result;
80                                         }
81                         }
82                         if (result == null)
83                                 result = ResolveType (typeString);
84
85                         return result;
86                 }
87
88                 protected Type ResolveType (string typeString, ITypeResolutionService typeResolver) 
89                 {
90                         Type result = null;
91
92                         if (typeResolver != null)
93                                 result = typeResolver.GetType (typeString);
94
95                         if (result == null)
96                                 result = ResolveType (typeString);
97
98                         return result;
99                 }
100         }
101 }