|
1 /* |
|
2 * Copyright (c) 2005-2007 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: OMA DS Folder object XML parser |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <SenXmlUtils.h> |
|
20 #include "OMADSFolderObject.h" |
|
21 #include "utf.h" |
|
22 #include "Logger.h" |
|
23 |
|
24 _LIT8(KOMADSFolderElement, "Folder"); |
|
25 _LIT8(KOMADSNameElement, "name"); |
|
26 _LIT8(KOMADSCreatedElement, "created"); |
|
27 _LIT8(KOMADSModifiedElement, "modified"); |
|
28 |
|
29 _LIT(KFolderBeginTag, "<Folder>"); |
|
30 _LIT(KFolderEndTag, "</Folder>"); |
|
31 _LIT(KNameBeginTag, "<name>"); |
|
32 _LIT(KNameEndTag, "</name>"); |
|
33 _LIT(KCreatedBeginTag, "<created>"); |
|
34 _LIT(KCreatedEndTag, "</created>"); |
|
35 _LIT(KModifiedBeginTag, "<modified>"); |
|
36 _LIT(KModifiedEndTag, "</modified>"); |
|
37 _LIT(KDateFormat, "%04d%02d%02dT%02d%02d%02d"); |
|
38 |
|
39 |
|
40 COMADSFolderObject* COMADSFolderObject::NewLC() |
|
41 { |
|
42 COMADSFolderObject* pSelf = new (ELeave) COMADSFolderObject; |
|
43 CleanupStack::PushL( pSelf ); |
|
44 pSelf->ConstructL(); |
|
45 return pSelf; |
|
46 } |
|
47 |
|
48 COMADSFolderObject* COMADSFolderObject::NewL() |
|
49 { |
|
50 COMADSFolderObject* pSelf = COMADSFolderObject::NewLC(); |
|
51 CleanupStack::Pop( pSelf ); |
|
52 return pSelf; |
|
53 } |
|
54 |
|
55 COMADSFolderObject::~COMADSFolderObject() |
|
56 { |
|
57 delete iXmlReader; |
|
58 } |
|
59 |
|
60 void COMADSFolderObject::ConstructL() |
|
61 { |
|
62 iXmlReader = CSenXmlReader::NewL(); |
|
63 iXmlReader->SetContentHandler(*this); |
|
64 } |
|
65 |
|
66 TInt COMADSFolderObject::ImportFolderXml( RFs& aFs, const TDesC& aFilename ) |
|
67 { |
|
68 LOGGER_ENTERFN( "COMADSFolderObject::ImportFolderXml" ); |
|
69 |
|
70 TInt error; |
|
71 |
|
72 Reset(); |
|
73 TRAP( error, iXmlReader->ParseL( aFs, aFilename ) ) |
|
74 if( error != KErrNone ) |
|
75 { |
|
76 LOGGER_LEAVEFN( "COMADSFolderObject::ImportFolderXml" ); |
|
77 return error; |
|
78 } |
|
79 |
|
80 LOGGER_LEAVEFN( "COMADSFolderObject::ImportFolderXml" ); |
|
81 return iError; |
|
82 } |
|
83 |
|
84 TInt COMADSFolderObject::ImportFolderXml( const TDesC8& aData ) |
|
85 { |
|
86 LOGGER_ENTERFN( "COMADSFolderObject::ImportFolderXml" ); |
|
87 LOG(aData); |
|
88 |
|
89 TInt error; |
|
90 |
|
91 Reset(); |
|
92 |
|
93 TRAP(error, iXmlReader->ParseL(aData) ); |
|
94 if(error != KErrNone ) |
|
95 { |
|
96 LOGGER_LEAVEFN( "COMADSFolderObject::ImportFolderXml" ); |
|
97 return error; |
|
98 } |
|
99 LOGGER_LEAVEFN( "COMADSFolderObject::ImportFolderXml" ); |
|
100 return iError; |
|
101 } |
|
102 |
|
103 void COMADSFolderObject::ExportFolderXmlL(CBufBase& aBuffer) |
|
104 { |
|
105 iDesc = NULL; |
|
106 iBuffer = &aBuffer; |
|
107 iWriteBufPosition = 0; |
|
108 iWriteBufSize = aBuffer.Size(); |
|
109 |
|
110 ExportL(); |
|
111 } |
|
112 |
|
113 void COMADSFolderObject::ExportFolderXmlL(TDes8& aBuffer) |
|
114 { |
|
115 iDesc = &aBuffer; |
|
116 iBuffer = NULL; |
|
117 |
|
118 ExportL(); |
|
119 } |
|
120 |
|
121 void COMADSFolderObject::ExportL() |
|
122 { |
|
123 // Folder |
|
124 WriteL(KFolderBeginTag); |
|
125 |
|
126 // Name |
|
127 WriteL(KNameBeginTag); |
|
128 WriteEncodedXmlL(iName); |
|
129 WriteL(KNameEndTag); |
|
130 |
|
131 // Created Date |
|
132 TBuf<32> tempdate; |
|
133 tempdate.Format(KDateFormat, iCreatedDate.Year(), iCreatedDate.Month()+1, iCreatedDate.Day(), |
|
134 iCreatedDate.Hour(), iCreatedDate.Minute(), iCreatedDate.Second()); |
|
135 WriteL(KCreatedBeginTag); |
|
136 WriteL(tempdate); |
|
137 WriteL(KCreatedEndTag); |
|
138 |
|
139 // Modified Date |
|
140 tempdate.Format(KDateFormat, iModifiedDate.Year(), iModifiedDate.Month()+1, iModifiedDate.Day(), |
|
141 iModifiedDate.Hour(), iModifiedDate.Minute(), iModifiedDate.Second()); |
|
142 WriteL(KModifiedBeginTag); |
|
143 WriteL(tempdate); |
|
144 WriteL(KModifiedEndTag); |
|
145 |
|
146 // Folder end |
|
147 WriteL(KFolderEndTag); |
|
148 } |
|
149 |
|
150 void COMADSFolderObject::WriteL( const TDesC &aData ) |
|
151 { |
|
152 CnvUtfConverter converter; |
|
153 User::LeaveIfError( converter.ConvertFromUnicodeToUtf8( iTemp, aData ) ); |
|
154 |
|
155 if ( iBuffer ) |
|
156 { |
|
157 TInt newPosition = iWriteBufPosition + iTemp.Length(); |
|
158 |
|
159 if ( newPosition > iWriteBufSize ) |
|
160 { |
|
161 TInt expand = newPosition - iWriteBufSize; |
|
162 iBuffer->ExpandL( iWriteBufSize, expand ); |
|
163 iWriteBufSize += expand; |
|
164 } |
|
165 |
|
166 iBuffer->Write( iWriteBufPosition, iTemp ); |
|
167 iWriteBufPosition = newPosition; |
|
168 } |
|
169 else if( iDesc ) |
|
170 { |
|
171 iDesc->Append( iTemp ); |
|
172 } |
|
173 } |
|
174 |
|
175 void COMADSFolderObject::WriteEncodedXmlL( const TDesC &aData ) |
|
176 { |
|
177 CnvUtfConverter converter; |
|
178 User::LeaveIfError( converter.ConvertFromUnicodeToUtf8( iTemp, aData ) ); |
|
179 |
|
180 HBufC8* xmlData = SenXmlUtils::EncodeHttpCharactersLC( iTemp ); |
|
181 |
|
182 if ( iBuffer ) |
|
183 { |
|
184 TInt newPosition = iWriteBufPosition + xmlData->Length(); |
|
185 |
|
186 if ( newPosition > iWriteBufSize ) |
|
187 { |
|
188 TInt expand = newPosition - iWriteBufSize; |
|
189 iBuffer->ExpandL( iWriteBufSize, expand ); |
|
190 iWriteBufSize += expand; |
|
191 } |
|
192 |
|
193 iBuffer->Write( iWriteBufPosition, *xmlData ); |
|
194 iWriteBufPosition = newPosition; |
|
195 } |
|
196 else if( iDesc ) |
|
197 { |
|
198 iDesc->Append( *xmlData ); |
|
199 } |
|
200 |
|
201 CleanupStack::PopAndDestroy( xmlData ); |
|
202 } |
|
203 |
|
204 TInt COMADSFolderObject::StartDocument() |
|
205 { |
|
206 return KErrNone; |
|
207 } |
|
208 |
|
209 TInt COMADSFolderObject::EndDocument() |
|
210 { |
|
211 return KErrNone; |
|
212 } |
|
213 |
|
214 TInt COMADSFolderObject::StartElement(const TDesC8& /*aURI*/, |
|
215 const TDesC8& /*aLocalName*/, |
|
216 const TDesC8& aName, |
|
217 const RAttributeArray& /*apAttrs*/) |
|
218 { |
|
219 LOGGER_ENTERFN( "COMADSFolderObject::StartElement" ); |
|
220 iCurrentElement.Copy( aName ); |
|
221 LOGGER_LEAVEFN( "COMADSFolderObject::StartElement" ); |
|
222 return KErrNone; |
|
223 } |
|
224 |
|
225 TInt COMADSFolderObject::EndElement(const TDesC8& /*aURI*/, |
|
226 const TDesC8& /*aLocalName*/, |
|
227 const TDesC8& /*aName*/) |
|
228 { |
|
229 LOGGER_ENTERFN( "COMADSFolderObject::EndElement" ); |
|
230 iCurrentElement.SetLength(0); |
|
231 LOGGER_LEAVEFN( "COMADSFolderObject::EndElement" ); |
|
232 return KErrNone; |
|
233 } |
|
234 |
|
235 TInt COMADSFolderObject::Characters( const TDesC8& aBuf, TInt aStart, TInt aLength ) |
|
236 { |
|
237 LOGGER_ENTERFN( "COMADSFolderObject::Characters" ); |
|
238 LOG(aBuf); |
|
239 |
|
240 TInt error; |
|
241 |
|
242 if( iCurrentElement.Compare( KOMADSFolderElement ) == 0 ) |
|
243 { |
|
244 LOGGER_LEAVEFN( "COMADSFolderObject::Characters" ); |
|
245 return KErrNone; |
|
246 } |
|
247 |
|
248 else if ( iCurrentElement.Compare( KOMADSNameElement ) == 0 ) |
|
249 { |
|
250 CnvUtfConverter converter; |
|
251 TBuf<KMaxFolderNameLength> buf; |
|
252 error = converter.ConvertToUnicodeFromUtf8( buf, aBuf.Mid( aStart, aLength ) ); |
|
253 TInt length = buf.Length() + iName.Length(); |
|
254 |
|
255 if ( error > 0 ) |
|
256 { |
|
257 LOGGER_WRITE_1("Too long name, number of uncorverted bytes: %d", error); |
|
258 } |
|
259 else if ( error != KErrNone ) |
|
260 { |
|
261 LOGGER_WRITE_1("ConvertToUnicodeFromUtf8 failed with %d", error); |
|
262 iError = error; |
|
263 } |
|
264 else if ( length > iName.MaxLength() ) |
|
265 { |
|
266 LOGGER_WRITE_1("Too long name total: %d", length); |
|
267 } |
|
268 else |
|
269 { |
|
270 iName.Append( buf ); |
|
271 } |
|
272 } |
|
273 else if ( iCurrentElement.Compare( KOMADSCreatedElement ) == 0 ) |
|
274 { |
|
275 error = ParseDateString( aBuf.Mid( aStart, aLength ), iCreatedDate ); |
|
276 if( error != KErrNone ) |
|
277 { |
|
278 iError = error; |
|
279 } |
|
280 } |
|
281 else if ( iCurrentElement.Compare( KOMADSModifiedElement ) == 0 ) |
|
282 { |
|
283 error = ParseDateString( aBuf.Mid( aStart, aLength ), iModifiedDate ); |
|
284 if ( error != KErrNone ) |
|
285 { |
|
286 iError = error; |
|
287 } |
|
288 } |
|
289 LOGGER_LEAVEFN( "COMADSFolderObject::Characters" ); |
|
290 return KErrNone; |
|
291 } |
|
292 |
|
293 TInt COMADSFolderObject::ParseDateString(const TDesC8& aString, TDateTime& aDateTime) |
|
294 { |
|
295 // Ensure we don't read beyond the buffer limits |
|
296 if(aString.Length() < 15) |
|
297 return KErrArgument; |
|
298 |
|
299 // Extract the fields from the string |
|
300 TLex8 yearDesc(aString.Mid(0, 4)); |
|
301 TLex8 monthDesc(aString.Mid(4, 2)); |
|
302 TLex8 dayDesc(aString.Mid(6, 2)); |
|
303 // Skip one character here, it's supposed to be 'T' |
|
304 TLex8 hourDesc(aString.Mid(9, 2)); |
|
305 TLex8 minuteDesc(aString.Mid(11, 2)); |
|
306 TLex8 secondDesc(aString.Mid(13, 2)); |
|
307 |
|
308 TInt year, month, day, hour, minute, second; |
|
309 TInt error; |
|
310 |
|
311 // Fetch the values to temporary variables |
|
312 if((error = yearDesc.Val(year)) != KErrNone) |
|
313 return error; |
|
314 if((error = monthDesc.Val(month)) != KErrNone) |
|
315 return error; |
|
316 if((error = dayDesc.Val(day)) != KErrNone) |
|
317 return error; |
|
318 if((error = hourDesc.Val(hour)) != KErrNone) |
|
319 return error; |
|
320 if((error = minuteDesc.Val(minute)) != KErrNone) |
|
321 return error; |
|
322 if((error = secondDesc.Val(second)) != KErrNone) |
|
323 return error; |
|
324 |
|
325 // Assign values to datetime object |
|
326 if((error = aDateTime.SetYear(year)) != KErrNone) |
|
327 return error; |
|
328 if((error = aDateTime.SetMonth((TMonth)(month-1))) != KErrNone) |
|
329 return error; |
|
330 if((error = aDateTime.SetDay(day)) != KErrNone) |
|
331 return error; |
|
332 if((error = aDateTime.SetHour(hour)) != KErrNone) |
|
333 return error; |
|
334 if((error = aDateTime.SetMinute(minute)) != KErrNone) |
|
335 return error; |
|
336 if((error = aDateTime.SetSecond(second)) != KErrNone) |
|
337 return error; |
|
338 |
|
339 return KErrNone; |
|
340 } |
|
341 |
|
342 void COMADSFolderObject::Reset() |
|
343 { |
|
344 iError = KErrNone; |
|
345 iName.SetLength(0); |
|
346 iCreatedDate.Set(2005, EJanuary, 1, 0, 0, 0, 0); |
|
347 iModifiedDate.Set(2005, EJanuary, 1, 0, 0, 0, 0); |
|
348 } |
|
349 |
|
350 TInt COMADSFolderObject::Error(TInt aErrorCode) |
|
351 { |
|
352 iError = aErrorCode; |
|
353 return KErrNone; |
|
354 } |
|
355 |