1 /* |
|
2 * Copyright (c) 2010 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 |
|
18 |
|
19 |
|
20 #include <f32file.h> |
|
21 #include <utf.h> |
|
22 #include <xml/documentparameters.h> |
|
23 #include "creator_randomdataparser.h" |
|
24 #include "creator_traces.h" |
|
25 |
|
26 // XML element names |
|
27 _LIT(KRootElement, "creatordata"); |
|
28 _LIT(KFirstName, "firstname"); |
|
29 _LIT(KSurname, "surname"); |
|
30 _LIT(KCompany, "company"); |
|
31 _LIT(KAddress, "address"); |
|
32 _LIT(KJobTitle, "jobtitle"); |
|
33 _LIT(KPhoneNumber, "phonenumber"); |
|
34 _LIT(KGroupName, "groupname"); |
|
35 _LIT(KMeetingReason, "meetingreason"); |
|
36 _LIT(KMeetingPlace, "meetingplace"); |
|
37 _LIT(KMemoText, "memo"); |
|
38 _LIT(KAnniversaryReason, "anniversary"); |
|
39 _LIT(KToDoText, "todo"); |
|
40 _LIT(KReminderText, "reminder"); |
|
41 _LIT(KMessageSubject, "messagesubject"); |
|
42 _LIT(KMessageText, "messagetext"); |
|
43 _LIT(KCity, "city"); |
|
44 _LIT(KCountry, "country"); |
|
45 _LIT(KPostcode, "postcode"); |
|
46 _LIT(KState, "state"); |
|
47 _LIT(KPobox, "pobox"); |
|
48 _LIT(KPrefix, "prefix"); |
|
49 _LIT(KSuffix, "suffix"); |
|
50 _LIT(KLandmarkName, "landmarkname"); |
|
51 _LIT(KLandmarkDescription, "landmarkdescription"); |
|
52 |
|
53 using namespace Xml; |
|
54 |
|
55 CCreatorRandomDataParser::CCreatorRandomDataParser() |
|
56 { |
|
57 } |
|
58 |
|
59 void CCreatorRandomDataParser::ConstructL() |
|
60 { |
|
61 LOGSTRING("Creator: CCreatorRandomDataParser::ConstructL"); |
|
62 iElementNameArray = new (ELeave) CDesCArrayFlat(32); |
|
63 iElementNameArray->InsertL(EFirstName, KFirstName); |
|
64 iElementNameArray->InsertL(ESurname, KSurname); |
|
65 iElementNameArray->InsertL(ECompany, KCompany); |
|
66 iElementNameArray->InsertL(EAddress, KAddress); |
|
67 iElementNameArray->InsertL(EJobTitle, KJobTitle); |
|
68 iElementNameArray->InsertL(EPhoneNumber, KPhoneNumber); |
|
69 iElementNameArray->InsertL(EGroupName, KGroupName); |
|
70 iElementNameArray->InsertL(EMeetingReason, KMeetingReason); |
|
71 iElementNameArray->InsertL(EMeetingPlace, KMeetingPlace); |
|
72 iElementNameArray->InsertL(EMemoText, KMemoText); |
|
73 iElementNameArray->InsertL(EAnniversaryReason, KAnniversaryReason); |
|
74 iElementNameArray->InsertL(EToDoText, KToDoText); |
|
75 iElementNameArray->InsertL(EReminderText, KReminderText); |
|
76 iElementNameArray->InsertL(EMessageSubject, KMessageSubject); |
|
77 iElementNameArray->InsertL(EMessageText, KMessageText); |
|
78 iElementNameArray->InsertL(ECity, KCity); |
|
79 iElementNameArray->InsertL(ECountry, KCountry); |
|
80 iElementNameArray->InsertL(EPostcode, KPostcode); |
|
81 iElementNameArray->InsertL(EState, KState); |
|
82 iElementNameArray->InsertL(EPobox, KPobox); |
|
83 iElementNameArray->InsertL(EPrefix, KPrefix); |
|
84 iElementNameArray->InsertL(ESuffix, KSuffix); |
|
85 iElementNameArray->InsertL(ELandmarkName, KLandmarkName); |
|
86 iElementNameArray->InsertL(ELandmarkDescription, KLandmarkDescription); |
|
87 iContentData = HBufC::NewL(16); |
|
88 iResultArray = new (ELeave) CDesCArrayFlat(16); |
|
89 } |
|
90 |
|
91 CCreatorRandomDataParser* CCreatorRandomDataParser::NewL() |
|
92 { |
|
93 LOGSTRING("Creator: CCreatorRandomDataParser::NewL"); |
|
94 CCreatorRandomDataParser* self = CCreatorRandomDataParser::NewLC(); |
|
95 CleanupStack::Pop(); |
|
96 return self; |
|
97 } |
|
98 |
|
99 CCreatorRandomDataParser* CCreatorRandomDataParser::NewLC() |
|
100 { |
|
101 LOGSTRING("Creator: CCreatorRandomDataParser::NewLC"); |
|
102 CCreatorRandomDataParser* self = new (ELeave) CCreatorRandomDataParser; |
|
103 CleanupStack::PushL(self); |
|
104 self->ConstructL(); |
|
105 return self; |
|
106 } |
|
107 |
|
108 CCreatorRandomDataParser::~CCreatorRandomDataParser() |
|
109 { |
|
110 delete iParser; |
|
111 delete iResultArray; |
|
112 delete iContentData; |
|
113 delete iElementNameArray; |
|
114 } |
|
115 |
|
116 void CCreatorRandomDataParser::GetTextFileMode(RFile& aFile, TInt& aFileSize) |
|
117 { |
|
118 LOGSTRING("Creator: CCreatorRandomDataParser::GetTextFileMode"); |
|
119 iScriptTextFormat = EFormatANSIASCII; |
|
120 |
|
121 // if we are working with text files, check the type first |
|
122 TBuf8<4> BOM; |
|
123 |
|
124 // first check for UTF-16 |
|
125 if (aFileSize >= 2 && aFile.Read(0, BOM, 2) == KErrNone) |
|
126 { |
|
127 if (BOM.Length()==2 && BOM[0]==0xFF && BOM[1]==0xFE) |
|
128 { |
|
129 iScriptTextFormat = EFormatUTF16LE; |
|
130 aFileSize -= 2; |
|
131 return; |
|
132 } |
|
133 else if (BOM.Length()==2 && BOM[0]==0xFE && BOM[1]==0xFF) |
|
134 { |
|
135 iScriptTextFormat = EFormatUTF16BE; |
|
136 aFileSize -= 2; |
|
137 return; |
|
138 } |
|
139 } |
|
140 // then check for UTF-8 |
|
141 if (aFileSize >= 3 && aFile.Read(0, BOM, 3) == KErrNone) |
|
142 { |
|
143 if (BOM.Length()==3 && BOM[0]==0xEF && BOM[1]==0xBB && BOM[2]==0xBF) |
|
144 { |
|
145 iScriptTextFormat = EFormatUTF8; |
|
146 aFileSize -= 3; |
|
147 return; |
|
148 } |
|
149 } |
|
150 |
|
151 // none of those, seek back to beginning |
|
152 TInt pos(0); |
|
153 aFile.Seek(ESeekStart, pos); |
|
154 } |
|
155 |
|
156 |
|
157 |
|
158 CDesCArrayFlat* CCreatorRandomDataParser::ParseL(const TDesC& aFileName, const TRandomDataType aDataType) |
|
159 { |
|
160 LOGSTRING2("Creator: CCreatorRandomDataParser::ParseL: %S", &aFileName); |
|
161 |
|
162 iDataType = aDataType; |
|
163 |
|
164 // Create and start XML-parser |
|
165 if( iParser == 0) |
|
166 { |
|
167 _LIT8(KXmlMimeType, "text/xml"); |
|
168 iParser = CParser::NewL(KXmlMimeType, *this); |
|
169 } |
|
170 |
|
171 RFs filesession; |
|
172 CleanupClosePushL(filesession); |
|
173 User::LeaveIfError( filesession.Connect() ); |
|
174 |
|
175 iResultArray->Reset(); |
|
176 |
|
177 Xml::ParseL(*iParser, filesession, aFileName); |
|
178 |
|
179 CleanupStack::PopAndDestroy(&filesession); // filesession |
|
180 |
|
181 CDesCArrayFlat* tempArray = new (ELeave) CDesCArrayFlat(8); |
|
182 CleanupStack::PushL(tempArray); |
|
183 for (TInt i = 0; i < iResultArray->MdcaCount(); i++) |
|
184 { |
|
185 tempArray->AppendL(iResultArray->MdcaPoint(i)); |
|
186 } |
|
187 CleanupStack::Pop(tempArray); |
|
188 return tempArray; |
|
189 } |
|
190 |
|
191 void CCreatorRandomDataParser::OnStartDocumentL(const Xml::RDocumentParameters& /*aDocParam*/, TInt aErrorCode) |
|
192 { |
|
193 LOGSTRING2("Creator: CCreatorRandomDataParser::OnStartDocumentL (Error code: %d)", aErrorCode); |
|
194 User::LeaveIfError(aErrorCode); |
|
195 iInsideRootElement = EFalse; |
|
196 iContentParsingActive = EFalse; |
|
197 } |
|
198 |
|
199 void CCreatorRandomDataParser::OnEndDocumentL(TInt aErrorCode) |
|
200 { |
|
201 LOGSTRING2("Creator: CCreatorRandomDataParser::OnEndDocumentL (Error code: %d)", aErrorCode); |
|
202 User::LeaveIfError(aErrorCode); |
|
203 } |
|
204 |
|
205 void CCreatorRandomDataParser::OnStartPrefixMappingL(const RString& /*aPrefix*/, |
|
206 const RString& /*aUri*/, |
|
207 TInt aErrorCode) |
|
208 { |
|
209 LOGSTRING2("Creator: CCreatorRandomDataParser::OnStartPrefixMappingL (Error code: %d)", aErrorCode); |
|
210 User::LeaveIfError(aErrorCode); |
|
211 } |
|
212 |
|
213 void CCreatorRandomDataParser::OnEndPrefixMappingL(const RString& /*aPrefix*/, TInt aErrorCode) |
|
214 { |
|
215 LOGSTRING2("Creator: CCreatorRandomDataParser::OnEndPrefixMappingL (Error code: %d)", aErrorCode); |
|
216 User::LeaveIfError(aErrorCode); |
|
217 } |
|
218 |
|
219 void CCreatorRandomDataParser::OnIgnorableWhiteSpaceL(const TDesC8& /*aBytes*/, TInt aErrorCode) |
|
220 { |
|
221 LOGSTRING2("Creator: CCreatorRandomDataParser::OnIgnorableWhiteSpaceL (Error code: %d)", aErrorCode); |
|
222 User::LeaveIfError(aErrorCode); |
|
223 } |
|
224 |
|
225 void CCreatorRandomDataParser::OnSkippedEntityL(const RString& /*aName*/, TInt aErrorCode) |
|
226 { |
|
227 LOGSTRING2("Creator: CCreatorRandomDataParser::OnSkippedEntityL (Error code: %d)", aErrorCode); |
|
228 User::LeaveIfError(aErrorCode); |
|
229 } |
|
230 |
|
231 void CCreatorRandomDataParser::OnProcessingInstructionL(const TDesC8& /*aTarget*/, |
|
232 const TDesC8& /*aData*/, |
|
233 TInt aErrorCode) |
|
234 { |
|
235 LOGSTRING2("Creator: CCreatorRandomDataParser::OnProcessingInstructionL (Error code: %d)", aErrorCode); |
|
236 User::LeaveIfError(aErrorCode); |
|
237 } |
|
238 |
|
239 void CCreatorRandomDataParser::OnError(TInt aErrorCode) |
|
240 { |
|
241 LOGSTRING2("Creator: CCreatorRandomDataParser::OnError: %d", aErrorCode); |
|
242 |
|
243 iLastError = aErrorCode; |
|
244 } |
|
245 |
|
246 TAny* CCreatorRandomDataParser::GetExtendedInterface(const TInt32 aUid) |
|
247 { |
|
248 LOGSTRING2("Creator: CCreatorRandomDataParser::GetExtendedInterface (UID: %d)", aUid); |
|
249 return this; |
|
250 } |
|
251 |
|
252 void CCreatorRandomDataParser::OnStartElementL(const Xml::RTagInfo& aElement, |
|
253 const Xml::RAttributeArray& /*aAttributes*/, |
|
254 TInt aErrorCode) |
|
255 { |
|
256 LOGSTRING2("Creator: CCreatorRandomDataParser::OnStartElementL (Error code: %d)", aErrorCode); |
|
257 User::LeaveIfError(aErrorCode); |
|
258 |
|
259 const TDesC8& elementName8 = aElement.LocalName().DesC(); |
|
260 HBufC* elementName16 = Convert8BitTo16BitLC(elementName8); |
|
261 if (iInsideRootElement || (elementName16->Des() == KRootElement)) |
|
262 { |
|
263 iInsideRootElement = ETrue; |
|
264 } |
|
265 if (iInsideRootElement && (elementName16->Des() == iElementNameArray->MdcaPoint(iDataType))) |
|
266 { |
|
267 iContentParsingActive = ETrue; |
|
268 iContentData->Des().Zero(); |
|
269 } |
|
270 CleanupStack::PopAndDestroy(elementName16); |
|
271 } |
|
272 |
|
273 void CCreatorRandomDataParser::OnEndElementL(const Xml::RTagInfo& aElement, TInt aErrorCode) |
|
274 { |
|
275 LOGSTRING2("Creator: CCreatorRandomDataParser::OnEndElementL (Error code: %d)", aErrorCode); |
|
276 User::LeaveIfError(aErrorCode); |
|
277 |
|
278 const TDesC8& elementName8 = aElement.LocalName().DesC(); |
|
279 HBufC* elementName16 = Convert8BitTo16BitLC(elementName8); |
|
280 if (!iInsideRootElement || (elementName16->Des() == KRootElement)) |
|
281 { |
|
282 iInsideRootElement = EFalse; |
|
283 } |
|
284 CleanupStack::PopAndDestroy(elementName16); |
|
285 |
|
286 if (iContentParsingActive) |
|
287 { |
|
288 if (iContentData->Des().Length() > 0) |
|
289 { |
|
290 iResultArray->AppendL(*iContentData); |
|
291 } |
|
292 iContentParsingActive = EFalse; |
|
293 } |
|
294 } |
|
295 |
|
296 void CCreatorRandomDataParser::OnContentL(const TDesC8& aBytes, TInt aErrorCode) |
|
297 { |
|
298 LOGSTRING2("Creator: CCreatorRandomDataParser::OnContentL (Error code: %d)", aErrorCode); |
|
299 User::LeaveIfError(aErrorCode); |
|
300 |
|
301 if (iContentParsingActive) |
|
302 { |
|
303 HBufC* data16 = Convert8BitTo16BitLC(aBytes); |
|
304 TInt newLen = iContentData->Des().Length() + data16->Des().Length(); |
|
305 iContentData = iContentData->ReAllocL(newLen); |
|
306 iContentData->Des().Append(*data16); |
|
307 CleanupStack::PopAndDestroy(data16); |
|
308 } |
|
309 } |
|
310 |
|
311 HBufC16* CCreatorRandomDataParser::Convert8BitTo16BitLC(const TDesC8& aInput) |
|
312 { |
|
313 LOGSTRING("Creator: CCreatorRandomDataParser::Convert8BitTo16BitLC"); |
|
314 HBufC16* output = NULL; |
|
315 |
|
316 output = CnvUtfConverter::ConvertToUnicodeFromUtf8L(aInput); |
|
317 |
|
318 CleanupStack::PushL(output); |
|
319 return output; |
|
320 } |
|
321 |
|
322 TInt CCreatorRandomDataParser::GetError() const |
|
323 { |
|
324 return iLastError; |
|
325 } |
|