|
Examples The following examples are to illustrate the use of the services involved.
|
//***************************************************************************************************************** //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 |
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 ); } |
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 ); } |
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! } } |
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 ); } |
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; } } } |
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 ); } |
|
author |
Andreas Schlüns |
|
|
last modification |
19.11.2001 |