|
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 // |
|
15 |
|
16 #include <e32std.h> |
|
17 #include <ecom/implementationproxy.h> |
|
18 |
|
19 #include <xml/contenthandler.h> |
|
20 #include <xml/stringdictionarycollection.h> |
|
21 |
|
22 #include "xmlparserplugin.h" |
|
23 |
|
24 using namespace Xml; |
|
25 |
|
26 const TInt KExpatBufferSize = 2048; |
|
27 |
|
28 MParser* CXmlParserPlugin::EcomNewL(TAny* aInitParams) |
|
29 { |
|
30 // The cast to the MParser interface is a particular to the Xml Framework. Ecom plug-ins usually |
|
31 // return the pointer to the C class. |
|
32 return static_cast<MParser*>(CXmlParserPlugin::NewL(aInitParams)); |
|
33 } |
|
34 |
|
35 /* |
|
36 CXmlParserPlugin::NewL is for testing and can only be accessed by linking directly to XmlParser.o |
|
37 |
|
38 aDebugFailCount is used to configure parser heap failure during construction testing - the |
|
39 usual heap failure macros do not work for our purposes here as it is a private heap. See |
|
40 CExpat for details. |
|
41 */ |
|
42 CXmlParserPlugin* CXmlParserPlugin::NewL(TAny* aInitParams, TInt aDebugFailCount) |
|
43 { |
|
44 TParserInitParams* p = reinterpret_cast<TParserInitParams*>(aInitParams); |
|
45 |
|
46 CXmlParserPlugin* self = new(ELeave) CXmlParserPlugin(p->iContentHandler); |
|
47 |
|
48 CleanupStack::PushL(self); |
|
49 self->ConstructL(p->iStringDictionaryCollection, p->iCharSetConverter, p->iElementStack, aDebugFailCount); |
|
50 CleanupStack::Pop(self); |
|
51 |
|
52 return self; |
|
53 } |
|
54 |
|
55 CXmlParserPlugin::CXmlParserPlugin(MContentHandler* aContentHandler) : |
|
56 iContentHandler(*aContentHandler) |
|
57 { |
|
58 } |
|
59 |
|
60 void CXmlParserPlugin::ConstructL(RStringDictionaryCollection* aStringDictionaryCollection, CCharSetConverter* aCharSetConverter, |
|
61 RElementStack* aElementStack, TInt aDebugFailCount) |
|
62 { |
|
63 iExpat = CExpat::NewL(iContentHandler, aStringDictionaryCollection->StringPool(), *aCharSetConverter, *aElementStack, aDebugFailCount); |
|
64 } |
|
65 |
|
66 /* |
|
67 Used by framework to delete this object |
|
68 */ |
|
69 void CXmlParserPlugin::Release() |
|
70 { |
|
71 delete this; |
|
72 } |
|
73 |
|
74 /* |
|
75 Private destructor - only accessible from Release method |
|
76 */ |
|
77 CXmlParserPlugin::~CXmlParserPlugin() |
|
78 { |
|
79 delete iExpat; |
|
80 } |
|
81 |
|
82 |
|
83 TInt CXmlParserPlugin::EnableFeature(TInt aParseMode) |
|
84 { |
|
85 return iExpat->EnableFeature(aParseMode); |
|
86 } |
|
87 |
|
88 TInt CXmlParserPlugin::DisableFeature(TInt aParseMode) |
|
89 { |
|
90 return iExpat->DisableFeature(aParseMode); |
|
91 } |
|
92 |
|
93 TBool CXmlParserPlugin::IsFeatureEnabled(TInt aParseMode) const |
|
94 { |
|
95 return iExpat->IsFeatureEnabled(aParseMode); |
|
96 } |
|
97 |
|
98 |
|
99 void CXmlParserPlugin::ParseChunkL(const TDesC8& aChunk) |
|
100 { |
|
101 // If a Reset failed, iExpatResetError will have been set. Here we report it by leaving with the error, |
|
102 // which will in turn be passed to MContentHandler::OnError. |
|
103 User::LeaveIfError(iExpatResetError); |
|
104 ParseChunkInPartsL(aChunk); |
|
105 } |
|
106 |
|
107 void CXmlParserPlugin::ParseLastChunkL(const TDesC8& aFinalChunk) |
|
108 { |
|
109 // If a Reset failed, iExpatResetError will have been set. Here we report it by leaving with the error, |
|
110 // which will in turn be passed to MContentHandler::OnError. |
|
111 User::LeaveIfError(iExpatResetError); |
|
112 |
|
113 // Could be that there is some enourmous piece of data here. |
|
114 ParseChunkInPartsL(aFinalChunk); |
|
115 |
|
116 // We need a buffer even though we have nothing to parse, |
|
117 // as any previous buffers are destroyed. |
|
118 // We are just going through the sequence of completing the parsing gracefully. |
|
119 TDes8& buf = iExpat->GetBufferL(1); |
|
120 buf = _L8(""); |
|
121 iExpat->ParseLastL(); |
|
122 |
|
123 // TRAP doesn't set its result variable unless a leave occurs - so reset it first. |
|
124 iExpatResetError = KErrNone; |
|
125 |
|
126 // The Reset interface of the Xml Framework provides no mechanism for failure reporting. If iExpat.ResetL |
|
127 // fails we set iExpatResetError. The error will be reported to MContentHandler::OnError when an attempt |
|
128 // is made by the client to begin parsing (see RunL). |
|
129 TRAP(iExpatResetError, iExpat->ResetL()); |
|
130 } |
|
131 |
|
132 void CXmlParserPlugin::ParseChunkInPartsL(const TDesC8& aChunk) |
|
133 { |
|
134 // The Expat parser has a buffer of 64k that it stores its state variables in |
|
135 // and allocates its heap memory from. |
|
136 // We do this because we may need to upgrade the version of Expat later and this |
|
137 // makes it easier. |
|
138 // Beacuse of this we copy the buffer that could have a performance issue, |
|
139 // we will address this if it later arises. |
|
140 // We parse the descriptor in parts as we do not want to blow the buffer. |
|
141 // Even though the descriptor may already be passed in chunks they may be too big. |
|
142 |
|
143 if (aChunk.Length() == 0) |
|
144 { |
|
145 return; |
|
146 } |
|
147 |
|
148 TPtrC8 part; |
|
149 TInt offset = 0; |
|
150 TInt size = aChunk.Size(); |
|
151 TInt partSize = size; |
|
152 |
|
153 do |
|
154 { |
|
155 TDes8& buf = iExpat->GetBufferL(KExpatBufferSize); |
|
156 |
|
157 if (partSize > KExpatBufferSize) |
|
158 { |
|
159 partSize = KExpatBufferSize; |
|
160 } |
|
161 |
|
162 part.Set (aChunk.Mid(offset, partSize)); |
|
163 |
|
164 buf = part; |
|
165 iExpat->ParseL(); |
|
166 |
|
167 // The remaining part stats. |
|
168 offset += part.Length(); |
|
169 partSize = size - offset; |
|
170 |
|
171 } while (partSize); |
|
172 } |
|
173 |
|
174 void CXmlParserPlugin::SetContentSink(class MContentHandler &aContentSink) |
|
175 { |
|
176 iExpat->SetContentSink(aContentSink); |
|
177 } |
|
178 |
|
179 RHeap* CXmlParserPlugin::GetInternalHeap() const |
|
180 { |
|
181 return iExpat->GetInternalHeap(); |
|
182 } |
|
183 |
|
184 // |
|
185 // Ecom implementation infrastructure |
|
186 // |
|
187 |
|
188 // KUidXmlParserPlugin is the unique UID for our implementation of the MParser interface. This interface is |
|
189 // 'published' as KParserInterfaceUid defined in Xml/FrameworkConstants.h. KUidXmlParserPlugin must be |
|
190 // the same as the value of implementation_uid in the file 101faa0a.rss |
|
191 const TInt KUidXmlParserPlugin = 0x101FAA0C; |
|
192 |
|
193 const TImplementationProxy ImplementationTable[] = { |
|
194 IMPLEMENTATION_PROXY_ENTRY(KUidXmlParserPlugin, CXmlParserPlugin::EcomNewL) |
|
195 }; |
|
196 |
|
197 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount) |
|
198 { |
|
199 aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy); |
|
200 |
|
201 return ImplementationTable; |
|
202 } |
|
203 |