Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / dtrans / source / cnttype / wbench / testcnttype.cxx
blob4c95f07ac749b59ff14a0e9ae0317bbedde889cb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 //_________________________________________________________________________________________________________________________
30 // other includes
31 //_________________________________________________________________________________________________________________________
32 #include <cppuhelper/servicefactory.hxx>
33 #include <com/sun/star/lang/XTypeProvider.hpp>
34 #include <com/sun/star/lang/IllegalArgumentException.hpp>
35 #include <com/sun/star/container/NoSuchElementException.hpp>
36 #include <com/sun/star/datatransfer/XMimeContentType.hpp>
37 #include <com/sun/star/datatransfer/XMimeContentTypeFactory.hpp>
38 #include <com/sun/star/lang/XComponent.hpp>
39 #include <rtl/ustring.hxx>
40 #include <sal/types.h>
41 #include <osl/diagnose.h>
43 #include <stdio.h>
45 #include <vector>
47 //-------------------------------------------------------------
48 // my defines
49 //-------------------------------------------------------------
51 #define TEST_CLIPBOARD
52 #define RDB_SYSPATH "d:\\projects\\src621\\dtrans\\wntmsci7\\bin\\applicat.rdb"
54 //------------------------------------------------------------
55 // namesapces
56 //------------------------------------------------------------
58 using namespace ::rtl;
59 using namespace ::std;
60 using namespace ::cppu;
61 using namespace ::com::sun::star::datatransfer;
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::lang;
64 using namespace ::com::sun::star::container;
66 //------------------------------------------------------------
67 // globales
68 //------------------------------------------------------------
70 //################################################################
73 void CheckMimeContentType( const OUString& aCntType, const OUString& aType, const OUString& aSubtype, sal_Int32 nParams )
76 Reference< XMimeContentType > xMimeCntType = xMCntTypeFactory->createMimeContentType( aCntType );
78 OSL_ASSERT( aType == xMimeCntType->getMediaType ( ) );
79 OSL_ASSERT( aSubtype == xMimeCntType->getMediaSubtype ( ) );
81 try
83 Sequence< OUString > seqParam = xMimeCntType->getParameters( );
84 OSL_ASSERT( seqParam.getLength( ) == nParams );
86 OUString param;
87 OUString pvalue;
88 for ( sal_Int32 i = 0; i < seqParam.getLength( ); i++ )
90 param = seqParam[i];
91 OSL_ASSERT( xMimeCntType->hasParameter( param ) );
93 pvalue = xMimeCntType->getParameterValue( param );
96 pvalue = xMimeCntType->getParameterValue( OUString("aparam") );
98 catch( IllegalArgumentException& )
100 printf( "FAILED: Invalid Mime Contenttype detected\n" );
102 catch( NoSuchElementException& )
109 //----------------------------------------------------------------
111 //----------------------------------------------------------------
113 void ShutdownServiceMgr( Reference< XMultiServiceFactory >& SrvMgr )
115 // Cast factory to XComponent
116 Reference< XComponent > xComponent( SrvMgr, UNO_QUERY );
118 if ( !xComponent.is() )
119 OSL_FAIL("Error shuting down");
121 // Dispose and clear factory
122 xComponent->dispose();
123 SrvMgr.clear();
124 SrvMgr = Reference< XMultiServiceFactory >();
127 //----------------------------------------------------------------
129 //----------------------------------------------------------------
131 sal_Bool readCntTypesFromFileIntoVector( char* fname, vector< string >& vecData )
133 FILE* fstream;
135 fstream = fopen( fname, "r+" );
136 if ( !fstream )
137 return sal_False;
139 // set pointer to file start
140 fseek( fstream, 0L, SEEK_SET );
142 char line[1024];
143 while ( fscanf( fstream, "%[^\n]s", line ) != EOF )
145 vecData.push_back( line );
146 fgetc( fstream );
149 fclose( fstream );
151 return sal_True;
154 //----------------------------------------------------------------
156 //----------------------------------------------------------------
158 sal_Bool processCntTypesAndWriteResultIntoFile( char* fname, vector< string >& vecData, Reference< XMimeContentTypeFactory > cnttypeFactory )
160 FILE* fstream;
162 fstream = fopen( fname, "w" );
163 if ( !fstream )
164 return sal_False;
166 // set pointer to file start
167 fseek( fstream, 0L, SEEK_SET );
169 vector< string >::iterator iter_end = vecData.end( );
170 for ( vector< string >::iterator iter = vecData.begin( ); iter != iter_end; ++iter )
174 fprintf( fstream, "Gelesen: %s\n", iter->c_str( ) );
176 Reference< XMimeContentType > xMCntTyp = cnttypeFactory->createMimeContentType( OUString::createFromAscii( iter->c_str( ) ) );
178 fwprintf( fstream, OUString("Type: %s\n"), xMCntTyp->getMediaType( ).getStr( ) );
179 fwprintf( fstream, OUString("Subtype: %s\n"), xMCntTyp->getMediaSubtype( ).getStr( ) );
181 Sequence< OUString > seqParam = xMCntTyp->getParameters( );
182 sal_Int32 nParams = seqParam.getLength( );
184 for ( sal_Int32 i = 0; i < nParams; i++ )
186 fwprintf( fstream, OUString("PName: %s\n"), seqParam[i].getStr( ) );
187 fwprintf( fstream, OUString("PValue: %s\n"), xMCntTyp->getParameterValue( seqParam[i] ).getStr( ) );
190 catch( IllegalArgumentException& ex )
192 fwprintf( fstream, OUString("Fehlerhafter Content-Type gelesen!!!\n\n") );
194 catch( NoSuchElementException& )
196 fwprintf( fstream, OUString("Parameterwert nicht vorhanden\n") );
198 catch( ... )
200 fwprintf( fstream, OUString("Unbekannter Fehler!!!\n\n") );
203 fwprintf( fstream, OUString("\n#############################################\n\n") );
206 fclose( fstream );
208 return sal_True;
211 //----------------------------------------------------------------
212 // main
213 //----------------------------------------------------------------
215 int SAL_CALL main( int nArgc, char* argv[] )
217 if ( nArgc != 3 )
218 printf( "Start with: testcnttype input-file output-file\n" );
220 //-------------------------------------------------
221 // get the global service-manager
222 //-------------------------------------------------
223 OUString rdbName = OUString( RDB_SYSPATH );
224 Reference< XMultiServiceFactory > g_xFactory( createRegistryServiceFactory( rdbName ) );
226 // Print a message if an error occurred.
227 if ( !g_xFactory.is( ) )
229 OSL_FAIL("Can't create RegistryServiceFactory");
230 return(-1);
233 vector< string > vecCntTypes;
235 // open input-file and read the data
236 if ( !readCntTypesFromFileIntoVector( argv[1], vecCntTypes ) )
238 printf( "Can't open input file" );
239 ShutdownServiceMgr( g_xFactory );
242 Reference< XMimeContentTypeFactory >
243 xMCntTypeFactory( g_xFactory->createInstance( OUString("com.sun.star.datatransfer.MimeContentTypeFactory") ), UNO_QUERY );
245 if ( !xMCntTypeFactory.is( ) )
247 OSL_FAIL( "Error creating MimeContentTypeFactory Service" );
248 return(-1);
251 if ( !processCntTypesAndWriteResultIntoFile( argv[2], vecCntTypes, xMCntTypeFactory ) )
253 printf( "Can't open output file" );
254 ShutdownServiceMgr( g_xFactory );
257 //--------------------------------------------------
258 // shutdown the service manager
259 //--------------------------------------------------
261 ShutdownServiceMgr( g_xFactory );
263 return 0;
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */