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