Re: API to retrieve document metadata

Ariel Constenla-Haile <[email protected]>
Newsgroups gmane.comp.openoffice.questions
Message-ID <20111025162858.GB9416@localhost>
Hi Alona,

On Tue, Oct 25, 2011 at 11:12:42AM -0400, Alona Rossen wrote:
> Hi Ariel,
> 
> Could you please provide a C++ example of how to access
> getDocumentProperties() method after the document has been loaded? In
> other words, we cannot understand how to bridge between xComponent,
> which we get after document is loaded and getDocumentProperties().

when you load a document you get a css.lang.XComponent.
Depending on the document you loaded, this document implements different
interfaces.

The interfaces a document supports are documented in the document
service:

* a Writer document is documented in the service css.text.TextDocument
    http://api.openoffice.org/docs/common/ref/com/sun/star/text/TextDocument.html
        This service includes css.text.GenericTextDocument
        http://api.openoffice.org/docs/common/ref/com/sun/star/text/GenericTextDocument.html
            This service includes css.document.OfficeDocument   
            http://api.openoffice.org/docs/common/ref/com/sun/star/document/OfficeDocument.html
            You can see there all the interfaces an OfficeDocument
            implements

To get an interface from an object (your xComponent here), just query for it:

css::uno::Reference< SOME_INTERFACE > xInterface( xComponent, css::uno::QUERY);
if ( xInterface.is() )
{
    // here do your job
}


You can do the same research for all other OOo documents:
* a Draw document
* a Presentation
* etc

See the attached example.


Regards
-- 
Ariel Constenla-Haile
La Plata, Argentina
client_app.cxx (text/plain, 5 KB)
#define C2U( constAsciiStr ) \
        ( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( constAsciiStr ) ) )

#define U2C( ouString ) \
        ( ::rtl::OUStringToOString( ouString, RTL_TEXTENCODING_UTF8 ).getStr() )

// -------------------------------------------------------------------------------------

#include <vector>
#include <iostream>
#include <cstdio>
#include <osl/file.hxx>
#include <cppuhelper/bootstrap.hxx>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/frame/FrameSearchFlag.hpp>
#include <com/sun/star/uno/Reference.h>
#include <com/sun/star/uno/XComponentContext.hpp>

using namespace ::com::sun::star;


int SAL_CALL main ( const int argc, const char* argv[] )
{
    int ret = 0;

    if ( argc != 2 )
    {
        std::cerr << "Usage: " << std::endl <<
                     "       " << argv[0] << " DOC_TO_LOAD" << std::endl <<
                     "where  " << std::endl <<
                     "       DOC_TO_LOAD is the system path of the document to load" << std::endl;
        return 1;
    }

    rtl::OUString path = rtl::OUString::createFromAscii( argv[1] );
    rtl::OUString path_url;
    if ( osl_File_E_None != osl_getFileURLFromSystemPath( path.pData, &path_url.pData ) )
    {
        std::cerr << "Can not convert " << argv[1] << " to URL." << std::endl;
        return 1;
    }

    try
    {
        uno::Reference< uno::XComponentContext > rxContext ( ::cppu::bootstrap() );

        uno::Reference< frame::XComponentLoader > rxComponentLoader(
            rxContext->getServiceManager()->createInstanceWithContext(
                C2U( "com.sun.star.frame.Desktop" ),
                rxContext ),
            uno::UNO_QUERY );

        uno::Reference< lang::XComponent > xComponent =
            rxComponentLoader->loadComponentFromURL(
                path_url, 
                C2U( "_default" ), 
                frame::FrameSearchFlag::ALL,
                uno::Sequence< beans::PropertyValue >() );

        uno::Reference< document::XDocumentPropertiesSupplier> xDocPropsSup( xComponent, uno::UNO_QUERY );
        if ( !xDocPropsSup.is() )
        {
            std::cerr << "No Document Props. Supplier!" << std::endl;
            return 1;
        }

        uno::Reference< document::XDocumentProperties > xDocProps = xDocPropsSup->getDocumentProperties();
        if ( !xDocProps.is() )
        {
            std::cerr << "No Document Properties!" << std::endl;
            return 1;
        }

        // Attributes
        rtl::OUString Title        = xDocProps->getTitle();
        rtl::OUString Subject      = xDocProps->getSubject();
        rtl::OUString Description  = xDocProps->getDescription();

        rtl::OUString Generator    = xDocProps->getGenerator();
        rtl::OUString Author       = xDocProps->getAuthor();

        util::DateTime CreationDate     = xDocProps->getCreationDate();
        rtl::OUString ModifiedBy        = xDocProps->getModifiedBy();
        util::DateTime ModificationDate = xDocProps->getModificationDate();

        uno::Sequence< ::rtl::OUString > Keywords = xDocProps->getKeywords();
        lang::Locale Language = xDocProps->getLanguage();

        rtl::OUString PrintedBy  = xDocProps->getPrintedBy();
        util::DateTime PrintDate = xDocProps->getPrintDate();

        rtl::OUString TemplateName  = xDocProps->getTemplateName();
        rtl::OUString TemplateURL   = xDocProps->getTemplateURL();
        util::DateTime TemplateDate = xDocProps->getTemplateDate();

        rtl::OUString AutoloadURL   = xDocProps->getAutoloadURL();
        sal_Int32 AutoloadSecs      = xDocProps->getAutoloadSecs();
        rtl::OUString DefaultTarget = xDocProps->getDefaultTarget();

        uno::Sequence< beans::NamedValue > DocumentStatistics = xDocProps->getDocumentStatistics();

        sal_Int16 EditingCycles   = xDocProps->getEditingCycles();
        sal_Int32 EditingDuration = xDocProps->getEditingDuration();
        
        std::cout << "Title :        " << U2C( Title )        <<  std::endl;
        std::cout << "Subject :      " << U2C( Subject )      <<  std::endl;
        std::cout << "Description :  " << U2C( Description )  <<  std::endl;
        std::cout << "Generator :    " << U2C( Generator )    <<  std::endl;
        std::cout << "Author :       " << U2C( Author )       <<  std::endl;
        std::cout << "ModifiedBy :   " << U2C( ModifiedBy )   <<  std::endl;
        std::cout << "PrintedBy :    " << U2C( PrintedBy )    <<  std::endl;
        std::cout << "TemplateName : " << U2C( TemplateName ) <<  std::endl;
        std::cout << "TemplateURL :  " << U2C( TemplateURL )  <<  std::endl;
        std::cout << "AutoloadURL :  " << U2C( AutoloadURL )  <<  std::endl;

    }
    catch ( uno::RuntimeException& e )
    {
        ret = 1;
        OSL_ASSERT ( false );
    }
    catch ( uno::Exception& e )
    {
        ret = 1;
        OSL_ASSERT ( false );
    }
    return ret;
}
// kate: indent-mode cstyle; space-indent on; indent-width 4;
signature.asc (application/pgp-signature, 836 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iQIcBAEBCAAGBQJOpuPKAAoJEMjP1bm45QNWYPYP/2ObrnqRKMQ5Kth5N9Oi99FX
DfT7nSqVFp6E+zBFs4hN43+Y2BF5XdKQ39VjSb2P/chWNbP4F8qnateFB1BNMntS
tMbASrd+qNu0Qu/TOs16wlgkBVnLQEqsCg3+iQABIboQ9a+ojkmREWjgnESJpYkk
+ITgs3cnGuymLKtzNvBJ8T6OBpJpYHBy1rTsn0XHPtyYwWmsn6BNIwNKy6wxKKyN
bYnQCuOWLlrm3gETX2OLmYlxnP4L0n9b/WbuSuggsdwF4O7I71b5J0U6Q8T32Aky
4ab1rtdy6GLRQ3VcUweLcGAe1xty1Vzml9O56fpYbjVTaq74FfqliVgi6GaUg0Dr
of/AEhOpczfeQa0K+y+exyy/YFMcHeYuRytqA7YXIGAdQZLSgz0wlhpwsgK+D4mb
ubpHI9DLlmu9a0ANTrX9VI5QgraEePmbiLXRPe2JhqQ+XSkdFsll+81m0KZPd8d6
6thdftJvXZF37lLCmd7q41wS1Z3faqdgYZe4zEYnqgAdGtV5IHDHbRgNo9vFHa03
XeYjdzkUnUn/ci0PcT2dqp8I4lCAKGd8Vkt2zENLiNyQ1gN6UAiJxGBkx8Cc/jVJ
CPiUDSdfkqjTGuQRgspLektx5BW13Op7xrRMdpzg9yiH2COdfabILAAXQypyuymC
AZjCF0vaTerBg7/40N5I
=1F8O
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.