|
1 /* |
|
2 * Copyright (c) 2005-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: CapParser implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 |
|
21 #include "capparser.h" |
|
22 #include "caputils.h" |
|
23 |
|
24 const char KFind1 = '&'; |
|
25 const char KFind2 = '<'; |
|
26 const char KFind3 = '>'; |
|
27 const char KFind4 = '"'; |
|
28 const char KFind5 = '\''; |
|
29 |
|
30 _LIT( KReplace1, "&" ); |
|
31 _LIT( KReplace2, "<" ); |
|
32 _LIT( KReplace3, ">" ); |
|
33 _LIT( KReplace4, """ ); |
|
34 _LIT( KReplace5, "'" ); |
|
35 |
|
36 // ============================= MEMBER FUNCTIONS =============================== |
|
37 |
|
38 // ----------------------------------------------------------------------------- |
|
39 // CapParser::GetTagL(TDes& aText, TInt aId) |
|
40 // Returns capability element tag (eg. "Memory") |
|
41 // ----------------------------------------------------------------------------- |
|
42 // |
|
43 void CapParser::GetTagL(TDes& aText, TInt aId) |
|
44 { |
|
45 aText=KNullDesC; |
|
46 |
|
47 TInt count=NUMXMLTOKENS; |
|
48 for (TInt i=0; i<count; i++) |
|
49 { |
|
50 TXmlToken t=KXmlTokens[i]; |
|
51 if (t.id==aId) |
|
52 { |
|
53 aText=t.tag; |
|
54 return; |
|
55 } |
|
56 } |
|
57 User::Leave(KErrNotFound); |
|
58 } |
|
59 |
|
60 // ----------------------------------------------------------------------------- |
|
61 // CapParser::ElementId(const TDesC& aText) |
|
62 // Returns capability element id (eg. EMemory). |
|
63 // ----------------------------------------------------------------------------- |
|
64 // |
|
65 TInt CapParser::ElementId(const TDesC& aText) |
|
66 { |
|
67 TBuf<KTagSize>buf; |
|
68 TInt count=NUMXMLTOKENS; |
|
69 for (TInt i=0; i<count; i++) |
|
70 { |
|
71 TXmlToken t=KXmlTokens[i]; |
|
72 buf=t.tag; |
|
73 if (buf.Compare(aText)==0) |
|
74 { |
|
75 return t.id; |
|
76 } |
|
77 } |
|
78 return KErrNotFound; |
|
79 } |
|
80 |
|
81 // ----------------------------------------------------------------------------- |
|
82 // CapParser::ParseElement(const TDesC& aText, TInt& aId, TInt& aType) |
|
83 // Parses element's id and type (eg. "<Memory>" -> EMemory, EElementBegin) |
|
84 // ----------------------------------------------------------------------------- |
|
85 // |
|
86 void CapParser::ParseElement(const TDesC& aText, TInt& aId, TInt& aType) |
|
87 { |
|
88 TXmlParser parse; |
|
89 aId=KErrNotFound; |
|
90 |
|
91 parse.Set(aText); |
|
92 aType=parse.Type(); |
|
93 if (aType==TXmlParser::EElementUnknown) |
|
94 { |
|
95 return; |
|
96 } |
|
97 |
|
98 TPtrC ptr=parse.Tag(); |
|
99 aId=ElementId(ptr); |
|
100 } |
|
101 |
|
102 // ----------------------------------------------------------------------------- |
|
103 // CapParser::MakeElementL(TDes& aText, TInt aId, const TDesC& aValue) |
|
104 // Constructs element with value (eg. "<Free>23456</Free>"). |
|
105 // ----------------------------------------------------------------------------- |
|
106 // |
|
107 void CapParser::MakeElementL(TDes& aText, TInt aId, const TDesC& aValue) |
|
108 { |
|
109 aText=KNullDesC; |
|
110 ReplaceSpecialCharsL( aText, aValue ); |
|
111 |
|
112 TBuf<KTagSize> buf; |
|
113 MakeElementL( buf, aId, TXmlParser::EElementBegin ); |
|
114 |
|
115 TInt len = aText.Length() + buf.Length() + buf.Length() + 1; |
|
116 if ( len > aText.MaxLength()) |
|
117 { |
|
118 User::Leave(KErrTooBig); |
|
119 } |
|
120 |
|
121 aText.Insert( 0, buf ); |
|
122 MakeElementL( buf, aId, TXmlParser::EElementEnd ); |
|
123 aText.Append( buf ); |
|
124 } |
|
125 |
|
126 // ----------------------------------------------------------------------------- |
|
127 // CapParser::MakeElementL(TDes& aText, TInt aId, TInt aType) |
|
128 // Constructs element without value (eg. "<Memory>" or "</Memory>"). |
|
129 // ----------------------------------------------------------------------------- |
|
130 // |
|
131 void CapParser::MakeElementL(TDes& aText, TInt aId, TInt aType) |
|
132 { |
|
133 _LIT(KFormat1, "<%S>"); |
|
134 _LIT(KFormat2, "</%S>"); |
|
135 _LIT(KFormat3, "<%S/>"); |
|
136 |
|
137 aText=KNullDesC; |
|
138 TBuf<KTagSize> buf; |
|
139 GetTagL(buf, aId); |
|
140 |
|
141 switch(aType) |
|
142 { |
|
143 case TXmlParser::EElementBegin: |
|
144 aText.Format(KFormat1, &buf); |
|
145 break; |
|
146 case TXmlParser::EElementEnd: |
|
147 aText.Format(KFormat2, &buf); |
|
148 break; |
|
149 case TXmlParser::EElementEmpty: |
|
150 aText.Format(KFormat3, &buf); |
|
151 break; |
|
152 default: |
|
153 CapUtil::Panic(KErrArgument); |
|
154 break; |
|
155 } |
|
156 } |
|
157 |
|
158 // ----------------------------------------------------------------------------- |
|
159 // CapParser::MakeElementL(TDes& aText, TInt aId, const TDesC& aVersion, |
|
160 // const TDesC& aDate) |
|
161 // Constructs element with attributes "Version" and "Date"(eg. |
|
162 // "<FW Version="2.2"/>"). |
|
163 // ----------------------------------------------------------------------------- |
|
164 // |
|
165 void CapParser::MakeElementL(TDes& aText, TInt aId, const TDesC& aVersion, |
|
166 const TDesC& aDate) |
|
167 { |
|
168 _LIT(KFormat1, "<%S %S=\"%S\" %S=\"%S\"/>"); |
|
169 _LIT(KFormat2, "<%S %S=\"%S\"/>"); |
|
170 |
|
171 aText=KNullDesC; |
|
172 |
|
173 TBuf<KTagSize> element; |
|
174 TBuf<KTagSize> versionTag; |
|
175 TBuf<KTagSize> dateTag; |
|
176 |
|
177 GetTagL(element, aId); |
|
178 GetTagL(versionTag, EAttVersion); |
|
179 GetTagL(dateTag, EAttDate); |
|
180 |
|
181 if (aDate.Length()>0) |
|
182 { |
|
183 aText.Format(KFormat1, &element, &versionTag, &aVersion, &dateTag, |
|
184 &aDate); |
|
185 } |
|
186 else |
|
187 { |
|
188 aText.Format(KFormat2, &element, &versionTag, &aVersion); |
|
189 } |
|
190 } |
|
191 |
|
192 // ----------------------------------------------------------------------------- |
|
193 // CapParser::ReplaceSpecialCharsL( TDes& aText, const TDesC& aValue ) |
|
194 // Replaces special characters to xml compliant. |
|
195 // ----------------------------------------------------------------------------- |
|
196 // |
|
197 void CapParser::ReplaceSpecialCharsL( TDes& aText, const TDesC& aValue ) |
|
198 { |
|
199 if ( aValue.Length() > aText.MaxLength() ) |
|
200 { |
|
201 User::Leave(KErrTooBig); |
|
202 } |
|
203 aText.Copy( aValue ); |
|
204 |
|
205 //Replace special characters |
|
206 for( TInt i = 0; i < aText.Length(); i++ ) |
|
207 { |
|
208 switch( aText[i] ) |
|
209 { |
|
210 case KFind1: |
|
211 if ( aText.Length()+KReplace1().Length()-1 > aText.MaxLength() ) |
|
212 { |
|
213 User::Leave(KErrTooBig); |
|
214 } |
|
215 aText.Replace( i, 1, KReplace1 ); |
|
216 i+=KReplace1().Length()-1; |
|
217 break; |
|
218 case KFind2: |
|
219 if ( aText.Length()+KReplace2().Length()-1 > aText.MaxLength() ) |
|
220 { |
|
221 User::Leave(KErrTooBig); |
|
222 } |
|
223 aText.Replace( i, 1, KReplace2 ); |
|
224 i+=KReplace2().Length()-1; |
|
225 break; |
|
226 case KFind3: |
|
227 if ( aText.Length()+KReplace3().Length()-1 > aText.MaxLength() ) |
|
228 { |
|
229 User::Leave(KErrTooBig); |
|
230 } |
|
231 aText.Replace( i, 1, KReplace3 ); |
|
232 i+=KReplace3().Length()-1; |
|
233 break; |
|
234 case KFind4: |
|
235 if ( aText.Length()+KReplace4().Length()-1 > aText.MaxLength() ) |
|
236 { |
|
237 User::Leave(KErrTooBig); |
|
238 } |
|
239 aText.Replace( i, 1, KReplace4 ); |
|
240 i+=KReplace4().Length()-1; |
|
241 break; |
|
242 case KFind5: |
|
243 if ( aText.Length()+KReplace5().Length()-1 > aText.MaxLength() ) |
|
244 { |
|
245 User::Leave(KErrTooBig); |
|
246 } |
|
247 aText.Replace( i, 1, KReplace5 ); |
|
248 i+=KReplace5().Length()-1; |
|
249 break; |
|
250 default: |
|
251 break; |
|
252 } |
|
253 } |
|
254 } |
|
255 |
|
256 // ----------------------------------------------------------------------------- |
|
257 // TXmlParser::Set(const TDesC& aText) |
|
258 // Sets a tag |
|
259 // ----------------------------------------------------------------------------- |
|
260 // |
|
261 void TXmlParser::Set(const TDesC& aText) |
|
262 { |
|
263 iOriginal.Set(aText); |
|
264 TrimOriginal(); |
|
265 Init(); |
|
266 Parse(); |
|
267 } |
|
268 |
|
269 // ----------------------------------------------------------------------------- |
|
270 // TXmlParser::Init() |
|
271 // Inits the parser |
|
272 // ----------------------------------------------------------------------------- |
|
273 // |
|
274 void TXmlParser::Init() |
|
275 { |
|
276 iType=EElementUnknown; |
|
277 iTag.Set(KNullDesC); |
|
278 iValue.Set(KNullDesC); |
|
279 } |
|
280 |
|
281 // ----------------------------------------------------------------------------- |
|
282 // TXmlParser::GetTag(TDes& aText) const |
|
283 // Gets the tag |
|
284 // ----------------------------------------------------------------------------- |
|
285 // |
|
286 void TXmlParser::GetTag(TDes& aText) const |
|
287 { |
|
288 StrCopy(aText, iTag); |
|
289 } |
|
290 |
|
291 // ----------------------------------------------------------------------------- |
|
292 // TXmlParser::Tag() const |
|
293 // Returns a pointer to a tag |
|
294 // ----------------------------------------------------------------------------- |
|
295 // |
|
296 TPtrC TXmlParser::Tag() const |
|
297 { |
|
298 return iTag; |
|
299 } |
|
300 |
|
301 // ----------------------------------------------------------------------------- |
|
302 // TXmlParser::Type() const |
|
303 // Returns type |
|
304 // ----------------------------------------------------------------------------- |
|
305 // |
|
306 TInt TXmlParser::Type() const |
|
307 { |
|
308 return iType; |
|
309 } |
|
310 |
|
311 // ----------------------------------------------------------------------------- |
|
312 // TXmlParser::Parse() |
|
313 // Parses xml-element. |
|
314 // ----------------------------------------------------------------------------- |
|
315 // |
|
316 void TXmlParser::Parse() |
|
317 { |
|
318 iType=EElementUnknown; |
|
319 |
|
320 // this must be the first check |
|
321 if (ElementUnknown()) |
|
322 { |
|
323 return; |
|
324 } |
|
325 |
|
326 if (ElementBegin()) |
|
327 { |
|
328 return; |
|
329 } |
|
330 |
|
331 if (ElementEnd()) |
|
332 { |
|
333 return; |
|
334 } |
|
335 |
|
336 if (ElementValue()) |
|
337 { |
|
338 return; |
|
339 } |
|
340 |
|
341 if (ElementComment()) |
|
342 { |
|
343 return; |
|
344 } |
|
345 |
|
346 if (ElementDocType()) |
|
347 { |
|
348 return; |
|
349 } |
|
350 |
|
351 if (ElementVersion()) |
|
352 { |
|
353 return; |
|
354 } |
|
355 |
|
356 if (ElementEmpty()) |
|
357 { |
|
358 return; |
|
359 } |
|
360 |
|
361 if (ElementAtt()) |
|
362 { |
|
363 return; |
|
364 } |
|
365 } |
|
366 |
|
367 // ----------------------------------------------------------------------------- |
|
368 // TXmlParser::ElementUnknown() |
|
369 // Parses unknown elements |
|
370 // ----------------------------------------------------------------------------- |
|
371 // |
|
372 TBool TXmlParser::ElementUnknown() |
|
373 { |
|
374 TPtrC str=Str(); |
|
375 TInt len=str.Length(); |
|
376 const TInt KXmlElementMinLength = 3; |
|
377 if ( len < KXmlElementMinLength ) |
|
378 { |
|
379 return ETrue; // too short to be xml element |
|
380 } |
|
381 |
|
382 |
|
383 iFirst=str.Locate('<'); |
|
384 iLast=str.LocateReverse('>'); |
|
385 iSlash=str.LocateReverse('/'); |
|
386 |
|
387 if (iFirst!=0 || iLast!=len-1) |
|
388 { |
|
389 return ETrue; // first char must be "<" and last ">" |
|
390 } |
|
391 |
|
392 return EFalse; |
|
393 } |
|
394 |
|
395 // ----------------------------------------------------------------------------- |
|
396 // TXmlParser::ElementBegin() |
|
397 // Parses beginning tag (eg. <General>) |
|
398 // ----------------------------------------------------------------------------- |
|
399 // |
|
400 TBool TXmlParser::ElementBegin() |
|
401 { |
|
402 TPtrC str=Str(); |
|
403 |
|
404 if (iSlash!=KErrNotFound) |
|
405 { |
|
406 return EFalse; |
|
407 } |
|
408 |
|
409 // remove possible attributes |
|
410 TInt pos=str.Locate(' '); |
|
411 if (pos==KErrNotFound) |
|
412 { |
|
413 pos=iLast; |
|
414 } |
|
415 |
|
416 TPtrC ptr=SubStr(iFirst, pos); |
|
417 |
|
418 if (!ValidTag(ptr)) |
|
419 { |
|
420 return EFalse; |
|
421 } |
|
422 |
|
423 iType=EElementBegin; |
|
424 iTag.Set(ptr); |
|
425 |
|
426 return ETrue; |
|
427 } |
|
428 |
|
429 // ----------------------------------------------------------------------------- |
|
430 // TXmlParser::ElementEnd() |
|
431 // Parses ending tag (eg. </General>) |
|
432 // ----------------------------------------------------------------------------- |
|
433 // |
|
434 TBool TXmlParser::ElementEnd() |
|
435 { |
|
436 if (iSlash!=iFirst+1) |
|
437 { |
|
438 return EFalse; |
|
439 } |
|
440 |
|
441 TPtrC ptr=SubStr(iSlash, iLast); |
|
442 if (!ValidTag(ptr)) |
|
443 { |
|
444 return EFalse; |
|
445 } |
|
446 |
|
447 iType=EElementEnd; |
|
448 iTag.Set(ptr); |
|
449 return ETrue; |
|
450 } |
|
451 |
|
452 // ----------------------------------------------------------------------------- |
|
453 // TXmlParser::ElementValue() |
|
454 // Parses value (eg. <Language>en</Language>) |
|
455 // ----------------------------------------------------------------------------- |
|
456 // |
|
457 TBool TXmlParser::ElementValue() |
|
458 { |
|
459 TPtrC str=Str(); |
|
460 |
|
461 TInt pos1=str.Locate('>'); |
|
462 TInt pos2=str.LocateReverse('<'); |
|
463 |
|
464 if (pos1<pos2 && iSlash==pos2+1) |
|
465 { |
|
466 TPtrC ptr1=SubStr(iFirst, pos1); |
|
467 TPtrC ptr2=SubStr(iSlash, iLast); |
|
468 if (!ValidTag(ptr1) || ptr1.Compare(ptr2)!=0) |
|
469 { |
|
470 return EFalse; |
|
471 } |
|
472 |
|
473 iType=EElementValue; |
|
474 iTag.Set(ptr1); |
|
475 iValue.Set(SubStr(pos1, pos2)); |
|
476 return ETrue; |
|
477 } |
|
478 return EFalse; |
|
479 } |
|
480 |
|
481 // ----------------------------------------------------------------------------- |
|
482 // TXmlParser::ElementEmpty() |
|
483 // Parses empty element (eg. <CaseSenN/>) |
|
484 // ----------------------------------------------------------------------------- |
|
485 // |
|
486 TBool TXmlParser::ElementEmpty() |
|
487 { |
|
488 if (iSlash!=iLast-1) |
|
489 { |
|
490 return EFalse; |
|
491 } |
|
492 |
|
493 TPtrC ptr=SubStr(iFirst, iSlash); |
|
494 if (!ValidTag(ptr)) |
|
495 { |
|
496 return EFalse; |
|
497 } |
|
498 |
|
499 iType=EElementEmpty; |
|
500 iTag.Set(ptr); |
|
501 return ETrue; |
|
502 } |
|
503 |
|
504 // ----------------------------------------------------------------------------- |
|
505 // TXmlParser::ElementVersion() |
|
506 // Identifies version element ("<?xml version="1.0" ?>") |
|
507 // ----------------------------------------------------------------------------- |
|
508 // |
|
509 TBool TXmlParser::ElementVersion() |
|
510 { |
|
511 TPtrC str=Str(); |
|
512 |
|
513 if ( str.Length()>=3 && str[1]=='?' && str[2]=='x') |
|
514 { |
|
515 iType=EElementVersion; |
|
516 return ETrue; |
|
517 } |
|
518 return EFalse; |
|
519 } |
|
520 |
|
521 // ----------------------------------------------------------------------------- |
|
522 // TXmlParser::ElementDocType() |
|
523 // Identifies document type element |
|
524 // ("<!DOCTYPE Capability SYSTEM "obex-capability.dtd">"). |
|
525 // ----------------------------------------------------------------------------- |
|
526 // |
|
527 TBool TXmlParser::ElementDocType() |
|
528 { |
|
529 TPtrC str=Str(); |
|
530 const TInt KElementMinLength = 10; |
|
531 if ( str.Length() < KElementMinLength ) |
|
532 { |
|
533 return EFalse; |
|
534 } |
|
535 |
|
536 if (str[1]=='!' && str[2]=='D' && str[3]=='O' && str[4]=='C') |
|
537 { |
|
538 iType=EElementDocType; |
|
539 return ETrue; |
|
540 } |
|
541 return EFalse; |
|
542 } |
|
543 |
|
544 // ----------------------------------------------------------------------------- |
|
545 // TXmlParser::ElementComment() |
|
546 // Identifies comment element ("<!-- OBEX Capability Object -->"). |
|
547 // ----------------------------------------------------------------------------- |
|
548 // |
|
549 TBool TXmlParser::ElementComment() |
|
550 { |
|
551 TPtrC str=Str(); |
|
552 |
|
553 if (str.Length()<7) |
|
554 { |
|
555 return EFalse; |
|
556 } |
|
557 |
|
558 if (str[1]=='!' && str[2]=='-' && str[3]=='-') |
|
559 { |
|
560 iType=EElementComment; |
|
561 return ETrue; |
|
562 } |
|
563 |
|
564 return EFalse; |
|
565 } |
|
566 |
|
567 // ----------------------------------------------------------------------------- |
|
568 // TXmlParser::ElementAtt() |
|
569 // Parses attribute element (eg. <HW Version="1.5"/>) |
|
570 // Note: Attribute values cannot contain equals (=) or quotations (") |
|
571 // ----------------------------------------------------------------------------- |
|
572 // |
|
573 TBool TXmlParser::ElementAtt() |
|
574 { |
|
575 TPtrC str=Str(); |
|
576 TInt num1=Count(str, '='); |
|
577 TInt num2=Count(str, '"'); |
|
578 TInt pos=str.Locate(' '); // find end of tag |
|
579 |
|
580 if (iSlash==iLast-1 && num1>0 && num2==(2*num1) && pos!=KErrNotFound) |
|
581 { |
|
582 TPtrC ptr1=SubStr(iFirst, pos); |
|
583 if (!ValidTag(ptr1)) |
|
584 { |
|
585 return EFalse; |
|
586 } |
|
587 |
|
588 iType=EElementAtt; |
|
589 iTag.Set(ptr1); |
|
590 TPtrC ptr2=SubStr(pos, iSlash); |
|
591 iValue.Set(ptr2); |
|
592 return ETrue; |
|
593 } |
|
594 return EFalse; |
|
595 } |
|
596 |
|
597 // ----------------------------------------------------------------------------- |
|
598 // TXmlParser::Str() const |
|
599 // Locates the position of the given character |
|
600 // ----------------------------------------------------------------------------- |
|
601 // |
|
602 TPtrC TXmlParser::Str() const |
|
603 { |
|
604 return iText; |
|
605 } |
|
606 |
|
607 // ----------------------------------------------------------------------------- |
|
608 // TXmlParser::FirstNonSpace(const TDesC& aText) |
|
609 // Returns the position of the first non space character |
|
610 // ----------------------------------------------------------------------------- |
|
611 // |
|
612 TInt TXmlParser::FirstNonSpace(const TDesC& aText) |
|
613 { |
|
614 TInt len=aText.Length(); |
|
615 for (TInt i=0; i<len; i++) |
|
616 { |
|
617 TChar c=aText[i]; |
|
618 if (!c.IsSpace()) |
|
619 { |
|
620 return i; |
|
621 } |
|
622 } |
|
623 return KErrNotFound; |
|
624 } |
|
625 |
|
626 // ----------------------------------------------------------------------------- |
|
627 // TXmlParser::LastNonSpace(const TDesC& aText) |
|
628 // Returns the position of the last non space character |
|
629 // ----------------------------------------------------------------------------- |
|
630 // |
|
631 TInt TXmlParser::LastNonSpace(const TDesC& aText) |
|
632 { |
|
633 TInt last=aText.Length()-1; |
|
634 for (TInt i=last; i>=0; i--) |
|
635 { |
|
636 TChar c=aText[i]; |
|
637 if (!c.IsSpace()) |
|
638 { |
|
639 return i; |
|
640 } |
|
641 } |
|
642 return KErrNotFound; |
|
643 } |
|
644 |
|
645 // ----------------------------------------------------------------------------- |
|
646 // TXmlParser::SubStr(const TDesC& aText, TInt pos1, TInt pos2) |
|
647 // Function returns a sub-string between aPos1 and aPos2 |
|
648 // ----------------------------------------------------------------------------- |
|
649 // |
|
650 TPtrC TXmlParser::SubStr(const TDesC& aText, TInt pos1, TInt pos2) |
|
651 { |
|
652 if (pos1<0 || pos2>=aText.Length() || pos1>=pos2) |
|
653 { |
|
654 return TPtrC(); // return empty descriptor |
|
655 } |
|
656 |
|
657 TInt len=pos2-pos1; |
|
658 return aText.Mid(pos1+1, len-1); |
|
659 } |
|
660 |
|
661 // ----------------------------------------------------------------------------- |
|
662 // TXmlParser::SubStr(TInt pos1, TInt pos2) |
|
663 // Function returns a sub-string between aPos1 and aPos2 |
|
664 // ----------------------------------------------------------------------------- |
|
665 // |
|
666 TPtrC TXmlParser::SubStr(TInt pos1, TInt pos2) |
|
667 { |
|
668 TPtrC str=Str(); |
|
669 return SubStr(str, pos1, pos2); |
|
670 } |
|
671 |
|
672 // ----------------------------------------------------------------------------- |
|
673 // TXmlParser::StrCopy(TDes& aTarget, const TDesC& aSource) |
|
674 // Function performs string copy with length checking. |
|
675 // ----------------------------------------------------------------------------- |
|
676 // |
|
677 void TXmlParser::StrCopy(TDes& aTarget, const TDesC& aSource) |
|
678 { |
|
679 TInt len=aTarget.MaxLength(); |
|
680 if(len<aSource.Length()) |
|
681 { |
|
682 aTarget.Copy(aSource.Left(len)); |
|
683 } |
|
684 else |
|
685 { |
|
686 aTarget.Copy(aSource); |
|
687 } |
|
688 } |
|
689 |
|
690 // ----------------------------------------------------------------------------- |
|
691 // TXmlParser::Count(const TDesC& aText, const TChar aChar) |
|
692 // Returns the number of characters (aChar) |
|
693 // ----------------------------------------------------------------------------- |
|
694 // |
|
695 TInt TXmlParser::Count(const TDesC& aText, const TChar aChar) |
|
696 { |
|
697 TInt count=0; |
|
698 TInt len=aText.Length(); |
|
699 |
|
700 for (TInt i=0; i<len; i++) |
|
701 { |
|
702 TChar ch=aText[i]; |
|
703 if (ch == aChar) |
|
704 { |
|
705 count++; |
|
706 } |
|
707 } |
|
708 return count; |
|
709 } |
|
710 |
|
711 // ----------------------------------------------------------------------------- |
|
712 // TXmlParser::TrimOriginal() |
|
713 // Trimmer function |
|
714 // ----------------------------------------------------------------------------- |
|
715 // |
|
716 void TXmlParser::TrimOriginal() |
|
717 { |
|
718 // trim iOriginal and place into iText |
|
719 iText.Set(KNullDesC); |
|
720 TInt first=FirstNonSpace(iOriginal); |
|
721 TInt last=LastNonSpace(iOriginal); |
|
722 if(first!=KErrNotFound && last!=KErrNotFound && last>first+1) |
|
723 { |
|
724 TPtrC ptr=iOriginal.Mid(first, last-first+1); |
|
725 iText.Set(ptr); |
|
726 } |
|
727 } |
|
728 |
|
729 // ----------------------------------------------------------------------------- |
|
730 // TXmlParser::ValidChar(const TChar aChar) |
|
731 // Validates the character |
|
732 // ----------------------------------------------------------------------------- |
|
733 // |
|
734 TBool TXmlParser::ValidChar(const TChar aChar) |
|
735 { |
|
736 if (aChar >= 'A' && aChar <= 'Z') |
|
737 { |
|
738 return ETrue; |
|
739 } |
|
740 if (aChar >= 'a' && aChar <= 'z') |
|
741 { |
|
742 return ETrue; |
|
743 } |
|
744 |
|
745 return EFalse; |
|
746 } |
|
747 |
|
748 // ----------------------------------------------------------------------------- |
|
749 // TXmlParser::ValidTag(const TDesC& aText) |
|
750 // Validates the tag |
|
751 // ----------------------------------------------------------------------------- |
|
752 // |
|
753 TBool TXmlParser::ValidTag(const TDesC& aText) |
|
754 { |
|
755 TInt len=aText.Length(); |
|
756 for (TInt i=0; i<len; i++) |
|
757 { |
|
758 if (!ValidChar(aText[i])) |
|
759 { |
|
760 return EFalse; |
|
761 } |
|
762 } |
|
763 return ETrue; |
|
764 } |
|
765 |
|
766 // End of file |