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 <utf.h> |
|
21 #include "omadsfolderobject.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, |
|
134 iCreatedDate.Day(), iCreatedDate.Hour(), iCreatedDate.Minute(), iCreatedDate.Second() ); |
|
135 |
|
136 WriteL( KCreatedBeginTag ); |
|
137 WriteL( tempdate ); |
|
138 WriteL( KCreatedEndTag ); |
|
139 |
|
140 // Modified Date |
|
141 tempdate.Format( KDateFormat, iModifiedDate.Year(), iModifiedDate.Month() + 1, |
|
142 iModifiedDate.Day(), iModifiedDate.Hour(), iModifiedDate.Minute(), iModifiedDate.Second() ); |
|
143 |
|
144 WriteL( KModifiedBeginTag ); |
|
145 WriteL( tempdate ); |
|
146 WriteL( KModifiedEndTag ); |
|
147 |
|
148 // Folder end |
|
149 WriteL(KFolderEndTag); |
|
150 } |
|
151 |
|
152 void COMADSFolderObject::WriteL( const TDesC &aData ) |
|
153 { |
|
154 CnvUtfConverter converter; |
|
155 User::LeaveIfError( converter.ConvertFromUnicodeToUtf8( iTemp, aData ) ); |
|
156 |
|
157 if ( iBuffer ) |
|
158 { |
|
159 TInt newPosition = iWriteBufPosition + iTemp.Length(); |
|
160 |
|
161 if ( newPosition > iWriteBufSize ) |
|
162 { |
|
163 TInt expand = newPosition - iWriteBufSize; |
|
164 iBuffer->ExpandL( iWriteBufSize, expand ); |
|
165 iWriteBufSize += expand; |
|
166 } |
|
167 |
|
168 iBuffer->Write( iWriteBufPosition, iTemp ); |
|
169 iWriteBufPosition = newPosition; |
|
170 } |
|
171 else if( iDesc ) |
|
172 { |
|
173 iDesc->Append( iTemp ); |
|
174 } |
|
175 } |
|
176 |
|
177 void COMADSFolderObject::WriteEncodedXmlL( const TDesC &aData ) |
|
178 { |
|
179 CnvUtfConverter converter; |
|
180 User::LeaveIfError( converter.ConvertFromUnicodeToUtf8( iTemp, aData ) ); |
|
181 |
|
182 HBufC8* xmlData = SenXmlUtils::EncodeHttpCharactersLC( iTemp ); |
|
183 |
|
184 if ( iBuffer ) |
|
185 { |
|
186 TInt newPosition = iWriteBufPosition + xmlData->Length(); |
|
187 |
|
188 if ( newPosition > iWriteBufSize ) |
|
189 { |
|
190 TInt expand = newPosition - iWriteBufSize; |
|
191 iBuffer->ExpandL( iWriteBufSize, expand ); |
|
192 iWriteBufSize += expand; |
|
193 } |
|
194 |
|
195 iBuffer->Write( iWriteBufPosition, *xmlData ); |
|
196 iWriteBufPosition = newPosition; |
|
197 } |
|
198 else if( iDesc ) |
|
199 { |
|
200 iDesc->Append( *xmlData ); |
|
201 } |
|
202 |
|
203 CleanupStack::PopAndDestroy( xmlData ); |
|
204 } |
|
205 |
|
206 TInt COMADSFolderObject::StartDocument() |
|
207 { |
|
208 return KErrNone; |
|
209 } |
|
210 |
|
211 TInt COMADSFolderObject::EndDocument() |
|
212 { |
|
213 return KErrNone; |
|
214 } |
|
215 |
|
216 TInt COMADSFolderObject::StartElement(const TDesC8& /*aURI*/, |
|
217 const TDesC8& /*aLocalName*/, |
|
218 const TDesC8& aName, |
|
219 const RAttributeArray& /*apAttrs*/) |
|
220 { |
|
221 LOGGER_ENTERFN( "COMADSFolderObject::StartElement" ); |
|
222 LOG( aName ); |
|
223 iCurrentElement.Copy( aName ); |
|
224 LOGGER_LEAVEFN( "COMADSFolderObject::StartElement" ); |
|
225 return KErrNone; |
|
226 } |
|
227 |
|
228 TInt COMADSFolderObject::EndElement( const TDesC8& /*aURI*/, |
|
229 const TDesC8& /*aLocalName*/, const TDesC8& /*aName*/ ) |
|
230 { |
|
231 LOGGER_ENTERFN( "COMADSFolderObject::EndElement" ); |
|
232 iCurrentElement.SetLength(0); |
|
233 LOGGER_LEAVEFN( "COMADSFolderObject::EndElement" ); |
|
234 return KErrNone; |
|
235 } |
|
236 |
|
237 TInt COMADSFolderObject::Characters( const TDesC8& aBuf, TInt aStart, TInt aLength ) |
|
238 { |
|
239 LOGGER_ENTERFN( "COMADSFolderObject::Characters" ); |
|
240 LOG(aBuf); |
|
241 |
|
242 TInt error; |
|
243 |
|
244 if( iCurrentElement.Compare( KOMADSFolderElement ) == 0 ) |
|
245 { |
|
246 LOGGER_LEAVEFN( "COMADSFolderObject::Characters" ); |
|
247 return KErrNone; |
|
248 } |
|
249 |
|
250 else if ( iCurrentElement.Compare( KOMADSNameElement ) == 0 ) |
|
251 { |
|
252 CnvUtfConverter converter; |
|
253 TBuf<KMaxFolderNameLength> buf; |
|
254 error = converter.ConvertToUnicodeFromUtf8( buf, aBuf.Mid( aStart, aLength ) ); |
|
255 TInt length = buf.Length() + iName.Length(); |
|
256 |
|
257 if ( error > 0 ) |
|
258 { |
|
259 LOGGER_MSG_EC("Too long name, number of uncorverted bytes: %d", error); |
|
260 } |
|
261 else if ( error != KErrNone ) |
|
262 { |
|
263 LOGGER_MSG_EC("ConvertToUnicodeFromUtf8 failed with %d", error); |
|
264 iError = error; |
|
265 } |
|
266 else if ( length > iName.MaxLength() ) |
|
267 { |
|
268 LOGGER_MSG_EC("Too long name total: %d", length); |
|
269 } |
|
270 else |
|
271 { |
|
272 iName.Append( buf ); |
|
273 } |
|
274 } |
|
275 else if ( iCurrentElement.Compare( KOMADSCreatedElement ) == 0 ) |
|
276 { |
|
277 error = ParseDateString( aBuf.Mid( aStart, aLength ), iCreatedDate ); |
|
278 if( error != KErrNone ) |
|
279 { |
|
280 iError = error; |
|
281 } |
|
282 } |
|
283 else if ( iCurrentElement.Compare( KOMADSModifiedElement ) == 0 ) |
|
284 { |
|
285 error = ParseDateString( aBuf.Mid( aStart, aLength ), iModifiedDate ); |
|
286 if ( error != KErrNone ) |
|
287 { |
|
288 iError = error; |
|
289 } |
|
290 } |
|
291 LOGGER_LEAVEFN( "COMADSFolderObject::Characters" ); |
|
292 return KErrNone; |
|
293 } |
|
294 |
|
295 TInt COMADSFolderObject::ParseDateString(const TDesC8& aString, TDateTime& aDateTime) |
|
296 { |
|
297 // Ensure we don't read beyond the buffer limits |
|
298 if ( aString.Length() < 15 ) |
|
299 { |
|
300 return KErrArgument; |
|
301 } |
|
302 // Extract the fields from the string |
|
303 TLex8 yearDesc( aString.Mid( 0, 4 ) ); |
|
304 TLex8 monthDesc( aString.Mid( 4, 2 ) ); |
|
305 TLex8 dayDesc( aString.Mid( 6, 2 ) ); |
|
306 // Skip one character here, it's supposed to be 'T' |
|
307 TLex8 hourDesc( aString.Mid( 9, 2 ) ); |
|
308 TLex8 minuteDesc( aString.Mid( 11, 2 ) ); |
|
309 TLex8 secondDesc( aString.Mid( 13, 2 ) ); |
|
310 |
|
311 |
|
312 TInt year, month, day, hour, minute, second; |
|
313 TInt error; |
|
314 |
|
315 // Fetch the values to temporary variables |
|
316 if ( ( error = yearDesc.Val(year) ) != KErrNone ) |
|
317 return error; |
|
318 if ( ( error = monthDesc.Val(month) ) != KErrNone ) |
|
319 return error; |
|
320 if ( ( error = dayDesc.Val(day) ) != KErrNone ) |
|
321 return error; |
|
322 if ( ( error = hourDesc.Val(hour) ) != KErrNone ) |
|
323 return error; |
|
324 if ( ( error = minuteDesc.Val(minute) ) != KErrNone ) |
|
325 return error; |
|
326 if ( ( error = secondDesc.Val(second) ) != KErrNone ) |
|
327 return error; |
|
328 |
|
329 // Assign values to datetime object |
|
330 if ( ( error = aDateTime.SetYear(year) ) != KErrNone ) |
|
331 return error; |
|
332 if ( ( error = aDateTime.SetMonth((TMonth)(month-1) )) != KErrNone ) |
|
333 return error; |
|
334 if ( ( error = aDateTime.SetDay(day) ) != KErrNone ) |
|
335 return error; |
|
336 if ( ( error = aDateTime.SetHour(hour) ) != KErrNone ) |
|
337 return error; |
|
338 if ( ( error = aDateTime.SetMinute(minute) ) != KErrNone ) |
|
339 return error; |
|
340 if ( ( error = aDateTime.SetSecond(second) ) != KErrNone ) |
|
341 return error; |
|
342 |
|
343 return KErrNone; |
|
344 } |
|
345 |
|
346 void COMADSFolderObject::Reset() |
|
347 { |
|
348 iError = KErrNone; |
|
349 iName.SetLength( 0 ); |
|
350 iCreatedDate.Set( 2005, EJanuary, 1, 0, 0, 0, 0 ); |
|
351 iModifiedDate.Set( 2005, EJanuary, 1, 0, 0, 0, 0 ); |
|
352 } |
|
353 |
|
354 TInt COMADSFolderObject::Error( TInt aErrorCode ) |
|
355 { |
|
356 iError = aErrorCode; |
|
357 return KErrNone; |
|
358 } |
|