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_scriptparser.h" |
|
24 #include "creator_scriptelementfactory.h" |
|
25 #include "creator_traces.h" |
|
26 |
|
27 using namespace Xml; |
|
28 |
|
29 CCreatorScriptParser::CCreatorScriptParser() |
|
30 : iDefaultElement(TCreatorScriptElementInfo(TCreatorScriptElementInfo::EStatusUndefined, 0)) |
|
31 { |
|
32 } |
|
33 void CCreatorScriptParser::ConstructL(CCreatorEngine* aEngine) |
|
34 { |
|
35 LOGSTRING("Creator: CCreatorScriptParser::ConstructL"); |
|
36 iEngine = aEngine; |
|
37 } |
|
38 |
|
39 CCreatorScriptParser* CCreatorScriptParser::NewL(CCreatorEngine* aEngine) |
|
40 { |
|
41 LOGSTRING("Creator: CCreatorScriptParser::NewL"); |
|
42 CCreatorScriptParser* self = CCreatorScriptParser::NewLC(aEngine); |
|
43 CleanupStack::Pop(); |
|
44 return self; |
|
45 } |
|
46 CCreatorScriptParser* CCreatorScriptParser::NewLC(CCreatorEngine* aEngine) |
|
47 { |
|
48 LOGSTRING("Creator: CCreatorScriptParser::NewLC"); |
|
49 CCreatorScriptParser* self = new (ELeave) CCreatorScriptParser; |
|
50 CleanupStack::PushL(self); |
|
51 self->ConstructL(aEngine); |
|
52 return self; |
|
53 } |
|
54 |
|
55 CCreatorScriptParser::~CCreatorScriptParser() |
|
56 { |
|
57 delete iParser; |
|
58 if( iElementStack.Count() > 0 ) |
|
59 { |
|
60 delete iElementStack[0].iElement; |
|
61 } |
|
62 iElementStack.Reset(); |
|
63 iElementStack.Close(); |
|
64 } |
|
65 |
|
66 void CCreatorScriptParser::GetTextFileMode(RFile& aFile, TInt& aFileSize) |
|
67 { |
|
68 LOGSTRING("Creator: CCreatorScriptParser::GetTextFileMode"); |
|
69 iScriptTextFormat = EFormatANSIASCII; |
|
70 |
|
71 // if we are working with text files, check the type first |
|
72 TBuf8<4> BOM; |
|
73 |
|
74 // first check for UTF-16 |
|
75 if (aFileSize >= 2 && aFile.Read(0, BOM, 2) == KErrNone) |
|
76 { |
|
77 if (BOM.Length()==2 && BOM[0]==0xFF && BOM[1]==0xFE) |
|
78 { |
|
79 iScriptTextFormat = EFormatUTF16LE; |
|
80 aFileSize -= 2; |
|
81 return; |
|
82 } |
|
83 else if (BOM.Length()==2 && BOM[0]==0xFE && BOM[1]==0xFF) |
|
84 { |
|
85 iScriptTextFormat = EFormatUTF16BE; |
|
86 aFileSize -= 2; |
|
87 return; |
|
88 } |
|
89 } |
|
90 // then check for UTF-8 |
|
91 if (aFileSize >= 3 && aFile.Read(0, BOM, 3) == KErrNone) |
|
92 { |
|
93 if (BOM.Length()==3 && BOM[0]==0xEF && BOM[1]==0xBB && BOM[2]==0xBF) |
|
94 { |
|
95 iScriptTextFormat = EFormatUTF8; |
|
96 aFileSize -= 3; |
|
97 return; |
|
98 } |
|
99 } |
|
100 |
|
101 // none of those, seek back to beginning |
|
102 TInt pos(0); |
|
103 aFile.Seek(ESeekStart, pos); |
|
104 } |
|
105 |
|
106 |
|
107 |
|
108 void CCreatorScriptParser::ParseL(const TDesC& aFileName) |
|
109 { |
|
110 LOGSTRING2("Creator: CCreatorScriptParser::ParseL: %S", &aFileName); |
|
111 // Create and start XML-parser |
|
112 if( iParser == 0) |
|
113 { |
|
114 _LIT8(KXmlMimeType, "text/xml"); |
|
115 iParser = CParser::NewL(KXmlMimeType, *this); |
|
116 } |
|
117 |
|
118 RFs filesession; |
|
119 CleanupClosePushL(filesession); |
|
120 User::LeaveIfError( filesession.Connect() ); |
|
121 Xml::ParseL(*iParser, filesession, aFileName); |
|
122 |
|
123 CleanupStack::PopAndDestroy(&filesession); // filesession |
|
124 } |
|
125 |
|
126 void CCreatorScriptParser::ParseL(RFile& aFile) |
|
127 { |
|
128 LOGSTRING("Creator: CCreatorScriptParser::ParseL"); |
|
129 TInt filesize = 0; |
|
130 aFile.Size(filesize); |
|
131 GetTextFileMode(aFile, filesize); |
|
132 // Create and start XML-parser |
|
133 if( iParser == 0) |
|
134 { |
|
135 _LIT8(KXmlMimeType, "text/xml"); |
|
136 iParser = CParser::NewL(KXmlMimeType, *this); |
|
137 } |
|
138 Xml::ParseL(*iParser, aFile); |
|
139 } |
|
140 |
|
141 void CCreatorScriptParser::OnStartDocumentL(const Xml::RDocumentParameters& aDocParam, TInt aErrorCode) |
|
142 { |
|
143 LOGSTRING2("Creator: CCreatorScriptParser::OnStartDocumentL (Error code: %d)", aErrorCode); |
|
144 User::LeaveIfError(aErrorCode); |
|
145 } |
|
146 |
|
147 void CCreatorScriptParser::OnEndDocumentL(TInt aErrorCode) |
|
148 { |
|
149 LOGSTRING2("Creator: CCreatorScriptParser::OnEndDocumentL (Error code: %d)", aErrorCode); |
|
150 User::LeaveIfError(aErrorCode); |
|
151 iEngine->SortCommands(); |
|
152 } |
|
153 |
|
154 void CCreatorScriptParser::OnStartPrefixMappingL( const RString& /*aPrefix*/, |
|
155 const RString& /*aUri*/, |
|
156 TInt aErrorCode) |
|
157 { |
|
158 LOGSTRING2("Creator: CCreatorScriptParser::OnStartPrefixMappingL (Error code: %d)", aErrorCode); |
|
159 User::LeaveIfError(aErrorCode); |
|
160 } |
|
161 |
|
162 void CCreatorScriptParser::OnEndPrefixMappingL(const RString& /*aPrefix*/, TInt aErrorCode) |
|
163 { |
|
164 LOGSTRING2("Creator: CCreatorScriptParser::OnEndPrefixMappingL (Error code: %d)", aErrorCode); |
|
165 User::LeaveIfError(aErrorCode); |
|
166 } |
|
167 void CCreatorScriptParser::OnIgnorableWhiteSpaceL(const TDesC8& /*aBytes*/, TInt aErrorCode) |
|
168 { |
|
169 LOGSTRING2("Creator: CCreatorScriptParser::OnIgnorableWhiteSpaceL (Error code: %d)", aErrorCode); |
|
170 User::LeaveIfError(aErrorCode); |
|
171 } |
|
172 void CCreatorScriptParser::OnSkippedEntityL(const RString& /*aName*/, TInt aErrorCode) |
|
173 { |
|
174 LOGSTRING2("Creator: CCreatorScriptParser::OnSkippedEntityL (Error code: %d)", aErrorCode); |
|
175 User::LeaveIfError(aErrorCode); |
|
176 } |
|
177 void CCreatorScriptParser::OnProcessingInstructionL( const TDesC8& /*aTarget*/, |
|
178 const TDesC8& /*aData*/, |
|
179 TInt aErrorCode) |
|
180 { |
|
181 LOGSTRING2("Creator: CCreatorScriptParser::OnProcessingInstructionL (Error code: %d)", aErrorCode); |
|
182 User::LeaveIfError(aErrorCode); |
|
183 } |
|
184 void CCreatorScriptParser::OnError(TInt aErrorCode) |
|
185 { |
|
186 LOGSTRING2("Creator: CCreatorScriptParser::OnError: %d", aErrorCode); |
|
187 |
|
188 iLastError = aErrorCode; |
|
189 |
|
190 // Delete all elements from the stack: |
|
191 while(iElementStack.Count() > 0 ) |
|
192 { |
|
193 TCreatorScriptElementInfo previousElem = LastElementInfo(); |
|
194 delete previousElem.iElement; |
|
195 previousElem.iElement = 0; |
|
196 iElementStack.Remove(iElementStack.Count()-1); |
|
197 } |
|
198 } |
|
199 TAny* CCreatorScriptParser::GetExtendedInterface(const TInt32 aUid) |
|
200 { |
|
201 LOGSTRING2("Creator: CCreatorScriptParser::GetExtendedInterface (UID: %d)", aUid); |
|
202 return this; |
|
203 } |
|
204 |
|
205 const TCreatorScriptElementInfo& CCreatorScriptParser::LastElementInfo() const |
|
206 { |
|
207 LOGSTRING("Creator: CCreatorScriptParser::LastElementInfo"); |
|
208 if(iElementStack.Count() > 0) |
|
209 { |
|
210 return iElementStack[iElementStack.Count()-1]; |
|
211 } |
|
212 return iDefaultElement; |
|
213 } |
|
214 |
|
215 /** |
|
216 * Create an element and add attributes to it. |
|
217 */ |
|
218 void CCreatorScriptParser::OnStartElementL( const RTagInfo& aElement, |
|
219 const RAttributeArray& aAttributes, |
|
220 TInt aErrorCode) |
|
221 { |
|
222 LOGSTRING2("Creator: CCreatorScriptParser::OnStartElementL (Error code: %d)", aErrorCode); |
|
223 User::LeaveIfError(aErrorCode); |
|
224 const TDesC8& elementName8 = aElement.LocalName().DesC(); |
|
225 HBufC* elementName16 = Convert8BitTo16BitLC(elementName8); |
|
226 |
|
227 const TCreatorScriptElementInfo& context = LastElementInfo(); |
|
228 |
|
229 CCreatorScriptElement* element = 0; |
|
230 if( context.iElement ) |
|
231 { |
|
232 HBufC* fullContext = HBufC::NewL( |
|
233 context.iElement->Context().Length() + |
|
234 context.iElement->Name().Length() + |
|
235 2); |
|
236 CleanupStack::PushL(fullContext); |
|
237 if(context.iElement->Context() != KNullDesC && |
|
238 context.iElement->Context().Length() > 0 ) |
|
239 { |
|
240 fullContext->Des().Copy(context.iElement->Context()); |
|
241 _LIT(KContextSeparator, "::"); |
|
242 fullContext->Des().Append(TPtrC(KContextSeparator)); |
|
243 } |
|
244 const TDesC& parentName = context.iElement->Name(); |
|
245 if(parentName.Length() > 0 ) |
|
246 { |
|
247 fullContext->Des().Append(parentName); |
|
248 } |
|
249 element = TCreatorScriptElementFactory::CreateElementL( iEngine, |
|
250 elementName16->Des(), |
|
251 fullContext->Des()); |
|
252 CleanupStack::PopAndDestroy(fullContext); // fullContext |
|
253 } |
|
254 else |
|
255 { |
|
256 element = TCreatorScriptElementFactory::CreateElementL(iEngine, elementName16->Des()); |
|
257 } |
|
258 |
|
259 CleanupStack::PopAndDestroy(elementName16); |
|
260 CleanupStack::PushL(element); |
|
261 |
|
262 if( element == 0 ) |
|
263 { |
|
264 // Add NULL pointer to stack. Otherwise the stack would go out of sync. in ::OnEndElement(). |
|
265 TCreatorScriptElementInfo info(TCreatorScriptElementInfo::EStatusParsing, element); |
|
266 iElementStack.AppendL(info); |
|
267 CleanupStack::Pop(element); |
|
268 return; |
|
269 } |
|
270 |
|
271 // Add attributes to the element: |
|
272 TInt attrCount = aAttributes.Count(); |
|
273 for(TInt i = 0; i < attrCount; ++i) |
|
274 { |
|
275 const RAttribute& attribute = aAttributes[i]; |
|
276 const RTagInfo& nameInfo = attribute.Attribute(); |
|
277 const TDesC8& attrName8 = nameInfo.LocalName().DesC(); |
|
278 const TDesC8& value8 = attribute.Value().DesC(); |
|
279 // Convert data to 16 bit: |
|
280 HBufC* attrName16 = Convert8BitTo16BitLC(attrName8); |
|
281 HBufC* value16 = Convert8BitTo16BitLC(value8); |
|
282 CCreatorScriptAttribute* scriptAttr = CCreatorScriptAttribute::NewLC(attrName16->Des(), value16->Des()); |
|
283 element->AddAttributeL(scriptAttr); |
|
284 CleanupStack::Pop(scriptAttr); |
|
285 CleanupStack::PopAndDestroy(value16); |
|
286 CleanupStack::PopAndDestroy(attrName16); |
|
287 } |
|
288 |
|
289 // Save element to the stack: |
|
290 TCreatorScriptElementInfo info(TCreatorScriptElementInfo::EStatusParsing, element); |
|
291 iElementStack.AppendL(info); |
|
292 CleanupStack::Pop(element); |
|
293 } |
|
294 |
|
295 /** |
|
296 * Executes the command if needed. Also caches the element for future use, if needed. |
|
297 */ |
|
298 void CCreatorScriptParser::OnEndElementL(const RTagInfo& /*aElement*/, TInt aErrorCode) |
|
299 { |
|
300 LOGSTRING2("Creator: CCreatorScriptParser::OnEndElementL (Error code: %d)", aErrorCode); |
|
301 User::LeaveIfError(aErrorCode); |
|
302 |
|
303 if( iElementStack.Count() == 0 ) |
|
304 { |
|
305 return; |
|
306 } |
|
307 // Get element from the stack: |
|
308 TCreatorScriptElementInfo elementInfo = LastElementInfo(); |
|
309 |
|
310 if( elementInfo.iElement == 0 ) |
|
311 { |
|
312 // Remove element from the stack: |
|
313 iElementStack.Remove(iElementStack.Count()-1); |
|
314 return; |
|
315 } |
|
316 |
|
317 // Execute the command if this is a command element: |
|
318 if( elementInfo.iElement->IsCommandElement() ) |
|
319 { |
|
320 elementInfo.iElement->ExecuteCommandL(); |
|
321 } |
|
322 |
|
323 // Remove element from the stack: |
|
324 iElementStack.Remove(iElementStack.Count()-1); |
|
325 |
|
326 // Get the previous element from the stack: |
|
327 if( iElementStack.Count() > 0 ) |
|
328 { |
|
329 TCreatorScriptElementInfo previousElem = LastElementInfo(); |
|
330 |
|
331 if( previousElem.iElement->IsRoot() ) |
|
332 { |
|
333 // This element is not a sub-element (except of the root element, of course). |
|
334 if( elementInfo.iElement->IsCacheNeeded() ) |
|
335 { |
|
336 // Add element to the cache, since it will be needed in future. |
|
337 elementInfo.iElement->AddToCacheL(); |
|
338 } |
|
339 //else |
|
340 // { |
|
341 // Not needed anymore, so element can be deleted: |
|
342 delete elementInfo.iElement; |
|
343 elementInfo.iElement = 0; |
|
344 // } |
|
345 } |
|
346 else |
|
347 { |
|
348 // There is a parent element (other than root), so we add this element |
|
349 // as a sub-element fot it. |
|
350 previousElem.iElement->AddSubElementL(elementInfo.iElement); |
|
351 } |
|
352 } |
|
353 else |
|
354 { |
|
355 // Delete root element, which will delete recursively all sub-elements: |
|
356 delete elementInfo.iElement; |
|
357 elementInfo.iElement = 0; |
|
358 } |
|
359 } |
|
360 |
|
361 /** |
|
362 * Add content to element. |
|
363 */ |
|
364 void CCreatorScriptParser::OnContentL(const TDesC8& aBytes, TInt aErrorCode) |
|
365 { |
|
366 LOGSTRING2("Creator: CCreatorScriptParser::OnContentL (Error code: %d)", aErrorCode); |
|
367 User::LeaveIfError(aErrorCode); |
|
368 if( iElementStack.Count() > 0 ) |
|
369 { |
|
370 HBufC* contentData = Convert8BitTo16BitLC(aBytes); |
|
371 TCreatorScriptElementInfo elementInfo = iElementStack[iElementStack.Count()-1]; |
|
372 if( elementInfo.iElement ) |
|
373 { |
|
374 elementInfo.iElement->AppendContentL(*contentData); |
|
375 } |
|
376 CleanupStack::PopAndDestroy(contentData); |
|
377 } |
|
378 } |
|
379 |
|
380 HBufC16* CCreatorScriptParser::Convert8BitTo16BitLC(const TDesC8& aInput) |
|
381 { |
|
382 LOGSTRING("Creator: CCreatorScriptParser::Convert8BitTo16BitLC"); |
|
383 HBufC16* output = NULL; |
|
384 |
|
385 output = CnvUtfConverter::ConvertToUnicodeFromUtf8L(aInput); |
|
386 |
|
387 CleanupStack::PushL(output); |
|
388 return output; |
|
389 } |
|
390 |
|
391 TInt CCreatorScriptParser::GetError() const |
|
392 { |
|
393 return iLastError; |
|
394 } |
|