1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // CSMILDTD.cpp |
|
15 // @file |
|
16 // This file contains the definition of the SMILDTD class |
|
17 // which describes the SMIL DTD and is responsible for validation |
|
18 // of SMIL documents |
|
19 // |
|
20 // |
|
21 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS |
|
22 #include "t_smildtdenum.h" |
|
23 #endif |
|
24 #include "t_SmilDtd.h" |
|
25 #include "t_SmilData.h" |
|
26 #include <stringpool.h> |
|
27 #include "smilelements.h" |
|
28 #include "smilattributes.h" |
|
29 #include "smilgenericelements.h" |
|
30 #include <gmxmlconstants.h> |
|
31 |
|
32 // |
|
33 // Global functions // |
|
34 // |
|
35 |
|
36 |
|
37 EXPORT_C CSMILDtd* CSMILDtd::NewL() |
|
38 // |
|
39 // Two phase static factory function constructor |
|
40 // @return Created CSMILDtd |
|
41 // @leave can Leave due to OOM |
|
42 // |
|
43 { |
|
44 CSMILDtd* self = NewLC(); |
|
45 CleanupStack::Pop(); |
|
46 return self; |
|
47 } |
|
48 |
|
49 EXPORT_C CSMILDtd* CSMILDtd::NewLC() |
|
50 // |
|
51 // Two phase static factory function constructor |
|
52 // @return Created CSMILDtd |
|
53 // @leave can Leave due to OOM |
|
54 // |
|
55 { |
|
56 CSMILDtd* self = new (ELeave) CSMILDtd(); |
|
57 CleanupStack::PushL(self); |
|
58 self->ConstructL(); |
|
59 return self; |
|
60 } |
|
61 |
|
62 void CSMILDtd::ConstructL() |
|
63 // |
|
64 // Second stage constructor |
|
65 // @leave can Leave due to OOM |
|
66 // |
|
67 { |
|
68 // nothing to do |
|
69 } |
|
70 |
|
71 |
|
72 CSMILDtd::CSMILDtd() |
|
73 { |
|
74 } |
|
75 |
|
76 |
|
77 EXPORT_C CSMILDtd::~CSMILDtd() |
|
78 { |
|
79 } |
|
80 |
|
81 TBool CSMILDtd::IsValidElementL(const TDesC& aElement) const |
|
82 { |
|
83 // |
|
84 // Checks to see if the element name passed in has been specified in the string table |
|
85 // @param aElementName the element name to be checked |
|
86 // @return ETrue if defined in the string table else EFalse |
|
87 // @leave Leave due to OOM |
|
88 // |
|
89 |
|
90 // Use the string table SMILElements to validate the element name |
|
91 TBool validElement = EFalse; |
|
92 |
|
93 RStringPool pool; |
|
94 CleanupClosePushL(pool); |
|
95 pool.OpenL(SMILElements::Table); |
|
96 |
|
97 int numTableEntries = SMILElements::Table.iCount; |
|
98 if(numTableEntries != 0) |
|
99 { |
|
100 // Using a binary search since the table is always sorted alphabetically by element. |
|
101 |
|
102 // Set us search indices to outer bounds of array |
|
103 TInt left = 0; |
|
104 TInt right = numTableEntries - 1; |
|
105 TInt compareResult = 0; |
|
106 TInt pos; |
|
107 while (right >= left) |
|
108 { |
|
109 pos = (left + right) / 2; |
|
110 |
|
111 RStringF tableEntry = pool.StringF(pos, SMILElements::Table); |
|
112 HBufC* buf = HBufC::NewLC(tableEntry.DesC().Length()); |
|
113 buf->Des().Copy(tableEntry.DesC()); |
|
114 |
|
115 TLex string(*buf); |
|
116 |
|
117 TPtrC token = string.NextToken(); |
|
118 compareResult = aElement.Compare(token); |
|
119 if(compareResult == 0) |
|
120 validElement = ETrue; |
|
121 |
|
122 CleanupStack::PopAndDestroy(buf); |
|
123 |
|
124 |
|
125 if(compareResult == 0) |
|
126 break; |
|
127 else if (compareResult > 0) |
|
128 left = pos + 1; |
|
129 else |
|
130 right = pos - 1; |
|
131 } |
|
132 |
|
133 } |
|
134 CleanupStack::PopAndDestroy(); // close pool |
|
135 return validElement; |
|
136 |
|
137 } |
|
138 |
|
139 TInt CSMILDtd::IsValidAttributeForElementL(const TDesC& aElement, const TDesC& aAttribute, const TDesC& aAttributeValue) const |
|
140 { |
|
141 // |
|
142 // Checks that both the Attribute Name & Attribute Value are valid using string tables stored in the document |
|
143 // @param aAttributeName - name of attribute to be checked |
|
144 // @param aAttributeValue - value of attribute to be checked |
|
145 // @return KErrNone if attribute & value are valid, otherwise KErrXMLBadAttributeName or KErrXMLBadAttributeValue |
|
146 // @leave Leave due to OOM |
|
147 // |
|
148 |
|
149 |
|
150 // To reduce the size of the element/attribute/value generic element names are used |
|
151 // So for example 'img' and 'video' are both represented by the genericelement 'media' |
|
152 // A table exists (SMILGenericElements) detailing these relationships |
|
153 |
|
154 // So first we see if there is a generic element name for this element |
|
155 // If not then genericElementName will just be the element name passed in |
|
156 HBufC* genericElementName = NULL; |
|
157 |
|
158 |
|
159 RStringPool pool; |
|
160 CleanupClosePushL(pool); |
|
161 pool.OpenL(SMILGenericElements::Table); |
|
162 |
|
163 TInt numTableEntries = SMILGenericElements::Table.iCount; |
|
164 if(numTableEntries != 0) |
|
165 { |
|
166 // Using a binary search since the table is always sorted alphabetically by element. |
|
167 |
|
168 // Set us search indices to outer bounds of array |
|
169 TInt left = 0; |
|
170 TInt right = numTableEntries - 1; |
|
171 TInt pos; |
|
172 while (right >= left) |
|
173 { |
|
174 pos = (left + right) / 2; |
|
175 |
|
176 RStringF elementAndGeneric = pool.StringF(pos, SMILGenericElements::Table); |
|
177 |
|
178 HBufC* buf = HBufC::NewLC(elementAndGeneric.DesC().Length()); |
|
179 buf->Des().Copy(elementAndGeneric.DesC()); |
|
180 TLex string(*buf); |
|
181 |
|
182 TPtrC token = string.NextToken(); |
|
183 TInt compare = aElement.Compare(token); |
|
184 |
|
185 if(compare == 0) |
|
186 { |
|
187 // We've got a match so use the generic name |
|
188 // This isn't pushed on the CleanupStack here as we need to be able to pop |
|
189 // buf and pool whilst genericElementName is still in scope. It will be |
|
190 // pushed once these aren't necessary. Because of this THERE MUST BE |
|
191 // NOTHING THAT LEAVES UNTIL genericElementName IS PUT ONTO THE CLEANUPSTACK |
|
192 genericElementName =(string.NextToken()).AllocL(); |
|
193 } |
|
194 |
|
195 CleanupStack::PopAndDestroy(buf); //buf |
|
196 |
|
197 |
|
198 if(compare == 0) |
|
199 break; |
|
200 else if (compare > 0) |
|
201 left = pos + 1; |
|
202 else |
|
203 right = pos - 1; |
|
204 } |
|
205 } |
|
206 |
|
207 CleanupStack::PopAndDestroy(); // Close pool |
|
208 |
|
209 if(genericElementName != NULL) |
|
210 { |
|
211 CleanupStack::PushL(genericElementName); |
|
212 } |
|
213 else |
|
214 { |
|
215 // We didn't find a generic name so use the element name passed in |
|
216 genericElementName = HBufC::NewLC(aElement.Length()); |
|
217 genericElementName->Des().Copy(aElement); |
|
218 } |
|
219 |
|
220 // Using the generic element name test to see if we have a valid attribute and value |
|
221 |
|
222 // assume the attribute name is invalid |
|
223 TInt error = KErrXMLBadAttributeName; |
|
224 |
|
225 // retrieve the attributeValue string table |
|
226 // this is of the form elementName attributeName attribValue1 attribValue2 ... |
|
227 |
|
228 CleanupClosePushL(pool); |
|
229 pool.OpenL(SMILAttributes::Table); |
|
230 |
|
231 numTableEntries = SMILAttributes::Table.iCount; |
|
232 if(numTableEntries != 0) |
|
233 { |
|
234 // Using a binary search since the table is always sorted alphabetically by element+attribute. |
|
235 |
|
236 // Set us search indices to outer bounds of array |
|
237 TInt left = 0; |
|
238 TInt right = numTableEntries - 1; |
|
239 TInt compareResult = 0; |
|
240 TInt pos; |
|
241 while (right >= left) |
|
242 { |
|
243 pos = (left + right) / 2; |
|
244 |
|
245 RStringF tableEntry = pool.StringF(pos, SMILAttributes::Table); |
|
246 HBufC* buf = HBufC::NewLC(tableEntry.DesC().Length()); |
|
247 buf->Des().Copy(tableEntry.DesC()); |
|
248 |
|
249 TLex string(*buf); |
|
250 |
|
251 // Get the element name from the string table (the first token) |
|
252 TPtrC token = string.NextToken(); |
|
253 // Is this the element name we are interested in |
|
254 compareResult = genericElementName->Compare(token); |
|
255 if(compareResult == 0) |
|
256 { |
|
257 // we're looking at an entry in the string table for this element |
|
258 // so test to see if it's the correct attribute too. |
|
259 token.Set(string.NextToken()); |
|
260 compareResult = aAttribute.Compare(token); |
|
261 if(compareResult == 0) |
|
262 { |
|
263 // we've got the correct entry in the table (both element & attribute match) |
|
264 // so now assume the error is an incorrect attribute value |
|
265 error = KErrXMLBadAttributeValue; |
|
266 |
|
267 // get hold of the first valid attribure value |
|
268 token.Set(string.NextToken()); |
|
269 // if we don't have a list of attribute values then we can assume whatever we've got is valid |
|
270 // so set the error to KErrNone |
|
271 if (token.Length() == 0) |
|
272 error = KErrNone; |
|
273 else |
|
274 { |
|
275 // Cycle through all listed attribute values to see if we have a valid one |
|
276 while (token.Length() != 0) |
|
277 { |
|
278 if (aAttributeValue.Compare(token) == 0) |
|
279 { |
|
280 // value of attribute is valid |
|
281 error = KErrNone; |
|
282 break; |
|
283 } |
|
284 else |
|
285 token.Set(string.NextToken()); |
|
286 |
|
287 } |
|
288 } |
|
289 |
|
290 } |
|
291 |
|
292 } |
|
293 CleanupStack::PopAndDestroy(buf); // buf |
|
294 if (compareResult == 0) // Matching item found |
|
295 break; |
|
296 else if (compareResult > 0) |
|
297 left = pos + 1; |
|
298 else |
|
299 right = pos - 1; |
|
300 } |
|
301 |
|
302 } |
|
303 CleanupStack::PopAndDestroy(2); // pool, genericElementName |
|
304 |
|
305 return error; |
|
306 } |
|
307 |
|
308 TBool CSMILDtd::AreValidChildElementsL(const TDesC& aParentElement, const CDesCArray& aChildElements) const |
|
309 // Function to determine whether the parent/child relationship is valid in DTD |
|
310 // @return ETrue if parent/child relationship is valid |
|
311 // @param aParentElement the name of the parent element to be tested |
|
312 // @param aChildElements an array of child element name to be tested |
|
313 // @leave leave due to OOM |
|
314 // |
|
315 { |
|
316 TBool retVal = EFalse; |
|
317 |
|
318 |
|
319 |
|
320 if(aParentElement == KSMILDTDElta) |
|
321 { |
|
322 retVal = CheckValidChildren(SMILDTDAChildStates,KSMILDTDAChildStateTranCount, aChildElements); |
|
323 } |
|
324 |
|
325 else if( aParentElement == KSMILDTDEltDoc) |
|
326 { |
|
327 retVal = ETrue; |
|
328 } |
|
329 |
|
330 |
|
331 else if(aParentElement == KSMILDTDEltanimation || aParentElement == KSMILDTDEltaudio || aParentElement == KSMILDTDEltimg || aParentElement == KSMILDTDEltref |
|
332 || aParentElement == KSMILDTDElttext || aParentElement == KSMILDTDElttextstream || aParentElement == KSMILDTDEltvideo) |
|
333 { |
|
334 retVal = CheckValidChildren(SMILDTDMediaChildStates, KSMILDTDMediaChildStateTranCount, aChildElements); |
|
335 } |
|
336 |
|
337 else if(aParentElement == KSMILDTDEltbody) |
|
338 { |
|
339 retVal = CheckValidChildren(SMILDTDBodyChildStates, KSMILDTDBodyChildStateTranCount, aChildElements); |
|
340 } |
|
341 |
|
342 else if(aParentElement == KSMILDTDElthead) |
|
343 { |
|
344 retVal = CheckValidChildren(SMILDTDHeadChildStates, KSMILDTDHeadChildStateTranCount, aChildElements); |
|
345 } |
|
346 |
|
347 else if(aParentElement == KSMILDTDEltlayout) |
|
348 { |
|
349 retVal = CheckValidChildren(SMILDTDLayoutChildStates, KSMILDTDLayoutChildStateTranCount, aChildElements); |
|
350 } |
|
351 |
|
352 else if(aParentElement == KSMILDTDEltpar || aParentElement == KSMILDTDEltseq) |
|
353 { |
|
354 retVal = CheckValidChildren(SMILDTDTimingChildStates, KSMILDTDTimingChildStateTranCount, aChildElements); |
|
355 } |
|
356 |
|
357 else if(aParentElement == KSMILDTDEltsmil) |
|
358 { |
|
359 retVal = CheckValidChildren(SMILDTDSmilChildStates, KSMILDTDSmilChildStateTranCount, aChildElements); |
|
360 } |
|
361 |
|
362 else if(aParentElement == KSMILDTDEltswitch) |
|
363 { |
|
364 retVal = CheckValidChildren(SMILDTDSwitchChildStates, KSMILDTDSwitchChildStateTranCount, aChildElements); |
|
365 } |
|
366 |
|
367 return retVal; |
|
368 } |
|
369 |
|
370 |
|
371 TBool CSMILDtd::CheckValidChildren(const TSMILDTDChildStateType aStateTrans[],TInt aStateCount, const CDesCArray& aChildElements) const |
|
372 // |
|
373 // Checks child element ownership based on a Finite State Machine |
|
374 // @param aFirstChild - pointer to first child element |
|
375 // @param aStateTrans - Array of state transition elements. |
|
376 // The elements must be ordered by tag name first and then starting state |
|
377 // as this routine uses the ordering to drive an efficient search. |
|
378 // @param aStateCount - the number of state transitions in the array |
|
379 // @return true if the list of children matches the defined state machine |
|
380 // |
|
381 { |
|
382 // This routine works by considering the allowed set of child elements as a Finite State |
|
383 // Machine. When tracing through the list of children, each child encountered causes |
|
384 // a state transition. The actual states are 'between' elements. The states are |
|
385 // simply referred to by numbers, 0 is the starting state, the legal final state is |
|
386 // state -1, other states are positive integers (the actual values have no significance, |
|
387 // only the transitions and the start and end are of importance. |
|
388 // When the list of children ends, a special 'empty tag' element is considered to be |
|
389 // found. If this empty tag element causes a transition to the final state then the list |
|
390 // has been successfully traversed. |
|
391 // If, at any point, a child element is encountered which does not lead to a valid |
|
392 // transition from the current state then the list is invalid. By considering the |
|
393 // empty tag element to be on the end of the list we handle the requirements for valid |
|
394 // completion. |
|
395 // This routine is general - it just needs to be fed a set of state transitions for a specific |
|
396 // element type. |
|
397 |
|
398 TBool returnValue = true; // We are successful until proved otherwise |
|
399 if( aStateCount < 1 ) |
|
400 { |
|
401 returnValue = false; // Just check for a duff count |
|
402 } |
|
403 TInt fromState=KSMILDTDStartState; // Current state - the one we are looking for a transition from |
|
404 TInt toState=KSMILDTDEndState; // State to which this tag leads us - initialised to avoid warning |
|
405 TInt midPoint= aStateCount / 2; // Middle of the state array, used for binary search |
|
406 TInt initJump = midPoint / 2; // Size of initial jump for binary search |
|
407 TInt tranArrInd; // Index into the state transition array |
|
408 |
|
409 // Prime the search with the initial state and the tag for the first element |
|
410 // We skip nodes which are not elements (e.g. text, comments or processing instructions) |
|
411 |
|
412 |
|
413 for (TInt i = 0; i<aChildElements.Count(); i++) |
|
414 { |
|
415 // Check for the 'empty tag'. We could take a local copy but that would |
|
416 // not be most efficient so we have slightly more complex code. |
|
417 |
|
418 { |
|
419 // Given a current state and tag, find a valid transition. |
|
420 // Use a binary search - the array is sorted on tag name first and then |
|
421 // from state. We binary chop to a suitable level and then do a linear |
|
422 // search until we hit pay-dirt or find an element which proves. We |
|
423 // only bother to binary search based on the tag name. |
|
424 // This is a home baked binary chop and could potentially be made more elegant. |
|
425 |
|
426 tranArrInd = midPoint; |
|
427 TInt jump = initJump; |
|
428 TBool keepChopping = true; |
|
429 TInt compVal; |
|
430 TPtrC tranTag; |
|
431 while(keepChopping) |
|
432 { |
|
433 tranTag.Set( aStateTrans[tranArrInd].TagName, aStateTrans[tranArrInd].TagLength); |
|
434 |
|
435 compVal = aChildElements[i].Compare(tranTag); |
|
436 if(compVal < 0) |
|
437 { |
|
438 tranArrInd -= jump; |
|
439 } |
|
440 else if(compVal > 0) |
|
441 { |
|
442 tranArrInd += jump; |
|
443 } |
|
444 jump = jump / 2; |
|
445 if((compVal == 0) || (jump < KSMILDTDMinJump)) |
|
446 { |
|
447 keepChopping = false; |
|
448 } |
|
449 }// endwhile |
|
450 // We have now finished binary chopping, either because we matched the tag or because |
|
451 // We got to a small jump size. Now do a linear scan, up or down, to fimd a match. |
|
452 |
|
453 TBool up = true; // Direction of scan |
|
454 tranTag.Set( aStateTrans[tranArrInd].TagName, aStateTrans[tranArrInd].TagLength); |
|
455 compVal = aChildElements[i].Compare(tranTag); |
|
456 if((compVal < 0) || |
|
457 ((compVal == 0) && (fromState < aStateTrans[tranArrInd].FromState))) |
|
458 { |
|
459 up = false; |
|
460 } |
|
461 if( up ) |
|
462 { |
|
463 while((tranArrInd < aStateCount) && |
|
464 ((compVal > 0) || |
|
465 ((compVal == 0) && (fromState > aStateTrans[tranArrInd].FromState)))) |
|
466 { |
|
467 tranArrInd ++; |
|
468 tranTag.Set( aStateTrans[tranArrInd].TagName, aStateTrans[tranArrInd].TagLength); |
|
469 if(tranArrInd < aStateCount) |
|
470 { |
|
471 compVal = aChildElements[i].Compare(tranTag); |
|
472 } |
|
473 }// endwhile stepping up |
|
474 } |
|
475 else |
|
476 { |
|
477 while((tranArrInd >= 0) && |
|
478 ((compVal < 0) || |
|
479 ((compVal == 0) && (fromState < aStateTrans[tranArrInd].FromState)))) |
|
480 { |
|
481 tranArrInd --; |
|
482 tranTag.Set( aStateTrans[tranArrInd].TagName, aStateTrans[tranArrInd].TagLength); |
|
483 if(tranArrInd >= 0) |
|
484 { |
|
485 compVal = aChildElements[i].Compare(tranTag); |
|
486 } |
|
487 }// endwhile stepping down |
|
488 } |
|
489 // If we have a match, fine, else this is an illegal transition |
|
490 if((tranArrInd >= 0) && (tranArrInd < aStateCount) && |
|
491 (compVal == 0) && (fromState == aStateTrans[tranArrInd].FromState)) |
|
492 { |
|
493 toState = aStateTrans[tranArrInd].ToState; |
|
494 } |
|
495 else |
|
496 { |
|
497 returnValue = false; |
|
498 break; |
|
499 } |
|
500 }//end else not reached end of list of children |
|
501 |
|
502 fromState = toState; |
|
503 }// endfor |
|
504 |
|
505 if(returnValue) |
|
506 { |
|
507 tranArrInd = 0; |
|
508 while((tranArrInd < aStateCount) && |
|
509 (aStateTrans[tranArrInd].FromState != fromState) && |
|
510 (aStateTrans[tranArrInd].TagLength == 0)) |
|
511 { |
|
512 tranArrInd++; |
|
513 } |
|
514 if((tranArrInd < aStateCount) && |
|
515 (aStateTrans[tranArrInd].FromState == fromState) && |
|
516 (aStateTrans[tranArrInd].TagLength == 0)) |
|
517 { |
|
518 toState = aStateTrans[tranArrInd].ToState ; // Better be the final state! |
|
519 } |
|
520 else |
|
521 { |
|
522 returnValue = false ; // No legal transition |
|
523 } |
|
524 } |
|
525 |
|
526 |
|
527 return returnValue; |
|
528 } |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 TBool CSMILDtd::CanElementHaveChildren(const TDesC& aElement) const |
|
534 // |
|
535 // Function to determine whether it is valid for a particular element to |
|
536 // have children |
|
537 // @param aElement the name of the element to be tested |
|
538 // @return ETrue if it is valid for element to have children |
|
539 // |
|
540 { |
|
541 TBool retVal = ETrue; |
|
542 if(aElement == KSMILDTDEltanchor || aElement == KSMILDTDEltmeta || aElement == KSMILDTDEltroot_layout |
|
543 || aElement == KSMILDTDEltregion || aElement == KSMILDTDEltarea || aElement == KSMILDTDEltmetadata |
|
544 || aElement == KSMILDTDEltprefetch || aElement == KSMILDTDEltTrans) |
|
545 retVal = EFalse; |
|
546 |
|
547 return retVal; |
|
548 |
|
549 } |
|