Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages | Examples

SimpleEnumReader/SimpleEnumReader.cpp

This sample shows to read a DWF package (DWFToolkit::DWFPackageReader) an its manifest (DWFToolkit::DWFManifest), enumerate its sections (DWFToolkit::DWFSection, DWFToolkit::DWFGlobalSection) and contents, and hook filters (DWFToolkit::DWFDuplicateAttributeFilter) into the XML parser (DWFToolkit::DWFXMLCallback).

// SimpleEnumReader.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

using namespace std;
using namespace DWFCore;
using namespace DWFToolkit;


//
// Certain older versions of AutoCAD (e.g. 2002) 
// are known to produce section descriptor documents
// with malformed XML, in particular, duplicating the xmlns:EPlot attribute.
// The DWFXMLCallback interface provides a method for hooking up a filter
// (essentially a buffering input stream) between the document source and
// the XML parser.  The toolkit contains one such filter for detecting and
// discarding duplicate attributes.  Several words of warning:
// first, the filter slows the read/parse process considerably, testing shows
// anyhere from 9% - 31% over a small sample set, your mileage my vary;
// second, the algorthim was developed with the above mentioned problem in mind,
// as such, it may or may not really fix super bad XML, but then, it's not really
// supposed to...
// Anyhow, define this macro to enable the fixing code for this sample.
//
#define _DWFTK_SAMPLE_FIX_BAD_EPLOT_DESCRIPTOR_XML    1


void dump_bookmarks( const DWFToolkit::DWFBookmark* pRoot )
{
    static size_t l = 0;
    size_t i;

    for (i=0;i<l;i++)
    {
        wcout << "  ";
    }

    if (pRoot->name())
    {
        wcout << (const wchar_t*)(pRoot->name()) << endl;
    }

    const DWFToolkit::DWFBookmark::tList& rChildren = pRoot->getChildBookmarks();

    for (i=0; i < rChildren.size(); i++)
    {
        l++;
        dump_bookmarks( rChildren[i] );
        l--;
    }
}

int main(int argc, char* argv[])
{

    if (argc < 2)
    {
        wcout << L"Usage:" << argv[0] << L" file.dwf" << endl;
        return ( 0 );
    }

    try
    {
        DWFFile oDWF( argv[1] );
        DWFToolkit::DWFPackageReader oReader( oDWF );

        DWFToolkit::DWFPackageReader::tPackageInfo tInfo;
        oReader.getPackageInfo( tInfo );

        wchar_t zBuffer[256] = {0};

        if (tInfo.eType != DWFPackageReader::eDWFPackage)
        {
            _DWFCORE_SWPRINTF( zBuffer, 256, L"File is not a DWF package [%s]",
                        (tInfo.eType == DWFPackageReader::eW2DStream) ? L"W2D Stream" :
                        (tInfo.eType == DWFPackageReader::eDWFStream) ? L"DWF Stream (<6.0)" :
                        (tInfo.eType == DWFPackageReader::eZIPFile) ? L"ZIP Archive" : L"Unknown" );

            wcout << zBuffer << endl;
            exit( 0 );
        }

        _DWFCORE_SWPRINTF( zBuffer, 256, L"DWF Package version [%0.2f]", (float)(tInfo.nVersion)/100.0f );
        wcout << zBuffer << endl;

        wcout << L"Reading the manifest..." << endl;

        DWFToolkit::DWFManifest& rManifest = oReader.getManifest();

        _DWFCORE_SWPRINTF( zBuffer, 256, L"\tVersion: %0.2g", rManifest.version() );
        wcout << zBuffer << endl;
        wcout << L"\tObject ID: " << (const wchar_t*)rManifest.objectID() << endl;

        DWFToolkit::DWFManifest::tInterfaceIterator* piInterfaces = rManifest.getInterfaces();

        if (piInterfaces)
        {
            for (;piInterfaces->valid(); piInterfaces->next())
            {
                wcout << L"\tInterface found: " << (const wchar_t*)(piInterfaces->value()->name()) << endl;
            }

            DWFCORE_FREE_OBJECT( piInterfaces );
        }

        DWFToolkit::DWFProperty* pProperty = NULL;
        DWFToolkit::DWFProperty::tMap::Iterator* piProperties = rManifest.getProperties();

        if (piProperties)
        {
            for (;piProperties->valid(); piProperties->next())
            {
                pProperty = piProperties->value();
                wcout << L"\tProperty found: " << (const wchar_t*)(pProperty->name());

                if (pProperty->value())
                {
                    wcout << L" [" << (const wchar_t*)(pProperty->value()) << L"]";
                }

                if (pProperty->category())
                {
                    wcout << L" [" << (const wchar_t*)(pProperty->category()) << L"]";
                }

                wcout << endl;
            }

            DWFCORE_FREE_OBJECT( piProperties );
        }

        DWFToolkit::DWFSection* pSection = NULL;
        DWFToolkit::DWFManifest::SectionIterator* piSections = rManifest.getSections();

        if (piSections)
        {
            for (;piSections->valid(); piSections->next())
            {
                pSection = piSections->get();

                //
                // piSections->key() will also return the section name...
                //
                wcout << L"\tSection found: " << (const wchar_t*)(pSection->name());

                if (pSection->type())
                {
                    wcout << L" [" << (const wchar_t*)(pSection->type()) << L"]";
                }

                if (pSection->title())
                {
                    wcout << L" [" << (const wchar_t*)(pSection->title()) << L"]";
                }

                wcout << endl;
            }

            wcout << "Reading section descriptors..." << endl << endl;

            for (piSections->reset(); piSections->valid(); piSections->next())
            {
                pSection = piSections->get();


#ifdef  _DWFTK_SAMPLE_FIX_BAD_EPLOT_DESCRIPTOR_XML

                    //
                    // some sections might not also be descriptor readers which implement this interface...
                    //
                DWFToolkit::DWFXMLCallback* pXMLCallback = dynamic_cast<DWFToolkit::DWFXMLCallback*>(pSection);
                if (pXMLCallback)
                {
                    wcout << L"XML fixing requested - inserting duplicate attribute filter.\n";

                    //
                    // hook up the filter - as and added bonus tell the section to delete it too
                    //
                    DWFToolkit::DWFDuplicateAttributeFilter* pFilter = DWFCORE_ALLOC_OBJECT( DWFToolkit::DWFDuplicateAttributeFilter );
                    pXMLCallback->setStreamFilter( pFilter, true );
                }

#endif


                pSection->readDescriptor();

                DWFToolkit::DWFGlobalSection* pGlobal = dynamic_cast<DWFToolkit::DWFGlobalSection*>(pSection);

                if (pGlobal == NULL)
                {
                    wcout << L"\t[OK] (" << pSection->order() << L") v" << pSection->version();

                    if (pSection->name())
                    {
                        wcout << L" [" << (const wchar_t*)(pSection->name()) << L"]";
                    }

                    if (pSection->objectID())
                    {
                        wcout << L" [" << (const wchar_t*)(pSection->objectID()) << L"]";
                    }

                    wcout << endl;
                }
                else
                {
                    wcout << L"\t[OK] (Global Section)" << endl;

                    const DWFToolkit::DWFBookmark* pRoot = pGlobal->bookmark();
                    if (pRoot)
                    {
                        wcout << L"\tDumping bookmarks..." << endl;
                        dump_bookmarks( pRoot );
                    }
                }
            }

            DWFCORE_FREE_OBJECT( piSections );
        }

        wcout << L"OK\n";
    }
    catch (DWFException& ex)
    {
        wcout << ex.type() << endl;
        wcout << ex.message() << endl;
        wcout << ex.function() << endl;
        wcout << ex.file() << endl;
        wcout << ex.line() << endl;
    }

    return 0;
}



Generated on Tue May 17 12:38:49 2005 for Autodesk DWF Toolkit by  doxygen 1.4.1