1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testcnttype.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dtrans.hxx"
35 //_________________________________________________________________________________________________________________________
37 //_________________________________________________________________________________________________________________________
38 #include <cppuhelper/servicefactory.hxx>
39 #include <com/sun/star/lang/XTypeProvider.hpp>
40 #include <com/sun/star/lang/IllegalArgumentException.hpp>
41 #include <com/sun/star/container/NoSuchElementException.hpp>
42 #include <com/sun/star/datatransfer/XMimeContentType.hpp>
43 #include <com/sun/star/datatransfer/XMimeContentTypeFactory.hpp>
44 #include <com/sun/star/lang/XComponent.hpp>
45 #include <rtl/ustring.hxx>
46 #include <sal/types.h>
47 #include <osl/diagnose.h>
53 //-------------------------------------------------------------
55 //-------------------------------------------------------------
57 #define TEST_CLIPBOARD
58 #define RDB_SYSPATH "d:\\projects\\src621\\dtrans\\wntmsci7\\bin\\applicat.rdb"
60 //------------------------------------------------------------
62 //------------------------------------------------------------
64 using namespace ::rtl
;
65 using namespace ::std
;
66 using namespace ::cppu
;
67 using namespace ::com::sun::star::datatransfer
;
68 using namespace ::com::sun::star::uno
;
69 using namespace ::com::sun::star::lang
;
70 using namespace ::com::sun::star::container
;
72 //------------------------------------------------------------
74 //------------------------------------------------------------
76 //################################################################
79 void CheckMimeContentType( const OUString& aCntType, const OUString& aType, const OUString& aSubtype, sal_Int32 nParams )
82 Reference< XMimeContentType > xMimeCntType = xMCntTypeFactory->createMimeContentType( aCntType );
84 OSL_ASSERT( aType == xMimeCntType->getMediaType ( ) );
85 OSL_ASSERT( aSubtype == xMimeCntType->getMediaSubtype ( ) );
89 Sequence< OUString > seqParam = xMimeCntType->getParameters( );
90 OSL_ASSERT( seqParam.getLength( ) == nParams );
94 for ( sal_Int32 i = 0; i < seqParam.getLength( ); i++ )
97 OSL_ASSERT( xMimeCntType->hasParameter( param ) );
99 pvalue = xMimeCntType->getParameterValue( param );
102 pvalue = xMimeCntType->getParameterValue( OUString::createFromAscii( "aparam" ) );
104 catch( IllegalArgumentException& )
106 printf( "FAILED: Invalid Mime Contenttype detected\n" );
108 catch( NoSuchElementException& )
115 //----------------------------------------------------------------
117 //----------------------------------------------------------------
119 void ShutdownServiceMgr( Reference
< XMultiServiceFactory
>& SrvMgr
)
121 // Cast factory to XComponent
122 Reference
< XComponent
> xComponent( SrvMgr
, UNO_QUERY
);
124 if ( !xComponent
.is() )
125 OSL_ENSURE(sal_False
, "Error shuting down");
127 // Dispose and clear factory
128 xComponent
->dispose();
130 SrvMgr
= Reference
< XMultiServiceFactory
>();
133 //----------------------------------------------------------------
135 //----------------------------------------------------------------
137 sal_Bool
readCntTypesFromFileIntoVector( char* fname
, vector
< string
>& vecData
)
141 fstream
= fopen( fname
, "r+" );
145 // set pointer to file start
146 fseek( fstream
, 0L, SEEK_SET
);
149 while ( fscanf( fstream
, "%[^\n]s", line
) != EOF
)
151 vecData
.push_back( line
);
160 //----------------------------------------------------------------
162 //----------------------------------------------------------------
164 sal_Bool
processCntTypesAndWriteResultIntoFile( char* fname
, vector
< string
>& vecData
, Reference
< XMimeContentTypeFactory
> cnttypeFactory
)
168 fstream
= fopen( fname
, "w" );
172 // set pointer to file start
173 fseek( fstream
, 0L, SEEK_SET
);
175 vector
< string
>::iterator iter_end
= vecData
.end( );
176 const char* pStr
= NULL
;
178 for ( vector
< string
>::iterator iter
= vecData
.begin( ); iter
!= iter_end
; ++iter
)
182 fprintf( fstream
, "Gelesen: %s\n", iter
->c_str( ) );
184 Reference
< XMimeContentType
> xMCntTyp
= cnttypeFactory
->createMimeContentType( OUString::createFromAscii( iter
->c_str( ) ) );
186 fwprintf( fstream
, OUString::createFromAscii( "Type: %s\n" ), xMCntTyp
->getMediaType( ).getStr( ) );
187 fwprintf( fstream
, OUString::createFromAscii( "Subtype: %s\n" ), xMCntTyp
->getMediaSubtype( ).getStr( ) );
189 Sequence
< OUString
> seqParam
= xMCntTyp
->getParameters( );
190 sal_Int32 nParams
= seqParam
.getLength( );
192 for ( sal_Int32 i
= 0; i
< nParams
; i
++ )
194 fwprintf( fstream
, OUString::createFromAscii("PName: %s\n" ), seqParam
[i
].getStr( ) );
195 fwprintf( fstream
, OUString::createFromAscii("PValue: %s\n" ), xMCntTyp
->getParameterValue( seqParam
[i
] ).getStr( ) );
198 catch( IllegalArgumentException
& ex
)
200 fwprintf( fstream
, OUString::createFromAscii( "Fehlerhafter Content-Type gelesen!!!\n\n" ) );
202 catch( NoSuchElementException
& )
204 fwprintf( fstream
, OUString::createFromAscii( "Parameterwert nicht vorhanden\n" ) );
208 fwprintf( fstream
, OUString::createFromAscii( "Unbekannter Fehler!!!\n\n" ) );
211 fwprintf( fstream
, OUString::createFromAscii( "\n#############################################\n\n" ) );
219 //----------------------------------------------------------------
221 //----------------------------------------------------------------
223 int SAL_CALL
main( int nArgc
, char* argv
[] )
226 printf( "Start with: testcnttype input-file output-file\n" );
228 //-------------------------------------------------
229 // get the global service-manager
230 //-------------------------------------------------
231 OUString rdbName
= OUString( RTL_CONSTASCII_USTRINGPARAM( RDB_SYSPATH
) );
232 Reference
< XMultiServiceFactory
> g_xFactory( createRegistryServiceFactory( rdbName
) );
234 // Print a message if an error occured.
235 if ( !g_xFactory
.is( ) )
237 OSL_ENSURE(sal_False
, "Can't create RegistryServiceFactory");
241 vector
< string
> vecCntTypes
;
243 // open input-file and read the data
244 if ( !readCntTypesFromFileIntoVector( argv
[1], vecCntTypes
) )
246 printf( "Can't open input file" );
247 ShutdownServiceMgr( g_xFactory
);
250 Reference
< XMimeContentTypeFactory
>
251 xMCntTypeFactory( g_xFactory
->createInstance( OUString::createFromAscii( "com.sun.star.datatransfer.MimeContentTypeFactory" ) ), UNO_QUERY
);
253 if ( !xMCntTypeFactory
.is( ) )
255 OSL_ENSURE( sal_False
, "Error creating MimeContentTypeFactory Service" );
259 if ( !processCntTypesAndWriteResultIntoFile( argv
[2], vecCntTypes
, xMCntTypeFactory
) )
261 printf( "Can't open output file" );
262 ShutdownServiceMgr( g_xFactory
);
265 //--------------------------------------------------
266 // shutdown the service manager
267 //--------------------------------------------------
269 ShutdownServiceMgr( g_xFactory
);