|
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 SymbianParserLib.Enums; |
|
22 using SymbianParserLib.Elements; |
|
23 using SymbianParserLib.Exceptions; |
|
24 using SymbianParserLib.BaseStructures; |
|
25 using SymbianParserLib.RegExTranslators; |
|
26 |
|
27 namespace SymbianParserLib.Engine |
|
28 { |
|
29 public class ParserEngine : BaseStructures.ParserElementBase, IEnumerable<ParserParagraph> |
|
30 { |
|
31 #region Delegates and events |
|
32 public event BaseStructures.ParserElementBase.ElementCompleteHandler ParagraphComplete; |
|
33 #endregion |
|
34 |
|
35 #region Constructors |
|
36 public ParserEngine() |
|
37 { |
|
38 } |
|
39 #endregion |
|
40 |
|
41 #region API |
|
42 public bool OfferLine( ref string aLine ) |
|
43 { |
|
44 iCurrentLine = aLine; |
|
45 // |
|
46 ParserResponse response = Offer( ref aLine ); |
|
47 bool ret = response.WasHandled; |
|
48 // |
|
49 return ret; |
|
50 } |
|
51 |
|
52 public void Reset() |
|
53 { |
|
54 iParagraphs.Clear(); |
|
55 } |
|
56 |
|
57 public void Remove( ParserParagraph aParagraph ) |
|
58 { |
|
59 aParagraph.ElementComplete -= new SymbianParserLib.BaseStructures.ParserElementBase.ElementCompleteHandler( Paragraph_ElementComplete ); |
|
60 iParagraphs.Remove( aParagraph ); |
|
61 } |
|
62 |
|
63 public void RemoveRange( int aStartAt ) |
|
64 { |
|
65 int count = iParagraphs.Count - aStartAt; |
|
66 iParagraphs.RemoveRange( aStartAt, count ); |
|
67 } |
|
68 |
|
69 public void Add( params ParserParagraph[] aParagraphs ) |
|
70 { |
|
71 foreach ( ParserParagraph para in aParagraphs ) |
|
72 { |
|
73 Add( para ); |
|
74 } |
|
75 } |
|
76 |
|
77 public void Add( ParserParagraph aParagraph ) |
|
78 { |
|
79 aParagraph.Parent = this; |
|
80 aParagraph.ElementComplete += new SymbianParserLib.BaseStructures.ParserElementBase.ElementCompleteHandler( Paragraph_ElementComplete ); |
|
81 iParagraphs.Add( aParagraph ); |
|
82 } |
|
83 #endregion |
|
84 |
|
85 #region Properties |
|
86 public int Count |
|
87 { |
|
88 get { return iParagraphs.Count; } |
|
89 } |
|
90 |
|
91 public ParserParagraph this[ int aIndex ] |
|
92 { |
|
93 get { return iParagraphs[ aIndex ]; } |
|
94 } |
|
95 #endregion |
|
96 |
|
97 #region From ParserElementBase |
|
98 internal override string CurrentLine |
|
99 { |
|
100 get { return iCurrentLine; } |
|
101 } |
|
102 |
|
103 internal override ParserResponse Offer( ref string aLine ) |
|
104 { |
|
105 ++iLineNumber; |
|
106 // |
|
107 ParserResponse ret = new ParserResponse(); |
|
108 // |
|
109 do |
|
110 { |
|
111 ret = TryToConsumeLine( ref aLine ); |
|
112 } |
|
113 while ( ret.Type == ParserResponse.TResponseType.EResponseTypeHandledByRequiresReProcessing ); |
|
114 // |
|
115 return ret; |
|
116 } |
|
117 #endregion |
|
118 |
|
119 #region Internal methods |
|
120 private ParserResponse TryToConsumeLine( ref string aLine ) |
|
121 { |
|
122 ParserResponse ret = new ParserResponse(); |
|
123 // |
|
124 foreach ( ParserParagraph paragraph in iParagraphs ) |
|
125 { |
|
126 if ( !paragraph.IsDisabled ) |
|
127 { |
|
128 ret = paragraph.Offer( ref aLine ); |
|
129 if ( ret.Type != ParserResponse.TResponseType.EResponseTypeUnhandled ) |
|
130 { |
|
131 break; |
|
132 } |
|
133 } |
|
134 } |
|
135 // |
|
136 return ret; |
|
137 } |
|
138 #endregion |
|
139 |
|
140 #region Internal constants |
|
141 #endregion |
|
142 |
|
143 #region From IEnumerable<ParserParagraph> |
|
144 public IEnumerator<ParserParagraph> GetEnumerator() |
|
145 { |
|
146 return new ParserEngineEnumerator( this ); |
|
147 } |
|
148 |
|
149 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
|
150 { |
|
151 return new ParserEngineEnumerator( this ); |
|
152 } |
|
153 #endregion |
|
154 |
|
155 #region Internal event handlers |
|
156 void Paragraph_ElementComplete( SymbianParserLib.BaseStructures.ParserElementBase aElement ) |
|
157 { |
|
158 if ( ParagraphComplete != null ) |
|
159 { |
|
160 ParagraphComplete( aElement ); |
|
161 } |
|
162 } |
|
163 #endregion |
|
164 |
|
165 #region From System.Object |
|
166 public override string ToString() |
|
167 { |
|
168 return base.ToString(); |
|
169 } |
|
170 #endregion |
|
171 |
|
172 #region Data members |
|
173 private long iLineNumber = 0; |
|
174 private string iCurrentLine = string.Empty; |
|
175 private List<ParserParagraph> iParagraphs = new List<ParserParagraph>(); |
|
176 #endregion |
|
177 } |
|
178 |
|
179 #region Enumerator |
|
180 internal class ParserEngineEnumerator : IEnumerator<ParserParagraph> |
|
181 { |
|
182 #region Constructors |
|
183 public ParserEngineEnumerator( ParserEngine aObject ) |
|
184 { |
|
185 iObject = aObject; |
|
186 } |
|
187 #endregion |
|
188 |
|
189 #region IEnumerator Members |
|
190 public void Reset() |
|
191 { |
|
192 iCurrentIndex = -1; |
|
193 } |
|
194 |
|
195 public object Current |
|
196 { |
|
197 get |
|
198 { |
|
199 return iObject[ iCurrentIndex ]; |
|
200 } |
|
201 } |
|
202 |
|
203 public bool MoveNext() |
|
204 { |
|
205 return ( ++iCurrentIndex < iObject.Count ); |
|
206 } |
|
207 #endregion |
|
208 |
|
209 #region From IEnumerator<ParserParagraph> |
|
210 ParserParagraph IEnumerator<ParserParagraph>.Current |
|
211 { |
|
212 get { return iObject[ iCurrentIndex ]; } |
|
213 } |
|
214 #endregion |
|
215 |
|
216 #region From IDisposable |
|
217 public void Dispose() |
|
218 { |
|
219 } |
|
220 #endregion |
|
221 |
|
222 #region Data members |
|
223 private readonly ParserEngine iObject; |
|
224 private int iCurrentIndex = -1; |
|
225 #endregion |
|
226 } |
|
227 #endregion |
|
228 } |