|
1 // Copyright (c) 2006-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 #include <utf.h> |
|
17 #include <s32strm.h> |
|
18 |
|
19 #include "pbapxml.h" |
|
20 |
|
21 #include "btaccesshostlog.h" |
|
22 |
|
23 |
|
24 //DTD strings (in UTF-8 encoding) |
|
25 _LIT8(KXMLVersion, "<?xml version=\"1.0\"?>\n"); |
|
26 _LIT8(KXMLDocType, "<!DOCTYPE vcard-listing SYSTEM \"vcard-listing.dtd\">\n"); |
|
27 _LIT8(KXMLBegin, "<vCard-listing version=\"1.0\">\n"); |
|
28 _LIT8(KXMLEnd, "</vCard-listing>\n"); |
|
29 _LIT8(KXMLEntryPreamble, "<card handle = \""); |
|
30 _LIT8(KXMLEntryMidamble, "\" name = \""); |
|
31 _LIT8(KXMLEntryPostamble, "\"/>\n"); |
|
32 _LIT8(KXMLHandleExtension, ".vcf"); |
|
33 _LIT8(KXMLHandleFormat, "%x"); |
|
34 |
|
35 _LIT8(KXMLEscapedQuote, """); |
|
36 _LIT8(KXMLEscapedAmpersand, "&"); |
|
37 _LIT8(KXMLEscapedLessThan, "<"); |
|
38 _LIT8(KXMLEscapedGreaterThan, ">"); |
|
39 |
|
40 |
|
41 const TInt KXMLHandleLength = 8; // handle represented as 32-bit hex string |
|
42 |
|
43 |
|
44 /*static*/ void PbapDTD::WriteBeginL(RWriteStream& aWriteStream) |
|
45 { |
|
46 LOG_STATIC_FUNC |
|
47 aWriteStream.WriteL(KXMLVersion); |
|
48 aWriteStream.WriteL(KXMLDocType); |
|
49 aWriteStream.WriteL(KXMLBegin); |
|
50 } |
|
51 |
|
52 /*static*/ void PbapDTD::WriteListingEntryL(RWriteStream& aWriteStream, TInt aHandle, const TDesC& aName) |
|
53 { |
|
54 LOG_STATIC_FUNC |
|
55 // convert handle to hexadecimal string |
|
56 TBuf8<KXMLHandleLength> handleBuf; |
|
57 handleBuf.Format(KXMLHandleFormat, aHandle); |
|
58 |
|
59 // convert formatted name to UTF-8 |
|
60 RBuf8 name(CnvUtfConverter::ConvertFromUnicodeToUtf8L(aName)); |
|
61 CleanupClosePushL(name); |
|
62 |
|
63 // escape &,<,>, and " characters in name |
|
64 EscapeAttributeValueL(name); |
|
65 |
|
66 // write to the stream |
|
67 aWriteStream.WriteL(KXMLEntryPreamble); |
|
68 aWriteStream.WriteL(handleBuf); |
|
69 aWriteStream.WriteL(KXMLHandleExtension); |
|
70 aWriteStream.WriteL(KXMLEntryMidamble); |
|
71 aWriteStream.WriteL(name); |
|
72 aWriteStream.WriteL(KXMLEntryPostamble); |
|
73 |
|
74 CleanupStack::PopAndDestroy(); //name |
|
75 } |
|
76 |
|
77 |
|
78 /*static*/ void PbapDTD::WriteEndL(RWriteStream& aWriteStream) |
|
79 { |
|
80 LOG_STATIC_FUNC |
|
81 aWriteStream.WriteL(KXMLEnd); |
|
82 } |
|
83 |
|
84 /** |
|
85 Add XML escaping to attribute value. Replaces " with ", & with &, |
|
86 < with < and > with > (attribute values always surrounded by quotes |
|
87 so no need to escape apostrophes) |
|
88 */ |
|
89 /*static*/ void PbapDTD::EscapeAttributeValueL(RBuf8& aValue) |
|
90 { |
|
91 LOG_STATIC_FUNC |
|
92 // iterate through descriptor adding escaping if necessary. The descriptor has |
|
93 // to be resized each time an escape sequence is added, however these characters |
|
94 // are unlikely to appear in the attribute value |
|
95 for (TInt ii = 0; ii < aValue.Length(); ++ii) |
|
96 { |
|
97 switch (aValue[ii]) |
|
98 { |
|
99 case '\"': |
|
100 { |
|
101 aValue.ReAllocL(aValue.Length() + KXMLEscapedQuote().Length()); |
|
102 aValue.Replace(ii, 1, KXMLEscapedQuote); |
|
103 } |
|
104 break; |
|
105 case '&': |
|
106 { |
|
107 aValue.ReAllocL(aValue.Length() + KXMLEscapedAmpersand().Length()); |
|
108 aValue.Replace(ii, 1, KXMLEscapedAmpersand); |
|
109 } |
|
110 break; |
|
111 case '<': |
|
112 { |
|
113 aValue.ReAllocL(aValue.Length() + KXMLEscapedLessThan().Length()); |
|
114 aValue.Replace(ii, 1, KXMLEscapedLessThan); |
|
115 } |
|
116 break; |
|
117 case '>': |
|
118 { |
|
119 aValue.ReAllocL(aValue.Length() + KXMLEscapedGreaterThan().Length()); |
|
120 aValue.Replace(ii, 1, KXMLEscapedGreaterThan); |
|
121 } |
|
122 break; |
|
123 default: |
|
124 break; |
|
125 } |
|
126 } |
|
127 } |
|
128 |