Nokia N97 SDK
Example Applications Guide

XeDomContainers.cpp

00001 /*
00002 * XEDOMCONTAINERS.CPP
00003 * Copyright (c) 2007 Nokia Corporation.
00004 * This material, including documentation and any related computer
00005 * programs,is protected by copyright controlled by Nokia Corporation.
00006 */
00007 
00008 #include <e32base.h>
00009 #include "XeDomExample.h"
00010 
00011 
00012 /*************************************************************
00013 * This example demonstrates usage of internal (binary)&
00014 * external (chunk) data containers provided by XML engine
00015 * DOM. Creation of these containers, fetching the contents
00016 * of these containers, setting the contens of these containers,
00017 * specifying offsets,size in case of external containers,
00018 * getting the list of data containers for the document
00019 **************************************************************/
00020 EXPORT_C void CXEDomEx::DataContainersExampleL()
00021 {
00022     // usage of binary containers - subexample
00023         BinaryContainersL();
00024         
00025         // usage external containers - subexample
00026         ExternalContainersL();
00027 }
00028 
00029 
00030 void CXEDomEx::BinaryContainersL()
00031 {
00032         _LIT8(KElementName,"doc");
00033         _LIT8(KTarget,"test");
00034         _LIT8(KComment,"test comment");
00035         _LIT8(KValue,"test value");
00036         _LIT8(KNamespace,"testNamespace");
00037         _LIT8(KPrefix,"testPref");
00038         _LIT8(KStringName,"doc");
00039         _LIT8(KStringValue,"ATTR_value");
00040         _LIT8(KStringText,"test content");
00041         _LIT8(KCData,"test < test2 & \r\n> ;");
00042 
00043         RXmlEngDocument document;
00044         document.OpenL(iDomImpl);
00045         CleanupClosePushL(document);
00046 
00047         // Preparing a document before we deal with binary containers
00048         TXmlEngElement docEle = document.CreateDocumentElementL( KElementName,KNullDesC8 ,KNullDesC8 );
00049         TXmlEngTextNode textNode = document.CreateTextNodeL(KStringText);
00050         docEle.AppendChildL(textNode);
00051         TXmlEngCDATASection cdNode = document.CreateCDATASectionL(KCData);
00052         docEle.AppendChildL(cdNode);
00053         TXmlEngAttr atNode = document.CreateAttributeL(KStringName, KStringValue);
00054         docEle.AppendChildL(atNode);
00055         TXmlEngComment commentNode = document.CreateCommentL(KComment);
00056         docEle.AppendChildL(commentNode);
00057         TXmlEngProcessingInstruction piNode = document.CreateProcessingInstructionL(KTarget, KValue);
00058         docEle.AppendChildL(piNode);
00059         TXmlEngElement elementNode = document.CreateElementL(KElementName, KNamespace, KPrefix);
00060         docEle.AppendChildL(elementNode);
00061 
00062         // Binary data to be read
00063         TBuf<KFilePathSize> filepath1(iDrive);
00064         _LIT(KFilename1, "\\xedomexample\\input\\test.doc");
00065         filepath1.Append(KFilename1);   
00066         
00067         TBuf<KFilePathSize> filepath2(iDrive);
00068         _LIT(KFilename2, "\\xedomexample\\input\\test.jpg");
00069         filepath2.Append(KFilename2);   
00070                 
00071         HBufC8* str1;
00072         str1=ReadFileL(filepath1);
00073         CleanupStack::PushL(str1);
00074         
00075         HBufC8* str2;
00076         str2=ReadFileL(filepath2);
00077         CleanupStack::PushL(str2);
00078 
00079         _LIT8(data, "this is some kind of binary data");
00080         _LIT8(data1, "this is another kind of binary data 123");
00081         _LIT8(cid, "123456789@123456789");
00082         
00083         // Creates new binary container and copies the content string into it.
00084         // Pointer to the container is stored in the document's
00085         // data container list that can be fetched using GetDataContainerList().
00086         TXmlEngBinaryContainer binData = document.CreateBinaryContainerL(cid,data);
00087 
00088         // Sets contents of binary container
00089         binData.SetContentsL(data1);
00090 
00091         // Get content of the container.
00092         TPtrC8 fetchedData = binData.Contents();
00093         if(fetchedData.Compare(data1))
00094             User::Leave(KErrGeneral);
00095 
00096         // Check data size in bytes
00097         if(data1().Length() != binData.Size())
00098             User::Leave(KErrGeneral);
00099         
00100         _LIT8(cid2, "123456789@123456788");
00101         TXmlEngBinaryContainer binData1 = document.CreateBinaryContainerL(cid2, *str1);
00102         binData1.SetContentsL(*str2);
00103 
00104     _LIT8(cid3, "123456789@123456787");
00105         TXmlEngBinaryContainer binData2 = document.CreateBinaryContainerL(cid3, *str2);
00106         binData2.SetContentsL(*str1);
00107 
00108     RArray<TXmlEngDataContainer> binaryContainers;
00109     document.GetDataContainerList(binaryContainers);
00110     CleanupClosePushL(binaryContainers);
00111     
00112     if(binaryContainers.Count() != 3)
00113         User::Leave(KErrGeneral);
00114         
00115         CleanupStack::PopAndDestroy(); //binaryContainers
00116         CleanupStack::PopAndDestroy(str2);
00117         CleanupStack::PopAndDestroy(str1);
00118         CleanupStack::PopAndDestroy(); //document
00119 }
00120 
00121 
00122 
00123 void CXEDomEx::ExternalContainersL()
00124 {
00125         _LIT8(KElementName,"doc");
00126         _LIT8(KTarget,"test");
00127         _LIT8(KComment,"test comment");
00128         _LIT8(KValue,"test value");
00129         _LIT8(KNamespace,"testNamespace");
00130         _LIT8(KPrefix,"testPref");
00131         _LIT8(KStringName,"doc");
00132         _LIT8(KStringValue,"ATTR_value");
00133         _LIT8(KStringText,"test content");
00134         _LIT8(KCData,"test < test2 & \r\n> ;");
00135 
00136         RXmlEngDocument document;
00137         document.OpenL(iDomImpl);
00138         CleanupClosePushL(document);
00139 
00140         // Preparing an example document before we deal with binary containers
00141         TXmlEngElement docEle = document.CreateDocumentElementL( KElementName,KNullDesC8 ,KNullDesC8 );
00142         TXmlEngTextNode textNode = document.CreateTextNodeL(KStringText);
00143         docEle.AppendChildL(textNode);
00144         TXmlEngCDATASection cdNode = document.CreateCDATASectionL(KCData);
00145         docEle.AppendChildL(cdNode);
00146         TXmlEngAttr atNode = document.CreateAttributeL(KStringName, KStringValue);
00147         docEle.AppendChildL(atNode);
00148         TXmlEngComment commentNode = document.CreateCommentL(KComment);
00149         docEle.AppendChildL(commentNode);
00150         TXmlEngProcessingInstruction piNode = document.CreateProcessingInstructionL(KTarget, KValue);
00151         docEle.AppendChildL(piNode);
00152         TXmlEngElement elementNode = document.CreateElementL(KElementName, KNamespace, KPrefix);
00153         docEle.AppendChildL(elementNode);
00154 
00155         _LIT8(cid, "123456789@123456789");
00156         _LIT8(cid1, "11111111@123456789");
00157         _LIT8(cid2, "22222222@123456789");
00158         _LIT8(cid3, "33333333@123456789");
00159         _LIT(chunkName, "test chunk");
00160         const TInt size = 1000;
00161         const TInt maxSize = 5000;
00162         const TInt offset = 100;
00163         const TInt binarySize = 500;
00164 
00165     _LIT8(KText, "binary containers");
00166         TXmlEngElement el = document.CreateElementL(KText);
00167         document.DocumentElement().AppendChildL(el);
00168         
00169         RChunk chunk;
00170         chunk.CreateGlobal(chunkName, size, maxSize);
00171         CleanupClosePushL(chunk);
00172 
00173         // Creates new chunk container that stores reference to memory chunk
00174         TXmlEngChunkContainer binData = document.CreateChunkContainerL(cid, chunk,offset, binarySize);
00175         el.AppendChildL(binData);
00176         TXmlEngChunkContainer binData1 = document.CreateChunkContainerL(cid1, chunk,offset, binarySize);
00177         el.AppendChildL(binData1);
00178         TXmlEngChunkContainer binData2 = document.CreateChunkContainerL(cid2, chunk,offset, binarySize);
00179         el.AppendChildL(binData2);
00180         TXmlEngChunkContainer binData3 = document.CreateChunkContainerL(cid3, chunk,offset, binarySize);
00181         el.AppendChildL(binData3);
00182 
00183         RArray<TXmlEngDataContainer> list;
00184         CleanupClosePushL(list);
00185 
00186         // Retrieves an array of data containers owned by this document.
00187         document.GetDataContainerList(list);
00188         if(list.Count()!=4)
00189             User::Leave(KErrGeneral);
00190         if(list[0].Cid().Compare(cid)!=0)
00191             User::Leave(KErrGeneral);
00192 
00193         CleanupStack::PopAndDestroy(); //document
00194         CleanupStack::PopAndDestroy(); //chunk
00195         CleanupStack::PopAndDestroy(); //list
00196 }

© Nokia 2009

Back to top