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