|
1 /* |
|
2 * Copyright (c) 2002 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: XML generator |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // ------------------------------------------------------------------------------------------------ |
|
20 // Includes |
|
21 // ------------------------------------------------------------------------------------------------ |
|
22 #include "WBXMLDefs.h" |
|
23 #include "XMLGenerator.h" |
|
24 #include "XMLStaticWorkspace.h" |
|
25 #include "XMLDynamicWorkspace.h" |
|
26 #include "WBXMLGeneratorError.h" |
|
27 |
|
28 _LIT8(KWBXMLTagStart, "<"); |
|
29 _LIT8(KWBXMLTagStartEndTag, "</"); |
|
30 _LIT8(KWBXMLTagEndNoContent, "/>"); |
|
31 _LIT8(KWBXMLTagEnd, ">"); |
|
32 _LIT8(KWBXMLElemenentSeparator, "|"); |
|
33 _LIT8(KNamespaceBegin, " xmlns='"); |
|
34 _LIT8(KNamespaceEnd, "'"); |
|
35 _LIT8(KIndent, " "); |
|
36 _LIT8(KNewLine, "\r\n"); |
|
37 |
|
38 // ------------------------------------------------------------------------------------------------ |
|
39 // CXMLGenerator |
|
40 // ------------------------------------------------------------------------------------------------ |
|
41 EXPORT_C TPtrC8 CXMLGenerator::Document() |
|
42 { |
|
43 return Workspace()->Buffer(); |
|
44 } |
|
45 |
|
46 // ------------------------------------------------------------------------------------------------ |
|
47 EXPORT_C void CXMLGenerator::Reset() |
|
48 { |
|
49 iElemStack.Reset(); |
|
50 if( iOwnsWorkspace ) |
|
51 { |
|
52 Workspace()->Reset(); |
|
53 } |
|
54 iNSAppended = EFalse; |
|
55 } |
|
56 |
|
57 // ------------------------------------------------------------------------------------------------ |
|
58 EXPORT_C void CXMLGenerator::CreateStaticWorkspaceL( TInt aBufferSize ) |
|
59 { |
|
60 FreeWorkspace(); |
|
61 iWorkspace = CXMLStaticWorkspace::NewL(aBufferSize); |
|
62 iOwnsWorkspace = ETrue; |
|
63 } |
|
64 |
|
65 // ------------------------------------------------------------------------------------------------ |
|
66 EXPORT_C void CXMLGenerator::CreateDynamicWorkspaceL() |
|
67 { |
|
68 FreeWorkspace(); |
|
69 iWorkspace = CXMLDynamicWorkspace::NewL(); |
|
70 iOwnsWorkspace = ETrue; |
|
71 } |
|
72 |
|
73 // ------------------------------------------------------------------------------------------------ |
|
74 EXPORT_C CXMLWorkspace* CXMLGenerator::Workspace() const |
|
75 { |
|
76 return iWorkspace; |
|
77 } |
|
78 |
|
79 // ------------------------------------------------------------------------------------------------ |
|
80 EXPORT_C void CXMLGenerator::SetWorkspace( CXMLWorkspace* aBuffer, TBool aTakeOwnership ) |
|
81 { |
|
82 FreeWorkspace(); |
|
83 iOwnsWorkspace = aTakeOwnership; |
|
84 iWorkspace = aBuffer; |
|
85 } |
|
86 |
|
87 // ------------------------------------------------------------------------------------------------ |
|
88 void CXMLGenerator::FreeWorkspace() |
|
89 { |
|
90 if( iOwnsWorkspace ) |
|
91 { |
|
92 delete iWorkspace; |
|
93 iWorkspace = 0; |
|
94 } |
|
95 } |
|
96 |
|
97 // ------------------------------------------------------------------------------------------------ |
|
98 EXPORT_C CXMLGenerator::CXMLGenerator() |
|
99 { |
|
100 } |
|
101 |
|
102 // ------------------------------------------------------------------------------------------------ |
|
103 EXPORT_C CXMLGenerator::~CXMLGenerator() |
|
104 { |
|
105 delete iTable; |
|
106 delete iNSName; |
|
107 FreeWorkspace(); |
|
108 iElemStack.Reset(); |
|
109 } |
|
110 |
|
111 // ------------------------------------------------------------------------------------------------ |
|
112 EXPORT_C void CXMLGenerator::BeginDocumentL( TUint8 /*aVersion*/, TInt32 /*aPublicId*/, TUint32 /*aCharset*/, const TDesC8& /*aStringTbl*/ ) |
|
113 { |
|
114 } |
|
115 |
|
116 // ------------------------------------------------------------------------------------------------ |
|
117 EXPORT_C void CXMLGenerator::BeginElementL( TUint8 aElement, TBool aHasContent, TBool aHasAttributes ) |
|
118 { |
|
119 TBool addToElemStack(ETrue); |
|
120 IndentL(); |
|
121 Workspace()->WriteL(KWBXMLTagStart()); |
|
122 |
|
123 Workspace()->WriteL(TranslateElement(aElement)); |
|
124 |
|
125 if( ((iNSAppendType == CXMLGenerator::EAppendToFirstElementOnly) && !(iNSAppended)) || (iNSAppendType == CXMLGenerator::EAppendToEveryElement) ) |
|
126 { |
|
127 Workspace()->WriteL(KNamespaceBegin()); |
|
128 Workspace()->WriteL(*iNSName); |
|
129 Workspace()->WriteL(KNamespaceEnd()); |
|
130 iNSAppended = ETrue; |
|
131 } |
|
132 |
|
133 if( aHasAttributes ) |
|
134 { |
|
135 // attributes |
|
136 } |
|
137 |
|
138 if( aHasContent ) |
|
139 { |
|
140 Workspace()->WriteL(KWBXMLTagEnd()); |
|
141 } |
|
142 else |
|
143 { |
|
144 Workspace()->WriteL(KWBXMLTagEndNoContent()); |
|
145 addToElemStack = EFalse; |
|
146 } |
|
147 |
|
148 if( !iDontNewLine ) |
|
149 { |
|
150 Workspace()->WriteL(KNewLine()); |
|
151 } |
|
152 iDontNewLine = EFalse; |
|
153 |
|
154 if( addToElemStack ) |
|
155 { |
|
156 iElemStack.Insert(aElement, 0); |
|
157 } |
|
158 } |
|
159 |
|
160 // ------------------------------------------------------------------------------------------------ |
|
161 EXPORT_C void CXMLGenerator::SetTranslateTableL( const TDesC8& aTable ) |
|
162 { |
|
163 delete iTable; |
|
164 iTable = 0; |
|
165 iTable = aTable.AllocL(); |
|
166 } |
|
167 |
|
168 // ------------------------------------------------------------------------------------------------ |
|
169 EXPORT_C void CXMLGenerator::SetNamespaceNameL( const TDesC8& aName, TNSmlNamespaceAppendType aAppendType ) |
|
170 { |
|
171 iNSAppendType = aAppendType; |
|
172 delete iNSName; |
|
173 iNSName = 0; |
|
174 iNSName = aName.AllocL(); |
|
175 } |
|
176 |
|
177 // ------------------------------------------------------------------------------------------------ |
|
178 EXPORT_C void CXMLGenerator::EndElementL() |
|
179 { |
|
180 TUint8 elem = iElemStack[0]; |
|
181 iElemStack.Remove(0); |
|
182 if( !iDontIndent ) |
|
183 { |
|
184 IndentL(); |
|
185 } |
|
186 iDontIndent = EFalse; |
|
187 Workspace()->WriteL(KWBXMLTagStartEndTag()); |
|
188 Workspace()->WriteL(TranslateElement(elem)); |
|
189 Workspace()->WriteL(KWBXMLTagEnd()); |
|
190 Workspace()->WriteL(KNewLine()); |
|
191 } |
|
192 |
|
193 // ------------------------------------------------------------------------------------------------ |
|
194 EXPORT_C void CXMLGenerator::AddElementL( TUint8 aElement, const TDesC8& aContent, const TXMLContentFormat aFormat ) |
|
195 { |
|
196 iDontNewLine = ETrue; |
|
197 BeginElementL(aElement, ETrue); |
|
198 if( aFormat == EXMLContentFormatOpaque ) |
|
199 { |
|
200 WriteOpaqueDataL(aContent); |
|
201 } |
|
202 else |
|
203 { |
|
204 WriteInlineStringL(aContent); |
|
205 } |
|
206 iDontIndent = ETrue; |
|
207 EndElementL(); |
|
208 } |
|
209 |
|
210 // ------------------------------------------------------------------------------------------------ |
|
211 EXPORT_C void CXMLGenerator::AddElementL( TUint8 /*aElement*/, const TDesC8& /*aContent*/, const CWBXMLAttributes& /*aAttributes*/, const TXMLContentFormat /*aFormat*/ ) |
|
212 { |
|
213 } |
|
214 |
|
215 // ------------------------------------------------------------------------------------------------ |
|
216 EXPORT_C void CXMLGenerator::AddEntityL( TUint32 /*aEntity*/ ) |
|
217 { |
|
218 } |
|
219 |
|
220 // ------------------------------------------------------------------------------------------------ |
|
221 EXPORT_C void CXMLGenerator::AddExt_IL( TInt /*aExtNum*/, const TDesC8& /*aContent*/ ) |
|
222 { |
|
223 } |
|
224 |
|
225 // ------------------------------------------------------------------------------------------------ |
|
226 EXPORT_C void CXMLGenerator::AddExt_TL( TInt /*aExtNum*/, TUint32 /*aContent*/ ) |
|
227 { |
|
228 } |
|
229 |
|
230 // ------------------------------------------------------------------------------------------------ |
|
231 EXPORT_C void CXMLGenerator::AddExtL( TInt /*aExtNum*/ ) |
|
232 { |
|
233 } |
|
234 |
|
235 // ------------------------------------------------------------------------------------------------ |
|
236 EXPORT_C void CXMLGenerator::SwitchCodePageL( TUint8 /*aNewCodePage*/ ) |
|
237 { |
|
238 } |
|
239 |
|
240 // ------------------------------------------------------------------------------------------------ |
|
241 EXPORT_C TInt CXMLGenerator::HandleResult( TInt aResult, TInt aTreshold ) |
|
242 { |
|
243 switch( aResult ) |
|
244 { |
|
245 case KErrNone: |
|
246 if( Workspace()->FreeSize() < aTreshold ) |
|
247 { |
|
248 Workspace()->Rollback(); |
|
249 return KWBXMLGeneratorBufferFull; |
|
250 } |
|
251 Workspace()->Commit(); |
|
252 return KWBXMLGeneratorOk; |
|
253 |
|
254 case KErrTooBig: |
|
255 Workspace()->Rollback(); |
|
256 return KWBXMLGeneratorBufferFull; |
|
257 |
|
258 default: |
|
259 return KErrGeneral; |
|
260 } |
|
261 } |
|
262 |
|
263 // ------------------------------------------------------------------------------------------------ |
|
264 EXPORT_C TInt CXMLGenerator::IndentLevel() const |
|
265 { |
|
266 return iElemStack.Count(); |
|
267 } |
|
268 |
|
269 // ------------------------------------------------------------------------------------------------ |
|
270 EXPORT_C void CXMLGenerator::SetIndentLevel( TInt aLevel ) |
|
271 { |
|
272 iInitialIndentLevel = aLevel; |
|
273 } |
|
274 |
|
275 // ------------------------------------------------------------------------------------------------ |
|
276 void CXMLGenerator::WriteMUint32L( TUint32 /*aValue*/ ) |
|
277 { |
|
278 } |
|
279 |
|
280 // ------------------------------------------------------------------------------------------------ |
|
281 void CXMLGenerator::WriteOpaqueDataL( const TDesC8& aData ) |
|
282 { |
|
283 Workspace()->WriteL(aData); |
|
284 } |
|
285 |
|
286 // ------------------------------------------------------------------------------------------------ |
|
287 void CXMLGenerator::WriteInlineStringL( const TDesC8& aData ) |
|
288 { |
|
289 WriteOpaqueDataL(aData); |
|
290 } |
|
291 |
|
292 // ------------------------------------------------------------------------------------------------ |
|
293 TPtrC8 CXMLGenerator::TranslateElement( TUint8 aElement ) |
|
294 { |
|
295 TPtrC8 buf(*iTable); |
|
296 while( aElement-- ) |
|
297 { |
|
298 TInt pos = buf.Find(KWBXMLElemenentSeparator()); |
|
299 if( pos == KErrNotFound ) |
|
300 { |
|
301 return TPtrC8(); |
|
302 } |
|
303 buf.Set(buf.Right(buf.Length() - pos - 1)); |
|
304 } |
|
305 |
|
306 TInt pos = buf.Find(KWBXMLElemenentSeparator()); |
|
307 |
|
308 if( pos != KErrNotFound ) |
|
309 { |
|
310 buf.Set(buf.Left(pos)); |
|
311 } |
|
312 |
|
313 return buf; |
|
314 } |
|
315 |
|
316 // ------------------------------------------------------------------------------------------------ |
|
317 void CXMLGenerator::IndentL() |
|
318 { |
|
319 for( TInt i = 0; i < iElemStack.Count() + iInitialIndentLevel; i++ ) |
|
320 { |
|
321 Workspace()->WriteL(KIndent()); |
|
322 } |
|
323 } |