|
1 /* |
|
2 * Copyright (c) 2004-2007 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: Handler for XML file modify. Defines the class to store and modify XML data. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #ifdef _DEBUG |
|
20 #include <driveinfo.h> |
|
21 #include <pathinfo.h> |
|
22 #endif |
|
23 |
|
24 #include "cxmlhandler.h" |
|
25 #include "clog.h" |
|
26 |
|
27 //-------------------------------------------------------------------------------------------- |
|
28 // |
|
29 // CXmlHandler::NewL |
|
30 // |
|
31 //-------------------------------------------------------------------------------------------- |
|
32 CXmlHandler* CXmlHandler::NewL() |
|
33 { |
|
34 CXmlHandler *self = NewLC(); |
|
35 CleanupStack::Pop(); // self |
|
36 |
|
37 return self; |
|
38 } |
|
39 |
|
40 //-------------------------------------------------------------------------------------------- |
|
41 // |
|
42 // CXmlHandler::NewLC |
|
43 // |
|
44 //-------------------------------------------------------------------------------------------- |
|
45 CXmlHandler* CXmlHandler::NewLC() |
|
46 { |
|
47 CXmlHandler *self = new (ELeave) CXmlHandler(); |
|
48 CleanupStack::PushL(self); |
|
49 self->ConstructL(); |
|
50 return self; |
|
51 } |
|
52 |
|
53 //-------------------------------------------------------------------------------------------- |
|
54 // |
|
55 // CXmlHandler::~CXmlHandler |
|
56 // |
|
57 //-------------------------------------------------------------------------------------------- |
|
58 CXmlHandler::~CXmlHandler() |
|
59 { |
|
60 if(iDataBuf) |
|
61 delete iDataBuf; |
|
62 iDataBuf = NULL; |
|
63 |
|
64 Reset(); |
|
65 |
|
66 iFs.Close(); |
|
67 } |
|
68 |
|
69 //-------------------------------------------------------------------------------------------- |
|
70 // |
|
71 // CXmlHandler::ConstructL |
|
72 // |
|
73 //-------------------------------------------------------------------------------------------- |
|
74 void CXmlHandler::ConstructL() |
|
75 { |
|
76 LOG("[CXmlHandler::ConstructL]\t"); |
|
77 User::LeaveIfError( iFs.Connect() ); |
|
78 Reset(); |
|
79 } |
|
80 |
|
81 //-------------------------------------------------------------------------------------------- |
|
82 // |
|
83 // CXmlHandler::InitFromFileL |
|
84 // |
|
85 //-------------------------------------------------------------------------------------------- |
|
86 void CXmlHandler::InitFromFileL(const TDesC& aFile, const TInt aId) |
|
87 { |
|
88 LOG("[CXmlHandler::InitFromFileL]\t"); |
|
89 Reset(); |
|
90 |
|
91 ReadFileL(aFile); |
|
92 iBufferId = aId; |
|
93 |
|
94 LOG("[CXmlHandler::InitFromFileL]\t Done."); |
|
95 } |
|
96 |
|
97 //-------------------------------------------------------------------------------------------- |
|
98 // |
|
99 // CXmlHandler::InitFromBufferL |
|
100 // |
|
101 //-------------------------------------------------------------------------------------------- |
|
102 void CXmlHandler::InitFromBufferL(const TDesC8& aBuffer, const TInt aId) |
|
103 { |
|
104 LOG("[CXmlHandler::InitFromBufferL]\t"); |
|
105 |
|
106 Reset(); |
|
107 |
|
108 if(0 >= aBuffer.Length()) |
|
109 { |
|
110 User::Leave(KErrArgument); |
|
111 } |
|
112 |
|
113 iDataBuf = static_cast<HBufC8*>(User::LeaveIfNull(aBuffer.AllocL())); |
|
114 iBufferId = aId; |
|
115 |
|
116 LOG("[CXmlHandler::InitFromBufferL]\t Done."); |
|
117 } |
|
118 |
|
119 //-------------------------------------------------------------------------------------------- |
|
120 // |
|
121 // CXmlHandler::Reset |
|
122 // |
|
123 //-------------------------------------------------------------------------------------------- |
|
124 void CXmlHandler::Reset() |
|
125 { |
|
126 LOG("[CXmlHandler::Reset]\t"); |
|
127 |
|
128 if(iDataBuf) |
|
129 delete iDataBuf; |
|
130 iDataBuf = NULL; |
|
131 |
|
132 iBufferId = KErrNotFound; |
|
133 } |
|
134 |
|
135 //-------------------------------------------------------------------------------------------- |
|
136 // |
|
137 // CXmlHandler::Reset |
|
138 // |
|
139 //-------------------------------------------------------------------------------------------- |
|
140 TInt CXmlHandler::BufferId() |
|
141 { |
|
142 return iBufferId; |
|
143 } |
|
144 |
|
145 //-------------------------------------------------------------------------------------------- |
|
146 // |
|
147 // CXmlHandler::Reset |
|
148 // |
|
149 //-------------------------------------------------------------------------------------------- |
|
150 TPtrC8 CXmlHandler::Buffer() |
|
151 { |
|
152 if(iDataBuf) |
|
153 return iDataBuf->Des(); |
|
154 |
|
155 return KNullDesC8(); |
|
156 } |
|
157 |
|
158 // PRIVATE |
|
159 |
|
160 //-------------------------------------------------------------------------------------------- |
|
161 // |
|
162 // CXmlHandler::CXmlHandler |
|
163 // |
|
164 //-------------------------------------------------------------------------------------------- |
|
165 CXmlHandler::CXmlHandler() |
|
166 { |
|
167 } |
|
168 |
|
169 //-------------------------------------------------------------------------------------------- |
|
170 // |
|
171 // CXmlHandler::FinalizeBufferL |
|
172 // |
|
173 //-------------------------------------------------------------------------------------------- |
|
174 TPtrC8 CXmlHandler::FinalizeBufferL(const TDesC8& aTag) |
|
175 { |
|
176 LOG("[CXmlHandler]\t FinalizeBufferL"); |
|
177 |
|
178 User::LeaveIfNull(iDataBuf); |
|
179 |
|
180 // Cut the template to the end tag |
|
181 TInt pos = iDataBuf->Find(aTag); |
|
182 if(KErrNotFound == pos) |
|
183 User::Leave(KErrCorrupt); |
|
184 |
|
185 TInt dataLen = pos + aTag.Length(); |
|
186 TPtrC8 tail = iDataBuf->Left(dataLen); |
|
187 |
|
188 return tail; |
|
189 } |
|
190 |
|
191 //-------------------------------------------------------------------------------------------- |
|
192 // |
|
193 // CXmlHandler::DeleteBufferData |
|
194 // |
|
195 //-------------------------------------------------------------------------------------------- |
|
196 void CXmlHandler::DeleteBufferData(TInt aOffset, TInt aLength) |
|
197 { |
|
198 iDataBuf->Des().Delete(aOffset, aLength); |
|
199 |
|
200 } |
|
201 //-------------------------------------------------------------------------------------------- |
|
202 // |
|
203 // CXmlHandler::ReadL |
|
204 // |
|
205 //-------------------------------------------------------------------------------------------- |
|
206 void CXmlHandler::ReadFileL(const TDesC& aFileName) |
|
207 { |
|
208 LOG("[CXmlHandler]\t ReadFileL"); |
|
209 // Open the template file and read it into buffer |
|
210 RFile templateFile; |
|
211 CleanupClosePushL(templateFile); |
|
212 |
|
213 User::LeaveIfError( templateFile.Open( iFs, aFileName, EFileRead) ); |
|
214 |
|
215 TInt fileSize = 0; |
|
216 TInt err = templateFile.Size(fileSize); |
|
217 if(KErrNone != err) |
|
218 fileSize = KBtMaxFileSize; |
|
219 |
|
220 iDataBuf = HBufC8::NewL(fileSize); |
|
221 TPtr8 bufferPtr( iDataBuf->Des() ); |
|
222 |
|
223 bufferPtr.Zero(); |
|
224 templateFile.Read(bufferPtr); |
|
225 |
|
226 CleanupStack::PopAndDestroy(&templateFile); |
|
227 } |
|
228 |
|
229 //-------------------------------------------------------------------------------------------- |
|
230 // |
|
231 // CXmlHandler::GetDataL |
|
232 // |
|
233 //-------------------------------------------------------------------------------------------- |
|
234 TPtrC8 CXmlHandler::GetDataL(const TDesC8 &aStart, const TDesC8 &aEnd, TInt& aOffset) |
|
235 { |
|
236 User::LeaveIfNull(iDataBuf); |
|
237 return ParseValue(iDataBuf->Des(), aStart, aEnd, aOffset); |
|
238 } |
|
239 |
|
240 //-------------------------------------------------------------------------------------------- |
|
241 // |
|
242 // CXmlHandler::GetDataListL |
|
243 // |
|
244 //-------------------------------------------------------------------------------------------- |
|
245 void CXmlHandler::GetDataListL(const TDesC8 &aStart, const TDesC8 &aEnd, CDesC8ArrayFlat& aList) |
|
246 { |
|
247 User::LeaveIfNull(iDataBuf); |
|
248 ParseValueListL(iDataBuf->Des(), aStart, aEnd, aList); |
|
249 } |
|
250 |
|
251 //-------------------------------------------------------------------------------------------- |
|
252 // |
|
253 // CXmlHandler::ParseValue |
|
254 // |
|
255 //-------------------------------------------------------------------------------------------- |
|
256 TPtrC8 CXmlHandler::ParseValue(const TDesC8 &aData, const TDesC8 &aStart, const TDesC8 &aEnd, TInt& aOffset) |
|
257 { |
|
258 // LOG82("[CXmlHandler::ParseValue]\t s: %S, e: %S", &aStart, &aEnd); |
|
259 aOffset = aData.Find(aStart); |
|
260 if(0 > aOffset) |
|
261 { |
|
262 return KNullDesC8(); |
|
263 } |
|
264 |
|
265 TInt end = aData.Find(aEnd); |
|
266 TInt len = end-aOffset-aStart.Length(); |
|
267 if(0 >= len) |
|
268 { |
|
269 aOffset = end; |
|
270 return KNullDesC8(); |
|
271 } |
|
272 |
|
273 return aData.Mid(aOffset+aStart.Length(), len); |
|
274 } |
|
275 |
|
276 |
|
277 //-------------------------------------------------------------------------------------------- |
|
278 // |
|
279 // CXmlHandler::ParseValueList |
|
280 // |
|
281 //-------------------------------------------------------------------------------------------- |
|
282 void CXmlHandler::ParseValueListL(const TDesC8 &aData, const TDesC8 &aStart, const TDesC8 &aEnd, CDesC8ArrayFlat& aList) |
|
283 { |
|
284 LOG("[CXmlHandler::ParseValueListL]\t "); |
|
285 TPtrC8 tmpPtr = aData; |
|
286 |
|
287 TInt offset = tmpPtr.Find(aStart); |
|
288 while(offset != KErrNotFound) |
|
289 { |
|
290 TInt end = tmpPtr.Find(aEnd); |
|
291 if(0 > end) |
|
292 { |
|
293 return; |
|
294 } |
|
295 TPtrC8 valuePtr = tmpPtr.Mid(offset+aStart.Length(), end-offset-aStart.Length()); |
|
296 |
|
297 HBufC8* listEntry = valuePtr.AllocLC(); |
|
298 aList.AppendL(listEntry->Des()); |
|
299 CleanupStack::PopAndDestroy(); // listEntry |
|
300 |
|
301 tmpPtr.Set( tmpPtr.Right(tmpPtr.Length()-(end+aEnd.Length()) ) ); |
|
302 offset = tmpPtr.Find(aStart); |
|
303 |
|
304 } |
|
305 } |
|
306 |
|
307 |
|
308 //-------------------------------------------------------------------------------------------- |
|
309 // |
|
310 // CXmlHandler::ComposeDataL |
|
311 // |
|
312 //-------------------------------------------------------------------------------------------- |
|
313 void CXmlHandler::ComposeDataL(const TInt aValue, const TDesC8 &aTag) |
|
314 { |
|
315 TBuf8<KDefaultRealWidth> valueStr; |
|
316 valueStr.Zero(); |
|
317 valueStr.AppendNum(aValue); |
|
318 |
|
319 ComposeDataL(valueStr, aTag); |
|
320 } |
|
321 //-------------------------------------------------------------------------------------------- |
|
322 // |
|
323 // CXmlHandler::ComposeDataL |
|
324 // |
|
325 //-------------------------------------------------------------------------------------------- |
|
326 void CXmlHandler::ComposeDataL(const TDesC8 &aData, const TDesC8 &aTag) |
|
327 { |
|
328 User::LeaveIfNull(iDataBuf); |
|
329 |
|
330 TInt pos = iDataBuf->Find(aTag); |
|
331 if(KErrNotFound == pos) |
|
332 User::Leave(KErrCorrupt); |
|
333 |
|
334 while(KErrNotFound != pos) |
|
335 { |
|
336 TInt requiredSize = iDataBuf->Des().Length() + aData.Length(); |
|
337 if(iDataBuf->Des().MaxLength() < requiredSize) |
|
338 { |
|
339 iDataBuf = iDataBuf->ReAllocL(requiredSize); |
|
340 } |
|
341 iDataBuf->Des().Replace(pos, aTag.Length(), aData); |
|
342 pos = iDataBuf->Find(aTag); |
|
343 } |
|
344 } |
|
345 |
|
346 //-------------------------------------------------------------------------------------------- |
|
347 // |
|
348 // CXmlHandler::InsertDataL |
|
349 // |
|
350 //-------------------------------------------------------------------------------------------- |
|
351 void CXmlHandler::InsertDataL(const TDesC8 &aData, const TInt aOffset) |
|
352 { |
|
353 TInt requiredSize = iDataBuf->Des().Length() + aData.Length(); |
|
354 if(iDataBuf->Des().MaxLength() < requiredSize) |
|
355 { |
|
356 iDataBuf = iDataBuf->ReAllocL(requiredSize); |
|
357 } |
|
358 iDataBuf->Des().Insert(aOffset, aData); |
|
359 } |
|
360 |
|
361 // End of file |