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