|
1 // Copyright (c) 2000-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 // |
|
15 |
|
16 // Local includes |
|
17 // |
|
18 #include "XmlValid.h" |
|
19 |
|
20 // System includes |
|
21 // |
|
22 #include <cdtdmodel.h> |
|
23 #include <xmlelemt.h> |
|
24 #include <cbnfnode.h> |
|
25 #include <cnodeleteattribute.h> |
|
26 #include <xmllib.h> |
|
27 |
|
28 |
|
29 CXmlValidator::CXmlValidator(CBNFNode& aDTD, CAttributeLookupTable& aALUT) : iDTD(aDTD), iALUT(aALUT) |
|
30 { |
|
31 } |
|
32 |
|
33 CXmlValidator::~CXmlValidator() |
|
34 { |
|
35 delete iCounterArray; |
|
36 } |
|
37 |
|
38 CXmlValidator* CXmlValidator::NewL(CBNFNode& aDTD, CAttributeLookupTable& aALUT) |
|
39 { |
|
40 CXmlValidator* self = new(ELeave) CXmlValidator(aDTD, aALUT); |
|
41 CleanupStack::PushL(self); |
|
42 self->ConstructL(); |
|
43 CleanupStack::Pop(self); |
|
44 return(self); |
|
45 } |
|
46 |
|
47 void CXmlValidator::ConstructL() |
|
48 { |
|
49 iCounterArray = new(ELeave) CArrayFixFlat<TInt>(3); |
|
50 } |
|
51 |
|
52 TInt CXmlValidator::ValidateTreeL(CXmlElement* aRootNode) |
|
53 { |
|
54 if( aRootNode == NULL ) |
|
55 { |
|
56 return( EWapErrXmlLibMissingDocument ); |
|
57 } |
|
58 CNode* currentNode = aRootNode; |
|
59 CNode* childNode = NULL; |
|
60 TInt returnCode = KErrNone; |
|
61 |
|
62 FOREVER // Hihi :) |
|
63 { |
|
64 if( childNode == NULL ) |
|
65 { |
|
66 // We entered this node first time, going down the tree |
|
67 |
|
68 returnCode = ValidateNodeL((CXmlElement*)currentNode); |
|
69 if( returnCode != KErrNone ) |
|
70 { |
|
71 return( returnCode ); |
|
72 } |
|
73 |
|
74 childNode = currentNode->NextChild(); // The first born... |
|
75 } |
|
76 else |
|
77 { |
|
78 // We returned from a child node |
|
79 childNode = currentNode->NextChild(childNode); |
|
80 } |
|
81 |
|
82 if( childNode == NULL ) |
|
83 { |
|
84 if( currentNode == aRootNode ) |
|
85 { |
|
86 // The tree is done. |
|
87 return( KErrNone ); |
|
88 } |
|
89 // Go up the tree |
|
90 childNode = currentNode; |
|
91 currentNode = currentNode->Parent(); |
|
92 } |
|
93 else |
|
94 { |
|
95 currentNode = childNode; |
|
96 childNode = NULL; |
|
97 } |
|
98 } |
|
99 } |
|
100 |
|
101 TInt CXmlValidator::ValidateNodeL(CXmlElement* aNode) |
|
102 { |
|
103 TInt returnCode = KErrNone; |
|
104 |
|
105 CBNFNode* dtdNode = REINTERPRET_CAST(CBNFNode*,iDTD.Attribute((const TDesC*)(aNode->Type()))); |
|
106 if( dtdNode == NULL ) |
|
107 { |
|
108 return( EWapErrXmlLibIllegalTagName ); |
|
109 } |
|
110 returnCode = ValidateNodeAttributesL(aNode, dtdNode); |
|
111 if( returnCode == KErrNone ) |
|
112 { |
|
113 returnCode = ValidateNodeChildrenL(aNode, dtdNode); |
|
114 } |
|
115 |
|
116 // Here we could perform the validation of Node's data |
|
117 |
|
118 return( returnCode ); |
|
119 } |
|
120 |
|
121 |
|
122 TInt CXmlValidator::ValidateNodeChildrenL(CXmlElement* aDocumentNode, CBNFNode* aDTDNode) |
|
123 { |
|
124 iCurrentDocumentChild = (CXmlElement*)aDocumentNode->NextChild(); |
|
125 if( iCurrentDocumentChild != NULL && iDTD.Attribute((const TDesC*)(iCurrentDocumentChild->Type())) == NULL ) |
|
126 { |
|
127 return( EWapErrXmlLibIllegalTagName ); |
|
128 } |
|
129 |
|
130 TInt returnValue = KErrNone; |
|
131 TBool nodeFinished = EFalse; |
|
132 iDTDChildNode = NULL; |
|
133 CBNFNode* dtdNode; |
|
134 iDTDNodeStack.Clear(); |
|
135 iDTDNodeStack.PushL(aDTDNode); |
|
136 iCounterArray->Reset(); |
|
137 iNodeMatched = EFalse; |
|
138 while( iCurrentDocumentChild != NULL && !iDTDNodeStack.IsEmpty() ) |
|
139 { |
|
140 dtdNode = iDTDNodeStack.Head(); |
|
141 nodeFinished = EFalse; |
|
142 switch(dtdNode->Type()) |
|
143 { |
|
144 case EReference: |
|
145 returnValue = HandleReferenceL(dtdNode, nodeFinished); |
|
146 break; |
|
147 case ERoot: |
|
148 case EAnd: |
|
149 returnValue = HandleAndL(dtdNode, nodeFinished); |
|
150 break; |
|
151 case EOr: |
|
152 returnValue = HandleOrL(dtdNode, nodeFinished); |
|
153 break; |
|
154 case EOptional: |
|
155 returnValue = HandleOptionalL(dtdNode, nodeFinished); |
|
156 break; |
|
157 case ENMore: |
|
158 returnValue = HandleNMoreL(dtdNode, nodeFinished); |
|
159 break; |
|
160 case EIncomplete: |
|
161 nodeFinished = ETrue; |
|
162 iNodeMatched = ETrue; |
|
163 break; |
|
164 } |
|
165 |
|
166 if( returnValue != KErrNone ) |
|
167 { |
|
168 return( returnValue ); |
|
169 } |
|
170 |
|
171 if( nodeFinished ) |
|
172 { |
|
173 iDTDChildNode = dtdNode; |
|
174 iDTDNodeStack.Pop(); |
|
175 } |
|
176 else |
|
177 { |
|
178 iDTDChildNode = NULL; |
|
179 } |
|
180 |
|
181 if( iNodeMatched ) |
|
182 { |
|
183 iCurrentDocumentChild = (CXmlElement*)aDocumentNode->NextChild(iCurrentDocumentChild); |
|
184 if( iCurrentDocumentChild != NULL && iDTD.Attribute((const TDesC*)(iCurrentDocumentChild->Type())) == NULL ) |
|
185 { |
|
186 return( EWapErrXmlLibIllegalTagName ); |
|
187 } |
|
188 iNodeMatched = EFalse; |
|
189 iSubNodeMatch = EMatch; |
|
190 } |
|
191 } |
|
192 |
|
193 if( iCurrentDocumentChild == NULL ) |
|
194 { |
|
195 return( KErrNone ); |
|
196 } |
|
197 else |
|
198 { |
|
199 return( EWapErrXmlLibInvalidDocumentStructure ); |
|
200 } |
|
201 } |
|
202 |
|
203 TInt CXmlValidator::HandleReferenceL(CBNFNode* aDTDNode, TBool& aNodeFinished) |
|
204 { |
|
205 if( iDTDChildNode == NULL ) |
|
206 { |
|
207 CNoDeleteAttributeT<CBNFNode*>* attributeNode = REINTERPRET_CAST(CNoDeleteAttributeT<CBNFNode*>*, aDTDNode->Attribute(CBNFNode::KReference())); |
|
208 CBNFNode* referencedNode = attributeNode->Attribute(); |
|
209 |
|
210 const TDesC* dtdNodeName = NodeName(referencedNode); |
|
211 if( (*dtdNodeName)[0] != '%' ) |
|
212 { |
|
213 aNodeFinished = ETrue; |
|
214 if( iCurrentDocumentChild->Type()->Compare(*dtdNodeName) == 0 || |
|
215 ( iCurrentDocumentChild->Type()->Compare(KCDataID) == 0 && |
|
216 dtdNodeName->Compare(KPCDataID) == 0 ) |
|
217 ) |
|
218 { |
|
219 iNodeMatched = ETrue; |
|
220 } |
|
221 else |
|
222 { |
|
223 iNodeMatched = EFalse; |
|
224 } |
|
225 } |
|
226 else |
|
227 { |
|
228 aNodeFinished = EFalse; |
|
229 iDTDNodeStack.PushL(referencedNode); |
|
230 } |
|
231 } |
|
232 else |
|
233 { |
|
234 aNodeFinished = ETrue; |
|
235 } |
|
236 return( KErrNone ); |
|
237 } |
|
238 |
|
239 TInt CXmlValidator::HandleAndL(CBNFNode* aDTDNode, TBool& aNodeFinished) |
|
240 { |
|
241 if( iDTDChildNode == NULL ) |
|
242 { |
|
243 iSubNodeMatch = EMiss; |
|
244 aNodeFinished = EFalse; |
|
245 iDTDNodeStack.PushL((CBNFNode*)aDTDNode->NextChild()); |
|
246 iCounterArray->InsertL(0, 0); |
|
247 } |
|
248 else |
|
249 { |
|
250 if( (iSubNodeMatch == EMatch || iSubNodeMatch == EConditionalMiss) && aDTDNode->NextChild(iDTDChildNode)!=NULL ) |
|
251 { |
|
252 if( iSubNodeMatch == EMatch ) |
|
253 { |
|
254 (*iCounterArray)[0]++; |
|
255 } |
|
256 aNodeFinished = EFalse; |
|
257 iSubNodeMatch = EMiss; |
|
258 iDTDNodeStack.PushL( (CBNFNode*)aDTDNode->NextChild(iDTDChildNode) ); |
|
259 } |
|
260 else |
|
261 { |
|
262 if( iCounterArray->At(0) > 0 && iSubNodeMatch == EConditionalMiss ) |
|
263 iSubNodeMatch = EMatch; |
|
264 aNodeFinished = ETrue; |
|
265 iCounterArray->Delete(0); |
|
266 } |
|
267 } |
|
268 return( KErrNone ); |
|
269 } |
|
270 |
|
271 TInt CXmlValidator::HandleOrL(CBNFNode* aDTDNode, TBool& aNodeFinished) |
|
272 { |
|
273 if( iDTDChildNode == NULL ) |
|
274 { |
|
275 aNodeFinished = EFalse; |
|
276 iSubNodeMatch = EMiss; |
|
277 iDTDNodeStack.PushL((CBNFNode*)aDTDNode->NextChild()); |
|
278 iDocumentChildMarkStack.PushL(iCurrentDocumentChild); |
|
279 } |
|
280 else |
|
281 { |
|
282 if( (iSubNodeMatch == EMiss || iSubNodeMatch == EConditionalMiss) && aDTDNode->NextChild(iDTDChildNode)!=NULL ) |
|
283 { |
|
284 aNodeFinished = EFalse; |
|
285 iDTDNodeStack.PushL((CBNFNode*)aDTDNode->NextChild(iDTDChildNode) ); |
|
286 iCurrentDocumentChild = iDocumentChildMarkStack.Head(); |
|
287 } |
|
288 else |
|
289 { |
|
290 if( iSubNodeMatch != EMatch ) |
|
291 { |
|
292 iCurrentDocumentChild = iDocumentChildMarkStack.Pop(); |
|
293 } |
|
294 else |
|
295 { |
|
296 iDocumentChildMarkStack.Pop(); |
|
297 } |
|
298 |
|
299 aNodeFinished = ETrue; |
|
300 } |
|
301 } |
|
302 return( KErrNone ); |
|
303 } |
|
304 |
|
305 TInt CXmlValidator::HandleOptionalL(CBNFNode* aDTDNode, TBool& aNodeFinished) |
|
306 { |
|
307 if( iDTDChildNode == NULL ) |
|
308 { |
|
309 aNodeFinished = EFalse; |
|
310 iDTDNodeStack.PushL((CBNFNode*)aDTDNode->NextChild()); |
|
311 } |
|
312 else |
|
313 { |
|
314 aNodeFinished = ETrue; |
|
315 if( iSubNodeMatch == EMiss ) |
|
316 { |
|
317 iSubNodeMatch = EConditionalMiss; |
|
318 } |
|
319 } |
|
320 return( KErrNone ); |
|
321 } |
|
322 |
|
323 TInt CXmlValidator::HandleNMoreL(CBNFNode* aDTDNode, TBool& aNodeFinished) |
|
324 { |
|
325 if( iDTDChildNode == NULL ) |
|
326 { |
|
327 iSubNodeMatch = EMiss; |
|
328 iDTDNodeStack.PushL((CBNFNode*)aDTDNode->NextChild()); |
|
329 iCounterArray->InsertL(0, 0); |
|
330 aNodeFinished = EFalse; |
|
331 } |
|
332 else |
|
333 { |
|
334 if( iSubNodeMatch == EMatch ) |
|
335 { |
|
336 (*iCounterArray)[0]++; |
|
337 iDTDNodeStack.PushL((CBNFNode*)aDTDNode->NextChild()); |
|
338 iSubNodeMatch = EMiss; |
|
339 aNodeFinished = EFalse; |
|
340 } |
|
341 else |
|
342 { |
|
343 aNodeFinished = ETrue; |
|
344 if( iCounterArray->At(0) > 0 ) |
|
345 { |
|
346 iSubNodeMatch = EMatch; |
|
347 } |
|
348 else if( aDTDNode->AttributeExists(CBNFNode::KNMoreMinimum()) ) |
|
349 { |
|
350 iSubNodeMatch = EMiss; |
|
351 } |
|
352 else |
|
353 { |
|
354 iSubNodeMatch = EConditionalMiss; |
|
355 } |
|
356 iCounterArray->Delete(0); |
|
357 } |
|
358 } |
|
359 return( KErrNone ); |
|
360 } |
|
361 |
|
362 const TDesC* CXmlValidator::NodeName(CBNFNode* aNode) |
|
363 { |
|
364 TInt count=iDTD.AttributeCount(); |
|
365 const TDesC* nodeId = NULL; |
|
366 for (TInt i = 0; i < count; i++) |
|
367 { |
|
368 const TDesC* type; |
|
369 if (aNode == iDTD.AttributeByIndex(i,type)) |
|
370 { |
|
371 nodeId = type; |
|
372 break; |
|
373 } |
|
374 } |
|
375 return nodeId; |
|
376 } |
|
377 |
|
378 |
|
379 TInt CXmlValidator::ValidateNodeAttributesL(CXmlElement* aDocumentNode, CBNFNode* aDTDNode) |
|
380 // aDTDNode is the node from the DTD tree corresponding to the aDocumentNode |
|
381 { |
|
382 |
|
383 TInt i; |
|
384 TInt count=aDTDNode->AttributeCount(); |
|
385 for(i=0; i < count; i++) |
|
386 { |
|
387 const TDesC* attributeName; |
|
388 CDTDModel::CDTDElementAttribute* DTDAttribute = REINTERPRET_CAST(CDTDModel::CDTDElementAttribute*, aDTDNode->AttributeByIndex(i,attributeName) ); |
|
389 |
|
390 if( DTDAttribute->iValueType == CDTDModel::CDTDElementAttribute::EReference ) |
|
391 { |
|
392 CNoDeleteAttributeT<CBNFNode*>* attributeNode = REINTERPRET_CAST(CNoDeleteAttributeT<CBNFNode*>*,DTDAttribute->iType->Attribute(CBNFNode::KReference())); |
|
393 TInt refReturnValue = ValidateNodeAttributesL(aDocumentNode, attributeNode->Attribute()); |
|
394 if( refReturnValue != KErrNone ) |
|
395 { |
|
396 return(refReturnValue); |
|
397 } |
|
398 continue; |
|
399 } |
|
400 |
|
401 // const TDesC* attributeName = aDTDNode->AttributeTypeByIndex(i); |
|
402 const TDesC* documentAttributeValue = aDocumentNode->Attribute(*attributeName); |
|
403 if( documentAttributeValue != NULL ) |
|
404 { |
|
405 if( DTDAttribute->iValueType == CDTDModel::CDTDElementAttribute::EFixed ) |
|
406 { |
|
407 if( documentAttributeValue->Compare(*(DTDAttribute->iValue->Data()) ) != 0 ) |
|
408 { |
|
409 return( EWapErrXmlLibIllegalFixedAttributeValue ); |
|
410 } |
|
411 } |
|
412 else |
|
413 { |
|
414 TPtr attrPtr(((HBufC*)documentAttributeValue)->Des()); |
|
415 TInt errorCode = NormalizeAndCheckAttributeValueL(attrPtr, DTDAttribute->iType); |
|
416 if( errorCode != KErrNone ) |
|
417 { |
|
418 return( errorCode ); |
|
419 } |
|
420 } |
|
421 } |
|
422 else |
|
423 { |
|
424 switch( DTDAttribute->iValueType ) |
|
425 { |
|
426 case CDTDModel::CDTDElementAttribute::ERequired: |
|
427 { |
|
428 return( EWapErrXmlLibMissingRequiredAttribute ); |
|
429 } // break; |
|
430 case CDTDModel::CDTDElementAttribute::EFixed: |
|
431 case CDTDModel::CDTDElementAttribute::EDefault: |
|
432 aDocumentNode->SetAttributeL( *attributeName, *(DTDAttribute->iValue->Data()), iALUT); |
|
433 break; |
|
434 case CDTDModel::CDTDElementAttribute::EImplied: |
|
435 default: |
|
436 break; |
|
437 } |
|
438 } |
|
439 } |
|
440 return(KErrNone); |
|
441 } |
|
442 |
|
443 |
|
444 TInt CXmlValidator::NormalizeAndCheckAttributeValueL(TPtr& aAttributeValue, CBNFNode* aAttributeValueTypeRoot) |
|
445 { |
|
446 NormalizeAttributeValueWhiteSpaces(aAttributeValue); |
|
447 |
|
448 TInt returnValue = KErrNone; |
|
449 TBool nodeFinished = EFalse; |
|
450 iDTDChildNode = NULL; |
|
451 CBNFNode* dtdNode; |
|
452 iDTDNodeStack.Clear(); |
|
453 iDTDNodeStack.PushL(aAttributeValueTypeRoot); |
|
454 iCounterArray->Reset(); |
|
455 iNodeMatched = EFalse; |
|
456 while( !iNodeMatched && !iDTDNodeStack.IsEmpty() ) |
|
457 { |
|
458 dtdNode = iDTDNodeStack.Head(); |
|
459 nodeFinished = EFalse; |
|
460 switch(dtdNode->Type()) |
|
461 { |
|
462 case EReference: |
|
463 returnValue = HandleReferenceInAttributeValueTypeL(dtdNode, nodeFinished); |
|
464 break; |
|
465 case ERoot: |
|
466 case EAnd: |
|
467 returnValue = HandleAndL(dtdNode, nodeFinished); |
|
468 break; |
|
469 case EOr: |
|
470 returnValue = HandleOrL(dtdNode, nodeFinished); |
|
471 break; |
|
472 case EOptional: |
|
473 returnValue = HandleOptionalL(dtdNode, nodeFinished); |
|
474 break; |
|
475 case ENMore: |
|
476 returnValue = HandleNMoreL(dtdNode, nodeFinished); |
|
477 break; |
|
478 case EIncomplete: |
|
479 nodeFinished = ETrue; |
|
480 returnValue = HandleAttributeValueCheckAndNormalize( aAttributeValue, NodeName(dtdNode) ); |
|
481 break; |
|
482 } |
|
483 |
|
484 if( returnValue != KErrNone ) |
|
485 { |
|
486 return( returnValue ); |
|
487 } |
|
488 |
|
489 if( nodeFinished ) |
|
490 { |
|
491 iDTDChildNode = dtdNode; |
|
492 iDTDNodeStack.Pop(); |
|
493 } |
|
494 else |
|
495 { |
|
496 iDTDChildNode = NULL; |
|
497 } |
|
498 } |
|
499 |
|
500 if( iNodeMatched ) |
|
501 { |
|
502 return( KErrNone ); |
|
503 } |
|
504 else |
|
505 { |
|
506 return( EWapErrXmlLibIllegalAttributeValue ); |
|
507 } |
|
508 } |
|
509 |
|
510 TInt CXmlValidator::HandleReferenceInAttributeValueTypeL(CBNFNode* aDTDNode, TBool& aNodeFinished) |
|
511 { |
|
512 if( iDTDChildNode == NULL ) |
|
513 { |
|
514 CNoDeleteAttributeT<CBNFNode*>* attributeNode = REINTERPRET_CAST(CNoDeleteAttributeT<CBNFNode*>*, aDTDNode->Attribute(CBNFNode::KReference())); |
|
515 CBNFNode* referencedNode = attributeNode->Attribute(); |
|
516 aNodeFinished = EFalse; |
|
517 iDTDNodeStack.PushL(referencedNode); |
|
518 } |
|
519 else |
|
520 { |
|
521 aNodeFinished = ETrue; |
|
522 } |
|
523 return( KErrNone ); |
|
524 } |
|
525 |
|
526 TInt CXmlValidator::HandleAttributeValueCheckAndNormalize(TPtr& aAttributeValue, const TDesC* aAttributeValueType) |
|
527 { |
|
528 _LIT( KNMTOKENID, "NMTOKEN"); |
|
529 _LIT( KIDID, "ID" ); |
|
530 |
|
531 if( aAttributeValueType->Compare(KCDataID) != 0 ) |
|
532 { |
|
533 // Xml specs 3.3.3, when declared calue is not CDATA, leading and trailing spaces |
|
534 // are wasted and space sequences are replaced with single space |
|
535 aAttributeValue.TrimAll(); |
|
536 } |
|
537 else |
|
538 { |
|
539 // The value type is CDATA |
|
540 // In future, here we could ran a check for the contents the attribute that it |
|
541 // corresponds to CDATA constraints |
|
542 iNodeMatched = ETrue; |
|
543 return( KErrNone ); |
|
544 } |
|
545 |
|
546 if( aAttributeValueType->Compare(KNMTOKENID) == 0 ) |
|
547 { |
|
548 // Here we could ran a check for the attribute contents to correspond |
|
549 // to NMTOKEN rules |
|
550 iNodeMatched = ETrue; |
|
551 } |
|
552 else if( aAttributeValueType->Compare(KIDID) == 0 ) |
|
553 { |
|
554 // Here we could ran a check for the attribute contents to correspond |
|
555 // to ID rules |
|
556 iNodeMatched = ETrue; |
|
557 } |
|
558 else |
|
559 { |
|
560 // The value aAttributeValueType contains an enumerated value. Check if this one matches to the attribute value |
|
561 if( aAttributeValue.Compare(*aAttributeValueType) == 0 ) |
|
562 { |
|
563 iNodeMatched = ETrue; |
|
564 } |
|
565 else |
|
566 { |
|
567 iNodeMatched = EFalse; |
|
568 } |
|
569 } |
|
570 |
|
571 return( KErrNone ); |
|
572 } |
|
573 |
|
574 void CXmlValidator::NormalizeAttributeValueWhiteSpaces(TPtr& aAttributeValue) |
|
575 { |
|
576 // White space normalization, XML specs 3.3.3 |
|
577 TInt i; |
|
578 TInt length=aAttributeValue.Length(); |
|
579 TText* stringPtr=(TText*)aAttributeValue.Ptr(); |
|
580 for(i=0; i < length; i++) |
|
581 { |
|
582 TText ch=*stringPtr; |
|
583 if( ch==0xD && i<length-1 && *(stringPtr+1)==0xA) |
|
584 { |
|
585 aAttributeValue.Delete(i,1); |
|
586 *stringPtr=0x20; |
|
587 length--; |
|
588 } |
|
589 if( ch==0x9 || ch==0xA || ch==0xD) |
|
590 { |
|
591 *stringPtr = 0x20; |
|
592 } |
|
593 ++stringPtr; |
|
594 } |
|
595 } |