|
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 |
|
21 namespace SymbianParserLib.BaseStructures |
|
22 { |
|
23 public abstract class ParserElementBase |
|
24 { |
|
25 #region Internal enumerations |
|
26 [Flags] |
|
27 internal enum TElementBaseFlags |
|
28 { |
|
29 EElementBaseFlagsNone = 0, |
|
30 EElementBaseFlagsIsDisabled = 1, |
|
31 EElementBaseFlagsIsNeverEnding = 2, |
|
32 EElementBaseFlagsIsComplete = 4, |
|
33 EElementBaseFlagsSetDisabledWhenComplete = 8, |
|
34 } |
|
35 #endregion |
|
36 |
|
37 #region Delegates and events |
|
38 public delegate void ElementCompleteHandler( ParserElementBase aElement ); |
|
39 public event ElementCompleteHandler ElementComplete; |
|
40 #endregion |
|
41 |
|
42 #region Constructors |
|
43 protected ParserElementBase() |
|
44 : this( string.Empty ) |
|
45 { |
|
46 } |
|
47 |
|
48 protected ParserElementBase( string aName ) |
|
49 : this( aName, null ) |
|
50 { |
|
51 } |
|
52 |
|
53 protected ParserElementBase( string aName, object aTag ) |
|
54 : this( aName, null, aTag ) |
|
55 { |
|
56 } |
|
57 |
|
58 protected ParserElementBase( ParserElementBase aParent ) |
|
59 : this( string.Empty, aParent, null ) |
|
60 { |
|
61 } |
|
62 |
|
63 protected ParserElementBase( string aName, ParserElementBase aParent, object aTag ) |
|
64 { |
|
65 iName = aName; |
|
66 iParent = aParent; |
|
67 iTag = aTag; |
|
68 |
|
69 // Default behaviour is to disable the item when it is complete |
|
70 DisableWhenComplete = true; |
|
71 } |
|
72 #endregion |
|
73 |
|
74 #region Abstract API |
|
75 internal abstract ParserResponse Offer( ref string aLine ); |
|
76 |
|
77 internal virtual string CurrentLine |
|
78 { |
|
79 get { return string.Empty; } |
|
80 } |
|
81 |
|
82 internal virtual void OnDisableWhenComplete() |
|
83 { |
|
84 } |
|
85 |
|
86 internal virtual void OnNeverEnding() |
|
87 { |
|
88 } |
|
89 #endregion |
|
90 |
|
91 #region API |
|
92 public string GetCurrentLine() |
|
93 { |
|
94 string line = string.Empty; |
|
95 // |
|
96 ParserElementBase element = this; |
|
97 while ( line == string.Empty && element != null ) |
|
98 { |
|
99 line = element.CurrentLine; |
|
100 element = element.Parent; |
|
101 } |
|
102 // |
|
103 return line; |
|
104 } |
|
105 |
|
106 public void SetRepetitions( int aValue ) |
|
107 { |
|
108 iRepetitions = aValue; |
|
109 IsComplete = ( aValue == 0 ) && !IsNeverEnding; |
|
110 IsDisabled = ( IsComplete && DisableWhenComplete ); |
|
111 } |
|
112 #endregion |
|
113 |
|
114 #region Properties |
|
115 public bool DisableWhenComplete |
|
116 { |
|
117 get { return ( iElementFlags & TElementBaseFlags.EElementBaseFlagsSetDisabledWhenComplete ) == TElementBaseFlags.EElementBaseFlagsSetDisabledWhenComplete; } |
|
118 set |
|
119 { |
|
120 if ( value ) |
|
121 { |
|
122 iElementFlags |= TElementBaseFlags.EElementBaseFlagsSetDisabledWhenComplete; |
|
123 } |
|
124 else |
|
125 { |
|
126 iElementFlags &= ~TElementBaseFlags.EElementBaseFlagsSetDisabledWhenComplete; |
|
127 } |
|
128 |
|
129 OnDisableWhenComplete(); |
|
130 } |
|
131 } |
|
132 |
|
133 public string Name |
|
134 { |
|
135 get { return iName; } |
|
136 set { iName = value; } |
|
137 } |
|
138 |
|
139 public object Tag |
|
140 { |
|
141 get { return iTag; } |
|
142 set { iTag = value; } |
|
143 } |
|
144 |
|
145 public int Repetitions |
|
146 { |
|
147 get { return iRepetitions; } |
|
148 } |
|
149 |
|
150 public bool IsDisabled |
|
151 { |
|
152 get { return ( iElementFlags & TElementBaseFlags.EElementBaseFlagsIsDisabled ) == TElementBaseFlags.EElementBaseFlagsIsDisabled; } |
|
153 set |
|
154 { |
|
155 if ( value ) |
|
156 { |
|
157 iElementFlags |= TElementBaseFlags.EElementBaseFlagsIsDisabled; |
|
158 } |
|
159 else |
|
160 { |
|
161 iElementFlags &= ~TElementBaseFlags.EElementBaseFlagsIsDisabled; |
|
162 } |
|
163 } |
|
164 } |
|
165 |
|
166 public bool IsNeverEnding |
|
167 { |
|
168 get { return ( iElementFlags & TElementBaseFlags.EElementBaseFlagsIsNeverEnding ) == TElementBaseFlags.EElementBaseFlagsIsNeverEnding; } |
|
169 set |
|
170 { |
|
171 if ( value ) |
|
172 { |
|
173 iElementFlags |= TElementBaseFlags.EElementBaseFlagsIsNeverEnding; |
|
174 } |
|
175 else |
|
176 { |
|
177 iElementFlags &= ~TElementBaseFlags.EElementBaseFlagsIsNeverEnding; |
|
178 } |
|
179 |
|
180 OnNeverEnding(); |
|
181 } |
|
182 } |
|
183 |
|
184 public bool IsComplete |
|
185 { |
|
186 get { return ( iElementFlags & TElementBaseFlags.EElementBaseFlagsIsComplete ) == TElementBaseFlags.EElementBaseFlagsIsComplete; } |
|
187 set |
|
188 { |
|
189 bool isComplete = false; |
|
190 |
|
191 // Decrement the number of repetitions and if the final value is zero |
|
192 // then the line is 'really' complete. |
|
193 if ( value == true ) |
|
194 { |
|
195 if ( !IsNeverEnding ) |
|
196 { |
|
197 --iRepetitions; |
|
198 } |
|
199 |
|
200 isComplete = ( iRepetitions <= 0 ) || IsNeverEnding; |
|
201 } |
|
202 else |
|
203 { |
|
204 isComplete = value; |
|
205 } |
|
206 |
|
207 // Final value update |
|
208 if ( isComplete ) |
|
209 { |
|
210 iElementFlags |= TElementBaseFlags.EElementBaseFlagsIsComplete; |
|
211 } |
|
212 else |
|
213 { |
|
214 iElementFlags &= ~TElementBaseFlags.EElementBaseFlagsIsComplete; |
|
215 } |
|
216 |
|
217 if ( isComplete ) |
|
218 { |
|
219 if ( DisableWhenComplete ) |
|
220 { |
|
221 IsDisabled = true; |
|
222 } |
|
223 |
|
224 OnElementComplete(); |
|
225 } |
|
226 |
|
227 // If we're never ending, then the item is never really complete |
|
228 if ( IsNeverEnding ) |
|
229 { |
|
230 iElementFlags &= ~TElementBaseFlags.EElementBaseFlagsIsComplete; |
|
231 } |
|
232 } |
|
233 } |
|
234 |
|
235 public ParserElementBase Parent |
|
236 { |
|
237 get { return iParent; } |
|
238 set { iParent = value; } |
|
239 } |
|
240 #endregion |
|
241 |
|
242 #region Internal methods |
|
243 protected void OnElementComplete() |
|
244 { |
|
245 if ( ElementComplete != null ) |
|
246 { |
|
247 ElementComplete( this ); |
|
248 } |
|
249 } |
|
250 #endregion |
|
251 |
|
252 #region Internal constants |
|
253 #endregion |
|
254 |
|
255 #region From System.Object |
|
256 public override string ToString() |
|
257 { |
|
258 return Name; |
|
259 } |
|
260 #endregion |
|
261 |
|
262 #region Data members |
|
263 private string iName; |
|
264 private object iTag = null; |
|
265 private TElementBaseFlags iElementFlags = TElementBaseFlags.EElementBaseFlagsNone; |
|
266 private int iRepetitions = 1; |
|
267 private ParserElementBase iParent = null; |
|
268 #endregion |
|
269 } |
|
270 } |