|
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: ?Description |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include <xml/parser.h> |
|
21 #include <stringpool.h> |
|
22 #include <utf.h> |
|
23 #include <e32cmn.h> |
|
24 #include <bautils.h> |
|
25 |
|
26 #include "iaupdatexmlparser.h" |
|
27 #include "iaupdatexmlsubparser.h" |
|
28 #include "iaupdatedebug.h" |
|
29 |
|
30 |
|
31 // The text type to parse |
|
32 _LIT8( KXmlType, "text/xml" ); |
|
33 |
|
34 |
|
35 EXPORT_C CIAUpdateXmlParser* CIAUpdateXmlParser::NewL( |
|
36 CIAUpdateXmlSubParser* aSubParser ) |
|
37 { |
|
38 CIAUpdateXmlParser* self = |
|
39 CIAUpdateXmlParser::NewLC( aSubParser ); |
|
40 CleanupStack::Pop( self ); |
|
41 return self; |
|
42 } |
|
43 |
|
44 |
|
45 EXPORT_C CIAUpdateXmlParser* CIAUpdateXmlParser::NewLC( |
|
46 CIAUpdateXmlSubParser* aSubParser ) |
|
47 { |
|
48 CIAUpdateXmlParser* self = |
|
49 new( ELeave ) CIAUpdateXmlParser( aSubParser ); |
|
50 CleanupStack::PushL( self ); |
|
51 self->ConstructL(); |
|
52 return self; |
|
53 } |
|
54 |
|
55 |
|
56 EXPORT_C CIAUpdateXmlParser::CIAUpdateXmlParser( |
|
57 CIAUpdateXmlSubParser* aSubParser ) |
|
58 : CBase(), |
|
59 iSubParser( aSubParser ) |
|
60 { |
|
61 |
|
62 } |
|
63 |
|
64 |
|
65 EXPORT_C void CIAUpdateXmlParser::ConstructL() |
|
66 { |
|
67 if ( !iSubParser ) |
|
68 { |
|
69 User::Leave( KErrNotFound ); |
|
70 } |
|
71 iParser = Xml::CParser::NewL( KXmlType, *iSubParser ); |
|
72 } |
|
73 |
|
74 |
|
75 EXPORT_C CIAUpdateXmlParser::~CIAUpdateXmlParser() |
|
76 { |
|
77 delete iParser; |
|
78 delete iSubParser; |
|
79 } |
|
80 |
|
81 |
|
82 EXPORT_C void CIAUpdateXmlParser::ParseFileL( |
|
83 const TDesC& aFilePath ) |
|
84 { |
|
85 HBufC8* data( ReadFileL( aFilePath ) ); |
|
86 CleanupStack::PushL( data ); |
|
87 |
|
88 ParseL( *data ); |
|
89 |
|
90 CleanupStack::PopAndDestroy( data ); |
|
91 } |
|
92 |
|
93 |
|
94 EXPORT_C void CIAUpdateXmlParser::ParsePrivateFileL( |
|
95 const TDesC& aFileName ) |
|
96 { |
|
97 RFs fsSession; |
|
98 User::LeaveIfError( fsSession.Connect() ); |
|
99 CleanupClosePushL( fsSession ); |
|
100 |
|
101 // This will set the correct drive and private path |
|
102 // for the file server session. |
|
103 SetPrivateDriveL( fsSession, aFileName ); |
|
104 |
|
105 TFileName configFilePath; |
|
106 User::LeaveIfError( fsSession.SessionPath( configFilePath ) ); |
|
107 configFilePath.Append( aFileName ); |
|
108 |
|
109 CleanupStack::PopAndDestroy( &fsSession ); |
|
110 |
|
111 ParseFileL( configFilePath ); |
|
112 } |
|
113 |
|
114 |
|
115 EXPORT_C void CIAUpdateXmlParser::ParseL( const TDesC8& aData ) |
|
116 { |
|
117 // Use parent class functions to handle the parsing |
|
118 Parser().ParseBeginL(); |
|
119 Parser().ParseL( aData ); |
|
120 Parser().ParseEndL(); |
|
121 } |
|
122 |
|
123 |
|
124 EXPORT_C void CIAUpdateXmlParser::ParseL( const TDesC& aData ) |
|
125 { |
|
126 HBufC8* utf8( ConvertUnicodeToUtf8L( aData ) ); |
|
127 CleanupStack::PushL( utf8 ); |
|
128 |
|
129 ParseL( *utf8 ); |
|
130 |
|
131 CleanupStack::PopAndDestroy( utf8 ); |
|
132 } |
|
133 |
|
134 |
|
135 EXPORT_C CIAUpdateXmlSubParser& CIAUpdateXmlParser::SubParser() |
|
136 { |
|
137 return *iSubParser; |
|
138 } |
|
139 |
|
140 |
|
141 Xml::CParser& CIAUpdateXmlParser::Parser() |
|
142 { |
|
143 return *iParser; |
|
144 } |
|
145 |
|
146 |
|
147 HBufC8* CIAUpdateXmlParser::ReadFileL( const TDesC& aFilePath ) |
|
148 { |
|
149 RFs fs; |
|
150 |
|
151 User::LeaveIfError( fs.Connect() ); |
|
152 CleanupClosePushL( fs ); |
|
153 |
|
154 RFile file; |
|
155 User::LeaveIfError( file.Open( fs, |
|
156 aFilePath, |
|
157 EFileRead ) ); |
|
158 CleanupClosePushL( file ); |
|
159 TInt size; |
|
160 User::LeaveIfError( file.Size( size ) ); |
|
161 HBufC8* buffer = HBufC8::NewLC( size ); |
|
162 TPtr8 ptr( buffer->Des() ); |
|
163 User::LeaveIfError( file.Read( ptr, size ) ); |
|
164 CleanupStack::Pop( buffer ); |
|
165 CleanupStack::PopAndDestroy( &file ); |
|
166 |
|
167 CleanupStack::PopAndDestroy( &fs ); |
|
168 |
|
169 return buffer; |
|
170 } |
|
171 |
|
172 |
|
173 HBufC8* CIAUpdateXmlParser::ConvertUnicodeToUtf8L( |
|
174 const TDesC16& aUnicodeText ) |
|
175 { |
|
176 const TInt KConvertBufferSize( 64 ); |
|
177 |
|
178 // Place converted data here, |
|
179 // initial size double the conversion buffer. |
|
180 HBufC8* convertedData = HBufC8::NewL( KConvertBufferSize * 2 ); |
|
181 CleanupStack::PushL( convertedData ); |
|
182 TPtr8 destination( convertedData->Des() ); |
|
183 |
|
184 // Create a small output buffer |
|
185 TBuf8< KConvertBufferSize > outputBuffer; |
|
186 |
|
187 // Create a buffer for the unconverted text - initialised with the |
|
188 // input text |
|
189 TPtrC16 remainderOfUnicodeText( aUnicodeText ); |
|
190 |
|
191 for ( ;; ) // conversion loop |
|
192 { |
|
193 // Start conversion. When the output buffer is full, return the |
|
194 // number of characters that were not converted |
|
195 const TInt returnValue( |
|
196 CnvUtfConverter::ConvertFromUnicodeToUtf8( |
|
197 outputBuffer, |
|
198 remainderOfUnicodeText ) ); |
|
199 |
|
200 // check to see that the descriptor isn’t corrupt |
|
201 // - leave if it is |
|
202 if ( returnValue == CnvUtfConverter::EErrorIllFormedInput ) |
|
203 { |
|
204 User::Leave( KErrCorrupt ); |
|
205 } |
|
206 else if ( returnValue < 0 ) |
|
207 { |
|
208 // future-proof against "TError" expanding |
|
209 User::Leave( KErrGeneral ); |
|
210 } |
|
211 |
|
212 // Do something here to store the contents of the output buffer. |
|
213 if ( destination.Length() + outputBuffer.Length() >= |
|
214 destination.MaxLength() ) |
|
215 { |
|
216 HBufC8* newBuffer = convertedData->ReAllocL( |
|
217 ( destination.MaxLength() + outputBuffer.Length() ) * 2 ); |
|
218 |
|
219 CleanupStack::Pop( convertedData ); |
|
220 convertedData = newBuffer; |
|
221 CleanupStack::PushL( convertedData ); |
|
222 destination.Set( convertedData->Des() ); |
|
223 } |
|
224 |
|
225 destination.Append( outputBuffer ); |
|
226 outputBuffer.Zero(); |
|
227 |
|
228 // Finish conversion if there are no unconverted characters |
|
229 // in the remainder buffer |
|
230 if ( returnValue == 0 ) |
|
231 { |
|
232 break; |
|
233 } |
|
234 |
|
235 // Remove the converted source text from the remainder buffer. |
|
236 // The remainder buffer is then fed back into loop |
|
237 remainderOfUnicodeText.Set( |
|
238 remainderOfUnicodeText.Right( returnValue ) ); |
|
239 } |
|
240 |
|
241 CleanupStack::Pop( convertedData ); |
|
242 return convertedData; |
|
243 } |
|
244 |
|
245 |
|
246 void CIAUpdateXmlParser::SetPrivateDriveL( |
|
247 RFs& aFs, |
|
248 const TDesC& aFileName ) const |
|
249 { |
|
250 IAUPDATE_TRACE_1("[IAUPDATE] CIAUpdateXmlParser::SetPrivateDriveL() begin: %S", |
|
251 &aFileName); |
|
252 |
|
253 // This will set the correct drive and private path |
|
254 // for the file server session. |
|
255 |
|
256 // First try to find the file from the private directory |
|
257 // of the drive where the process exists. |
|
258 RProcess process; |
|
259 |
|
260 // Set the session private path according to |
|
261 // the process file name drive. |
|
262 TInt driveNum( |
|
263 SetSessionPrivatePathL( aFs, process.FileName() ) ); |
|
264 |
|
265 // Get the session path that was set above. |
|
266 IAUPDATE_TRACE_1("[IAUPDATE] Find file: %d", driveNum); |
|
267 TFileName sessionPath; |
|
268 User::LeaveIfError( aFs.SessionPath( sessionPath ) ); |
|
269 IAUPDATE_TRACE_1("[IAUPDATE] Session path: %S", &sessionPath); |
|
270 |
|
271 // Use the file finder to check if the file actually exists |
|
272 // in the given drive path. If it does not, the file finder |
|
273 // will automatically check from other drives. So, here we |
|
274 // should always find the file if any exists. |
|
275 TFindFile finder( aFs ); |
|
276 User::LeaveIfError( finder.FindByDir( aFileName, sessionPath ) ); |
|
277 |
|
278 // The drive may have changed if the file was not found from |
|
279 // the first suggested drive. So, be sure to have the correct |
|
280 // private path. |
|
281 driveNum = SetSessionPrivatePathL( aFs, finder.File() ); |
|
282 |
|
283 // Use the drive info to check if the drive is ROM drive. |
|
284 // We prefer non ROM drives. But, accept ROM if nothing else is |
|
285 // available. |
|
286 IAUPDATE_TRACE_1("[IAUPDATE] Check ROM info: %d", driveNum); |
|
287 TDriveInfo info; |
|
288 User::LeaveIfError( aFs.Drive( info, driveNum ) ); |
|
289 TBool isRomDrive( info.iDriveAtt & KDriveAttRom ); |
|
290 if ( !isRomDrive ) |
|
291 { |
|
292 // The current file is not in ROM drive so use that. |
|
293 IAUPDATE_TRACE("[IAUPDATE] First file search done. Non ROM found."); |
|
294 IAUPDATE_TRACE("[IAUPDATE] CIAUpdateXmlParser::SetPrivateDriveL() end"); |
|
295 return; |
|
296 } |
|
297 |
|
298 // Because previous finding was ROM file, try to find a non ROM file. |
|
299 IAUPDATE_TRACE("[IAUPDATE] Try to find non ROM file."); |
|
300 TInt findErrorCode( finder.Find() ); |
|
301 if ( findErrorCode == KErrNotFound ) |
|
302 { |
|
303 // Because no new file is found, use the current settings. |
|
304 IAUPDATE_TRACE("[IAUPDATE] Second search done. No file found."); |
|
305 IAUPDATE_TRACE("[IAUPDATE] CIAUpdateXmlParser::SetPrivateDriveL() end"); |
|
306 return; |
|
307 } |
|
308 User::LeaveIfError( findErrorCode ); |
|
309 |
|
310 IAUPDATE_TRACE("[IAUPDATE] New file found. Use that."); |
|
311 // Update the session path for the correct file. |
|
312 SetSessionPrivatePathL( aFs, finder.File() ); |
|
313 |
|
314 IAUPDATE_TRACE("[IAUPDATE] CIAUpdateXmlParser::SetPrivateDriveL() end"); |
|
315 } |
|
316 |
|
317 |
|
318 TInt CIAUpdateXmlParser::SetSessionPrivatePathL( |
|
319 RFs& aFs, |
|
320 const TDesC& aPath ) const |
|
321 { |
|
322 IAUPDATE_TRACE_1("[IAUPDATE] CIAUpdateXmlParser::SetSessionPrivateL() begin: %S", |
|
323 &aPath); |
|
324 |
|
325 // Use the parser to get the drive information from the path. |
|
326 TParsePtrC parser( aPath ); |
|
327 |
|
328 if ( !parser.DrivePresent() ) |
|
329 { |
|
330 IAUPDATE_TRACE("[IAUPDATE] ERROR: Missing drive info."); |
|
331 User::Leave( KErrArgument ); |
|
332 } |
|
333 |
|
334 // Drive check was passed above. |
|
335 // So, drive information is safe to use. |
|
336 const TDesC& drive( parser.Drive() ); |
|
337 const TChar driveChar( drive[ 0 ] ); |
|
338 TInt driveNum( EDriveA ); |
|
339 User::LeaveIfError( |
|
340 RFs::CharToDrive( driveChar, driveNum ) ); |
|
341 IAUPDATE_TRACE_2("[IAUPDATE] Drive: %S, %d", &drive, driveNum ); |
|
342 |
|
343 // Set the file drive to be file session private path drive. |
|
344 User::LeaveIfError( aFs.SetSessionToPrivate( driveNum ) ); |
|
345 |
|
346 IAUPDATE_TRACE_1("[IAUPDATE] CIAUpdateXmlParser::SetSessionPrivateL() end: %d", |
|
347 driveNum); |
|
348 |
|
349 return driveNum; |
|
350 } |
|
351 |