| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> |
| <HTML> |
| <head> |
| <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=iso-8859-1"> |
| <TITLE>Type Detection - Examples</TITLE> |
| <META NAME="GENERATOR" CONTENT="StarOffice/5.2 (Win32)"> |
| <META NAME="CREATED" CONTENT="20010222;14370867"> |
| <META NAME="CHANGEDBY" CONTENT="Mathias Bauer"> |
| <META NAME="CHANGED" CONTENT="20010411;13383007"> |
| </head> |
| <body> |
| <TABLE WIDTH=100% BORDER=0 CELLPADDING=0 CELLSPACING=0 STYLE="page-break-before: always"> |
| <TR> |
| <TD> |
| <P ALIGN=LEFT><A HREF="services.html"><<</A></P> |
| </TD> |
| <TD> |
| <P ALIGN=CENTER><BR> |
| </P> |
| </TD> |
| <TD> |
| <P ALIGN=RIGHT><A HREF="reference.html">>></A></P> |
| </TD> |
| </TR> |
| </TABLE> |
| <HR SIZE=3 COLOR="#000000"> |
| <CENTER> |
| <TABLE WIDTH=1289 BORDER=0 CELLPADDING=2 CELLSPACING=0> |
| <COL WIDTH=1285> |
| <TR> |
| <TD WIDTH=1285> |
| <P><FONT SIZE=5>Examples</FONT> |
| </P> |
| <P>The following examples are to illustrate the use of the |
| services involved.</P> |
| <UL> |
| <LI><P STYLE="margin-bottom: 0cm">Creation of the necessary |
| objects |
| </P> |
| <LI><P STYLE="margin-bottom: 0cm"><A HREF="#flat_detection">Flat</A> |
| or <A HREF="#deep_detection">deep</A> detection of a type on the |
| basis of different parameters |
| </P> |
| <LI><P STYLE="margin-bottom: 0cm"><A HREF="#search_loader">Search |
| and use of a FrameLoader object</A> |
| </P> |
| <LI><P STYLE="margin-bottom: 0cm"><A HREF="#search_filter">Search |
| and use of a Filter object to import a document</A> (Export |
| similar!) |
| </P> |
| <LI><P STYLE="margin-bottom: 0cm"><A HREF="#read_filtercfg">Reading |
| of the entire filters configuration</A> (Loader/Types similar! |
| ... however at different services!) |
| </P> |
| <LI><P><A HREF="#register_filter">Register a new filter</A> |
| (Loader/Types similar!) |
| </P> |
| </UL> |
| </TD> |
| </TR> |
| <TR> |
| <TD WIDTH=1285> |
| <P><A NAME="includes"></A><BR><FONT SIZE=4>Includes, Declarations |
| ...</FONT> |
| </P> |
| </TD> |
| </TR> |
| <TR BGCOLOR="#ffffcc"> |
| <TD WIDTH=1285> |
| <PRE>//***************************************************************************************************************** |
| //includes interfaces |
| //***************************************************************************************************************** |
| #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_ |
| #include < com/sun/star/lang/XMultiServiceFactory.hpp> |
| #endif |
| |
| #ifndef _COM_SUN_STAR_DOCUMENT_XTYPEDETECTION_HPP_ |
| #include < com/sun/star/document/XTypeDetection.hpp> |
| #endif |
| |
| #ifndef _COM_SUN_STAR_FRAME_XFRAMELOADER_HPP_ |
| #include < com/sun/star/frame/XFrameLoader.hpp> |
| #endif |
| |
| #ifndef _COM_SUN_STAR_FRAME_XSYNCHRONOUSFRAMELOADER_HPP_ |
| #include < com/sun/star/frame/XSynchronousFrameLoader.hpp> |
| #endif |
| |
| #ifndef _COM_SUN_STAR_DOCUMENT_XFILTER_HPP_ |
| #include < com/sun/star/document/XFilter.hpp> |
| #endif |
| |
| #ifndef _COM_SUN_STAR_DOCUMENT_XIMPORTER_HPP_ |
| #include < com/sun/star/document/XImporter.hpp> |
| #endif |
| |
| #ifndef _COM_SUN_STAR_LANG_XCOMPONENT_HPP_ |
| #include < com/sun/star/lang/XComponent.hpp> |
| #endif |
| |
| #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_ |
| #include < com/sun/star/beans/XPropertySet.hpp> |
| #endif |
| |
| #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_ |
| #include < com/sun/star/container/XNameAccess.hpp> |
| #endif |
| |
| #ifndef _COM_SUN_STAR_CONTAINER_XNAMECONTAINER_HPP_ |
| #include < com/sun/star/container/XNameContainer.hpp> |
| #endif |
| |
| //***************************************************************************************************************** |
| // uno interfaces |
| //***************************************************************************************************************** |
| #ifndef _COM_SUN_STAR_UNO_REFERENCE_H_ |
| #include < com/sun/star/uno/Reference.h> |
| #endif |
| |
| #ifndef _COM_SUN_STAR_UNO_SEQUENCE_H_ |
| #include < com/sun/star/uno/Sequence.h> |
| #endif |
| |
| #ifndef _COM_SUN_STAR_UNO_ANY_H_ |
| #include < com/sun/star/uno/Any.h> |
| #endif |
| |
| //***************************************************************************************************************** |
| // others |
| //***************************************************************************************************************** |
| #ifndef _COMPHELPER_PROCESSFACTORY_HXX_ |
| #include < comphelper/processfactory.hxx> // for getProcessServiceFactory() |
| #endif |
| |
| //***************************************************************************************************************** |
| // namespaces |
| //***************************************************************************************************************** |
| using namespace ::com::sun::star::uno ; |
| using namespace ::com::sun::star::lang ; |
| using namespace ::com::sun::star::container ; |
| using namespace ::rtl ; |
| using namespace ::comphelper ; |
| |
| //***************************************************************************************************************** |
| // macros |
| //***************************************************************************************************************** |
| #define ASCII( SSTRING ) OUString(RTL_CONSTASCII_USTRINGPARAM( SSTRING )) // convert an ascii-value to unicode</PRE> |
| </TD> |
| </TR> |
| <TR> |
| <TD WIDTH=1285> |
| <P><A NAME="flat_detection"></A><BR><FONT SIZE=4>Flat Detection</FONT> |
| </P> |
| </TD> |
| </TR> |
| <TR BGCOLOR="#ffffcc"> |
| <TD WIDTH=1285> |
| <PRE>void example_flatDetection() |
| { |
| // Create UNO-service for type detection. |
| // Use global servicemanager as factory and cast return value to right interface. |
| Reference< XTypeDetection > xTypeDetection( getProcessServiceFactory()->createInstance( ASCII("com.sun.star.document.TypeDetection") ), UNO_QUERY ); |
| |
| // a) Make flat detection by URL only. |
| OUString sInternalTypeName; |
| sInternalTypeName = xTypeDetection->queryTypeByURL( ASCII("file:///c|/temp/test.txt") ); |
| |
| // b) Make flat detection by URL and an additional search parameter. |
| Sequence< PropertyValue > lDescriptor( 2 ); |
| |
| lDescriptor[0].Name = ASCII("FileName" ); |
| lDescriptor[0].Value <<= ASCII("file:///c|/temp/test.txt"); |
| |
| lDescriptor[1].Name = ASCII("ContentType" ); |
| lDescriptor[1].Value <<= ASCII("text/plain" ); |
| |
| sal_Bool bDeep = sal_False; |
| |
| // !!! These can add or change informations of given descriptor !!! |
| sInternalTypeName = xTypeDetection->queryTypeByDescriptor( lDescriptor, bDeep ); |
| }</PRE> |
| </TD> |
| </TR> |
| <TR> |
| <TD WIDTH=1285> |
| <P><A NAME="deep_detection"></A><BR><FONT SIZE=4>Deep Detection</FONT> |
| </P> |
| </TD> |
| </TR> |
| <TR BGCOLOR="#ffffcc"> |
| <TD WIDTH=1285> |
| <PRE>void example_deepDetection() |
| { |
| // Create UNO-service for type detection. |
| // Use global servicemanager as factory and cast return value to right interface. |
| Reference< XTypeDetection > xTypeDetection( getProcessServiceFactory()->createInstance( ASCII("com.sun.star.document.TypeDetection") ), UNO_QUERY ); |
| |
| // Make deep detection by URL and an additional search parameter. |
| Sequence< PropertyValue > lDescriptor( 2 ); |
| |
| lDescriptor[0].Name = ASCII("FileName" ); |
| lDescriptor[0].Value <<= ASCII("file:///c|/temp/test.txt"); |
| |
| lDescriptor[1].Name = ASCII("ContentType" ); |
| lDescriptor[1].Value <<= ASCII("text/plain" ); |
| |
| sal_Bool bDeep = sal_True; |
| |
| // !!! These can add or change informations of given descriptor !!! |
| sInternalTypeName = xTypeDetection->queryTypeByDescriptor( lDescriptor, bDeep ); |
| }</PRE> |
| </TD> |
| </TR> |
| <TR> |
| <TD WIDTH=1285> |
| <P><A NAME="search_loader"></A><BR><FONT SIZE=4>Search for and |
| Use of a FrameLoader Object</FONT> |
| </P> |
| </TD> |
| </TR> |
| <TR BGCOLOR="#ffffcc"> |
| <TD WIDTH=1285> |
| <PRE>void example_loadDocument() |
| { |
| // Create UNO-services for type detection and loading document. |
| // Use global servicemanager as factory and cast return value to right interface. |
| Reference< XTypeDetection > xTypeDetection( getProcessServiceFactory()->createInstance( ASCII("com.sun.star.document.TypeDetection" ) ), UNO_QUERY ); |
| Reference< XMultiServiceFactory > xLoaderFactory( getProcessServiceFactory()->createInstance( ASCII("com.sun.star.frame.FrameLoaderFactory") ), UNO_QUERY ); |
| |
| // Make deep detection by URL only. |
| Sequence< PropertyValue > lDescriptor( 1 ); |
| |
| lDescriptor[0].Name = ASCII("FileName" ); |
| lDescriptor[0].Value <<= ASCII("file:///c|/temp/test.txt"); |
| |
| sal_Bool bDeep = sal_True; |
| |
| sInternalTypeName = xTypeDetection->queryTypeByDescriptor( lDescriptor, bDeep ); |
| |
| // Search registered loader and create it. |
| Reference< XInterface > xLoaderInterface( xLoaderFactory->createInstance( sInternalTypeName ), UNO_QUERY ); |
| |
| // Check for supported load mechanism by this implementation? [async/sync] |
| Reference< XSynchronousFrameLoader > xSyncLoader ( xLoaderInterface, UNO_QUERY ); |
| Reference< XFrameLoader > xAsyncLoader( xLoaderInterface, UNO_QUERY ); |
| |
| // Don't forget to create a new target frame to load document in it! |
| // Use findFrame() or queryDispatch() mechanism to do that. |
| Reference< XFrame > xTarget = ...; |
| |
| if( xSyncLoader.is() == sal_True ) |
| { |
| sal_Bool bState = xSyncLoader->load( lDescriptor, xTarget ); |
| } |
| else |
| if( xAsyncLoader.is() == sal_True ) |
| { |
| xAsyncLoader->load( xTarget , |
| ASCII("file:///c|/temp/test.txt"), |
| lDescriptor , |
| this ); // We must support XLoadEventListener interface to get information about succesful loading! |
| } |
| }</PRE> |
| </TD> |
| </TR> |
| <TR> |
| <TD WIDTH=1285> |
| <P><A NAME="search_filter"></A><BR><FONT SIZE=4>Search for and |
| Use of a Filter Object</FONT> |
| </P> |
| </TD> |
| </TR> |
| <TR BGCOLOR="#ffffcc"> |
| <TD WIDTH=1285> |
| <PRE>void example_filterDocument() |
| { |
| // Create UNO-services for type detection and loading document. |
| // Use global servicemanager as factory and cast return value to right interface. |
| Reference< XTypeDetection > xTypeDetection( getProcessServiceFactory()->createInstance( ASCII("com.sun.star.document.TypeDetection") ), UNO_QUERY ); |
| Reference< XMultiServiceFactory > xFilterFactory( getProcessServiceFactory()->createInstance( ASCII("com.sun.star.document.FilterFactory") ), UNO_QUERY ); |
| |
| // Make deep detection by URL only. |
| Sequence< PropertyValue > lDescriptor( 1 ); |
| |
| lDescriptor[0].Name = ASCII("FileName" ); |
| lDescriptor[0].Value <<= ASCII("file:///c|/temp/test.txt"); |
| |
| sal_Bool bDeep = sal_True; |
| |
| sInternalTypeName = xTypeDetection->queryTypeByDescriptor( lDescriptor, bDeep ); |
| |
| // Search registered filter and create it. |
| // We will use it as an import filter here! cast it to right interface ... |
| Reference< XFilter > xFilter ( xFilterFactory->createInstance( sInternalTypeName ), UNO_QUERY ); |
| Reference< XImporter > xImportFilter( xFilter , UNO_QUERY ); |
| |
| // We must create the document service which is used by these filter. |
| // These information is available as a property of created filter! |
| Reference< XPropertySet > xFilterProperties( xFilter, UNO_QUERY ); |
| OUString sDocumentService; |
| xFilterProperties->getPropertyValue( ASCII("DocumentService") ) >>= sDocumentService; |
| Reference< XComponent > xDocument( getProcessServiceFactory()->createInstance( sDocumentService ), UNO_QUERY ); |
| |
| // Set target document service at import filter |
| // and start filter operation. |
| // (xImportFilter and xFilter are the same objects - but different interfaces!) |
| xImportFilter->setTargetDocument( xDocument ); |
| sal_Bool bState = xFilter->filter( lDescriptor ); |
| }</PRE> |
| </TD> |
| </TR> |
| <TR> |
| <TD WIDTH=1285> |
| <P><A NAME="read_filtercfg"></A><BR><FONT SIZE=4>Reading the |
| Filter Configuration</FONT> |
| </P> |
| </TD> |
| </TR> |
| <TR BGCOLOR="#ffffcc"> |
| <TD WIDTH=1285> |
| <PRE>void example_readFilterCFG() |
| { |
| // Create UNO-services FilterFactory - support name container interface too! |
| // Use global servicemanager as factory and cast return value to right interface. |
| Reference< XNameAccess > xFilterContainer( getProcessServiceFactory()->createInstance( ASCII("com.sun.star.document.FilterFactory") ), UNO_QUERY ); |
| |
| // Step over all registered filters. |
| Sequence< OUString > lFilterNames = xFilterContainer->getElementNames(); |
| sal_Int32 nNameCount = lFilterNames.getLength() ; |
| |
| for( sal_Int32 nName = 0; nName < nNameCount; ++nName ) |
| { |
| // Get all properties of current filter as Sequence< PropertyValue >. |
| // Step over all these properties and copy values to local variables. |
| Sequence< PropertyValue > lProperties; |
| xFilterContainer->getByName( lFilterNames[ nName ] ) >>= lProperties; |
| |
| OUString sType ; |
| OUString sUIName ; |
| OUString sDocumentService ; |
| OUString sFilterService ; |
| sal_Int32 nFlags ; |
| Sequence< OUString > lUserData ; |
| sal_Int32 nFileFormatVersion ; |
| OUString sTemplateName ; |
| |
| sal_Int32 nPropertyCount = lProperties.getLength(); |
| for( sal_Int32 nProperty = 0; nProperty < nPropertyCount; ++nProperty ) |
| { |
| if( lProperties[ nProperty ].Name == ASCII("Type") ) |
| lProperties[ nProperty ].Value >>= sType; |
| else |
| if( lProperties[ nProperty ].Name == ASCII("UIName") ) |
| lProperties[ nProperty ].Value >>= sUIName; |
| else |
| if( lProperties[ nProperty ].Name == ASCII("DocumentService") ) |
| lProperties[ nProperty ].Value >>= sDocumentService; |
| else |
| if( lProperties[ nProperty ].Name == ASCII("FilterService") ) |
| lProperties[ nProperty ].Value >>= sFilterService; |
| else |
| if( lProperties[ nProperty ].Name == ASCII("Flags") ) |
| lProperties[ nProperty ].Value >>= nFlags; |
| else |
| if( lProperties[ nProperty ].Name == ASCII("UserData") ) |
| lProperties[ nProperty ].Value >>= lUserData; |
| else |
| if( lProperties[ nProperty ].Name == ASCII("FileFormatVersion") ) |
| lProperties[ nProperty ].Value >>= nFileFormatVersion; |
| else |
| if( lProperties[ nProperty ].Name == ASCII("TemplateName") ) |
| lProperties[ nProperty ].Value >>= sTemplateName; |
| } |
| } |
| }</PRE> |
| </TD> |
| </TR> |
| <TR> |
| <TD WIDTH=1285> |
| <P><A NAME="register_filter"></A><BR><FONT SIZE=4>Registering a |
| New Filter</FONT> |
| </P> |
| </TD> |
| </TR> |
| <TR BGCOLOR="#ffffcc"> |
| <TD WIDTH=1285> |
| <PRE>void example_registerFilter() |
| { |
| // Create UNO-services FilterFactory - support name container interface too! |
| // Use global servicemanager as factory and cast return value to right interface. |
| Reference< XNameContainer > xFilterContainer( getProcessServiceFactory()->createInstance( ASCII("com.sun.star.document.FilterFactory") ), UNO_QUERY ); |
| |
| // Define all properties and values of new filter. |
| OUString sType = ASCII("type_to_register_for" ); |
| OUString sUIName = ASCII("Own Filter" ); |
| OUString sDocumentService = ASCII("com.my.company.DocumentService"); |
| OUString sFilterService = ASCII("com.my.company.FilterService" ); |
| sal_Int32 nFlags = 0 ; |
| sal_Int32 nFileFormatVersion = 0 ; |
| OUString sTemplateName = OUString() ; |
| Sequence< OUString > lUserData = Sequence< OUString >() ; |
| |
| // Pack it into a Sequence< PropertyValue > to support format of interface method. |
| // Order of elements in sequence isnt important ... We differ it by names only! |
| // You don't must give us all properties - we use default values for missing ones. |
| Sequence< PropertyValue > lProperties[8]; |
| |
| //-------------------------------------------------------------------------- |
| lProperties[0].Name = ASCII("Type") ; |
| lProperties[0].Value <<= sType ; |
| |
| /*ATTENTION |
| Register a human readable filtername for current the locale by using the |
| property „UIName“ or use the property "UINames" to set it for different |
| locales. |
| In the current build using „UINames“ does not work due to problems |
| in the configuration service, so currently always use „UIName“ ! |
| */ |
| |
| //-------------------------------------------------------------------------- |
| // set UIName for current locale |
| lProperties[1].Name = ASCII("UIName") ; |
| lProperties[1].Value <<= sUIName ; |
| |
| //-------------------------------------------------------------------------- |
| // set UINames for different locales – but not in current build, see above |
| // - name of property is an identifier for the locale |
| // - value is the value for these locale |
| // Sequence< PropertyValue > lUINames[2]; |
| // lUINames[0].Name = ASCII("de-DE" ); |
| // lUINames[0].Value <<= ASCII("Mein UIName"); |
| // lUINames[1].Name = ASCII("en-US" ); |
| // lUINames[1].Value <<= ASCII("my UIName" ); |
| // lProperties[1].Name = ASCII("UINames"); |
| // lProperties[1].Value <<= lUINames; |
| |
| //-------------------------------------------------------------------------- |
| lProperties[2].Name = ASCII("DocumentService") ; |
| lProperties[2].Value <<= sDocumentService ; |
| |
| //-------------------------------------------------------------------------- |
| lProperties[3].Name = ASCII("FilterService") ; |
| lProperties[3].Value <<= sFilterService ; |
| |
| //-------------------------------------------------------------------------- |
| lProperties[4].Name = ASCII("Flags") ; |
| lProperties[4].Value <<= nFlags ; |
| |
| //-------------------------------------------------------------------------- |
| lProperties[5].Name = ASCII("UserData") ; |
| lProperties[5].Value <<= lUserData ; |
| |
| //-------------------------------------------------------------------------- |
| lProperties[6].Name = ASCII("FileFormatVersion"); |
| lProperties[6].Value <<= nFileFormatVersion ; |
| |
| //-------------------------------------------------------------------------- |
| lProperties[7].Name = ASCII("TemplateName") ; |
| lProperties[7].Value <<= sTemplateName ; |
| |
| //-------------------------------------------------------------------------- |
| // Pack Sequence< PropertyValue > into an any and insert these new filter. |
| // Give us a filter name too ... but check for already existing ones before! |
| // Otherwise follow call throw an ElementExistException! |
| // (use XNameAccess to do that) |
| Any aPropertySet; |
| aPropertySet <<= lProperties; |
| xFilterContainer->insertByName( ASCII("my_own_Filter"), aPropertySet ); |
| }</PRE> |
| </TD> |
| </TR> |
| </TABLE> |
| </CENTER> |
| <TABLE WIDTH=100% BORDER=0 CELLPADDING=0 CELLSPACING=0> |
| <TR> |
| <TD> |
| <P ALIGN=LEFT><A HREF="services.html"><<</A></P> |
| </TD> |
| <TD> |
| <P ALIGN=CENTER><BR> |
| </P> |
| </TD> |
| <TD> |
| <P ALIGN=RIGHT><A HREF="reference.html">>></A></P> |
| </TD> |
| </TR> |
| </TABLE> |
| <HR SIZE=3 COLOR="#000000"> |
| <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0> |
| <TR> |
| <TD> |
| <P><STRONG><FONT SIZE=2><FONT FACE="Arial">author</FONT></FONT> </STRONG> |
| </P> |
| </TD> |
| <TD> |
| <P><FONT SIZE=2><FONT FACE="Arial"> Andreas |
| Schlüns</FONT></FONT> |
| </P> |
| </TD> |
| </TR> |
| <TR> |
| <TD> |
| <P><STRONG><FONT SIZE=2><FONT FACE="Arial">email</FONT></FONT> </STRONG> |
| </P> |
| </TD> |
| <TD> |
| <P> <A HREF="mailto:andreas.schluens@germany.sun.com"><FONT SIZE=2><FONT FACE="Arial">andreas.schluens@germany.sun.com</FONT></FONT></A> |
| </P> |
| </TD> |
| </TR> |
| <TR> |
| <TD> |
| <P><STRONG><FONT SIZE=2><FONT FACE="Arial">last modification</FONT></FONT> |
| </STRONG> |
| </P> |
| </TD> |
| <TD> |
| <P><FONT SIZE=2><FONT FACE="Arial"> 19.11.2001</FONT></FONT> |
| </P> |
| </TD> |
| </TR> |
| </TABLE> |
| <P><BR><BR> |
| </P> |
| </body> |
| </HTML> |