|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Collections.Generic; |
|
19 using System.Text; |
|
20 using System.Reflection; |
|
21 using System.ComponentModel; |
|
22 using SymbianParserLib.Enums; |
|
23 using SymbianParserLib.ValueStores; |
|
24 using SymbianParserLib.Elements.SubFields; |
|
25 |
|
26 namespace SymbianParserLib.BaseStructures |
|
27 { |
|
28 public abstract class ParserElementBaseWithValueStore : ParserElementBase |
|
29 { |
|
30 #region Constructors |
|
31 protected ParserElementBaseWithValueStore() |
|
32 : this( string.Empty ) |
|
33 { |
|
34 } |
|
35 |
|
36 protected ParserElementBaseWithValueStore( string aName ) |
|
37 : base( aName ) |
|
38 { |
|
39 } |
|
40 #endregion |
|
41 |
|
42 #region Abstract API |
|
43 internal abstract void SetTargetProperty( object aPropertyObject, string aPropertyName, int aIndex ); |
|
44 |
|
45 internal virtual void SetValue( ParserFieldFormatSpecifier aFormat, ParserFieldFormatValue aValue ) |
|
46 { |
|
47 ValueStore vs = GetValueStore( this ); |
|
48 if ( vs == null ) |
|
49 { |
|
50 // Make a new "store internally" value store |
|
51 iValueStore = new ValueStore(); |
|
52 vs = iValueStore; |
|
53 } |
|
54 // |
|
55 vs.SetValue( aFormat, aValue ); |
|
56 } |
|
57 #endregion |
|
58 |
|
59 #region API |
|
60 public virtual void SetTargetObject() |
|
61 { |
|
62 iValueStore = new ValueStore(); |
|
63 } |
|
64 |
|
65 public virtual void SetTargetProperty( object aPropertyObject, string aPropertyName ) |
|
66 { |
|
67 SetTargetProperty( aPropertyObject, aPropertyName, KGloballyApplicable ); |
|
68 } |
|
69 |
|
70 public virtual void SetTargetProperties( object aPropertyObjects, params string[] aPropertyNames ) |
|
71 { |
|
72 if ( aPropertyObjects == null ) |
|
73 { |
|
74 throw new ArgumentException( "Property object cannot be null" ); |
|
75 } |
|
76 |
|
77 int count = aPropertyNames.Length; |
|
78 if ( count == 0 ) |
|
79 { |
|
80 throw new ArgumentException( "Property name array must not be empty" ); |
|
81 } |
|
82 |
|
83 for ( int i = 0; i < count; i++ ) |
|
84 { |
|
85 string propName = aPropertyNames[ i ]; |
|
86 if ( propName.Length == 0 ) |
|
87 { |
|
88 throw new ArgumentException( "Property name is invalid" ); |
|
89 } |
|
90 |
|
91 SetTargetProperty( aPropertyObjects, propName, i ); |
|
92 } |
|
93 } |
|
94 |
|
95 public virtual void SetTargetProperties( object[] aPropertyObjects, params string[] aPropertyNames ) |
|
96 { |
|
97 if ( aPropertyObjects.Length == 0 || aPropertyNames.Length == 0 || aPropertyObjects.Length != aPropertyNames.Length ) |
|
98 { |
|
99 throw new ArgumentException( "Property object/names are invalid" ); |
|
100 } |
|
101 // |
|
102 int count = aPropertyNames.Length; |
|
103 for ( int i = 0; i < count; i++ ) |
|
104 { |
|
105 object obj = aPropertyObjects[ i ]; |
|
106 if ( obj == null ) |
|
107 { |
|
108 throw new ArgumentException( "Property object cannot be null" ); |
|
109 } |
|
110 |
|
111 string propName = aPropertyNames[ i ]; |
|
112 if ( propName.Length == 0 ) |
|
113 { |
|
114 throw new ArgumentException( "Property name is invalid" ); |
|
115 } |
|
116 |
|
117 SetTargetProperty( obj, propName, i ); |
|
118 } |
|
119 } |
|
120 |
|
121 public virtual void SetTargetMethod( object aMethodObject, string aMethodName ) |
|
122 { |
|
123 SetTargetMethod( aMethodObject, aMethodName, TValueStoreMethodArguments.EValueStoreMethodArgumentCalculateAtRuntime ); |
|
124 } |
|
125 |
|
126 public virtual void SetTargetMethod( object aMethodObject, string aMethodName, params TValueStoreMethodArguments[] aMethodArgs ) |
|
127 { |
|
128 // Check that the args are okay. |
|
129 foreach ( TValueStoreMethodArguments argType in aMethodArgs ) |
|
130 { |
|
131 if ( argType == TValueStoreMethodArguments.EValueStoreMethodArgumentCalculateAtRuntime ) |
|
132 { |
|
133 if ( aMethodArgs.Length != 1 ) |
|
134 { |
|
135 throw new ArgumentException( "Method arguments must contain only a single entry when using \'calculate at runtime\' approach" ); |
|
136 } |
|
137 } |
|
138 } |
|
139 |
|
140 iValueStore = new ValueStore(); |
|
141 iValueStore.SetTargetMethod( aMethodObject, aMethodName, aMethodArgs ); |
|
142 } |
|
143 #endregion |
|
144 |
|
145 #region Properties |
|
146 public bool IsInt |
|
147 { |
|
148 get |
|
149 { |
|
150 bool ret = ( iValueStore != null ) && iValueStore.IsInt; |
|
151 return ret; |
|
152 } |
|
153 } |
|
154 |
|
155 public bool IsUint |
|
156 { |
|
157 get |
|
158 { |
|
159 bool ret = ( iValueStore != null ) && iValueStore.IsUint; |
|
160 return ret; |
|
161 } |
|
162 } |
|
163 |
|
164 public bool IsString |
|
165 { |
|
166 get |
|
167 { |
|
168 bool ret = ( iValueStore != null ) && iValueStore.IsString; |
|
169 return ret; |
|
170 } |
|
171 } |
|
172 |
|
173 public int AsInt |
|
174 { |
|
175 get |
|
176 { |
|
177 int ret = 0; |
|
178 // |
|
179 if ( IsInt ) |
|
180 { |
|
181 ret = iValueStore.AsInt; |
|
182 } |
|
183 // |
|
184 return ret; |
|
185 } |
|
186 } |
|
187 |
|
188 public uint AsUint |
|
189 { |
|
190 get |
|
191 { |
|
192 uint ret = 0; |
|
193 // |
|
194 if ( IsUint ) |
|
195 { |
|
196 ret = iValueStore.AsUint; |
|
197 } |
|
198 // |
|
199 return ret; |
|
200 } |
|
201 } |
|
202 |
|
203 public string AsString |
|
204 { |
|
205 get |
|
206 { |
|
207 string ret = string.Empty; |
|
208 // |
|
209 if ( IsString ) |
|
210 { |
|
211 ret = iValueStore.AsString; |
|
212 } |
|
213 // |
|
214 return ret; |
|
215 } |
|
216 } |
|
217 #endregion |
|
218 |
|
219 #region Internal methods |
|
220 #endregion |
|
221 |
|
222 #region Internal static methods |
|
223 private static ValueStore GetValueStore( ParserElementBase aElement ) |
|
224 { |
|
225 ValueStore ret = null; |
|
226 // |
|
227 if ( aElement is ParserElementBaseWithValueStore ) |
|
228 { |
|
229 ParserElementBaseWithValueStore element = (ParserElementBaseWithValueStore) aElement; |
|
230 if ( element.iValueStore != null ) |
|
231 { |
|
232 ret = element.iValueStore; |
|
233 } |
|
234 else if ( element.Parent != null ) |
|
235 { |
|
236 ret = GetValueStore( element.Parent ); |
|
237 } |
|
238 } |
|
239 // |
|
240 return ret; |
|
241 } |
|
242 #endregion |
|
243 |
|
244 #region Internal constant |
|
245 protected const int KGloballyApplicable = -1; |
|
246 #endregion |
|
247 |
|
248 #region From System.Object |
|
249 public override string ToString() |
|
250 { |
|
251 return base.Name; |
|
252 } |
|
253 #endregion |
|
254 |
|
255 #region Data members |
|
256 protected ValueStore iValueStore = null; |
|
257 #endregion |
|
258 } |
|
259 } |